Skip to content

Instantly share code, notes, and snippets.

@AnisahTiaraPratiwi
AnisahTiaraPratiwi / Dictionaries1.py
Created March 23, 2021 09:01
The email_list function receives a dictionary, which contains domain names as keys, and a list of users as values. Fill in the blanks to generate a list that contains complete email addresses (e.g. [email protected]).
def email_list(domains):
emails = []
for domain, users in domains.items():
for user in users:
emails.append("{}@{}".format(user, domain))
return(emails)
print(email_list({"gmail.com": ["clark.kent", "diana.prince", "peter.parker"], "yahoo.com": ["barbara.gordon", "jean.grey"], "hotmail.com": ["bruce.wayne"]}))
@AnisahTiaraPratiwi
AnisahTiaraPratiwi / Dictionaries_vs_Lists
Created March 23, 2021 07:57
In Python, a dictionary can only hold a single value for a given key. To workaround this, our single value can be a list containing multiple values. Here we have a dictionary called "wardrobe" with items of clothing and their colors. Fill in the blanks to print a line for each item of clothing with each color, for example: "red shirt", "blue shi…
wardrobe = {"shirt":["red","blue","white"], "jeans":["blue","black"]}
for item, colors in wardrobe.items():
for color in colors:
print("{} {}".format(color, item))
def count_letters(text):
result = {}
for letter in text:
if letter not in letter:
result[letter] = 0
result[letter] += 1
return result
@AnisahTiaraPratiwi
AnisahTiaraPratiwi / Dictionary_Iteration.py
Created March 23, 2021 07:42
Now, it's your turn! Have a go at iterating over a dictionary! Complete the code to iterate through the keys and values of the cool_beasts dictionary. Remember that the items method returns a tuple of key, value for each element in the dictionary.
cool_beasts = {"octopuses":"tentacles", "dolphins":"fins", "rhinos":"horns"}
for animal, thing in cool_beasts.items():
print("{} have {}".format(animal, thing))
@AnisahTiaraPratiwi
AnisahTiaraPratiwi / Dictionary.py
Created March 23, 2021 07:35
The "toc" dictionary represents the table of contents for a book. Fill in the blanks to do the following: 1) Add an entry for Epilogue on page 39. 2) Change the page number for Chapter 3 to 24. 3) Display the new dictionary contents. 4) Display True if there is Chapter 5, False if there isn't.
toc = {"Introduction":1, "Chapter 1":4, "Chapter 2":11, "Chapter 3":25, "Chapter 4":30}
toc["Epilogue"] = 39
toc["Chapter 3"] = 24
print(toc)
print("Chapter 5" in toc)
___ # Epilogue starts on page 39
___ # Chapter 3 now starts on page 24
___ # What are the current contents of the dictionary?
___ # Is there a Chapter 5?
@AnisahTiaraPratiwi
AnisahTiaraPratiwi / Lists5.py
Created March 23, 2021 06:48
Question 6 The guest_list function reads in a list of tuples with the name, age, and profession of each party guest, and prints the sentence "Guest is X years old and works as __." for each one. For example, guest_list(('Ken', 30, "Chef"), ("Pat", 35, 'Lawyer'), ('Amanda', 25, "Engineer")) should print out: Ken is 30 years old and works as Chef.…
def guest_list(guests):
for name, age, job in guests:
print("{} is {} years old and works as {}".format(name, age, job))
guest_list([('Ken', 30, "Chef"), ("Pat", 35, 'Lawyer'), ('Amanda', 25, "Engineer")])
#Click Run to submit code
"""
Output should match:
Ken is 30 years old and works as Chef
@AnisahTiaraPratiwi
AnisahTiaraPratiwi / Lists4.py
Created March 23, 2021 06:48
Question 5 The group_list function accepts a group name and a list of members, and returns a string with the format: group_name: member1, member2, … For example, group_list("g", ["a","b","c"]) returns "g: a, b, c". Fill in the gaps in this function to do that.
def group_list(group, users):
members =", ".join(users)
return "{}: {}".format(group, members)
print(group_list("Marketing", ["Mike", "Karen", "Jake", "Tasha"])) # Should be "Marketing: Mike, Karen, Jake, Tasha"
print(group_list("Engineering", ["Kim", "Jay", "Tom"])) # Should be "Engineering: Kim, Jay, Tom"
print(group_list("Users", "")) # Should be "Users:"
@AnisahTiaraPratiwi
AnisahTiaraPratiwi / Lists3.py
Created March 23, 2021 06:47
3. Question 3 The permissions of a file in a Linux system are split into three sets of three permissions: read, write, and execute for the owner, group, and others. Each of the three values can be expressed as an octal number summing each permission, with 4 corresponding to read, 2 to write, and 1 to execute. Or it can be written with a string u…
def octal_to_string(octal):
result = ""
value_letters = [(4,"r"),(2,"w"),(1,"x")]
# Iterate over each of the digits in octal
for number in [int(n) for n in str(octal)]:
# Check for each of the permissions values
for value, letter in value_letters:
if number >= value:
result += letter
number -= value
@AnisahTiaraPratiwi
AnisahTiaraPratiwi / Lists2.py
Created March 23, 2021 06:46
Let's create a function that turns text into pig latin: a simple text transformation that modifies each word moving the first character to the end and appending "ay" to the end. For example, python ends up as ythonpay.
def pig_latin(text):
say = ""
# Separate the text into words
temp = []
words = text.split()
for word in words:
# Create the pig latin word and add it to the list
word = word[1:] + word[0] + 'ay'
temp.append(word)
# Turn the list back into a phrase
@AnisahTiaraPratiwi
AnisahTiaraPratiwi / Lists1.py
Created March 23, 2021 06:46
Question 1 Given a list of filenames, we want to rename all the files with extension hpp to the extension h. To do this, we would like to generate a new list called newfilenames, consisting of the new filenames. Fill in the blanks in the code using any of the methods you’ve learned thus far, like a for loop or a list comprehension.
filenames = ["program.c", "stdio.hpp", "sample.hpp", "a.out", "math.hpp", "hpp.out"]
# Generate newfilenames as a list containing the new filenames
# using as many lines of code as your chosen method requires.
newfilenames= [file.replace('.hpp','.h') for file in filenames]
print(newfilenames)
# Should be ["program.c", "stdio.h", "sample.h", "a.out", "math.h", "hpp.out"]