Skip to content

Instantly share code, notes, and snippets.

@tomharris
Created December 5, 2012 03:08
Show Gist options
  • Save tomharris/4211822 to your computer and use it in GitHub Desktop.
Save tomharris/4211822 to your computer and use it in GitHub Desktop.
class WordDictionary
def initialize(dictionary_filename = '/usr/share/dict/words')
@words = File.readlines(dictionary_filename)
@words = @words.collect { |word| word.strip } # File.readlines preserves the newlines at the end of the line
end
def words_with_five_vowels_in_ascending_order
@words.select { |word| word.count('aeiou') == 5 && word =~ /.*?[aA].*?[eE].*?[iI].*?[oO].*?[uU].*?/ }
end
def words_at_least_six_characters_with_letters_in_ascending_order
# words have to be normalized to the same capitalization, duplicate chars removed, and then sorted.
@words.select { |word| word.length >= 6 && word.downcase == word.downcase.squeeze.chars.sort.join }
end
end
dictionary = WordDictionary.new
puts '1. Find all the words in a dictionary that contain exactly five vowels (a, e, i, o and u) in ascending order.'
puts dictionary.words_with_five_vowels_in_ascending_order.join("\n")
#=> abstemious
#=> abstemiously
#=> abstentious
#=> acheilous
#=> acheirous
#=> acleistous
#=> affectious
#=> annelidous
#=> arsenious
#=> arterious
#=> bacterious
#=> caesious
#=> facetious
#=> facetiously
#=> fracedinous
#=> majestious
puts '2. Find all the words in a dictionary of length at least six that contain letters in strictly ascending alphabetical order.'
puts dictionary.words_at_least_six_characters_with_letters_in_ascending_order.join("\n")
#=> abdest
#=> acknow
#=> Adelops
#=> adipsy
#=> agnosy
#=> almost
#=> befist
#=> behint
#=> beknow
#=> bijoux
#=> biopsy
#=> chintz
#=> dehors
#=> dehort
#=> Deimos
#=> deinos
#=> dimpsy
#=> egilops
#=> ghosty
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment