0

这是我的任务:

编写一个程序,从键盘读取文本,直到找到感叹号('!')。

使用由字母“A”到“Z”下标的整数数组,计算每个字母出现的次数。在单独的计数器中,还计算“其他”字符的总数。

打印出找到最多的字母。(请注意,可能有多个字母附有最大计数。)另外,打印出找到最少次数的字母(或字母),但确保排除根本没有找到的字母。

这是我的代码:

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)

我知道计数器完全错误,但我似乎无法弄清楚。有任何想法吗?

编辑:

如果我输入字符串:“AAAAAAAAAAAAAAAAAAAaaaaaaaaaaaHHHHHh!”

它打印出来:

人物A出现最多。它出现了 29 次。1 字符 A 出现最少。出现了1次。3

显然第一个字符串是正确的,但第二个应该说字符 h 出现最少。

4

3 回答 3

2

在街区

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 将是不同字母被选为计数最高的字母的次数,而不是字母的索引。只需保存字符以便稍后输出,例如

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.")

类似地更改低检查,你应该回到你想要的附近

于 2013-08-15T01:54:36.073 回答
0

最常出现的项目是寻找模式。假设列表已排序,以下将起作用:

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

您可以很容易地在最小逻辑中工作 - 假设第一项是最少出现的,像模式一样跟踪它的计数,并将其存储在更改中。您需要在循环之后进行检查,以确保如果列表中最后排序的项目序列的计数小于存储的最小计数,则您将该项目及其计数设置为正确的值。只需将最终值附加到 return 语句,您就有一个包含 (mode, mode_count, least-occuring, least_count) 的元组。

由于这看起来像是一项家庭作业,我没有对最小材料进行编码,而且我还假设您不允许做一个涉及某些导入库的简单单行。如果您被允许使用它,那么我建议您使用计数器。

于 2013-08-15T01:48:23.203 回答
0

第一个字符串完全是出于运气。count_hi每当您看到一个字符出现的次数超过之前的最大值,然后使用原始消息中的该索引作为最常见的字符时,您就会增加。这是没有意义的。如果A将字符串中的第三个替换为 a B,结果将显示“字符 B 是出现次数最多的字符,出现 28 次”(因为 thenB将位于索引 2 处,它仍然是 的值count_hi + 1)。

您的代码有很多需要改进的地方,但是如果您替换count_hi = count_hi + 1max_chr = character然后打印max_chr而不是msg[count_hi + 1]. 然后您可以对 执行相同的操作min_chr,但请记住,根据您编写代码的方式,它会说这!是最不频繁的,只有 1 次出现。您需要剥离第!一个,或使用不同的方法(您可以轻松做到这一点,因为您已经知道每个字符的计数)。

于 2013-08-15T01:53:35.870 回答