Skip to content

Instantly share code, notes, and snippets.

@Griminy
Created January 21, 2025 12:08
Show Gist options
  • Save Griminy/15bf0c17a4f2c0794ba739c468db1604 to your computer and use it in GitHub Desktop.
Save Griminy/15bf0c17a4f2c0794ba739c468db1604 to your computer and use it in GitHub Desktop.
is_in_dictionary.rb
#!/usr/bin/env ruby
dict = ENV.fetch('DICTIONARY', ["две", "сотни", "тысячи"])
string = ENV.fetch('WORD', "двесотни")
def in_dictionary?(dictionary:, string:)
formatted_string = string.gsub(/\s/, '')
squashed_dictionary = dictionary.flatten.each_with_object([]) do |i, res|
res << i.to_s unless i.nil? || i.empty?
end.uniq
squashed_dictionary.length.times do |i|
there_is_a_match = squashed_dictionary.permutation(i + 1).map(&:join).include?(string)
return true if there_is_a_match
end
false
end
puts in_dictionary?(dictionary: dict, string: string)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment