Skip to content

Instantly share code, notes, and snippets.

@jamster
Created May 8, 2012 16:40
Show Gist options
  • Save jamster/2637100 to your computer and use it in GitHub Desktop.
Save jamster/2637100 to your computer and use it in GitHub Desktop.
require 'pp'
def perms(s)
base = [s.downcase, s.upcase]
case s.downcase
when 'a'
base << '@'
when 'o'
base << '0'
when 'e'
base << '3'
when 'i'
base << '!'
end
base
end
def combine(arr1, arr2)
output = []
arr1.each do |a1|
arr2.each do |a2|
output << "#{a1}#{a2}"
end
end
output
end
def guess(string)
string = string.split("") if string.is_a?(String)
start = perms string.shift
string.each do |l|
start = combine(start, perms(l))
end
start
end
puts guess("jamster")
def perms(s)
base = [s.downcase, s.upcase]
case s.downcase
when 'a'
base << '@'
when 'o'
base << '0'
when 'e'
base << '3'
when 'i'
base << '!'
end
base
end
class Array
def *(array)
output = []
return array if self.length == 0
self.each do |a1|
array.each do |a2|
output << "#{a1}#{a2}"
end
end
output
end
end
def guess(string)
string.split("").map{|s| perms(s) }.inject([]){|results, elem| results = results * elem}
end
puts guess 'jamster'
require "benchmark"
def perms(s)
base = [s.downcase, s.upcase]
case s.downcase
when 'a'
base << '@'
when 'o'
base << '0'
when 'e'
base << '3'
when 'i'
base << '!'
end
base
end
def combine(arr1, arr2)
output = []
arr1.each do |a1|
arr2.each do |a2|
output << "#{a1}#{a2}"
end
end
output
end
class Array
def blank?
self.length == 0
end
end
def guess(letters, solution=[])
letters = letters.split('') if letters.is_a?(String)
next_letter = letters.shift
if solution.blank?
return guess(letters, perms(next_letter))
elsif !next_letter.nil?
return guess(letters, combine(solution, perms(next_letter)))
else
return solution
end
end
# when does it get crazy long
# letters = ('a'..'z').to_a
# Benchmark.bmbm do |results|
# (0..25).to_a.each do |i|
# string = letters[0..i].join("")
# results.report(string) do
# output = guess(string)
# puts output.length
# end
# end
# end
puts guess('jamster')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment