How to Count Most & Least Common Characters in Input?
This is my assignment:
Write a program which reads in text from the keyboard until an exclamation
mark ('!') is found.
Using an array of integers subscripted by the letters 'A' through 'Z',
count the number occurrences of each letter. In a separate counter, also
count the total number of "other" characters.
Print out which letter was found the most times. (Note there may be more
than one letter which has the maximum count attached to it.) Also, print
out which letter (or letters) was found the least number of times, but
make certain to exclude letters which were not found at all.
And this is my code:
msg = input("What is your message? ")
print ()
num_alpha = 26
int_array = [0] * num_alpha
vowel = [0] * 10000
consanant = [0] * 10000
for alpha in range(num_alpha):
int_array[alpha] = chr(alpha + 65)
if int_array[alpha] == 'A' or int_array[alpha] == 'E' or
int_array[alpha] == 'I' or int_array[alpha] == 'O' or int_array[alpha]
== 'U':
vowel[alpha] = int_array[alpha]
else:
consanant[alpha] = int_array[alpha]
print()
lett = 0
otherch = 0
num_vowels = 0
num_consonants = 0
count_character = [0] * 100000
length = len(msg)
for character in msg.upper():
if character == "!":
otherch = otherch + 1
count_character[ord(character)] = count_character[ord(character)] + 1
break
elif character < "A" or character > "Z":
otherch = otherch + 1
count_character[ord(character)] = count_character[ord(character)] + 1
else:
lett = lett + 1
count_character[ord(character)] = count_character[ord(character)] + 1
alpha = ord(character) - ord('A')
if vowel[(alpha)] == (character):
num_vowels = num_vowels + 1
else:
num_consonants = num_consonants + 1
print()
print("Number of Letters =", lett)
print("Number of Other Characters = ", otherch)
print("Number of Vowels = ", num_vowels)
print("Number of Consanants = ", num_consonants)
print()
for character in msg.upper():
print("Character", character, "appeared" ,
count_character[ord(character)] , "time(s).")
if character == "!":
break
print()
max_letter = -999999999999
min_letter = 999999999999
count_hi = 0
count_low = 0
for character in msg.upper():
if count_character[ord(character)] > max_letter:
max_letter = count_character[ord(character)]
count_hi = count_hi + 1
print("Character" , msg[count_hi + 1] , "appeared the most. It appeared",
max_letter, "times.")
print(count_hi)
for character in msg.upper():
if count_character[ord(character)] < min_letter:
min_letter = count_character[ord(character)]
count_low = count_low + 1
print("Character" , msg[count_low + 1] , "appeared the least. It
appeared", min_letter, "times.")
print(count_low)
I know that the counter is completely wrong but I can't seem to figure it
out. Any ideas?
EDIT:
If i input the string : "AAAAAAAAAAAAAAAAAAAaaaaaaaaaaHHHHHh!"
it prints out:
Character A appeared the most. It appeared 29 times. 1 Character A
appeared the least. It appeared 1 times. 3
obviously the first string is right but the second one should say
character h appeared the least.
No comments:
Post a Comment