Created
August 14, 2019 19:13
-
-
Save aziflaj/23d20cd1679d033f58a65c1738e1610a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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