Scientific Computing
Create a file called dictionary-practice.py
and save in your cs100/ch4
folder.
Write a function toPirate(str)
and use a dictionary to solve problem 4.3 on pg 153 (end of chapter 4). Note: you may want to split a sentence into a list of words.
Test your function with a few different inputs. For example:
sentence = "hello there madam how are you"
print( toPirate(sentence) ) # will print "avast there proud beauty how be you"
combineDictionaries(d1, d2)
that takes two dictionaries as parameters and returns a new dictionary that includes the items from both.
Test your function with:
nhl1 = {
"Toronto": "Maple Leafs",
"Montreal": "Canadiens",
"Vancouver": "Canucks",
"Boston": "Bruins",
"Edmonton": "Oilers",
"New York": "Rangers"
}
nhl2 = {
"Detroit": "Red Wings",
"Pittsburgh": "Penguins",
"Philedlphia": "Flyers",
"Calgary": "Flames",
"Tampa Bay": "Lightning"
}
print( combineDictionaries(nhl1, nhl2) )
swapKeyValues(myDictionary)
that takes a dictionary as a parameter such that in the dictionary, the keys are names (as strings) and the values are phone numbers (also as strings). The function should return a new dictionary which swaps the key/value for each element. For example:
people = { "Heather": "330-555-5555", "Bob": "206-111-2222", "Alice": "123-456-7890"}
swapped = swapKeyValues(people)
print(swapped) # {'330-555-5555': 'Heather', '206-111-2222': 'Bob', '123-456-7890': 'Alice'}
Submit your working python file to Moodle.