我正試圖編寫一個方法來檢查我們是否在字符串中多次看到一個元素。這段代碼返回字符串,但不是布爾值---我不認爲它甚至返回正確的字符串!(Ruby)方法來檢查元素是否已經出現
任何人都可以建議嗎?
def repeating_letters?(str)
# downcase the whole str
# create a "seen letters" array
# iterate through each char in array
# if seen letters contains that character, return true
down = str.downcase
seen = []
down.each_char do |index|
if seen.include?(index)
TRUE
else
seen << index
end
end
end
p repeating_letters?("aA")
應該'repeating_letters( 「AA」)''回報或TRUE''FALSE'? – Stefan
'str.each_char.with_object(Hash.new(0))。any? {| c,h | h [c] + = 1; h [c] == 2}' – aqfaridi
這個問題是重複副本的副本。公平地說,接受的答案是無效的。 –