Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

need help going theough the pyrhon script step by step on each problem. Q1: Use fromkeys() method/function to build a dictionary based on the given

image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
need help going theough the pyrhon script step by step on each problem.
Q1: Use fromkeys() method/function to build a dictionary based on the given variables. ( 2 points) hint: use the methods we have learned in class to check how to use a new method/function. In [3]: H \# Given variables cars=("Car1","Car2","Car3")years=2023 In [4]: M \# use fromkeys() to build a dictionary Out[4]:{Car:2023,Car2:2023,Car:2023} Q2: Write a function called greeting0 that greets people with their name. (2 points) In [8]: M " define the function here (1.5 point ) In [9]: M \#test it with "Will smith" (0.5 point ) He11o, Will Smith. Q3: Write a function called mean() that calculates the average/mean values from a given list (2 points) ]: M= define the function here (1,5 point ) 11]: M=testitwithls1(e.5point)151=[3,5,87,54,3,22,90,6.8,7.333] Dut[11]:-3.5407777777777785 Q4: Wine a function called lesser_odd0 that roturns the lesser of two given numbers if both numbers are odd numbers, but retums the greater if one or both numbers are even numbers. ( 2.5 points) For example: - (1,3) retum 1 - (1,2) - rotum 2 hint: you can use min0 and max0 [12]: H= define the function here ( 2 points) [13]: H n test it with (0273,372646) and (37,31)(0.5 point ] 372646 37 Q5: Write a function called twenty_one() that checks the sum of two integers, if the sum equals to 21 refurn True, othorwise refurn False. (3 points) For example: - (20,1) - return True - (19,1) - return False [14]: M H define the function here (2.5 points) [15]: M " test it with (1,22) and (7,15) (e.,5 point) True false Q6: What is the datatype of the above oulputs (for Q5)? (1 point) Q7: Write a function called within() that checks the value of 100 deducts a number is within a given range. Here the range is - 10 to 10 . Return True if the result is within 10 to 10 , otherwise return False. (2.5 points) For example: - 10095=5 - return True - 10020=80 - return False M \# define the function here (2 peints) H \# test it with 102 and 102(0.5 point Q8: Write a function called five_neighbors() that checks a list of numbers, If there are a couple. of 55 next to each other, retum True. Otherwise, return Faise. (3.5 points) For example: - Is 1=[1,5,5] retum True - Is2 =[5,1,5] return False hints: 1. you may use range(len(hsts)) to stice the list and use for 100p to check the eiements one by one. 2. if idex 1=5 and index 2=5, means there are two 5 sext to each other 6]) H = define the function here (3 points) [1] ; MHestitwiththefolLowinglists(e,5point)1s2=[23,45,77,8,1,5,5,2,5,5,5]1s3=[23,5,45,5,77,5,2,5,8,5,1] Q9: Write a function called black jack(), if the sum of 3 integers is less than or equals to 21, retum their sum. The integers' range is from 1 to 11 . If their sum. greater than 21 and one of the integers is 11 , subtract 10 from the sum and return the sum values. Finally, if the sum still greater than 21 after deducting 10 from the value, retum "BUST", (5 points) For example: - 1+7+13=21 return 21 - 1+5+13=19 return 19 - 11+7+13==3110==21 retum 21 - 15+7+11=3310=23 return 1 I =3ST I define the function here (4 pointr) W i test it with the fot towing nuabers (d point) =(5,6,7) H(2,9,10) =(11,3,10) N (3,9,10) 18 21. 19 BUST Q10: Write a function called volume() that computes the volume of a sphere given its radius. (3 points) hint the volume equations is V=4/3r2 (use 3.14 to replace ) In [32]: M define the function here (2.5 points) In [33]: M \# test it with 7 and 5 (e.5 poipt) 1436.0266666666666523.3333333333334 Q11: Given a list of integers, write a function called count_odd() that returns the number of odd numbers in the list. (4.5 points) hint: this is asking to count the number of odd numbers, not just print the odd numbers In [69]:M Nefine the function here (3.5 points) In [7e]: H \# test it with the following list (1 point) 154=[1,2,3,4,5,6,7,8,9,11] Out[7e]6 Q12: Write a function called unique() to remove all the duplicates in a given list and remain the unique numbers. (4 points) In [46]: M \#efine the function here ( 3.5 points) In [47]: M \# test it with the following list ( .5 point) 1s5=[1,2,2,2,4,3,2,4,4,5,3,5,51,1,1,1] out [47]:[1,2,4,3,5,51] Q13: Import the Numpy library and create a 2D array using ones() function. (2 points) hint always check the descriptions/documentations when you see a new function. In [50]: H " for example: 2 rows 10 columns (this is not the only answer) Out[50]:array([[1,1.1,1,1,1,1,1,1,1.1,1.],[1,1,1,1,1,1,1,1.1.1.1]) Q14: Create a 33 matrix (2D) array using numbers from 0 to 8 ( 9 numbers). (2 points) In [51]: M " this is the only answer Out[52]:array([[6,1,2],[3,4,5],[6,7,8]}) Q15: Google the definition of "identity matrix" and create a 2D identity matrix using a Numpy build-in method/function, (3 points) hint: I want you to search for a new function without being aware of its name, but knowing the expected outputs. In [53]: M \# this is not the only answer, but the shape of identity matrix is the same Out[53]:array([[1,0.,0], [0.,1.0], [0.,0.,1]]. Q16: Create a 3D array that contains 32D arrays, each 2D array contains 4 rows, 6 columns. (2 points) In [55]: M \# this is not the only answer Q17: Try another combination of a 3D array using the data points from Q25. (1 point) Your data points can be different than mine. In [56]: M \# this is not the only answer In [4]: H given array matrix = np.arange (1,41), reshape (4,2,5) matrix Out[4]; array[[[[1,2,3,4,5],[6,7,8,9,10]],[[11,12,13,14,15],[16,17,18,19,28]][[21,22,23,24,25],[26,27,28,29,30]][[31,32,33,34,35],[36,37,38,39,40]]]) In [ ]: Q19: How many dimensions of the above array (Q27)? (1 points) In []=H Q20: Create a 4D array using random numbers. (2 points) You may set up a seed. (optional) In [64]:M W this is not the only answer Out[64]:array([[[[61,27,59,24,46]],[[34,47,45,37,46]]],[[[52,54,36,56,42]],[[60,61,49,56,38]]],[[[42,52,56,34,25]],[[46,23,49,39,57]]],[[[25,33,44,38,61]],[[56,52,45,25,26]]]]) Q21 (Extra points): Write a function called perfect() to check the perfect numbers in a given list. (2. points) hint the definition of perfect numbers: From Wikipedia: "In number theory, a perfect number is a positive integer that is equal to the sum of its positive divisors, excluding the number itseif. For instance, divisors 1, 2 and 3 (excluding itself), and 12+3=6, so 6 is a perfect number." source.hittevilen wikigedia orgiwikiperfect number 65]: M define the function ( 1.5 points) [66]: M= test it (0.5 points) Out [66]:[6,28]

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Big Data Systems A 360-degree Approach

Authors: Jawwad ShamsiMuhammad Khojaye

1st Edition

0429531575, 9780429531576

More Books

Students also viewed these Databases questions

Question

Why do some individuals confess to a crime they did not commit?

Answered: 1 week ago

Question

7 How can a culture encourage ethical (or unethical) behaviour?

Answered: 1 week ago