RubyChangeMaker 1.0.0
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.
- checksums.yaml +7 -0
- data/LICENSE +21 -0
- data/README.md +20 -0
- data/lib/rubychangemaker/rubychangemaker.rb +94 -0
- data/lib/rubychangemaker.rb +2 -0
- metadata +48 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 88d8521b4ce52d0023993f6949292c1d870eadc6
|
4
|
+
data.tar.gz: d70cc3443ed712cdc6d0d195813f7c5e13b26a6a
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: b3475c2dd2a50032373a06c8de15c05d1c999e5b130324578f86226d64adceeafc65d4813b8faec0b635fdc33c118795048e4b87b16ad983b326fa98f61ca12f
|
7
|
+
data.tar.gz: 0a23f0493fbe343e55e7492bab84d3dcb9d75bf705b83c87622bd12a2b86a705c7b4cfe7348a23b4e139c9aa2eb114c0bc35425713a1e3d3b6002b1f350d54e9
|
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2017 Nicholas Sardo
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
# Ruby Change Maker
|
2
|
+
Given a currency amount (assuming US Currency ) returns that amount expressed as the least amount of change using the least number of bills, coins, or combination of bills and coins.
|
3
|
+
## ALL EXPLANATIONS ARE ASSUMING OS X OR LINUX
|
4
|
+
|
5
|
+
### To Run:
|
6
|
+
```
|
7
|
+
cd into bin/ and type: './changemaker' (the file is already chmod'd)
|
8
|
+
```
|
9
|
+
|
10
|
+
### To Run Tests:
|
11
|
+
```
|
12
|
+
(require's cucumber)
|
13
|
+
cd into root directory ( 'changemaker/' ), then type: cucumber
|
14
|
+
```
|
15
|
+
|
16
|
+
### NOTES:
|
17
|
+
|
18
|
+
* Source is in **lib/changemaker/**.
|
19
|
+
* **lib/changemaker.rb** is just some boot-strapping for cucumber.
|
20
|
+
* **features/support/** again, boot-strapping for cucumber.
|
@@ -0,0 +1,94 @@
|
|
1
|
+
# Nicholas Sardo <ruby.code.artist@gmail.com>
|
2
|
+
|
3
|
+
###
|
4
|
+
# Return the change to be tendered for a given amount in the least denomination of currency
|
5
|
+
###
|
6
|
+
module RubyChangeMaker
|
7
|
+
|
8
|
+
class Change
|
9
|
+
|
10
|
+
#google: common usd denominations
|
11
|
+
BILLS = [ 100, 50, 20, 10, 5, 2, 1 ]
|
12
|
+
COINS = [ 50, 25, 10, 5, 1 ]
|
13
|
+
|
14
|
+
def initialize()
|
15
|
+
end
|
16
|
+
|
17
|
+
# split the denomination entered into bills and coins
|
18
|
+
# then, determine change using least amount of bills and coins
|
19
|
+
def make_change( amount )
|
20
|
+
#REMOVED IN REFACTOR: coins = changer( pull_cents( f ), COINS )
|
21
|
+
#REMOVED IN REFACTOR: bills = changer( pull_bills( f ), BILLS )
|
22
|
+
coins = changer( pull_currency( amount ){ | s, idx | currency = s[ idx + 1..-1 ] }, COINS )
|
23
|
+
bills = changer( pull_currency( amount ){ | s, idx | currency = s[ 0...idx ] }, BILLS )
|
24
|
+
[ bills, coins ]
|
25
|
+
end
|
26
|
+
|
27
|
+
private
|
28
|
+
|
29
|
+
#refactor method to reduce code duplication
|
30
|
+
def pull_currency( amount, &block )
|
31
|
+
amount_str = Float( amount ).to_s
|
32
|
+
idx = amount_str.index( "." )
|
33
|
+
currency = block.call( amount_str, idx )
|
34
|
+
currency.to_i
|
35
|
+
end
|
36
|
+
|
37
|
+
def changer ( amount, arr )
|
38
|
+
denominations = Hash.new( 0 )
|
39
|
+
|
40
|
+
#run through each standard currency denomination
|
41
|
+
for current_denomination in arr
|
42
|
+
|
43
|
+
#whole number of times denomination goes into start value
|
44
|
+
how_many = Integer( amount / current_denomination )
|
45
|
+
|
46
|
+
#new start value is remainder from above
|
47
|
+
amount %= current_denomination
|
48
|
+
|
49
|
+
#assign currency denomination as key, number of even divisions as value
|
50
|
+
denominations[ current_denomination ] = how_many
|
51
|
+
end
|
52
|
+
change = Hash.new( 0 )
|
53
|
+
denominations.each { | denomination, quantity | if quantity >= 1 then change[ denomination ] = quantity end }
|
54
|
+
|
55
|
+
change
|
56
|
+
end
|
57
|
+
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
|
62
|
+
=begin
|
63
|
+
###
|
64
|
+
#taken out in refactor
|
65
|
+
###
|
66
|
+
|
67
|
+
def pull_cents( amount )
|
68
|
+
# sanity cast
|
69
|
+
s = Float( amount ).to_s
|
70
|
+
|
71
|
+
# move to the dot in the amount and mark it
|
72
|
+
idx = s.index( "." )
|
73
|
+
|
74
|
+
# move one past the mark, and capture everything to end, i.e. the "cents"
|
75
|
+
cents = s[ idx + 1..-1 ]
|
76
|
+
|
77
|
+
# send it back
|
78
|
+
cents.to_i
|
79
|
+
end
|
80
|
+
|
81
|
+
def pull_bills( amount )
|
82
|
+
# sanity cast
|
83
|
+
s = Float( amount ).to_s
|
84
|
+
|
85
|
+
# find the dot
|
86
|
+
idx = s.index( "." )
|
87
|
+
|
88
|
+
# grab everyting up to but not including the dot, i.e. the "bills"
|
89
|
+
bill = s[ 0...idx ]
|
90
|
+
|
91
|
+
# send it back
|
92
|
+
bill.to_i
|
93
|
+
end
|
94
|
+
=end
|
metadata
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: RubyChangeMaker
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Nicholas Sardo
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-04-24 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: Given an amount of US Currency, returns it as the smallest number of
|
14
|
+
bills, coins, or combination
|
15
|
+
email: nsardo@aol.com
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- LICENSE
|
21
|
+
- README.md
|
22
|
+
- lib/rubychangemaker.rb
|
23
|
+
- lib/rubychangemaker/rubychangemaker.rb
|
24
|
+
homepage: https://rubygems.org/gems/RubyChangeMaker
|
25
|
+
licenses:
|
26
|
+
- MIT
|
27
|
+
metadata: {}
|
28
|
+
post_install_message:
|
29
|
+
rdoc_options: []
|
30
|
+
require_paths:
|
31
|
+
- lib
|
32
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
33
|
+
requirements:
|
34
|
+
- - ">="
|
35
|
+
- !ruby/object:Gem::Version
|
36
|
+
version: '0'
|
37
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - ">="
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '0'
|
42
|
+
requirements: []
|
43
|
+
rubyforge_project:
|
44
|
+
rubygems_version: 2.6.11
|
45
|
+
signing_key:
|
46
|
+
specification_version: 4
|
47
|
+
summary: Returns the least amount of change for a given amount of US Currency
|
48
|
+
test_files: []
|