Skip to content

Instantly share code, notes, and snippets.

@feiskyer
Created March 3, 2012 06:40
Show Gist options
  • Save feiskyer/1964733 to your computer and use it in GitHub Desktop.
Save feiskyer/1964733 to your computer and use it in GitHub Desktop.
Berkeley SaaS class HW1 Part 3: anagrams
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