这是我的任务:
编写一个程序,从键盘读取文本,直到 找到感叹号('!')。
使用由'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)
我知道柜台完全错了,但我似乎无法弄明白。有什么想法吗?
编辑:
如果我输入字符串:“AAAAAAAAAAAAAAAAAAAaaaaaaaaaaHHHHHh!”
打印出来:
角色A出现次数最多。它出现了29次。 1 角色A出现次数最少。它出现了1次。 3
显然第一个字符串是正确的,但第二个字符串应该说字符h出现的最少。
答案 0 :(得分: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.")
同样更改低检查,你应该回到你想要的地方
答案 1 :(得分: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,less-Ocuring,least_count)。
由于这看起来像是家庭作业,我没有编写min材料,我还假设你不允许做一个涉及一些导入库的简单单行程。如果您被允许使用它,那么我建议Counter.
答案 2 :(得分:0)
第一个字符串完全是出于好运。每当您看到一个出现超过之前最大值的字符时,您就会增加count_hi
,然后将原始邮件中的索引用作最常用的字符。这是没有意义的。如果您使用A
替换字符串中的第三个B
,结果会说“字符B是最常出现28个字符的字符”(因为那时B
将位于索引处2,仍然是count_hi + 1
)的值。
您的代码有很多需要改进的地方,但如果您使用count_hi = count_hi + 1
替换max_chr = character
,然后打印max_chr
而不是msg[count_hi + 1]
,则可以轻松获得正确的输出。然后你可以为min_chr
做同样的事情,但请记住,按照你编写代码的方式,它会说!
是最不频繁的,只有1次出现。您需要先剥离!
,或使用其他方法(您可以轻松完成,因为您已经拥有每个角色的计数)。