Question: 6 ari Question #1 Using Python's string capabilities, loops, and built-in functions, write the code for a Python user-defined function that removes all characters



6 ari Question #1 Using Python's string capabilities, loops, and built-in functions, write the code for a Python user-defined function that removes all characters from a string except digit characters. The function returns the resultant string. The function prototype MUST be: def digits_only(s) See table below for examples of the original and resultant strings. Original (s): "how many seas are there? 7" "In 1990 Guido Van Rossum invented Python!" "no digits in this string" "who is the #1 team in the nhl in 2024?" New: "7" "1990" "12024" Question #2 Using Python's string capabilities, loops, lists, and built-in functions, write the code for a Python user-defined function that displays 'n' rows of a Fibonacci sequence! 0 ratio: 0.000000 0 1 ratio: 0.000000 0 1 1 ratio 1.000000 0 1 1 2 ratio: 2.000000 0 1 1 2 3 ratio: 1.500000 0 1 1 2 3 5 ratio: 1.666667 0 1 1 2 3 5 8 ratio: 1.600000 0 1 1 2 3 5 8 13 ratio: 1.625000 0 1 1 2 3 5 8 13 21 ratio: 1.615385 The function prototype MUST be: def draw_fibonacci(n) A Fibonacci sequence begins with the values and 1 and each subsequent term in the sequence is comprised of the sum of the 2 previous terms. Each new value to display is separated from the previous value by a space and then the expression "ratio: n.xxxxxx" (where n.xxxxxx) represents the the ratio of the last term divided by the second last term. NOTE: Be careful of division by zero on the first term! For example, a 9 row Fibonacci sequence would be: OS OS 6 shar are Question #3 Write a Python user-defined function that accepts 2 strings of any length and displays a horizontal frequency graph comprised of asterisks (see below) of each ALPHABETIC letter in the first string whose character is contained in the second string (the frequency graph must be displayed in alphabetical order i.e. from a to z in lowercase). Spaces and all non-alphabetic characters are to be ignored! The frequency table must be displayed as: c=x *x (where c is a letter of the alphabet, x is the number of that letter in the pool string, and *x is x number of asterisks, followed by a new line). The function prototype MUST be: def char_frequency (pool, atoms) For example, the function call: char_frequency ("Python is such a great language!", "ptaes") would display: a=4 **** e=2 ** p=1 * S=2 ** t=2 ** HINT: Consider using Python's upper(), lower(), and ord() functions and string.ascii_lowercase to complete your solution. Question #4 Using Python's list and dictionary data structures, write the code for a user-defined function called dot_product that accepts a dictionary containing 'n' equal length one-dimensional lists such that each list is stored using a unique key. You may assume there are a minimum of 2 keys in the dictionary, but there may be 3, 4, 10, 1000, etc, or more keys. This function performs a dot product multiplication of ALL lists in the dictionary and returns the result. In mathematics, the dot product or scalar product is an algebraic operation that takes two equal-length sequences of numbers (usually coordinate vectors), and returns a single number. An example of a dot product of 2 lists [1, 2, 3] and [6, 7, 8] would be: [1, 2, 3] [6, 7, 8] 1 6 + 2= 7+ 3*8 = 44 ** The function prototype MUST be: def dot_product (d) : An example of a dot product of 3 lists [-1, 6, 4, 22], [5, 2, 8, -1], and [7, 3, 1, 5], would be: [-1, 6, 4, 22] [5, 2, 8, -1] [ 7, 3, 1, 5] -1 764 5 * 7+ 2 3 + * 81 + ** -77 22 * -1 M 5 = MAIN PROGRAM: Your solution may ONLY use the python modules listed below # program: L1main.py #author: danny abesdris # date: january 22, 2024 #purpose: python main() program for PRG550 WINTER 2024 Lab #1 import math import string import collections def digits_only(s) : your code here... # end def def draw_fibonacci(n) : your code here... # end def def char_frequency (pool, atoms) : your code here... # end def def dot product(d) : your code here... # end def def main(): test_s = [ "how many seas are there? 7", "In 1990 Guido Van Rossum invented Python!", "no digits in this string", "who is the #1 team in the nhl in 2024?", "sequence 1 code 1-1A sequence 2 code 1-1A-2B sequence 3 code 1B-2B-3 Code 000 destruct 0"] print ("==========") for s in test_s : # end for print(digits_only(s)) print("==========") draw_fibonacci(17) print("==========") char_frequency ("Python is such a great language!", "ptaes") s2 = '''See yourself You are the steps you take You and you and that's the only way Shake shake yourself You're every move you make So the story goes... -Rabin/Anderson/Squire/Horn- "Owner of a Lonely Heart"*** print("==========") draw_fibonacci(17) print("==========") char_frequency ("Python is such a great language!", "ptaes") s2 = ''See yourself You are the steps you take You and you and that's the only way Shake shake yourself You're every move you make So the story goes... -Rabin/Anderson/Squire/Horn- "Owner of a Lonely Heart" print("==========") char_frequency (s2, "cpYdeOTsk") - d1 = {"key1":[1, 2, 3], "key2": [6, 7, 8]} d2 = (2:[-1, 6, 4, 22], 3: [5, 2, 8, -1], 4:[7, 3, 1, 5]} f name - print("==========") print(dot_product(d1)) print("==========") print(dot_product (d2)) end main() main() == 11 _main___": The OUTPUT should be EXACTLY as displayed below: 7 1990 12024 111211231230000 ====== 0 ratio: 0.000000 01 ratio: 0.000000 0 1 1 ratio: 1.000000 0 1 1 2 ratio: 2.000000 0 1 1 2 3 ratio: 1.500000 0 1 1 2 3 5 ratio: 1.666667 0 1 1 2 3 5 8 ratio: 1.600000 0 1 1 2 3 5 8 13 ratio: 1.625000 0 1 1 2 3 5 8 13 21 ratio: 1.615385 5 8 13 21 34 ratio: 1.619048 0 1 1 2 3 0 1 1 2 3 5 8 13 0 1 1 2 3 5 8 13 21 34 55 ratio: 1.617647 21 34 55 89 ratio: 1.618182 0 1 1 2 3 5 8 13 21 34 55 89 144 ratio: 1.617978 21 34 55 89 144 233 ratio: 1.618056 21 34 55 89 144 233 377 ratio: 1.618026 0 1 1 2 3 5 8 13 0 1 1 2 3 5 8 13 0 1 1 2 3 5 8 13 0 1 1 2 3 5 8 13 21 21 34 55 89 144 233 377 610 ratio: 1.618037 34 55 89 144 233 377 610 987 ratio: 1.618033 ======== a=4 **** e=2 ** p=1 * S=2 ** t=2 ** c=0 d=3 *** e-23 ******* k=4 **** 0-18 ****************** p=1 * 5-13 *****: t-9 ********* y=13 ******** 44 ======= -77
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
