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
For each of the following Python programs P and input strings I, give the output P(I),...
For each of the following Python programs P and input strings I, give the output P(I), using the formal definition of P(I) given, and employing any reasonable reference computer C: Definition of P(I), the output of a Python program. Let P be a Python program with respect to a reference computer system C. Suppose P has main function M, and let I be a string of ASCII characters that will be used as input to P. The output of P...
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,...
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.
Use Python to Complete the following on a single text file and submit your code and...
Use Python to Complete the following on a single text file and submit your code and your output as separate documents. For each problem create the necessary list objects and write code to perform the following examples: Sum all the items in a list. Multiply all the items in a list. Get the largest number from a list. Get the smallest number from a list. Remove duplicates from a list. Check a list is empty or not. Clone or copy...
Function Example: Write a Python function that receives two integer arguments and writes out their sum...
Function Example: Write a Python function that receives two integer arguments and writes out their sum and their product. Assume no global variables. def writer(n1, n2): sum = n1 + n2 product = n1*n2 print("For the numbers", n1, "and", n2) print("the sum is", sum) print("and the product is", product) ... 1) Create a PYHW2 document that will contain your algorithms in flowchart and pseudocode form along with your screen shots of the running program. 2) Create the algorithm in both...
Implement the following functions in the given code: def random_suit_number(): def get_card_suit_string_from_number(n): def random_value_number(): def get_card_value_string_from_number(n):...
Implement the following functions in the given code: def random_suit_number(): def get_card_suit_string_from_number(n): def random_value_number(): def get_card_value_string_from_number(n): def is_leap_year(year): def get_letter_grade_version_1(x): def get_letter_grade_version_2(x): def get_letter_grade_version_3(x): Pay careful attention to the three different versions of the number-grade-to-letter-grade functions. Their behaviors are subtly different. Use the function descriptions provided in the code to replace the pass keyword with the necessary code. Remember: Parameters contain values that are passed in by the caller. You will need to make use of the parameters that each...
1. Write 3n + 1 Sequence String Generator function (1 point) In lab5.py complete the function...
1. Write 3n + 1 Sequence String Generator function (1 point) In lab5.py complete the function sequence that takes one parameter: n, which is the initial value of a sequence of integers. The 3n + 1 sequence starts with an integer, n in this case, wherein each successive integer of the sequence is calculated based on these rules: 1. If the current value of n is odd, then the next number in the sequence is three times the current number...
In Python: This will require you to write several functions, and then use them in a...
In Python: This will require you to write several functions, and then use them in a program. Logical Calculator The logical calculator does math, but with logical operators. In logic, we represent a bit with 0 as false and a bit with 1 as true. The logical operators are NOT, AND and OR. Bitwise logical calculations operate on each bit of the input. The NOT operator works on just one three-bit argument. NOT 011 = 100 The AND operator works...
Python Problem 1 Create a function, biggest_ip_sum, outside of the ServerClass class, that: 1. Takes two...
Python Problem 1 Create a function, biggest_ip_sum, outside of the ServerClass class, that: 1. Takes two ServerClass objects 2. Sums the octets of the IP together (example 127.0.0.1 = 127+0+0+1 = 128) 3. Returns the larger number Example: server_one = ServerClass("127.0.0.1") server_two = ServerClass("192.168.0.1") result = biggest_ip_sum(server_one, server_two) print(result) In the example above, result will be 361. _________________________________ Problem 1 Here is the code to start with class ServerClass: """ Server class for representing and manipulating servers. """ def __init__(self,...
PYTHON: Using the function random.random_sample from numpy package write two functions: * poissonRV(seed, mean,n) that returns...
PYTHON: Using the function random.random_sample from numpy package write two functions: * poissonRV(seed, mean,n) that returns for given seed a bunch of n Poisson distributed random numbers with the provided mean. You have to numerically calculate the inverse distribution function. funnyDiceRV(seed,n) that returns for given seed a bunch of n random number which describe a biased die with distribution ℙ({1})=ℙ({2})=ℙ({3})=ℙ({4})=ℙ({5})=1/10P({1})=P({2})=P({3})=P({4})=P({5})=1/10 and ℙ({6})=1/2P({6})=1/2.
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT