give the regex version (such as def isogram?(w) = p !w.match(/(.)(?:\1|-|\d)/i) for ` def isogram?(词) = 词.downcase.scan(/\w/) == 词.downcase.scan(/\w/).uniq` in ruby

The regex version of the def isogram?(w) = p !w.match(/(.)(?:\1|-|\d)/i) in Ruby would be the following:

main.rb
def isogram?(word)
  !(word =~ /(.)(?:\1|-|\d)/i)
end
54 chars
4 lines

Explanation:

  • word =~ /(.)(?:\1|-|\d)/i is the regular expression pattern that checks for the presence of a repeated character (\1 represents the same character as group 1), a hyphen (-), or a digit (\d).
  • The ! operator is used to negate the result of the match, so if any of these patterns are found, the method returns false, indicating that it is not an isogram. Otherwise, it returns true, indicating that it is an isogram.

Keep in mind that Ruby supports multiple ways of defining methods, and both examples are functionally equivalent.

related categories

gistlibby LogSnag