EECS 138-Python PRACTICE QUESTIONS Michael S. Branicky, 2020 Problem PQ1. Evaluating Python Expressions. Evaluate each of...
Fantastic news! We've Found the answer you've been seeking!
Question:
Transcribed Image Text:
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. 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.
Expert Answer:
Answer rating: 100% (QA)
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... View the full answer
Related Book For
Posted Date:
Students also viewed these programming questions
-
Let A, B be sets. Define: (a) the Cartesian product (A B) (b) the set of relations R between A and B (c) the identity relation A on the set A [3 marks] Suppose S, T are relations between A and B, and...
-
can someone solve this Modern workstations typically have memory systems that incorporate two or three levels of caching. Explain why they are designed like this. [4 marks] In order to investigate...
-
Can you provide an example of when you led a previous organization through a major operational change? Did you encounter any obstacles and how did you motivate and guide your team through these...
-
The following is a selected list of ratios comparing Long Inc. and Circular Corporation for a recent year: Instructions (a) Classify each of the above ratios as a liquidity (L), solvency (S), or...
-
None of the disk-scheduling disciplines, except FCFS, is truly fair (starvation may occur). a. Explain why this assertion is true. b. Describe a way to modify algorithms such as SCAN to ensure...
-
ZeeZee's Construction Company has the opportunity to select one of four projects (A, B, C, or D) or choose the null (do-nothing) alternative. Each project requires a single initial investment and has...
-
Soaking Wet, Inc., a manufacturer of gears for lawn sprinklers, is thinking about adding a new fully automated machine. This machine can produce gears that the company now produces on its third...
-
There are two primary reasons that guide my approach to back up files externally. First, there's the aspect of cost savings. Investing in a hard drive with sufficient storage capacity constitutes a...
-
Micro Chip Corporation (MCC) has a special PO Box for customer payment. Jane is responsible for: going to the post office every day, emptying the post office box, opening the mail, making a prelist,...
-
The table below shows the Consumer Price Index (CPI) for food in Canada for the years 1998 to 2018. Using regression techniques discussed in this section, analyze the data for trend. Develop a...
-
What will be the complexity of the above solution?
-
Refer to the previous exercise for a description of the data set. Build a default regression tree to predict the customers annual household spending on travel products (TravelSpend). Display the...
-
Analyze the fraction of overhead required by the list of children implementation, the left-child/right-sibling implementation, and the two linked implementations of Section 6.3.3. How do these...
-
Give an example that uses delayed branch with the three-segment pipeline of Sec. 9-5. Example: Three-Segment Instruction Pipeline A typical set of instructions for a RISC processor are listed in...
-
In 2010, Costco's reputation for rock-bottom pricing and razor-thin profit margins helped the company maintain its position as the nation's fourth-largest retailer and the No. 1 membership warehouse...
-
A statistical study shows that the fraction of television sets of a certain brand that are still in service after x years is given by f (x) = e-0.15x. (a) What fraction of the sets are still in...
-
A life insurance company quoted an annual premium of $387.50 (payable at the beginning of the year) for a $250,000 term insurance policy on a 35-year-old male nonsmoker. Alternatively, the insured...
-
A $100,000 mortgage loan at 7.2% compounded semiannually requires monthly payments based on a 25-year amortization. Assuming that the interest rate does not change for the entire 25 years, complete...
-
What is meant by the effective rate of interest?
-
Why is gifting an important estate planning tool? Why are assets that grow in value recommended as gifts?
-
Following his death in 2012, Zane Wulster's gross taxable estate was valued at $3,300,000. He has made a total of $200,000 of gifts that exceeded the annual gift tax exclusion. a. What is the amount...
-
Explain how the portable estate exemption, resulting from the 2010 Tax Relief Act, altered estate planning using family trusts.
Study smarter with the SolutionInn App