Created
May 11, 2022 20:44
-
-
Save netmask/6174ea07bacd06e1004dae5dd1b1f203 to your computer and use it in GitHub Desktop.
non consecutive next letter
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 replace(r) | |
starray = r.split("") | |
starray.each_with_index.reduce([]) do |word, current| | |
letter, index = current | |
next_letter = starray[index + 1] | |
word << | |
if letter == '?' | |
get_next_letter(word.last, next_letter) | |
else | |
letter | |
end | |
end.join | |
end | |
def get_next_letter(last, next_letter) | |
return 'a' unless last | |
non_consecutive = last.next | |
return non_consecutive if next_letter == '?' | |
if non_consecutive != next_letter | |
non_consecutive | |
else | |
get_next_letter(non_consecutive, next_letter) | |
end | |
end | |
puts replace("????ab?????") | |
#abcdabcdefg |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment