Little by little output of what I learned through the recently started Codewar
An isogram is a word that has no repeating letters, consecutive or non-consecutive. Implement a function that determines whether a string that contains only letters is an isogram. Assume the empty string is an isogram. Ignore letter case.
Create a method to check if the string passed as an argument is an isogram. However, uppercase and lowercase letters are ignored.
is_isogram("Dermatoglyphics" ) == true
is_isogram("aba" ) == false
is_isogram("moOse" ) == false # -- ignore letter case
def is_isogram(string)
string = string.downcase.chars
if string == string.uniq
return true
else
return false
end
end
(1) First, use downcase
to make the character string passed as an argument all lowercase, and then use chars
to put each character in the array.
(2) If each character put in the array and the array excluding duplicate characters by the ʻuniq methodare the same,
trueis returned. ③ If the array is different,
false` is returned.
def is_isogram(string)
string.downcase.chars.uniq == string.downcase.chars
end
Even if you don't bother to make a conditional branch, it will return true and false with ==
. .. .. ..
Recommended Posts