Skip to content

Instantly share code, notes, and snippets.

@af23me
Created May 17, 2024 23:55
Show Gist options
  • Save af23me/631f32103d44850c9b5b48a633202097 to your computer and use it in GitHub Desktop.
Save af23me/631f32103d44850c9b5b48a633202097 to your computer and use it in GitHub Desktop.
Test task for ruby
# 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