crash course on python week 4 quiz answers

crash course on python week 4 quiz answers

Week 4 Graded Assessment

17. Fill in the blanks to complete the “confirm_length” function. This function should return how many characters a string contains as long as it has one or more characters, otherwise it will return 0. Complete the string operations needed in this function so that input like "Monday" will produce the output "6".

18. Fill in the blank to complete the “string_words” function. This function should split up the words in the given “string” and return the number of words in the “string”. Complete the string operation and method needed in this function so that a function call like "string_words("Hello, World")" will return the output "2".

def string_words(string):
# Complete the return statement using both a string operation and
# a string method in a single line.
return ___


print(string_words("Hello, World")) # Should print 2
print(string_words("Python is awesome")) # Should print 3
print(string_words("Keep going")) # Should print 2
print(string_words("Have a nice day")) # Should print 4

def string_words(string):
# Complete the return statement using both a string operation and
# a string method in a single line.
returnlen(string.split())


print(string_words(“Hello, World”)) # Should print 2
print(string_words(“Python is awesome”)) # Should print 3
print(string_words(“Keep going”)) # Should print 2
print(string_words(“Have a nice day”)) # Should print 4

19. Consider the following scenario about using Python lists: A professor gave his two assistants, Aniyah and Imani, the task of keeping an attendance list of students in the order they arrived in the classroom. Aniyah was the first one to note which students arrived, and then Imani took over. After class, they each entered their lists into the computer and emailed them to the professor. The professor wants to combine the two lists into one and sort it in alphabetical order.

Complete the code by combining the two lists into one and then sorting the new list. This function should:

1. accept two lists through the function’s parameters;,

2. combine the two lists;

3. sort the combined list in alphabetical order;

4. return the new list.

def alphabetize_lists(list1, list2):


new_list = ___ # Initialize a new list.
___ # Combine the lists.
___ # Sort the combined lists.
new_list = ___ # Assign the combined lists to the "new_list".
return new_list


Aniyahs_list = ["Jacomo", "Emma", "Uli", "Nia", "Imani"]
Imanis_list = ["Loik", "Gabriel", "Ahmed", "Soraya"]


print(alphabetize_lists(Aniyahs_list, Imanis_list))
# Should print: ['Ahmed', 'Emma', 'Gabriel', 'Imani', 'Jacomo', 'Loik', 'Nia', 'Soraya', 'Uli']

def alphabetize_lists(list1, list2):
new_list = list1 + list2 # Combine the two lists using the ‘+’ operator.
new_list.sort() # Sort the combined list using the ‘sort()’ method.
return new_list

Aniyahs_list = [“Jacomo”, “Emma”, “Uli”, “Nia”, “Imani”]
Imanis_list = [“Loik”, “Gabriel”, “Ahmed”, “Soraya”]

print(alphabetize_lists(Aniyahs_list, Imanis_list))
# Should print: [‘Ahmed’, ‘Emma’, ‘Gabriel’, ‘Imani’, ‘Jacomo’, ‘Loik’, ‘Nia’, ‘Soraya’, ‘Uli’]

20. Fill in the blank to complete the “squares” function. This function should use a list comprehension to create a list of squared numbers (using either the expression n*n or n**2). The function receives two variables and should return the list of squares that occur between the “start” and “end” variables 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, 9]”.

def squares(start, end):
return [ ___ ] # Create the required list comprehension.


print(squares(2, 3)) # Should print [4, 9]
print(squares(1, 5)) # Should print [1, 4, 9, 16, 25]
print(squares(0, 10)) # Should print [0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

def squares(start, end):
return [n*n for n inrange(start, end+1)] # Create the required list comprehension.


print(squares(2, 3)) # Should print [4, 9]
print(squares(1, 5)) # Should print [1, 4, 9, 16, 25]
print(squares(0, 10)) # Should print [0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

21. Fill in the blanks to complete the “car_listing” function. This function accepts a “car_prices” dictionary. It should iterate through the keys (car models) and values (car prices) in that dictionary. For each item pair, the function should format a string so that a dictionary entry like ““Kia Soul“:19000” will print "A Kia Soul costs 19000 dollars". Each new string should appear on its own line.

def car_listing(car_prices):
result = ""
# Complete the for loop to iterate through the key and value items
# in the dictionary.
for __
result += ___ # Use a string method to format the required string.
return result

print(car_listing({"Kia Soul":19000, "Lamborghini Diablo":55000,
"Ford Fiesta":13000, "Toyota Prius":24000}))

# Should print:
# A Kia Soul costs 19000 dollars
# A Lamborghini Diablo costs 55000 dollars
# A Ford Fiesta costs 13000 dollars
# A Toyota Prius costs 24000 dollars

def car_listing(car_prices):
result = “”
# Complete the for loop to iterate through the key and value items
# in the dictionary.
for car, price in car_prices.items():
result += “A {} costs {} dollars\n”.format(car, price) # Use a string method to format the required string.
return result

print(car_listing({“Kia Soul”:19000, “Lamborghini Diablo”:55000, “Ford Fiesta”:13000, “Toyota Prius”:24000}))

# Should print:
# A Kia Soul costs 19000 dollars
# A Lamborghini Diablo costs 55000 dollars
# A Ford Fiesta costs 13000 dollars
# A Toyota Prius costs 24000 dollars

