Advertisement
Guest User

test.rb

a guest
Nov 7th, 2020
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 0.81 KB | None | 0 0
  1. # test.rb
  2.  
  3. def test vv={a: 1, b: 2, c: 3, d: 4, e: 5}
  4.     res= tmp= {}
  5.     #res={}; tmp={}
  6.     loop do
  7.       break if vv.empty?
  8.       vv.each do |a, v|
  9.         v= v.to_f
  10.         if a.to_s.ord.odd?
  11.           res[a]= (res[a]||0) + v
  12.         else
  13.           tmp[(a.to_s.ord+1).chr.to_sym]= (tmp[(a.to_s.ord+1).chr.to_sym]||0) + v
  14.         end
  15.       end
  16.       vv, tmp= tmp, {}  
  17.     end
  18.   res
  19. end
  20.  
  21. def test2 vv={a: 1, b: 2, c: 3, d: 4, e: 5}
  22.     #res= tmp= {}
  23.     res={}; tmp={}
  24.     loop do
  25.       break if vv.empty?
  26.       vv.each do |a, v|
  27.         v= v.to_f
  28.         if a.to_s.ord.odd?
  29.           res[a]= (res[a]||0) + v
  30.         else
  31.           tmp[(a.to_s.ord+1).chr.to_sym]= (tmp[(a.to_s.ord+1).chr.to_sym]||0) + v
  32.         end
  33.       end
  34.       vv, tmp= tmp, {}  
  35.     end
  36.   res
  37. end
  38.  
  39.  
  40. p test==test2
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement