6 ari Question #1 Using Python's string capabilities, loops, and built-in functions, write the code for...
Fantastic news! We've Found the answer you've been seeking!
Question:
Transcribed Image Text:
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 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
Expert Answer:
Related Book For
Concepts of Database Management
ISBN: 978-111182591
7th edition
Authors: Philip J. Pratt, Joseph J. Adamski
Posted Date:
Students also viewed these algorithms questions
-
Q1. You have identified a market opportunity for home media players that would cater for older members of the population. Many older people have difficulty in understanding the operating principles...
-
MULTIPLE CHOICE: 6. The stage of production at which the individual jointproducts are identified is referred to as the: A. Split-off point B. Joint point C. Separate identification point D. Relative...
-
From greatest to least, rank the energies needed for these phase changes for equal amounts of H2O: a. From ice to ice water b. From ice water to boiling water c. From boiling water to steam?
-
Many years ago, Topnotch Knives issued a zero coupon bond with a $1,000 face value. The bond matures in three years. If the current market rate on similar bonds is 11 percent, (a) What is the bonds...
-
Following is a performance report for Electric Hero. Required (a) Calculate the variances, stating whether they are favourable or unfavourable, and suggest possible reasons for variances that you...
-
Write a sample use case scenario for a patient who sees a physician in the Kirt Clinic.
-
If individual values that underline ethics are developed at a young age, what might this suggest about the potential for ethical conflicts to arise within an organization?
-
Alice has invented a new card game to play with Bob. Alice made a deck of cards with random values between 1 and 52. Bob picks 5 cards. Then, he has to rearrange the cards so that by utilizing the...
-
Consider a two-stage compression refrigeration system shown below. The working fluid is refrigerant- 134a. The refrigerant leaves the condenser as a saturated liquid and is throttled to a flash...
-
5. In 2009, the world's fastest (and possibly coolest) man, Usain Bolt, set a world record for a 100 meter sprint with a time of 9.58 seconds. Round all answers to two decimal points in this problem....
-
! Required information [The following information applies to the questions displayed below.] Oslo Company prepared the following contribution format income statement based on a sales volume of 1,000...
-
Troy Engines, Limited, manufactures a variety of engines for use in heavy equipment. The company has always produced all of the necessary parts for its engines, including all of the carburetors. An...
-
A shaft carries four rotating masses A, B, C and D which are completely balanced. The masses B, C and D are 50 kg, 80 kg and 70 kg respectively. The masses C and D make angles of 90 and 195...
-
Most successful businesses today actively develop loyal customers who buy their brands again and again. After all, getting current customers to buy more is much easier than constantly seeking new...
-
Explain leadership strategies that HR will use for conflict management, communication improvement, employee motivation, and behavior improvement for apple company. Discuss how you plan to measure the...
-
What recommendations would you make to Big Four firms to help them (1) avoid confrontations with governmental officials in an authoritarian society and (2) deal effectively with such confrontations...
-
List the functional dependencies in the following table that concerns invoicing (an application Premiere Products is considering adding to its database), subject to the specified conditions. For a...
-
What are the DBAs database design responsibilities?
-
Describe a situation that could cause a lost update.
-
Why is macroeconomic forecasting so difficult? Does this difficulty mean economics is a worthless field of study?
-
Which of the following statements are positive in nature and which are normative? a. A tax cut will raise interest rates. b. A reduction in the payroll tax would primarily benefit poor and...
-
a. Calculate the total percentage growth in average labor productivity in the U.S. economy for the 1950s, 1960s, 1970s, 1980s, 1990s, and 2000s. Define average labor productivity for any year as real...
Study smarter with the SolutionInn App