Created
April 1, 2021 01:54
-
-
Save tangentus/4b2107f5e4f5f474bcf32ce8ba5fff99 to your computer and use it in GitHub Desktop.
Fun with Palindromes
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
KNOWN_PLANINDROMES = %w(racecar hannah).freeze | |
def unique_palindrome(books) | |
palindromes_per_book = {} | |
books.each do |book_name, book_words| | |
palindromes_per_book[book_name] = {} | |
# filter out any unknown palindromes | |
book_words.split(" ").each do |word| | |
next unless KNOWN_PLANINDROMES.include?(word) | |
current_value = palindromes_per_book[book_name][word] | |
palindromes_per_book[book_name][word] = current_value.nil? ? 1 : current_value + 1 | |
end | |
end | |
palindromes_per_book | |
end | |
all_books = { | |
book: "a racecar string of racecar and hannah" | |
} | |
puts unique_palindrome(all_books) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment