Question

Problem 2: Python 3 Implement a function called gee_whiz that does the following: given argument n,...

Problem 2: Python 3

Implement a function called gee_whiz that does the following: given argument n, a positive integer, it returns a list of n tuples corresponding to the numbers 1 through n (both inclusive): the tuple for the number k consists of k as the first component, and exactly one of the following strings as the second:

• the string 'two!' if k is divisible by 2
• the string 'three!' if k is divisible by 3
• the string 'five!' if k is divisible by 5
• the string 'two!three!' if k is divisible by 2 and 3
• the string 'three!five!' if k is divisible by 3 and 5
• the string 'two!five!' if k is divisible by 2 and 5
• the string 'two!three!five' if k is divisible by 2, 3 and 5 • the empty string otherwise

The most specific condition applies, e.g. if k is 12, the corresponding string is “two!three!”. For example, the function call

gee_whiz(30) returns the list of 30 tuples (not all shown) below:

Hint: You should be able to do this with fewer than 8 conditions inside the loop. Do not modify the doctests.

def gee_whiz(n):
"""Returns list of n tuples of the form (k, <string_k>)

Args:
n (int >=0)

>>> gee_whiz(6)
[(1, ''), (2, 'two!'), (3, 'three!'), (4, 'two!'), (5, 'five!'), (6, 'two!three!')]
>>> a = gee_whiz(20)
>>> a[14]
(15, 'three!five!')
"""
#code here

Homework Answers

Answer #1

Python code:

#initializing gee_whiz function
def gee_whiz(n):
    #initializing list to store tuples
    lis=[]
    #looping from 1 to n(inclusive)
    for i in range(1,n+1):
        #checking if number is divisible by 2,3,5
        if(i%2==0 and i%3==0 and i%5==0):
            #adding to lis, tuple for divisible by 2,3,5
            lis.append((i,'two!three!five'))
        #checking if number is divisible by 2,5
        elif(i%2==0 and i%5==0):
            #adding to lis, tuple for divisible by 2,5
            lis.append((i,'two!five!'))
        #checking if number is divisible by 3,5
        elif(i%3==0 and i%5==0):
            #adding to lis, tuple for divisible by 3,5
            lis.append((i,'three!five!'))
        #checking if number is divisible by 2,3
        elif(i%2==0 and i%3==0):
            #adding to lis, tuple for divisible by 2,3
            lis.append((i,'two!three!'))
        #checking if number is divisible by 2
        elif(i%2==0):
            #adding to lis, tuple for divisible by 2
            lis.append((i,'two!'))
        #checking if number is divisible by 3
        elif(i%3==0):
            #adding to lis, tuple for divisible by 3
            lis.append((i,'three!'))
        #checking if number is divisible by 5
        elif(i%5==0):
            #adding to lis, tuple for divisible by 5
            lis.append((i,'five!'))
        else:
            #adding to lis, tuple for divisible by none
            lis.append((i,''))
    #retuning the list
    return lis
#calling gee_whiz function printing the list for a sample value 6
print(gee_whiz(6))


Screenshot:


Output:

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Python pls create a function called search_position. This function returns a list of 2 tuples and...
Python pls create a function called search_position. This function returns a list of 2 tuples and the number should be start highest number. The first index is the number, and second are list of 2 tuples that sorted by position in alphabetical order: The first index will be position and second index will be champion's name(This also should be sorted by alphabetical order). team1 = {'Fiora': {'Top': 1, 'Mid': 4, 'Bottom': 3},'Olaf': {'Top': 3, 'Mid': 2, 'Support': 4},'Yasuo': {'Mid': 2,...
This is an intro to Python question: #Write a function called linear() that takes two parameters...
This is an intro to Python question: #Write a function called linear() that takes two parameters #- a list of strings and a string. Write this function so #that it returns the first index at which the string is #found within the list if the string is found, or False if #it is not found. You do not need to worry about searching #for the search string inside the individual strings within #the list: for example, linear(["bobby", "fred"], "bob") #should...
python 3 For this exercise you are to implement the function poly_iter in the array-backed list...
python 3 For this exercise you are to implement the function poly_iter in the array-backed list class, which, when called with positive integer parameters a, b, c, returns an iterator over the values in the underlying list at indexes a*i2+b*i+c, for i=0, 1, 2, ... E.g., given an array-backed list lst containing the elements [0, 1, 2, 3, 4, ..., 98, 99], the following code: for x in lst.poly_iter(2, 3, 4): print(x) will produce the output: 4 9 18 31...
In Python: Problem 5] Write a function that accepts a list as argument and returns a...
In Python: Problem 5] Write a function that accepts a list as argument and returns a list that contains: (1) The positive elements of the taken list (2) The negative elements of the taken list (3) The odd elements of the taken list (4) The even elements of the taken list
Create a function called, convert. This function receives a string parameter called word which only contains...
Create a function called, convert. This function receives a string parameter called word which only contains digits (the string represents a positive number) and returns a list of numbers. This is how the function works: - This function calculates the number of times each digit has repeated in the input string and then generates a number based on that using the following formula and adds it to a list. For instance, if the digit x has been repeated n times,...
python problem: ( use a loop to read each character from the string and insert into...
python problem: ( use a loop to read each character from the string and insert into the stack) 1. The function main a5.py continually asks the user for a string, and calls isPalindrome to check whether each string is a palindrome. A palindrome is a word or sequence that reads the same backward as forward, e.g., noon, madam or nurses run. 2. Your function must ignore spaces: when the user enters 'nurses run', the function returns True, to indicate that...
Python 3 Implement the function condense, which takes a "passage" (a string containing multiple sentences separated...
Python 3 Implement the function condense, which takes a "passage" (a string containing multiple sentences separated by the '\n' character), and returns a condensed version of that passage based on the following logic: the frequency with which each word appears in the entire passage is computed a score for each sentence in the passage is computed by summing the frequency of each word in the sentence the sentences with the top 3 scores (we assume there are no ties) are...
Please use Python 3+ Write a function called let_to_num. It should take a word and encode...
Please use Python 3+ Write a function called let_to_num. It should take a word and encode it into a series of numbers and dashes, with the numbers corresponding to the position of the letter in the alphabet (regardless of casing). If the character is not a letter, then ignore it. Examples of outputs: print(let_to_num('AZ')) # -> 1-26 print(let_to_num('')) # -> (empty string) print(let_to_num('A?Z')) # -> 1-26 print(let_to_num('AZ?')) # -> 1-26 print(let_to_num('AbzC')) # -> 1-2-26-3
Function name : matrixMultiplication Parameters : aMatrix (list), bMatrix (list) Returns Type: list of tuples Description...
Function name : matrixMultiplication Parameters : aMatrix (list), bMatrix (list) Returns Type: list of tuples Description : Write a function in PYTHON that takes in two matrices (list of tuples or list of lists) and multiplies the two matrices together. Assume that the given matrices will be valid, i.e. aMatrix will be n x m (n rows and m columns) and bMatrix will be m x ℓ (m rows and ℓ columns). *Assume elements of aMatrix & bMatrix will be...
Hello! I am confused on the code for this question. Write a complete Python function called...
Hello! I am confused on the code for this question. Write a complete Python function called Frog, with two integer parameters M and N, where N has the default value of 5. The function computes and returns one of two results: if N is odd it returns the equation 2 * M + N, but if it is even it returns the equation 3 * M - N instead.
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT