This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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})) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: |
NewerOlder