Hashmaps
In Python there is a built in hashtable known as a dictionary
The typical time complexity of a hashtable is O(1)
my_set = set([1, 2, 3, 2, 1])
print(my_set)
# What do you notice in the output?
# It didn't print the duplicates
#
# Why do you think Sets are in the same tech talk as Hashmaps/Hashtables?
# The set did not print the duplicates, and hashtables need unique keys
#
lover_album = {
"title": "Lover",
"artist": "Taylor Swift",
"year": 2019,
"genre": ["Pop", "Synth-pop"],
"tracks": {
1: "I Forgot That You Existed",
2: "Cruel Summer",
3: "Lover",
4: "The Man",
5: "The Archer",
6: "I Think He Knows",
7: "Miss Americana & The Heartbreak Prince",
8: "Paper Rings",
9: "Cornelia Street",
10: "Death By A Thousand Cuts",
11: "London Boy",
12: "Soon You'll Get Better (feat. Dixie Chicks)",
13: "False God",
14: "You Need To Calm Down",
15: "Afterglow",
16: "Me! (feat. Brendon Urie of Panic! At The Disco)",
17: "It's Nice To Have A Friend",
18: "Daylight"
}
}
# What data structures do you see?
# Dictionaries and lists
#
# Printing the dictionary
print(lover_album)
print(lover_album.get('tracks'))
# or
print(lover_album['tracks'])
print(lover_album.get('tracks')[4])
# or
print(lover_album['tracks'][4])
lover_album["producer"] = set(['Taylor Swift', 'Jack Antonoff', 'Joel Little', 'Taylor Swift', 'Louis Bell', 'Frank Dukes'])
# What can you change to make sure there are no duplicate producers?
# make it a set
#
# Printing the dictionary
print(lover_album)
lover_album["tracks"].update({19: "All Of The Girls You Loved Before"})
lover_album["genre"].append("Electropop")
# How would add an additional genre to the dictionary, like electropop?
# I did above. I used append, but you could also use insert and give a specific index
#
# Printing the dictionary
print(lover_album)
for k,v in lover_album.items(): # iterate using a for loop for key and value
print(str(k) + ": " + str(v))
for k,v in lover_album["tracks"].items():
print(str(k) + ": " + str(v))
# Write your own code to print tracks in readable format
# I did
#
def search():
search = input("What would you like to know about the album?")
if lover_album.get(search.lower()) == None:
print("Invalid Search")
else:
if search == "tracks":
track = input("Pick a track number")
if lover_album.get('tracks')[int(track)] == None:
print("Invalid Track")
else:
print(lover_album.get("tracks")[int(track)])
else:
print(lover_album.get(search.lower()))
search()
# This is a very basic code segment, how can you improve upon this code?
# I can use several if statements to account for typos or synonyms. I can also add an extra if statement to find a specific track.
#
print("Pros:\n Organized data\n Key/value system helpful for certain data\n getting data is instant")
print("Cons:\n Subject to typos and case sensitive\n Kind of a pain to write out\n Varying keys can be complex")
print("")
print("Dictionaries:\n Key/value system helpful for certain data ")
print("Lists:\n Simple numbered index\n Doesn't need formating")
IOTM = {
"title": "I'm on to Me",
"artist": "Rav",
"year": 2020,
"genre": ["Hip hop", "Rap"],
"producer": ["channel select", "Kill Bill: The Rapper", "Rav", "Scuare"],
"tracks": {
1: "Dandelions (Ft. Kill Bill: The Rapper)",
2: "Me? Never",
3: "Molasses (Ft. Kill Bill: The Rapper & Scuare)",
4: "Prosthetic Self-Love",
5: "Channel F (Ft. Kill Bill: The Rapper)",
6: "Dead End",
7: "To My Future Self (Ft. Scuare)"
}
}
Mr. Yeung, I've heard like two Taylor Swift songs, and I don't know either of their names