Skip to content

Instantly share code, notes, and snippets.

@AnisahTiaraPratiwi
AnisahTiaraPratiwi / Strings_Lists_Dictionaries_7.py
Created March 23, 2021 23:13
Question 7 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 = {}
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
@AnisahTiaraPratiwi
AnisahTiaraPratiwi / Strings_Lists_Dictionaries_6.py
Created March 23, 2021 23:12
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 in…
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))
@AnisahTiaraPratiwi
AnisahTiaraPratiwi / Strings_Lists_Dictionaries_5.py
Created March 23, 2021 23:11
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 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}))
@AnisahTiaraPratiwi
AnisahTiaraPratiwi / Strings_Lists_Dictionaries_4.py
Created March 23, 2021 23:11
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 [x*x for x 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]
@AnisahTiaraPratiwi
AnisahTiaraPratiwi / Strings_Lists_Dictionaries_3.py
Created March 23, 2021 23:10
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 …
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"]
@AnisahTiaraPratiwi
AnisahTiaraPratiwi / Strings_Lists_Dictionaries_2.py
Last active December 27, 2022 17:03
Question 2 The highlight_word function changes the given word in a sentence to its upper-case version. For example, highlight_word("Have a nice day", "nice") returns "Have a NICE day". Can you write this function in just one line?
def highlight_word(sentence, word):
return(sentence.replace(word,word.upper()))
print(highlight_word("Have a nice day", "nice"))
print(highlight_word("Shhh, don't be so loud!", "loud"))
print(highlight_word("Automating with Python is fun", "fun"))
def format_address(address_string):
# Declare variables
house_number = ""
street_name = ""
# Separate the address string into parts
address_string = address_string.split()
# Traverse through the address parts
for number in address_string:
# Determine if the address part is the
# house number or part of the street name
@AnisahTiaraPratiwi
AnisahTiaraPratiwi / Tuples.py
Created March 23, 2021 21:14
Let's use tuples to store information about a file: its name, its type and its size in bytes. Fill in the gaps in this code to return the size in kilobytes (a kilobyte is 1024 bytes) up to 2 decimal places.
def file_size(file_info):
name, type, size= file_info
return("{:.2f}".format(size / 1024))
print(file_size(('Class Assignment', 'docx', 17875))) # Should print 17.46
print(file_size(('Notes', 'txt', 496))) # Should print 0.48
print(file_size(('Program', 'py', 1239))) # Should print 1.21
@AnisahTiaraPratiwi
AnisahTiaraPratiwi / Dictionaries3.py
Created March 23, 2021 09:02
The add_prices function returns the total price of all of the groceries in the dictionary. Fill in the blanks to complete this function.
def add_prices(basket):
# Initialize the variable that will be used for the calculation
total = 0
# Iterate through the dictionary items
for item, price in basket.items():
# Add each price to the total calculation
# Hint: how do you access the values of
# dictionary items?
total += price
# Limit the return value to 2 decimal places
@AnisahTiaraPratiwi
AnisahTiaraPratiwi / Dictionaries2.py
Created March 23, 2021 09:01
The groups_per_user function receives a dictionary, which contains group names with the list of users. Users can belong to multiple groups. Fill in the blanks to return a dictionary with the users as keys and a list of their groups as values.
def groups_per_user(group_dictionary):
user_groups = {}
# Go through group_dictionary
for group, users in group_dictionary.items():
# Now go through the users in the group
for users in group_dictionary[group]:
# Now add the group to the the list of
if users in user_groups:
user_groups[users].append(group)
else: