From 228014218621e66a1919391a02e16d4beee57959 Mon Sep 17 00:00:00 2001 From: Juho Date: Wed, 20 Jan 2016 11:20:46 +0200 Subject: [PATCH] t12 --- koodi.rb | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/koodi.rb b/koodi.rb index 932f2b3..8314d18 100644 --- a/koodi.rb +++ b/koodi.rb @@ -1 +1,26 @@ +def luokittelu taulu + hsh = {:negatiivinen=>[], :positiivinen=>[], :parillinen=>[], :pariton=>[], :summa=>taulu.inject(:+)} + taulu.each do |x| + if x.positive? + hsh[:positiivinen].push(x) + elsif x.negative? + hsh[:negatiivinen].push(x) + end + if x.even? + hsh[:parillinen].push(x) + else + hsh[:pariton].push(x) + end + end + hsh[:positiivinen].sort! + hsh[:negatiivinen].sort! + hsh[:parillinen].sort! + hsh[:pariton].sort! + if hsh[:summa]==nil + hsh[:summa] = 0 + end + hsh +end # kirjoita koodi tänne +hash = luokittelu [1, 2, 3, 4, 5, 5] +puts hash.to_s