coinsort.rb 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/greedy.rb +33 -0
- metadata +46 -0
data/lib/greedy.rb
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
class Greedy
|
2
|
+
def initialize(unit, total, *coins)
|
3
|
+
@total_coins = 0
|
4
|
+
@unit = unit
|
5
|
+
@total = total
|
6
|
+
@currency = coins.sort
|
7
|
+
@currency = @currency.reverse
|
8
|
+
unless @currency.include?(1)
|
9
|
+
@currency.push(1)
|
10
|
+
end
|
11
|
+
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}"
|
19
|
+
end
|
20
|
+
puts "#{@total_coins} total coins"
|
21
|
+
end
|
22
|
+
end
|
23
|
+
def sort_us_coins(total, large_coins=false)
|
24
|
+
if large_coins
|
25
|
+
us = Greedy.new("cents", total, 100, 50, 25, 10, 5, 1)
|
26
|
+
us.sort_coins
|
27
|
+
else
|
28
|
+
us = Greedy.new("cents", total, 25, 10, 5, 1)
|
29
|
+
us.sort_coins
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
|
metadata
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: coinsort.rb
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Solomon Wise
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-05-03 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: ! 'Coin Sorting using the greedy algorithim, documentation coming soon,
|
15
|
+
for now just use the source code: https://gist.github.com/2576516'
|
16
|
+
email: slmnwise@gmail.com
|
17
|
+
executables: []
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- lib/greedy.rb
|
22
|
+
homepage: http://rubygems.org/gems/coinsort.rb
|
23
|
+
licenses: []
|
24
|
+
post_install_message:
|
25
|
+
rdoc_options: []
|
26
|
+
require_paths:
|
27
|
+
- lib
|
28
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
29
|
+
none: false
|
30
|
+
requirements:
|
31
|
+
- - ! '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
35
|
+
none: false
|
36
|
+
requirements:
|
37
|
+
- - ! '>='
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '0'
|
40
|
+
requirements: []
|
41
|
+
rubyforge_project:
|
42
|
+
rubygems_version: 1.8.21
|
43
|
+
signing_key:
|
44
|
+
specification_version: 3
|
45
|
+
summary: Coin Sort
|
46
|
+
test_files: []
|