Python Google Question version 2

 

Question: 57
 
A professor with two assistants, Jamie and Drew, wants an attendance list of the students, in the order that they arrived in the classroom. Drew was the first one to note which students arrived, and then Jamie took over. After the class, they each entered their lists into the computer and emailed them to the professor, who needs to combine them into one, in the order of each student's arrival. Jamie emailed a follow-up, saying that her list is in reverse order. Complete the steps to combine them into one list as follows: the contents of Drew's list, 
, to get an accurate list of the students as they arrived.followed by Jamie's list in reverse order
 
 
def combine_lists(list1, list2):
# Generate a new list containing the elements of list2
# Followed by the elements of list1 in reverse order
Jamies_list = ["Alice", "Cindy", "Bobby", "Jan", "Peter"]
Drews_list = ["Mike", "Carol", "Greg", "Marcia"]

print(combine_lists(Jamies_list, Drews_list))


 
 
 Solution:
 
 
def combine_lists(list1, list2):
# Generate a new list containing the elements of list2
# Followed by the elements of list1 in reverse order
new_list = list2
for i in reversed(range(len(list1))):
new_list.append(list1[i])
return new_list
Jamies_list = ["Alice", "Cindy", "Bobby", "Jan", "Peter"]
Drews_list = ["Mike", "Carol", "Greg", "Marcia"]8
print(combine_lists(Jamies_list, Drews_list))


 
 
Question: 58

Use a list comprehension to create a list of squared numbers (n*n). The function receives the variables start and end, and returns a list of squares of consecutive numbers between start and end inclusively. For example, squares(2, 3) should return [4, 9].

 

def squares(start, end):
return [ ___ ]

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

 

 Solution:

def squares(start, end):
return [ n*n for n in range(start, end+1) ]

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

 

 

Question: 59
 Complete the code to iterate through the keys and values of the 
car_prices dictionary, printing out some information about each one.


def car_listing(car_prices):
result = ""
for __:
result += "{} costs {} dollars".__ + "\n"
return result

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



 Solution:
def car_listing(car_prices):
result = ""
for cars in car_prices:
result += "{} costs {} dollars".format(cars,car_prices[cars]) + "\n"
return result

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


Taylor and Rory are hosting a party. They sent out invitations, and each one collected responses into dictionaries, with names of their friends and how many guests each friend is bringing. Each dictionary is a partial list, but Rory's list has more current information about the number of guests. Fill in the blanks to combine both dictionaries into one, with each friend listed only once, and the number of guests from Rory's dictionary taking precedence, if a name is included in both dictionaries. Then print the resulting dictionary.
 
def combine_guests(guests1, guests2):
# Combine both dictionaries into one, with each key listed
# only once, and the value from guests1 taking precedence

Rorys_guests = { "Adam":2, "Brenda":3, "David":1, "Jose":3, "Charlotte":2, "Terry":1, "Robert":4}
Taylors_guests = { "David":4, "Nancy":1, "Robert":2, "Adam":1, "Samantha":3, "Chris":5}

print(combine_guests(Rorys_guests, Taylors_guests))


 
 
  Solution:
def combine_guests(guests1, guests2):
# Combine both dictionaries into one, with each key listed
# only once, and the value from guests1 taking precedence
guests2.update(guests1)
return guests2

Rorys_guests = { "Adam":2, "Brenda":3, "David":1, "Jose":3, "Charlotte":2, "Terry":1, "Robert":4}
Taylors_guests = { "David":4, "Nancy":1, "Robert":2, "Adam":1, "Samantha":3, "Chris":5}

print(combine_guests(Rorys_guests, Taylors_guests))


 
  Question: 60
Use a dictionary to count the frequency of letters in the input string. 
Only letters should be counted, not blank spaces, numbers, or 
punctuation. Upper case should be considered the same as lower case. For
 example, count_letters("This is a sentence.") should return {'t': 2, 
'h': 1, 'i': 2, 's': 3, 'a': 1, 'e': 3, 'n': 2, 'c': 1}. 
 
 
def count_letters(text):
result = {}
# Go through each letter in the text
for letter in ___:
# Check if the letter needs to be counted or not
___
# Add or increment the value in the dictionary
___
return result

print(count_letters("AaBbCc"))
# Should be {'a': 2, 'b': 2, 'c': 2}

print(count_letters("Math is fun! 2+2=4"))
# Should be {'m': 1, 'a': 1, 't': 1, 'h': 1, 'i': 1, 's': 1, 'f': 1, 'u': 1, 'n': 1}

print(count_letters("This is a sentence."))
# Should be {'t': 2, 'h': 1, 'i': 2, 's': 3, 'a': 1, 'e': 3, 'n': 2, 'c': 1}
 

  Solution:

def count_letters(text):
result = {}
text = text.lower()
# Go through each letter in the text
for letter in text:
# Check if the letter needs to be counted or not
if letter .isalpha() and letter not in result:
result[letter] = text.lower().count(letter)
# Add or increment the value in the dictionary
___
return result

print(count_letters("AaBbCc"))
# Should be {'a': 2, 'b': 2, 'c': 2}

print(count_letters("Math is fun! 2+2=4"))
# Should be {'m': 1, 'a': 1, 't': 1, 'h': 1, 'i': 1, 's': 1, 'f': 1, 'u': 1, 'n': 1}

print(count_letters("This is a sentence."))
# Should be {'t': 2, 'h': 1, 'i': 2, 's': 3, 'a': 1, 'e': 3, 'n': 2, 'c': 1}





 Question: 61
What do the following commands return when animal = "Hippopotamus"? 
 
>>> print(animal[3:6])
>>> print(animal[-5])
>>> print(animal[10:])
 
a. ppo, t, mus 
b. pop, t, us
c. pop, t, us 
d. popo, t, mus 
 
 
 
Question: 62
What does the list "colors" contain after these commands are executed? 


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

a. ['red', 'white', 'yellow', 'blue']
b. ['red', 'yellow', 'white', 'blue'] 
c. ['red', 'yellow', 'blue']
d. ['red', 'white', 'yellow']

 

Question: 63
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()



a. {"router": "192.168.1.1", "localhost": "127.0.0.1", "google": "8.8.8.8"}

b. ["router", "192.168.1.1", "localhost", "127.0.0.1", "google", "8.8.8.8"]
c. ['192.168.1.1', '127.0.0.1', '8.8.8.8']
d. ['router', 'localhost', 'google']


 
 
 
إرسال تعليق (0)
أحدث أقدم