Created
May 17, 2024 23:55
-
-
Save af23me/631f32103d44850c9b5b48a633202097 to your computer and use it in GitHub Desktop.
Test task for ruby
This file contains hidden or 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
# Create a fork and workout the solution. do not capture solution here | |
def find_embedded_word(arr, random_string) | |
arr.detect do |word| | |
return word if random_string.include?(word) | |
random_substring = random_string.scan(/[#{word}]/).join('') | |
word.split('').all? { |letter| random_substring.slice!(letter) } | |
end | |
end | |
words = %w[cat baby dog bird car ax] | |
# string1 = 'tcabnihjs' | |
# find_embedded_word(words, string1) | |
string2 = 'tbcanihjs' | |
p find_embedded_word(words, string2) | |
# -> cat | |
string3 = 'baykkjl' | |
p find_embedded_word(words, string3) | |
# -> None / null | |
string4 = 'bbabylkkj' | |
p find_embedded_word(words, string4) | |
# -> baby | |
string5 = 'ccc' | |
p find_embedded_word(words, string5) | |
# -> None / null | |
string6 = 'breadmaking' | |
p find_embedded_word(words, string6) | |
# -> bird |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment