Created
March 3, 2012 06:40
-
-
Save feiskyer/1964733 to your computer and use it in GitHub Desktop.
Berkeley SaaS class HW1 Part 3: anagrams
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def anagrams?(w1, w2) | |
w1.downcase.split('').sort == w2.downcase.split('').sort | |
end | |
def combine_anagrams(words) | |
ret=[] | |
while words.length>0 do | |
i=0 | |
temp=[words[i]] | |
j=i+1 | |
while j<words.length do | |
if anagrams?(words[i],words[j]) | |
temp << words[j] | |
words.delete_at(j) | |
else | |
j=j+1 | |
end | |
end | |
ret<< temp | |
words.delete_at(i) | |
end | |
ret | |
end | |
p combine_anagrams(['cars', 'for', 'potatoes', 'racs', 'four','scar', 'creams', 'scream'] ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment