Created
January 29, 2024 01:25
-
-
Save satou-haruka-37/0f472ebce69270c3d54bda4241904303 to your computer and use it in GitHub Desktop.
Ruby Silver inject01
This file contains 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
a = [-1,2,3,4,5] | |
b = (4..6).to_a | |
puts "b:#{b}" | |
puts "---" | |
p a.inject(:+) + b.inject(:+) | |
puts "---" | |
p (a|b).inject(:-).abs | |
p (a|b).inject(:-).abs + (a&b).inject(:+) | |
puts "---" | |
p (a|b).inject(:*) | |
p b.inject(0){|x,y| x + y ** 3} | |
p (a|b).inject(:*) + b.inject(0){|x,y| x + y ** 3} | |
puts "---" | |
p (a||b).map(&:succ) # 0が含まれているためinject(:*)で掛け合わせると0 | |
p (a && b).inject(:*).abs2 | |
p (a||b).map(&:succ).inject(:*) * (a && b).inject(:*).abs2 # 結局ここまでは全て0 | |
p ((a||b).map(&:succ).inject(:*) * (a && b).inject(:*).abs2 + 29 ) # 最後の+29 だけ0じゃない |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment