Skip to content

Instantly share code, notes, and snippets.

@vjdhama
Created July 29, 2013 21:18
Show Gist options
  • Save vjdhama/6107943 to your computer and use it in GitHub Desktop.
Save vjdhama/6107943 to your computer and use it in GitHub Desktop.
Edx 169.1x Hw 1-3 Anagrams
#!/usr/bin/env ruby
class String
def sort_by_char
self.downcase.split(//).sort.join # Handle all test cases
end
end
def combine_anagrams(words)
sorted_arr = []
words.each do |word|
sorted_arr << word.sort_by_char
end
sorted_arr.uniq!
result = []
sorted_arr.each do |word|
result << words.select { |w| w.sort_by_char == word }
end
result
end
#Alternative One Liner
#def combine_anagrams(words)
# words.group_by {|i| i.downcase.chars.sort.join}.values
#end
puts combine_anagrams(['cars', 'for', 'potatoes', 'racs', 'four', 'scar', 'creams', 'scream'])
@ashish173
Copy link

anagrams cool man, let my internship finish i'll catch up with you..:)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment