Skip to content

Instantly share code, notes, and snippets.

@joshteng
Created September 11, 2013 18:07
Show Gist options
  • Save joshteng/6527454 to your computer and use it in GitHub Desktop.
Save joshteng/6527454 to your computer and use it in GitHub Desktop.
Ruby Basics 2
# The dictionary is just an arbitrary collection of strings.
# It need not contain English words, e.g., 'etlsm'.
dictionary = ['acres', 'cares', 'Cesar', 'races', 'smelt', 'melts', 'etlsm']
# If the input word happens to be in the dictionary, it should be in the the returned array, too.
# The list should also be case-insensitive.
anagrams_for('acres', dictionary) # => ['acres', 'cares', 'Cesar', 'races']
anagrams_for('ACRES', dictionary) # => ['acres', 'cares', 'Cesar', 'races']
anagrams_for('Cesar', dictionary) # => ['acres', 'cares', 'Cesar', 'races']
# Although "sacre" is not *in* the dictionary, several words in the dictionary are anagrams of "sacre"
anagrams_for('sacre', dictionary) # => ['acres', 'cares', 'Cesar', 'races']
# Neither the input word nor the words in the dictionary need to be valid English words
anagrams_for('etlsm', dictionary) # => ['smelt', 'melts', 'etlsm']
anagrams_for('unicorn', dictionary) # => []
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment