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.
Files changed (2) hide show
  1. data/lib/greedy.rb +18 -22
  2. metadata +2 -2
data/lib/greedy.rb CHANGED
@@ -1,36 +1,32 @@
1
1
  class Greedy
2
- def initialize(unit, total, *coins)
3
- @total_coins = 0
2
+
3
+ def initialize(unit, coins)
4
4
  @unit = unit
5
- @total = total
6
- @currency = coins.sort
7
- @currency = @currency.reverse
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
- def sort_coins
13
- @currency.each do |x|
14
- @pos = @total / x
15
- @pos = @pos.floor
16
- @total_coins += @pos
17
- @total -= x * @pos
18
- puts "#{@pos}: #{x} #{@unit}"
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 "#{@total_coins} total coins"
19
+ puts "#{total_coins} total coins"
21
20
  end
22
21
  end
23
- def sort_us_coins(total, large_coins=false)
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.sort_coins
26
+ us.print_coins
27
27
  else
28
28
  us = Greedy.new("cents", total, 25, 10, 5, 1)
29
- us.sort_coins
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.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-03 00:00:00.000000000 Z
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