crash course on python week 4 quiz answers – Shuffle Q/A 1

29. Fill in the blank to complete the “increments” function. This function should use a list comprehension to create a list of numbers incremented by 2 (n+2). The function receives two variables and should return a list of incremented consecutive numbers between “start” and “end” inclusively (meaning the range should include both the “start” and “end” values). Complete the list comprehension in this function so that input like “squares(2, 3)” will produce the output “[4, 5]”.

30. Fill in the blanks to complete the “endangered_animals” function. This function accepts a dictionary containing a list of endangered animals (keys) and their remaining population (values). For each key in the given “animal_dict” dictionary, format a string to print the name of the animal, with one animal name per line.

31. Consider the following scenario about using Python dictionaries:

Tessa and Rick are hosting a party. Together, they sent out invitations, and collected the responses in a dictionary, with names of their friends and the number of guests each friend will be bringing.

Complete the function so that the “check_guests” function retrieves the number of guests (value) the specified friend “guest” (key) is bringing. This function should:

1. accept a dictionary “guest_list” and a key “guest” variable passed through the function parameters;

2. print the values associated with the key variable.

32. What do the following commands return when genre = "transcendental"?

1. print(genre[:-8])

2. print(genre[-7:9])

  • transc, nd
  • endental, tr
  • dental, trans
  • ran, ental

33. What does the list "car_makes" contain after these commands are executed?

1. car_makes = ["Ford", "Volkswagen", "Toyota"]

2. car_makes.remove("Ford")

  • [null, ‘Porsche’, ‘Toyota’]
  • [‘Toyota’, ‘Ford’]
  • [‘Volkswagen’, ‘Toyota’]
  • [”, ‘Porsche’, ‘Vokswagen’, ‘Toyota’]

34. What do the following commands return?

1. speed_limits = {"street": 35, "highway": 65, "school": 15}

2. speed_limits["highway"]

  • [65]
  • {“highway”: 65}
  • 65
  • [“highway”, 65]

35. Fill in the blank to complete the “first_character” function. This function should return the first character of any string passed in. Complete the string operation needed in this function so that input like "Hello, World" will produce the output "H".

36. Fill in the blanks to complete the “countries” function. This function accepts a dictionary containing a list of continents (keys) and several countries from each continent (values). For each continent, format a string to print the names of the countries only. The values for each key should appear on their own line.

37. What do the following commands return?

host_addresses = {"router": "192.168.1.1", "localhost": "127.0.0.1", "google": "8.8.8.8"}
host_addresses.keys()

  • dict_keys({“router”: “192.168.1.1”, “localhost”: “127.0.0.1”, “google”: “8.8.8.8”})
  • dict_keys([‘192.168.1.1’, ‘127.0.0.1’, ‘8.8.8.8’])
  • dict_keys([“router”, “192.168.1.1”, “localhost”, “127.0.0.1”, “google”, “8.8.8.8”])
  • dict_keys([‘router’, ‘localhost’, ‘google’])

38. Consider the following scenario about using Python dictionaries:

A teacher is using a dictionary to store student grades. The grades are stored as a point value out of 100. Currently, the teacher has a dictionary setup for Term 1 grades and wants to duplicate it for Term 2. The student name keys in the dictionary should stay the same, but the grade values should be reset to 0.

Complete the “setup_gradebook” function so that input like “{"James": 93, "Felicity": 98, "Barakaa": 80}” will produce a resulting dictionary that contains “{"James": 0, "Felicity": 0, "Barakaa": 0}”. This function should:

accept a dictionary “old_gradebook” variable through the function’s parameters;

make a copy of the “old_gradebook” dictionary;

iterate over each key and value pair in the new dictionary;

replace the value for each key with the number 0;

return the new dictionary.

39. The format of the input variable “address_string” is: numeric house number, followed by the street name which may contain numbers and could be several words long (e.g., "123 Main Street", "1001 1st Ave", or "55 North Center Drive").

Complete the string methods needed in this function so that input like "123 Main Street" will produce the output "House number 123 on a street named Main Street". This function should:

1. accept a string through the parameters of the function;

2. separate the address string into new strings: house_number and street_name;

3. return the variables in the string format: "House number X on a street named Y".

40. Complete the function so that input like "This is a sentence." will return a dictionary that holds the count of each letter that occurs in the string: {'t': 2, 'h': 1, 'i': 2, 's': 3, 'a': 1, 'e': 3, 'n': 2, 'c': 1}. This function should:

1. accept a string “text” variable through the function’s parameters;

2. iterate over each character the input string to count the frequency of each letter found, (only letters should be counted, do not count blank spaces, numbers, or punctuation; keep in mind that Python is case sensitive);

3. populate the new dictionary with the letters as keys, ensuring each key is unique, and assign the value for each key with the count of that letter;

4. return the new dictionary.

41. What does the list "colors" contain after these commands are executed?

colors = ["red", "white", "blue"]
colors.insert(2, "yellow")

  • [‘red’, ‘white’, ‘yellow’]
  • [‘red’, ‘yellow’, ‘blue’]
  • [‘red’, ‘white’, ‘yellow’, ‘blue’]
  • [‘red’, ‘yellow’, ‘white’, ‘blue’]

Devendra Kumar

Project Management Apprentice at Google

Leave a Reply