hacks for tri 3 review ticket 5 (parallel computing, Hashmaps)
why do my ipynb files not work?
- Parallel computing hacks (start here!)
- HACCCS response
- notes from video
- list comprehension example
- END OF PARALLEL HACKS
- START OF HASHMAP HACKS
- working on hacks
- creating own album with python
- answering questions
Parallel computing hacks (start here!)
Hacks
AP Classroom. Provide answers and thoughts on theoritical question form college board Video in section 4.3. They start at about the 9 minute mark.
- Example 1
- Example 2
Data Structures. Build a List Comprehension example
- list = [calc(item) for item in items]
HACCCS response
notes from video
Sequential computing is a computational model in which operations are performed in order one at a time
Parallel computing is a computational model where the program is broken into multiple smaller sequential computing operations,, some of which are performed simultaneously.
Computers handle a lot of tasks at once
running tasks can be done sequentially, in parallel.
tasks are done one at a time in sequential programming (like conveyor belt)
IN PARALLEL COMPUTING:parallel computing schedules tasks to be executed at same tme parallel computing overall processes data faster and it scales more effectively.
simple Example
Core 1: task A takes 45 ms
Core 2: task B takes 50 ms
Core 3: Task C takes 25 ms; task D takes 30 ms
Core 4: Task E takes 40 ms
Total time: 55 ms (core 3 runs task C and D in sequence)
Basically, 5 tasks are spread out between 4 cores, and all complete tasks at once, finally being done when all cores finish their individual task.
Obviously, if we run all tasks in sequential on 1 core, the time becomes much longer.
Distributive computing: we now distribute our tasks among dfferent computers, not just among 1 physical computer. This needs network connection. Can take longer if network is bad.
Example 1
A particular computer has 2 identical processors which can run in parallel. Each process must be executed on 1 processor and each processor can only run 1 process at a time.
whats the minimum amount of time (approx) to execute all 3 processes when 2 processor run in paallel?
process X: 50 sec
process Y: 10 sec
process Z: 30 sec
I think this would take 50 seconds, as we can run process X on one processor while the other runs Y and Z.
I WAS RIGHT!
This is a pretty good example for why parallel is good, but how can we develop programs for parallel processing if we don't have multiple processors to test on?
Example 2
What is the difference in execution time between runniing the 2 processes in parallel in place of running on single processor?
sequential: 70 seconds
Parallel: 45 seconds
70 - 45 = 25 seconds
My ans: difference of 25 seconds
I was right
difference of 25 seconds is pretty large and seems unrealistic, but I think it demonstrates why we need to optimize for parallel computing.
list comprehension example
Data Structures. Build a List Comprehension example
- list = [calc(item) for item in items]
items = ["cinnamon", "cupcake batter", "fig"]
numbers = [1,2,20,40]
newlist = [x if x != "fig" else "date" for x in items] # remove fig from items
numlist = [x for x in numbers]
sum = 0
for y in numbers:
sum = sum + y
print(sum)
print(newlist)
END OF PARALLEL HACKS
START OF HASHMAP HACKS
Hacks
- Answer ALL questions in the code segments
- Create a diagram or comparison illustration (Canva).
- What are the pro and cons of using this data structure?
- Dictionary vs List
- Expand upon the code given to you, possible improvements in comments
-
Build your own album showing features of a python dictionary
-
For Mr. Yeung's class: Justify your favorite Taylor Swift song, answer may effect seed
working on hacks
creating own album with python
my_album = { # initialize the album need to run each time
"title": "I know how to use dictionaries",
"artist": "artist",
"year": 1984,
"genre": [],
"tracks": {
1: "U.N Owen Was her?",
}
}
def search_personal():
x = 1
while x == 1: # use while loop so we can continue searching
search = input("What would you like to know about the album? (type quit to leave)")
if search == "quit":
print("you have quit lol")
x = 0
elif my_album.get(search.lower()) == None:
print("Invalid Search")
else:
print(my_album.get(search.lower()))
#search_personal()
my_tracks = my_album["tracks"]
def addtracks():
x = 1
while x == 1: # use while loop so we can continue searching
newTrack = input("add a new track (type tracks), change artist (type artist), show album (type search), add genre (type genre) or type quit")
if newTrack == "quit":
x = 0
elif newTrack == "artist":
artists = input("type in artist name")
my_album["artist"] = artists
elif newTrack == "tracks":
newTrackz = input("type in new track name")
newTrack2 = input("add position of new track:")
my_tracks[newTrack2] = newTrackz
elif newTrack == "search":
search_personal()
elif newTrack == "genre":
genres = input("add a genre to the program")
my_album["genre"].append(genres)
newGenre = []
[newGenre.append(x) for x in my_album["genre"] if x not in newGenre] # use list comprehension to remove duplicates (wow im so good at coding)
my_album["genre"] = newGenre # add genre
print(my_album["genre"])
else:
print("unrecognized command!")
addtracks()
#search_personal()
print(my_album["tracks"])
my_set = set([1, 2, 3, 2, 1])
print(my_set)
# What do you notice in the output?
# THE output of the function is {1,2,3}.
# There are no duplicate values
# probably b/c we can't have duplciates in hashes.
# Why do you think Sets are in the same tech talk as Hashmaps/Hashtables?
# Sets are a good example of how collisions in hashing works and why we shouldn't have duplicate
# Sets are a tool we can use for hashing if we don't want to use dictionaries.
# Creating a dictionary with information about the album "Lover"
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?
# Lists
# all of this is compiled into a dictionary
# Printing the dictionary
print(lover_album)
# Retrieve value from dictionary with key
print(lover_album.get('tracks'))
# or
print(lover_album['tracks'])
# adding a value with a new key
lover_album["producer"] = ['Taylor Swift', 'Jack Antonoff', 'Joel Little', 'Taylor Swift', 'Louis Bell', 'Frank Dukes']
# What can you change to make sure there are no duplicate producers?
# Just remove the duplicate manually lol
# or use sets (that worked)
settest = set(lover_album["producer"])
# Printing the dictionary
print(lover_album["producer"])
print(settest)
# Adding a an key-value pair to an existing key
lover_album["tracks"].update({19: "All Of The Girls You Loved Before"})
# How would add an additional genre to the dictionary, like electropop?
#
lover_album["genre"] =["Pop", "Synth-pop", "electropop"] # will work, but maybe there is another way
#
lover_album["genre"].append("new genre") #this method uses list methods but we now have duplicates if we run multiple times
# Printing the dictionary
print(lover_album["genre"])
# Print lover_album in more readable format
for k,v in lover_album.items(): # iterate using a for loop for key and value
#print(str(k) + ": " + str(v)) dont need this....
pass
# Write your own code to print tracks in readable format
#
#
exe = 0
trackS = lover_album["tracks"]
num = 0
for k,v in trackS.items(): # iterate using a for loop for key and value
exe = exe + 1
num = num + 1
print("song", exe, ":", trackS[num])
# Using conditionals to retrieve a random song
def search():
x = 1
while x == 1: # use while loop so we can continue searching
search = input("What would you like to know about the album? (type quit to exit)")
if search == "quit":
x = 0
elif lover_album.get(search.lower()) == None:
print("Invalid Search")
else:
print(lover_album.get(search.lower()))
search()
# This is a very basic code segment, how can you improve upon this code?
# use while loop so that we can continue searching
#