22. Consider the following scenario about using Python dictionaries: Tessa and Rick are hosting a party. Both sent out invitations to their friends, and each one collected responses into dictionaries, with names of their friends and how many guests each friend was bringing. Each dictionary is a partial guest list, but Rick's guest list has more current information about the number of guests.

Complete the function to combine both dictionaries into one, with each friend listed only once, and the number of guests from Rick's dictionary taking precedence, if a name is included in both dictionaries. Then print the resulting dictionary. This function should:

1. accept two dictionaries through the function’s parameters;

2. combine both dictionaries into one, with each key listed only once;

3. the values from the “guests1” dictionary taking precedence, if a key is included in both dictionaries;

4. then print the new dictionary of combined items.

def combine_guests(guests1, guests2):
___ # Use a dictionary method to combine the dictionaries.
return guests2

Ricks_guests = { "Adam":2, "Camila":3, "David":1, "Jamal":3, "Charley":2,
"Titus":1, "Raj":4}
Tessas_guests = { "David":4, "Noemi":1, "Raj":2, "Adam":1, "Sakira":3, "Chidi":5}

print(combine_guests(Ricks_guests, Tessas_guests))
# Should print:
# {'David': 1, 'Noemi': 1, 'Raj': 4, 'Adam': 2, 'Sakira': 3, 'Chidi': 5, 'Camila': 3,
'Jamal': 3, 'Charley': 2, 'Titus': 1}

def combine_guests(guests1, guests2):
combined_guests = guests2.copy()
combined_guests.update(guests1)
return combined_guests

Ricks_guests = { “Adam”:2, “Camila”:3, “David”:1, “Jamal”:3, “Charley”:2, “Titus”:1, “Raj”:4}
Tessas_guests = { “David”:4, “Noemi”:1, “Raj”:2, “Adam”:1, “Sakira”:3, “Chidi”:5}

print(combine_guests(Ricks_guests, Tessas_guests))
# Should print:
# {‘David’: 1, ‘Noemi’: 1, ‘Raj’: 4, ‘Adam’: 2, ‘Sakira’: 3, ‘Chidi’: 5, ‘Camila’: 3, ‘Jamal’: 3, ‘Charley’: 2, ‘Titus’: 1}

23. Use a dictionary to count the frequency of numbers in the given “text” string. Only numbers should be counted. Do not count blank spaces, letters, or punctuation. Complete the function so that input like "1001000111101" will return a dictionary that holds the count of each number that occurs in the string {'1': 7, '0': 6}. This function should:

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

2. initialize an new dictionary;

3. iterate over each text character to check if the character is a number’

4. count the frequency of numbers in the input string, ignoring all other characters;

5. populate the new dictionary with the numbers as keys, ensuring each key is unique, and assign the value for each key with the count of that number;

6. return the new dictionary.

24. What do the following commands return when animal = "Hippopotamus"?

print(animal[3:6])
print(animal[-5])
print(animal[10:])

  • pop, t, us
  • ppop, o, s
  • popo, t, mus
  • ppo, t, mus

25. What does the list "music_genres" contain after these commands are executed?

music_genres = ["rock", "pop", "country"]
music_genres.append("blues")

  • [‘rock’, ‘pop’, ‘blues’]
  • [‘rock’, ‘blues’, ‘country’]
  • [‘rock’, ‘blues’, ‘pop’, ‘country’]
  • [‘rock’, ‘pop’, ‘country’, ‘blues’]

26. What do the following commands return?

teacher_names = {"Math": "Aniyah Cook", "Science": "Ines Bisset",
"Engineering": "Wayne Branon"}
teacher_names.values()

  • [‘Aniyah Cook’, ‘Ines Bisset’, ‘Wayne Branon”]
  • [“Math”, “Aniyah Cook”, “Science”, “Ines Bisset”, “Engineering”, “Wayne Branon”]
  • dict_values([‘Aniyah Cook’, ‘Ines Bisset’, ‘Wayne Branon’])
  • {“Math”: “Aniyah Cook”,”Science”: “Ines Bisset”, “Engineering”: “Wayne Branon”}

Shuffle Q/A 1

27. Fill in the blank to complete the “highlight_word” function. This function should change the given “word” to its upper-case version in a given “sentence”. Complete the string method needed in this function so that a function call like "highlight_word("Have a nice day", "nice")" will return the output "Have a NICE day".

28. Consider the following scenario about using Python lists:

A professor gave his two assistants, Jaime and Drew, the task of keeping an attendance list of students in the order they arrive in the classroom. Drew was the first one to note which students arrived, and then Jaime took over. After the class, they each entered their lists into the computer and emailed them to the professor. The professor wants to combine the two lists into one, in the order of each student's arrival. Jaime emailed a follow-up, saying that her list is in reverse order.

Complete the code to combine the two lists into one in the order of: the contents of Drew's list, followed by Jaime’s list in reverse order, to produce an accurate list of the students as they arrived. This function should:

accept two lists through the function’s parameters;

reverse the order of “list1”;

combine the two lists so that “list2” comes first, followed by “list1”;

return the new list.

Devendra Kumar

Project Management Apprentice at Google

Leave a Reply