To create a shortest possible isogram checker in Ruby that also scans for hyphens, dashes, and white space, you can use the following code:
main.rb146 chars5 lines
Explanation:
is_isogram?
method takes an input string as a parameter.gsub(/[-—\s]/, '')
replaces all hyphens, dashes, and white space in the input string with an empty string (''), effectively removing them from the string.downcase
converts the string to lowercase to ignore case sensitivity.chars
converts the string into an array of characters.uniq
removes the duplicates from the array of characters.length
returns the length of the array after removing duplicates.Usage:
main.rb215 chars6 lines
In the above example, "programming"
and "Ruby"
are isograms, while "Hello World"
and "race-car"
are not due to the repeated character 'l' and 'r' respectively. The last example "qwerty"
is also an isogram since it doesn't have any repeating characters after removing hyphens, dashes, and white space.
gistlibby LogSnag