Skip to content

Instantly share code, notes, and snippets.

@AndrewGuard
Created January 9, 2014 18:06
Show Gist options
  • Select an option

  • Save AndrewGuard/8338953 to your computer and use it in GitHub Desktop.

Select an option

Save AndrewGuard/8338953 to your computer and use it in GitHub Desktop.
# Currently works with seven letter words only
phone_number = "18002435377"
last_seven_digits = phone_number.to_s[-7..-1].split(//).map(&:to_i)
def return_letters(digit)
hash_map = {2 => ["a", "b", "c"],
3 => ["d", "e", "f"],
4 => ["g", "h", "i"],
5 => ["j", "k", "l"],
6 => ["m", "n", "o"],
7 => ["p", "q", "r", "s"],
8 => %w[t u v],
9 => %w[w x y z]
}
hash_map[digit]
end
list1 = return_letters(last_seven_digits[0])
list2 = return_letters(last_seven_digits[1])
list3 = return_letters(last_seven_digits[2])
list4 = return_letters(last_seven_digits[3])
list5 = return_letters(last_seven_digits[4])
list6 = return_letters(last_seven_digits[5])
list7 = return_letters(last_seven_digits[6])
def return_combinations(*args)
p "in return return_combinations"
length = args.length
p "length #{length}"
tmp = []
if length == 0
[]
else
list1, *rest = args
if rest.empty?
list1
else
list2 = rest[0]
tmp = list1.product(list2).map {|x| x.join("")}
rest.shift
# p tmp
# p rest
rest.unshift(tmp)
p rest
if rest.empty?
tmp
else
return_combinations(*rest)
end
end
end
end
puts "======================="
allcombina = return_combinations(list1, list2, list3, list4, list5, list6, list7)
file_data = {}
File.open("/usr/share/dict/words", 'r') do |file|
file.each_line do |line|
file_data[line.chomp.downcase] = 1
end
end
def validate_word(word, dict)
dict.has_key?(word.downcase)
end
def possible_words(list, dict)
list.select { |x| validate_word(x, dict) == true }
end
p possible_words( allcombina,file_data)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment