Add Lab1, Problem1 and Problem2
This commit is contained in:
parent
973661fcbf
commit
5978779a9a
3 changed files with 61 additions and 0 deletions
29
thinkcspy/Chapter-12-Exercises/Problem1.py
Normal file
29
thinkcspy/Chapter-12-Exercises/Problem1.py
Normal file
|
|
@ -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?"))
|
||||
Loading…
Reference in a new issue