Skip to content

Instantly share code, notes, and snippets.

@tangentus
Created April 1, 2021 01:54
Show Gist options
  • Save tangentus/4b2107f5e4f5f474bcf32ce8ba5fff99 to your computer and use it in GitHub Desktop.
Save tangentus/4b2107f5e4f5f474bcf32ce8ba5fff99 to your computer and use it in GitHub Desktop.
Fun with Palindromes
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