Today, your challenge is to help Bob with his coffee shop!
Bob's coffee shop is really busy, so busy in fact that if you do not have the right amount of change, you won't get your coffee as Bob doesn't like waiting for people to sort change. Drinks avaialble are Americano £2.20, Latte £2.30, Flat white £2.40 and Filter £3.50
Write a function that returns, "Here is your "+type+", have a nice day!" if they have the right change or returns "Sorry, exact change only, try again tomorrow!"
Want to propose a challenge idea for a future post? Email yo+challenge@dev.to with your suggestions!
This challenge comes from victoriabate on CodeWars. Thank you to CodeWars, who has licensed redistribution of this challenge under the 2-Clause BSD License!
Latest comments (30)
Python
My solution in js
Scala:
Erlang solution with tests.
Just perfect!
Here you have a version using only ruby hashes:
Outputs:
x86_64 assembly (System V ABI, GNU assembler), using the C standard library for printf.
coffee.S:
coffee.h:
I over engineered this but I felt like having a bit of fun with this one so lets have a Go shall we!?
coffee.go
coffee_test.go
I used this challenge as my "hello world" with Go. At first I tried a map thinking that it might be easy to have float64 type as key. Can you please post a solution using a map? Here's mine:
Hello Cherny!
You can most certainly use floats as keys in a map, you just need to indicate what type the keys are in the map declaration.
For your code above you have:
If you want to use floats as the keys, you simply need to indicate as so:
Then you could so something like this:
That would also enable you to pass in a float as an argument to the func instead of a string as well.
Don't forget to include logic for checking if they have exact change!
If you would still like me to post a solution using a map I would be more than happy to.
Note
In Golang, if you attempt to access a value of a map with a key that doesn't exist, you will get the zero value of the type stored in the map. That may be useful for the exact change logic check in your solution.
If we use your map for example:
would return "", since the key 5.0 doesn't exist and "" is the zero value for a string in Golang.
Tweaked to allow purchasing multiple drinks, inspired by Ben Halpern's solution.
Here's mine:
You are correct. I edited my original comment.