【发布时间】:2020-11-09 07:41:52
【问题描述】:
我正在处理一串字符 alphabet ="AABBBCCCCDDDDDEFGHIJKLMNOPQRSTUVWXYZZZZZZ" 我想创建一个 def 来计算字符串中唯一字符的数量和唯一字符的百分比,而不必使用 alphabet.count("A"), alphabet.count"("B"), alphabet.count("C"), etc etc 所以我不必浪费时间将每个字符繁琐地输入.count() 方法。
从某种意义上说,我取得了成功,我得到了我想要的输出,但是由于我如何构建 for 循环,输出会多次重复每个结果
这是我的代码:
alphabet ="AABBBCCCCDDDDDEFGHIJKLMNOPQRSTUVWXYZZZZZZ"
def count_num_of_uniq_chars(string)
len = string.length
len = len.to_f
for i in 0..len-1
uniq_char=string[i]
puts "uniq_chars --> #{uniq_char}"
count_of_uniq_char = string.count(string[i])
puts "count_of_uniq_char--> #{count_of_uniq_char}"
percent_of_uniq_char = ( (count_of_uniq_char / len) * 100 )
percent_of_uniq_char=percent_of_uniq_char.to_f
puts "there are #{count_of_uniq_char} letter '#{uniq_char}'s in the string which is #{percent_of_uniq_char}% of strings length "
puts
end # loop end
end #def end
count_num_of_uniq_chars(alphabet)
输出为:
uniq_chars --> A
count_of_uniq_char--> 2
there are 2 letter 'A's in the string which is 4.878048780487805% of strings length
uniq_chars --> A
count_of_uniq_char--> 2
there are 2 letter 'A's in the string which is 4.878048780487805% of strings length
uniq_chars --> B
count_of_uniq_char--> 3
there are 3 letter 'B's in the string which is 7.317073170731707% of strings length
uniq_chars --> B
count_of_uniq_char--> 3
there are 3 letter 'B's in the string which is 7.317073170731707% of strings length
uniq_chars --> B
count_of_uniq_char--> 3
there are 3 letter 'B's in the string which is 7.317073170731707% of strings length
uniq_chars --> C
count_of_uniq_char--> 4
there are 4 letter 'C's in the string which is 9.75609756097561% of strings length
uniq_chars --> C
count_of_uniq_char--> 4
there are 4 letter 'C's in the string which is 9.75609756097561% of strings length
uniq_chars --> C
count_of_uniq_char--> 4
there are 4 letter 'C's in the string which is 9.75609756097561% of strings length
uniq_chars --> C
count_of_uniq_char--> 4
there are 4 letter 'C's in the string which is 9.75609756097561% of strings length
uniq_chars --> D
count_of_uniq_char--> 5
there are 5 letter 'D's in the string which is 12.195121951219512% of strings length
uniq_chars --> D
count_of_uniq_char--> 5
there are 5 letter 'D's in the string which is 12.195121951219512% of strings length
uniq_chars --> D
count_of_uniq_char--> 5
there are 5 letter 'D's in the string which is 12.195121951219512% of strings length
uniq_chars --> D
count_of_uniq_char--> 5
there are 5 letter 'D's in the string which is 12.195121951219512% of strings length
uniq_chars --> D
count_of_uniq_char--> 5
there are 5 letter 'D's in the string which is 12.195121951219512% of strings length
uniq_chars --> E
count_of_uniq_char--> 1
there are 1 letter 'E's in the string which is 2.4390243902439024% of strings length
uniq_chars --> F
count_of_uniq_char--> 1
there are 1 letter 'F's in the string which is 2.4390243902439024% of strings length
uniq_chars --> G
count_of_uniq_char--> 1
there are 1 letter 'G's in the string which is 2.4390243902439024% of strings length
uniq_chars --> H
count_of_uniq_char--> 1
there are 1 letter 'H's in the string which is 2.4390243902439024% of strings length
uniq_chars --> I
count_of_uniq_char--> 1
there are 1 letter 'I's in the string which is 2.4390243902439024% of strings length
uniq_chars --> J
count_of_uniq_char--> 1
there are 1 letter 'J's in the string which is 2.4390243902439024% of strings length
uniq_chars --> K
count_of_uniq_char--> 1
there are 1 letter 'K's in the string which is 2.4390243902439024% of strings length
uniq_chars --> L
count_of_uniq_char--> 1
there are 1 letter 'L's in the string which is 2.4390243902439024% of strings length
uniq_chars --> M
count_of_uniq_char--> 1
there are 1 letter 'M's in the string which is 2.4390243902439024% of strings length
uniq_chars --> N
count_of_uniq_char--> 1
there are 1 letter 'N's in the string which is 2.4390243902439024% of strings length
uniq_chars --> O
count_of_uniq_char--> 1
there are 1 letter 'O's in the string which is 2.4390243902439024% of strings length
uniq_chars --> P
count_of_uniq_char--> 1
there are 1 letter 'P's in the string which is 2.4390243902439024% of strings length
uniq_chars --> Q
count_of_uniq_char--> 1
there are 1 letter 'Q's in the string which is 2.4390243902439024% of strings length
uniq_chars --> R
count_of_uniq_char--> 1
there are 1 letter 'R's in the string which is 2.4390243902439024% of strings length
uniq_chars --> S
count_of_uniq_char--> 1
there are 1 letter 'S's in the string which is 2.4390243902439024% of strings length
uniq_chars --> T
count_of_uniq_char--> 1
there are 1 letter 'T's in the string which is 2.4390243902439024% of strings length
uniq_chars --> U
count_of_uniq_char--> 1
there are 1 letter 'U's in the string which is 2.4390243902439024% of strings length
uniq_chars --> V
count_of_uniq_char--> 1
there are 1 letter 'V's in the string which is 2.4390243902439024% of strings length
uniq_chars --> W
count_of_uniq_char--> 1
there are 1 letter 'W's in the string which is 2.4390243902439024% of strings length
uniq_chars --> X
count_of_uniq_char--> 1
there are 1 letter 'X's in the string which is 2.4390243902439024% of strings length
uniq_chars --> Y
count_of_uniq_char--> 1
there are 1 letter 'Y's in the string which is 2.4390243902439024% of strings length
uniq_chars --> Z
count_of_uniq_char--> 6
there are 6 letter 'Z's in the string which is 14.634146341463413% of strings length
uniq_chars --> Z
count_of_uniq_char--> 6
there are 6 letter 'Z's in the string which is 14.634146341463413% of strings length
uniq_chars --> Z
count_of_uniq_char--> 6
there are 6 letter 'Z's in the string which is 14.634146341463413% of strings length
uniq_chars --> Z
count_of_uniq_char--> 6
there are 6 letter 'Z's in the string which is 14.634146341463413% of strings length
uniq_chars --> Z
count_of_uniq_char--> 6
there are 6 letter 'Z's in the string which is 14.634146341463413% of strings length
uniq_chars --> Z
count_of_uniq_char--> 6
there are 6 letter 'Z's in the string which is 14.634146341463413% of strings length
请注意,每个字母的输出语句根据该字母在字符串中出现的次数重复。无论字符串中出现多少次,如何让每个字母输出一次?
【问题讨论】:
-
使用String#count 效率非常低,因为它需要检查字符串中的每个字符是否存在每个唯一字符。您应该使用一种通过字符串进行单次传递的方法。
标签: ruby