From d27a19f41270b87f4435ed500aa17c79a3fa3342 Mon Sep 17 00:00:00 2001 From: Lensuman Date: Wed, 20 Jan 2016 01:30:24 +0200 Subject: [PATCH] Update koodi.rb --- koodi.rb | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/koodi.rb b/koodi.rb index 932f2b3..54e2398 100644 --- a/koodi.rb +++ b/koodi.rb @@ -1 +1,23 @@ # kirjoita koodi tänne + + +def luokittelu(t) + h = {:negatiivinen => [], :positiivinen => [], :parillinen => [], :pariton => [], :summa => 0} + + t.length.times do |i| + h[:negatiivinen] << t.at(i) if t.at(i) < 0 + h[:positiivinen] << t.at(i) if t.at(i) > -1 + h[:parillinen] << t.at(i) if t.at(i) % 2 == 0 + h[:pariton] << t.at(i) if t.at(i) % 2 == 1 + h[:summa] = h[:summa] + t.at(i) + end + + h[:negatiivinen] = h[:negatiivinen].sort + h[:positiivinen] = h[:positiivinen].sort + h[:parillinen] = h[:parillinen].sort + h[:pariton] = h[:pariton].sort + + return h +end + +puts luokittelu [7, 6, 3, 9, -1, -4, -6, 11, -5]