In: Computer Science
?
1 2 3 4 5 6 7 |
y: # g: # b: # i: ## n: # c: ## l: # |
?
1 2 3 4 5 6 7 |
b: # n: # i: ## y: # g: # l: # c: ## |
Hint: you may recall that copies of a string (even a 1-character string) may be concatenated together using the * operator.
Define a function histoprint2() that takes a dictionary representing a histogram, and prints out a sortedalmost-graphical representation of the histogram, one entry per output line (unlike histoprint1(), the function histoprint2() should always produce the same output). The entries should be sorted alphabetically by the keys.
For example, histoprint1 histoprint2(makeHistogram("bicycling")) should look something like this:?
1 2 3 4 5 6 7 |
b: # c: ## g: # i: ## l: # n: # y: # |
So for example, histoprint3((makeHistogram("A man, a plan, a canal: panama.")),1) would display the histogram with each hash mark representing one occurrence:
?
1 2 3 4 5 6 7 8 9 10 11 |
: ###### ,: ## .: # :: # A: # a: ######### c: # l: ## m: ## n: #### p: ## |
But histoprint3((makeHistogram("A man, a plan, a canal: panama.")),2) would display the histogram with each hash mark representing two occurrences:
?
1 2 3 4 5 6 7 8 9 10 11 |
: ### ,: # .: :: A: a: #### c: l: # m: # n: ## p: # |
Note: some of the entries now look as if they were empty/blank, because the scale is so big that the frequencies of those infrequent characters are less than a hash mark.
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
>>> myString = """Nah nah nah nah nah nah, nah nah nah, hey Jude Nah nah nah nah nah nah, nah nah nah, hey Jude Nah nah nah nah nah nah, nah nah nah, hey Jude Nah nah nah nah nah nah, nah nah nah, hey Jude Nah nah nah nah nah nah, nah nah nah, hey Jude Nah nah nah nah nah nah, nah nah nah, hey Jude Nah nah nah nah nah nah, nah nah nah, hey Jude Nah nah nah nah nah nah, nah nah nah, hey Jude Nah nah nah nah nah nah, nah nah nah, hey Jude Nah nah nah nah nah nah, nah nah nah, hey Jude Nah nah nah nah nah nah, nah nah nah, hey Jude Nah nah nah nah nah nah, nah nah nah, hey Jude Nah nah nah nah nah nah, nah nah nah, hey Jude Nah nah nah nah nah nah, nah nah nah, hey Jude Nah nah nah nah nah nah, nah nah nah, hey Jude Nah nah nah nah nah nah, nah nah nah, hey Jude""" >>> histoprint4(makeHistogram(myString)) : ##### : ##################################################### ,: ########### J: ##### N: ##### a: ################################################ d: ##### e: ########### h: ##################################################### n: ########################################### u: ##### y: ##### |
###################################################
below are the functions that can be used and referenced in this part
###################################################
def inHistogram(char,charRec):
""" returns True if character is in character Record, otherwise
false
string, charRecord -> Boolean Value"""
return char in charRec
def addToHistogram((char,charRec):
""" returns new character record with updated frequency of each
character occurance
character, charRecord(myCharRecord) -> dictionary of
character records"""
if char in charRec:
charRec[char]+=1
else:
charRec[char]=1
myCharRecord = {'K':1, 'o':3, 'k':1, 'm':1, '?':1}
def makeHistogram(pString):
""" returns number for frequency of character in charRec
dictionary
character, charRecord -> integer"""
aDict={}
for i in pString:
addToHistory(i,aDict)
return aDict
def getFrequency(char,charRec):
""" returns character records for argument entered
string -> dictionary of character records"""
if char in charRec:
return charRec[char]
else:
return 0 # otherwise return 0 because no frequency of that
character
def inHistogram(char, charRec):
""" returns True if character is in character Record, otherwise false
string, charRecord -> Boolean Value"""
return char in charRec
def addToHistogram(char, charRec):
""" returns new character record with updated frequency of each character occurance
character, charRecord(myCharRecord) -> dictionary of character records"""
if char in charRec:
charRec[char]+=1
else:
charRec[char]=1
myCharRecord = {'K': 1, 'o': 3, 'k': 1, 'm': 1, '?': 1}
def makeHistogram(pString):
""" returns number for frequency of character in charRec dictionary
character, charRecord -> integer"""
aDict={}
for i in pString:
addToHistogram(i, aDict)
return aDict
def getFrequency(char, charRec):
""" returns character records for argument entered
string -> dictionary of character records"""
if char in charRec:
return charRec[char]
else:
return 0 # otherwise return 0 because no frequency of that character
def histoprint1(aDict):
for key in aDict:
print(key,"#"*aDict[key],sep=":")
print()
def histoprint2(aDict):
lst = [key for key in aDict]
lst.sort()
for key in lst:
print(key,"#"*aDict[key],sep=":")
print()
def histoprint3(aDict, scale):
lst = [key for key in aDict]
lst.sort()
for key in lst:
print(key,"#"*(aDict[key]//scale),sep=":")
print()
def histoprint4(aDict):
max_fre = 0
lst = [key for key in aDict]
max_fre = max([getFrequency(x, aDict) for x in lst])
scale = (max_fre + 59) // 60
lst.sort()
for key in lst:
print(key,"#"*(aDict[key]//scale),sep=":")
print()
print("histoprint1")
histoprint1(makeHistogram("bicycling"))
print("histoprint2")
histoprint2(makeHistogram("bicycling"))
print("histoprint3")
histoprint3(makeHistogram("bicycling"),2)
print("histoprint4")
histoprint4(makeHistogram("""Nah nah nah nah nah nah, nah nah nah, hey Jude
Nah nah nah nah nah nah, nah nah nah, hey Jude
Nah nah nah nah nah nah, nah nah nah, hey Jude
Nah nah nah nah nah nah, nah nah nah, hey Jude
Nah nah nah nah nah nah, nah nah nah, hey Jude
Nah nah nah nah nah nah, nah nah nah, hey Jude
Nah nah nah nah nah nah, nah nah nah, hey Jude
Nah nah nah nah nah nah, nah nah nah, hey Jude
Nah nah nah nah nah nah, nah nah nah, hey Jude
Nah nah nah nah nah nah, nah nah nah, hey Jude
Nah nah nah nah nah nah, nah nah nah, hey Jude
Nah nah nah nah nah nah, nah nah nah, hey Jude
Nah nah nah nah nah nah, nah nah nah, hey Jude
Nah nah nah nah nah nah, nah nah nah, hey Jude
Nah nah nah nah nah nah, nah nah nah, hey Jude
Nah nah nah nah nah nah, nah nah nah, hey Jude"""))