CS Final Review Document

pdf

School

University of Houston *

*We aren’t endorsed by this school

Course

MISC

Subject

Computer Science

Date

Feb 20, 2024

Type

pdf

Pages

13

Uploaded by diegocarreon7604

Report
Q1. You are given that a variable aDict is a dictionary, variable aList is a list, variable aString is a string, and variable anInt is an integer. Each one of them has been given a valid value. You can assume that aDict , aList and aString have at least one element in them. The following is an example list aList: aList = ["Gruzinsky", "Voronov", "Belikova", "Ostrogradsky", "Rogova"] The following is an example dictionary aDict: aDict = {'Bananas':5, 'Grapes':1200, 'Apples':16} The following is an example string aString: aString = "Gruzinsky" Match the statements that are correct and have no error, those that MAY cause an error (depending on the values in the variables), and those that WILL cause and error. x = anInt / 2 # is correct and will not cause an error x = 2 / anInt # may cause an error if the value in anInt is zero (0). x = anInt / 0 # will cause an error (division by zero). aList = ["Gruzinsky", "Voronov", "Belikova", "Ostrogradsky", "Rogova"] aDict = {'Bananas':5, 'Grapes':1200, 'Apples':16} aString = "Gruzinsky" x = aString*4 x = aList[0]
Q2. Which of the following statements correctly passes exactly two arguments in the call to the function CallingTwo ? Match all statements that make the function call correctly and those that are incorrect. You can assume that the function and all variables are defined appropriately at that point in the program. Question 1 CallingTwo("This", 'That') Question 2 def CallingTwo('This', 'That') Question 3 CallingTwo{Adict, Alist} Question 4 CallingTwo('Time' + 'after time') CallingTwo(‘Timeafter time’) Question 5 CallingTwo([1,2,3], [4,5])
Q3. This dictionary studentrecords contains student Cougar Card ID numbers as integer keys, and their names as string values. The following is an example dictionary studentRecords : studentRecords = {111111: 'studentA', 222222: 'studentB', 333333: 'studentC'} Match the following examples of code with the output they print: The code could print all the student names, the Cougar Card ID numbers, both the names and the numbers, or something else. Note: The exact format of printing is not important, only that the names are all printed. Question 1 for id in studentrecords.keys(): print(studentrecords[id]) Question 2 print(studentrecords) Question 3 while id < len(studentrecords): print (studentrecords [id]) Question 4 for id in studentrecords.keys(): print (id)
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
Q4. Match each of the following bits of code with Correct or Incorrect. Correct code should print True if and only if list fruitList contains the string 'apple' and should print False otherwise. The following is an example list fruitList : fruitList = ['Bananas', 'Grapes', 'Apples'] Incorrect code will not generate the correct results. Question 1 if 'apple' in fruitList: print(True) else: print(False) Question 2 isapple = False for i in fruitList: if i == 'apple': isapple = True print(isapple) Question 3 for i in fruitList: if i == 'apple': isapple = True else: isapple = False print(isapple) Question 4
isapple = False for i in range(len(fruitList)): if fruitList[i] == 'apple': isapple = True print(isapple)
Q5. The following function amax takes a list ( intList ) of 10 positive integers as an argument. Select the correct option. def amax(intlist): maximum = 0 for num in range(len(intlist)): if intlist[num] > maximum: maximum = intlist[num] return maximum 1 The function will always return the maximum value integer in the parameter intlist . 2 The function will run to completion but may/will not return the max value integer in intlist . 3 The function may/will generate an error. 4 The function may/will loop infinitely.
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
Q6. Given that the file myFile.txt contains scores for students in a test as shown below: Henry 60 Michelle 36 David 88 Linda 52 Marco 48 What is the output of the following code? fh = open('myfile.txt','r') for line in fh.readlines(): items = line.split() if len(items[0]) > 5: print(items[0]) fh.close()
Q7. Given that the file myPara.txt contains the text as shown below: The Cow will Jump Over the Moon Today What is the output of the following code? f = open("myPara.txt", 'r') f2 = f.readline() ll = f2.split() for item in ll: if len(item) > 4: print(item)
Q8. What is the output? a = [1,2,3,4,5,6,7,8,9,10] total = 0 for i in a[::2]: total += a[-i] print(total)
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
Q9. You ask a friend to send you the function to determine if a number is prime or not. Unfortunately, it gets scrambled in the transfer. Unscramble the lines of code below to restore this function. 1 def is_prime(num): 2 for i in range(3,num,2): 3 if num == 2: 4 if num % i == 0: 5 if num % 2 == 0: 6 return False 7 return False 8 return True 9 return True 1. 1, 3, 8, 5, 6, 2, 4, 7, 9 2. 1, 2, 3, 8, 4, 7, 5, 9, 6 3. 1, 3, 9, 6, 5, 2, 4, 7, 8 4. 1, 2, 3, 4, 5, 6, 7, 8, 9 5. 1, 3, 6, 5, 8, 2, 4, 9, 7
Q10. A friend asks for your help on a website project. She explains that when new users register, they enter a phone number with area code. Unfortunately, people enter their numbers in many different ways: One person might use the format “(123)456-7890” another “234-567-8901”, and someone else “(345) 678 9120”, or 5551234567, and many more. She needs a function that will take as a parameter a phone number as a string. It could have any of the different formats. The function must return a cleaned phone-numbers, as a string, with just the numerals grouped together in a standard format and separated by spaces, i.e. “123 456 7890”. Complete the function clean_phone_number that will convert a phone number in any format to a string of just digits, removing everything but the numerals. Because of privacy considerations you may not print the number or ask for any user input inside the function. You may use string functions such as isdigit to help you.
Q11. Program both unindented and indented 12x12 multiplication tables and output them as MTable_U.txt and MTable_I.txt.
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
Q12. You will write a series of 6 functions that take either a string (first 4 functions) or a function with the string as its input (remaining 2 functions) as parameter. Assume that words in the string are separated by blank spaces (‘ ‘) or new lines (‘\n’). For the first 4 functions, as the first step, commas (‘,’) and periods (‘.’) must be excluded from the string. You need to write the following functions: Function 1 [5 points]: string_basics(text) reads an input string text and returns a list of three elements: • total number of words as an integer • total number of characters an integer • average word length as a floating number The outcome should be: [total number of words, total number of characters, average word length] EXAMPLE: More sunshine on tap for much of southeast Texas to start off the new workweek and we are keeping that trend in the forecast for much of this week. Monday will start off with temperatures near 50 degrees and by the afternoon highs climb near 70. A few high level clouds cannot be ruled out, but again it is a predominantly sunny day.