Skip to content

Instantly share code, notes, and snippets.

@aziflaj
Created August 14, 2019 19:13
Show Gist options
  • Save aziflaj/23d20cd1679d033f58a65c1738e1610a to your computer and use it in GitHub Desktop.
Save aziflaj/23d20cd1679d033f58a65c1738e1610a to your computer and use it in GitHub Desktop.
def initial_letter(rules)
lefts = []
rights = []
rules.each do |rule|
left, right = rule.chars
lefts << left
rights << right
end
lefts.each do |chr|
next if rights.include?(chr)
return chr
end
end
def find_follower(string, rules)
predecessor = string.last
next_rule = rules.find do |rule|
rule.chars.first == predecessor
end
next_rule[-1]
end
def find_word(rules)
# find the 1st letter
# join other letters
# voila
initial = initial_letter(rules)
string = [initial]
rules.count.times do
string << find_follower(string, rules)
end
string.join('')
end
puts find_word(["to", "je", "et", "on"]) # => jeton
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment