diff --git a/thinkcspy/Chapter-12-Exercises/Lab1.py b/thinkcspy/Chapter-12-Exercises/Lab1.py new file mode 100644 index 0000000..4f4b0d2 --- /dev/null +++ b/thinkcspy/Chapter-12-Exercises/Lab1.py @@ -0,0 +1,12 @@ +def countAll(text): + wordDict = {} + + for char in text: + if char not in wordDict and char.isalpha(): + wordDict[char] = text.count(char) + + return wordDict + + +print(countAll("banana")) +print(countAll("curly fries 123!")) \ No newline at end of file diff --git a/thinkcspy/Chapter-12-Exercises/Problem1.py b/thinkcspy/Chapter-12-Exercises/Problem1.py new file mode 100644 index 0000000..f1e13f0 --- /dev/null +++ b/thinkcspy/Chapter-12-Exercises/Problem1.py @@ -0,0 +1,29 @@ +def countAll(text): + wordDict = {} + text = text.lower() + + for char in text: + if char not in wordDict and char.isalpha(): + wordDict[char] = text.count(char) + + return wordDict + + +def sortList(text): + wordDict = countAll(text) + alphaList = list(wordDict.items()) + alphaList.sort() + + return alphaList + + +def printResult(text): + alphaList = sortList(text) + + print("You inputed: " + text) + + for char in alphaList: + print(char[0] + ": " + str(char[1])) + + +printResult(input("What word(s) would you like sorted?")) \ No newline at end of file diff --git a/thinkcspy/Chapter-12-Exercises/Problem2.py b/thinkcspy/Chapter-12-Exercises/Problem2.py new file mode 100644 index 0000000..f799f01 --- /dev/null +++ b/thinkcspy/Chapter-12-Exercises/Problem2.py @@ -0,0 +1,20 @@ +def add_fruit(inventory, fruit, quantity=0): + + if fruit not in inventory: + inventory[fruit] = quantity + else: + inventory[fruit] += quantity + + return inventory + +# make these tests work... +new_inventory = {} +print(new_inventory) +add_fruit(new_inventory, 'strawberries', 10) +# test that 'strawberries' in new_inventory +print(new_inventory.keys()) +# test that new_inventory['strawberries'] is 10 +print(new_inventory) +add_fruit(new_inventory, 'strawberries', 25) +# test that new_inventory['strawberries'] is now 35) +print(new_inventory) \ No newline at end of file