Skip to content

Instantly share code, notes, and snippets.

@benkoshy
Last active April 23, 2017 11:12
Show Gist options
  • Select an option

  • Save benkoshy/4600e6e27f80d7893383ee74384afc12 to your computer and use it in GitHub Desktop.

Select an option

Save benkoshy/4600e6e27f80d7893383ee74384afc12 to your computer and use it in GitHub Desktop.
Kata on exchanging coins
class Currency	

	# initialize with the dollar value that you
	# would like converted into coins:
	
	def initialize(amount)
		@amount = amount		
	end

	# call this method to get the change
	# associated for a dollar amount.
	# e.g. Currency.new(15).change should give you [10,5]
	
	def change			
		# start with an empty kitty and progressively add coins to it.
		
		kitty = []		

		until @amount == 0 do	
			# what coin should we add to the kitty?
			key_coin = coin_to_add

			# add coin to kitty

			kitty <<  key_coin

			# once we've added to kitty
			# we need to reduce the total sum
			@amount -= key_coin	

		end		

		kitty
	end

	
	def coin_to_add
		denomination_range_equivalent
	end

	def denomination_range_equivalent
		if @amount < 5
			1
		elsif @amount < 10
			5
		elsif @amount < 25 
			10
		elsif @amount < 50 
			25
		else
			50
		end
	end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment