Studying at CodeWar (ruby) ②

About this article

Little by little output of what I learned through the recently started Codewar

problem

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

The answer I thought

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.

The ideal answer

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

Studying at CodeWar (ruby) ②
Studying at CodeWar (ruby) ⑥ inject
Studying at CodeWar
Studying at CodeWar (ruby) ③ squeeze, gsub
Studying with CodeWar (ruby) ⑤ proc
Studying with CodeWar (ruby) ④ case ~ when
Start studying Ruby
<First post> I started studying Ruby
[At Coder] Introducing Competitive Pro with Ruby