سؤال

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.

هل كانت مفيدة؟

المحلول

In the block

for character in msg.upper():
    if count_character[ord(character)] > max_letter:
        max_letter = count_character[ord(character)]
        count_hi = count_hi + 1

count_hi will be the number of times a different letter was selected as the one with the highest count, not the index of the letter. Just save the character to output it later, like

for character in msg.upper():
    if count_character[ord(character)] > max_letter:
        max_letter = count_character[ord(character)]
        high_letter = character

print("Character" , high_letter , "appeared the most. It appeared", max_letter, "times.")

change the low check similarly and you should get back close to what you wanted

نصائح أخرى

The most-occuring item is finding the mode. The following will work assuming the list is sorted:

def get_mode(list):
   current_mode = list[0]
   new_mode = current_mode
   mode_count = 1
   top_count = 1

   for idx in range(0, len(list)):
      if list[idx] == modeChar:
         mode_count += 1
      else:
         if mode_count > top_count:
            new_mode = current_mode
            top_count = mode_count

         current_mode = char
         current_count = 1

   if mode_count > top_count:
      new_mode = current_mode
      top_count = current_count

   return new_mode, top_count

You can work in the min logic pretty easily - assume the first item is the least-occuring, keep track of its count like the mode, and store it on change. You'll need to include a check after the loop to ensure that if the last sorted sequence of items in the list has a count less than the stored minimum count, that you set that item and its count to the proper values. Just append the final values to the return statement, and you have a tuple with (mode, mode_count, least-occuring, least_count).

Since this looks like a homework assignment I didn't code the min material, and I'm also assuming you aren't allowed to do a simple one-liner involving some imported library. If you are allowed to use that, then I would suggest a Counter.

The first string is right out of sheer luck. You are increasing count_hi whenever you see a character that appears more than the previous maximum and then use that index from the original message as the most frequent character. That makes no sense. If you replaced the third A in the string with a B, the result would say that "character B is the most frequent character with 28 occurrences" (because then B would be at index 2, which would still be the value of count_hi + 1).

There is a lot to improve in your code, but you can easily get the right ouput if you replace count_hi = count_hi + 1 with max_chr = character and then print max_chr instead of msg[count_hi + 1]. Then you can do the same for the min_chr, but bear in mind that with the way you wrote the code, it will say that ! is the least frequent, with 1 occurrence. You need to strip the ! first, or use a different approach (which you could easily do, since you already have the counts for each character).

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top