daily pastebin goal
84%
SHARE
TWEET

Untitled

a guest Jan 12th, 2019 49 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Turnaround/ Solution to duplicate keys in a Hash
  2. sorted_words = Hash.new(0)
  3. words.each { |word| sorted_words[word]=word.downcase.chars.sort{ |a, b| a.casecmp(b) }.join
  4.    
  5. require 'pp'
  6. require 'set'
  7.  
  8. words = %w[cream scream scream creams]
  9.  
  10. hash = Hash.new{ |h,k| h[k] = Set.new }
  11.  
  12. words.each do |w|
  13.   hash[w.downcase.split('').sort] << w.downcase
  14. end
  15.  
  16. pp hash
  17.  
  18. => {["a", "c", "e", "m", "r"]=>#<Set: {"cream"}>,
  19.  ["a", "c", "e", "m", "r", "s"]=>#<Set: {"scream", "creams"}>}
  20.    
  21. puts hash['creams'.downcase.split('').sort].to_a.join(', ')
  22.    
  23. scream, creams
  24.    
  25. require 'pp'
  26. require 'set'
  27.  
  28. words = %w[cream creams scream scream]
  29.  
  30. hash = Hash.new{ |h,k| h[k] = [] }
  31.  
  32. words.each do |w|
  33.   hash[w.downcase.split('').sort] << w.downcase
  34. end
  35.  
  36. pp hash
  37.  
  38. => {["a", "c", "e", "m", "r"]=>["cream"],
  39.  ["a", "c", "e", "m", "r", "s"]=>["creams", "scream", "scream"]}
  40.  
  41. puts hash['creams'.downcase.split('').sort].to_a.sort.join(', ')
  42.  
  43. => creams, scream, scream
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top