coinsort.rb 1.0.0 → 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/greedy.rb +18 -22
- metadata +2 -2
data/lib/greedy.rb
CHANGED
@@ -1,36 +1,32 @@
|
|
1
1
|
class Greedy
|
2
|
-
|
3
|
-
|
2
|
+
|
3
|
+
def initialize(unit, coins)
|
4
4
|
@unit = unit
|
5
|
-
@
|
6
|
-
@
|
7
|
-
|
8
|
-
unless @currency.include?(1)
|
9
|
-
@currency.push(1)
|
5
|
+
@demoninations = coins.sort.reverse
|
6
|
+
unless @demoninations.include?(1)
|
7
|
+
@demoninations.push(1)
|
10
8
|
end
|
11
9
|
end
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
10
|
+
|
11
|
+
def print_coins(total)
|
12
|
+
total_coins = 0
|
13
|
+
@demoninations.each do |demonination|
|
14
|
+
quotient = (total / demonination).floor
|
15
|
+
total_coins += quotient
|
16
|
+
total -= demonination * quotient
|
17
|
+
puts "#{quotient}: #{demonination} #{@unit}"
|
19
18
|
end
|
20
|
-
puts "#{
|
19
|
+
puts "#{total_coins} total coins"
|
21
20
|
end
|
22
21
|
end
|
23
|
-
|
22
|
+
|
23
|
+
def print_us_coins(total, large_coins=false)
|
24
24
|
if large_coins
|
25
25
|
us = Greedy.new("cents", total, 100, 50, 25, 10, 5, 1)
|
26
|
-
us.
|
26
|
+
us.print_coins
|
27
27
|
else
|
28
28
|
us = Greedy.new("cents", total, 25, 10, 5, 1)
|
29
|
-
us.
|
29
|
+
us.print_coins
|
30
30
|
end
|
31
31
|
end
|
32
|
-
coins = Greedy.new("ooplas", 304, 28, 17, 5)
|
33
|
-
coins.sort_coins
|
34
|
-
sort_us_coins(543, large_coins=true)
|
35
|
-
|
36
32
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: coinsort.rb
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-05-
|
12
|
+
date: 2012-05-04 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
14
|
description: Coin Sorting using the greedy algorithim.
|
15
15
|
email: slmnwise@gmail.com
|