You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
32 lines
619 B
32 lines
619 B
# By alan snape
|
|
# All rights reserved.
|
|
|
|
module FunWithStrings
|
|
def palindrome?
|
|
str = self.downcase.gsub(/(\W|\d)/, '')
|
|
return str == str.reverse
|
|
end
|
|
def count_words
|
|
w = {}
|
|
self.downcase.split(/\W+/).each do |i|
|
|
w.has_key?(i) ? w[i] += 1 : w[i] = 1
|
|
end
|
|
w.delete("")
|
|
return w
|
|
end
|
|
def anagram_groups
|
|
w = {}
|
|
self.split.each do |s|
|
|
i = s.downcase.chars.sort.join
|
|
w.has_key?(i) ? w[i].push(s) : w[i] = [s]
|
|
end
|
|
return w.values
|
|
end
|
|
end
|
|
|
|
# make all the above functions available as instance methods on Strings:
|
|
|
|
class String
|
|
include FunWithStrings
|
|
end
|