Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

EECS 138-Python PRACTICE QUESTIONS Michael S. Branicky, 2020 Problem PQ1. Evaluating Python Expressions. Evaluate each of the following Python expres- sions, entering their values

imageimageimageimageimageimageimage

EECS 138-Python PRACTICE QUESTIONS Michael S. Branicky, 2020 Problem PQ1. Evaluating Python Expressions. Evaluate each of the following Python expres- sions, entering their values in the boxes provided (2 points each). You may use the area outside the boxes for calculations. Example: 3 = 2 Throughout, assume the following variable declarations/statements: filename= "Midterm.2.PDF" s="Python is fun!" prime [2, 3, 5, 7, 11, 13, 17, 19 ] mat= [[1, 2, 3], [4, 5, 6], [7, 8, 9] ] dotloc filename.index(".") (a) len(prime) (b) prime [5] (c) mat [1] [1] (d) mat [2] [1] (e) len(filename) (f) filename [3] (g) dotloc (h) filename [:dotloc] (i) filename.lower ().endswith(".pdf") False (j) "138"+s [5:] 2 EECS 138-Python Michael S. Branicky, 2020 Problem PQ2. Writing Python Statements. Produce the requested Python statements. Example: Write a one-line Python statement declaring a string storing a campaign slogan. slogan "Guido in 2024! " (a) [3 points] Write a one-line Python statement creating and initializing a list of exam grades containing the numbers 94, 77, 83, and 91. PRACTICE QUESTIONS 3 (b) [3 points] Use list comprehension to write a one-line Python statement creating and initializing to all zeros a 1-d array (list) to store daily temperatures for one (non-leap) year. (c) [3 points] Write a one-line Python statement creating a 2-d array roll (list of lists) that will be used in a game program to store the results of rolling two dice on each turn and initializing it to store these three rolls: 1, 2 then 3, 4 then 5, 6. (d) [3 points] Write one or more Python statements to print out all the values of the temper- ature list defined in part (b), assuming that it has been populated with non-zero data since its initialization. (e) [4 points] Write one or more Python statements to grab the "extension" (i.e., all char- acters after the last .') from str variable filename and store the result in the str named extension. Assume filename contains a '.' and both strings have been created and assigned. (f) [4 points] Write one or more Python statements to go through a slogan (stored in slogan, like the one in the Example above) and print "digit" for each character between 0 and 9. EECS 138-Python PRACTICE QUESTIONS (a) [5 points] Problem PQ3. Code Analysis. Fill in the values for the lists/variables indicated after the corresponding code has run. You must enter your final answers into the boxes provided below to receive credit. Feel free to use the area outside the boxes for preliminary calculations. Be careful! SIZE = 5 sums [0 for i in range (SIZE)] for i in range (SIZE): sums [i]=i*(1+1)/2 (b) [5 points] arr [7, 12, 24, 31, 49] 10 = 0 hi len (arr) - 1 while lo < hi: temp = arr [10] arr [lo]= arr [hi] arr [hi] = temp lo + 1 hi -= 1 (c) [10 points] The following code might appear in a game program that uses a grid world. for i in range (XMAX): for j in range (YMAX): Michael S. Branicky, 2020 sums idist = abs (i-goali) jdist = abs(j-goalj) world [i] [j] = idist jdist if world [i][j] > m: m = world [i][j] arr XMAX = 3 YMAX = 3, 3 world = [[ 0 for j in range (YMAX)] for i in range (XMAX)] goali = 1 goalj = 2 m = -1 world 4 m: EECS 138-Python PRACTICE QUESTIONS Problem PQ4. Modifying and Filling In Python Code. (a) [6 points] Here are the formulas for computing the mean, u, and standard deviation, o, of a set of n values: l = =1 Fi n x + x + + In n def mean (x): xsum = 0 for i in range (len(x)): 0 = xsum + x[i] return xsum/len(x) Michael S. Branicky, 2020 |=1(*; - )2 n =1 n 4 The following Python functions returns the mean of the values of a passed list, x, using the formula for the mean, u, given above. 5 Write another Python function (which could call the function above) to return the standard deviation of the values of a passed list, x. Also (for purposes of assigning partial credit), circle the formula for the standard deviation, o, that you used above. Problem PQ4 continued (b) [6 points] The text file autompg.dat contains 398 lines of automobile mileage data of the following, tab-separated data (where the character 'l' is used only to denote the tab locations): car name model year | number of cylinders | miles per gallon For example, the first two lines of the file might look like: Prius Escalade 2005 4 55.0 2005 8 16.5 Add Python expressions/statements to complete the program below, which should read the data from the file and display it to the screen. Assume car name contains no spaces. #Open the file input file.open( = # name is of type str # year and cyls are of type int #mpg is of type float print("Name \t Year \t # Cyls. \t MPG") # Get input from file for ) # Close the file input.close() : # within for loop body, display that line of data print (f"{name} \t {year} \t {cyls} \t {mpg}") # end for loop body EECS 138-Python PRACTICE QUESTIONS Problem PQ4 continued (c) [8 points] Assume that values is a 101-element list containing measurements from a scientific experiment, as declared in the Python code below. Add Python statements that would complete the program below to count the number of positive values, negative values, and zero values in the list, and (complete the code to) write out a message summarizing how many values of each type were found. values = [0 for i in range (101)] # LIST DECLARATION # OTHER CODE TO INITIALIZE values WOULD BE HERE (DO NOT WRITE) for i in Michael S. Branicky, 2020 # DECLARE/INITIALIZE YOUR OTHER VARIABLES HERE # end for loop body : # Print statements: COMPLETE AS NECESSARY print (f"# of positive entries: print (f"# of negative entries: print (f"# of zero entries: 7 ") ") ") EECS 138-Python PRACTICE QUESTIONS Problem PQ5. Writing Object-Oriented Python Code. (a) [15 points] Complete the code below for an Element class. It will have attributes for symbol, atomic number, and atomic weight. For example, helium has symbol He, atomic number 2, and atomic weight 4.003. Write a 3-parameter constructor __init__ that can be used to set all three attributes, with default attribute values of "", 0, and 0.0, respectively. Write getter and setter instance methods for the atomic number only. class Element: Michael S. Branicky, 2020 # test the Element class. 8 (b) [5 points] Add Python statements to create an application program below that (i) creates and initializes the Element object helium using your constructor and all three of its actual attribute values (listed above); (ii) creates an list of objects that could be used for all 118 elements of the periodic table, but with only the atomic number initialized to a non-default value.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

Copy code PQ 3 PQ 1 a len prime is 8 b prime5 refers to the 6th element of the prime list which is 13 c mat1 1 is 5 This refers to the second element of the second list within mat d mat 2 1 gives an e... 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_2

Step: 3

blur-text-image_3

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

Business Mathematics In Canada

Authors: Ernest Jerome

7th edition

978-0071091411, 71091416, 978-0070009899

More Books

Students also viewed these Programming questions

Question

What is meant by the effective rate of interest?

Answered: 1 week ago