字符串中最常见的字符 [英] Most common character in a string
问题描述
编写一个函数,该函数将由字母
字符组成的字符串作为输入参数,并返回最常见的字符。
忽略空格,即不要将任何空格都视为字符。
请注意,此处的大小写无关紧要,即小写
字符等于大写字符。如果
之间出现平局,则某些字符返回计数最高的最后一个字符
Write a function that takes a string consisting of alphabetic characters as input argument and returns the most common character. Ignore white spaces i.e. Do not count any white space as a character. Note that capitalization does not matter here i.e. that a lower case character is equal to a upper case character. In case of a tie between certain characters return the last character that has the most count
更新的代码
def most_common_character (input_str):
input_str = input_str.lower()
new_string = "".join(input_str.split())
print(new_string)
length = len(new_string)
print(length)
count = 1
j = 0
higher_count = 0
return_character = ""
for i in range(0, len(new_string)):
character = new_string[i]
while (length - 1):
if (character == new_string[j + 1]):
count += 1
j += 1
length -= 1
if (higher_count < count):
higher_count = count
return (character)
#Main Program
input_str = input("Enter a string: ")
result = most_common_character(input_str)
print(result)
上面是我的代码。我收到字符串索引超出范围
的错误,我不明白为什么。代码也只检查第一个字符的出现,我对如何继续到下一个字符并采用最大计数感到困惑。
The above is my code. I am getting an error of string index out of bound
which I can't understand why. Also the code only checks the occurrence of first character I am confused about how to proceed to the next character and take the maximum count?
运行代码时出现的错误:
> Your answer is NOT CORRECT Your code was tested with different inputs.
> For example when your function is called as shown below:
>
> most_common_character ('The cosmos is infinite')
>
> ############# Your function returns ############# e The returned variable type is: type 'str'
>
> ######### Correct return value should be ######## i The returned variable type is: type 'str'
>
> ####### Output of student print statements ###### thecosmosisinfinite 19
推荐答案
实际上您的代码几乎是正确的。您需要将 count
, j
, length
移到其中您的 for i在范围(0,len(new_string))
中,因为您需要在每次迭代中重新开始,并且如果计数
大于 higher_count
,则需要将该 character
保存为 return_character
并返回它而不是 character
,它总是字符串的最后一个字符,因为 character = new_string [i]
。
Actually your code is almost correct. You need to move count
, j
, length
inside of your for i in range(0, len(new_string))
because you need to start over on each iteration and also if count
is greater than higher_count
, you need to save that charater
as return_character
and return it instead of character
which is always last char of your string because of character = new_string[i]
.
我不明白您为什么使用 j + 1
和而length-1
。纠正它们之后,它现在也涵盖了平局情况。
I don't see why have you used j+1
and while length-1
. After correcting them, it now covers tie situations aswell.
def most_common_character (input_str):
input_str = input_str.lower()
new_string = "".join(input_str.split())
higher_count = 0
return_character = ""
for i in range(0, len(new_string)):
count = 0
length = len(new_string)
j = 0
character = new_string[i]
while length > 0:
if (character == new_string[j]):
count += 1
j += 1
length -= 1
if (higher_count <= count):
higher_count = count
return_character = character
return (return_character)
这篇关于字符串中最常见的字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!