Code Challenge #1 The Coin Calculator
We recently had interviews at work and gave each prospective employee a coding challenge. It is designed to make sure you are a competent programmer. With this being said our entire department decided to give it a go to see how we fared. Needless to say some did better than others.
Here is the challenge, the objective is simple, you have 30 minutes to write a class that will pass the following tests. The tests are currently in python but can be ported to any language you wish.
from calculator import Calculator
import unittest
class TestCoinCalculator(unittest.TestCase):
def setUp(self):
self.calculator = Calculator()
def test_using_int(self):
self.assertEqual('1 * £20 note, 1 * £2 coin, 1 * £1 coin', self.calculator.calculate(23))
def test_using_float(self):
self.assertEqual('1 * £20 note, 1 * £2 coin, 1 * £1 coin, 1 * 10p coin, 1 * 2p coin, 1 * 1p coin',
self.calculator.calculate(23.13))
def test_using_string(self):
self.assertEqual('1 * £20 note, 1 * £2 coin, 1 * £1 coin', self.calculator.calculate('23'))
def test_with_prefix(self):
self.assertEqual('1 * £20 note, 1 * £2 coin, 1 * £1 coin', self.calculator.calculate('£23'))
def test_with_suffix(self):
self.assertEqual('1 * £20 note, 1 * £2 coin, 1 * £1 coin', self.calculator.calculate('£23.00p'))
def test_multiple_notes(self):
self.assertEqual('2 * £20 note', self.calculator.calculate('40'))
def test_with_fractions(self):
self.assertEqual('1 * £20 note, 1 * 10p coin, 1 * 2p coin, 1 * 1p coin', self.calculator.calculate('20.13'))
def test_less_than_one(self):
self.assertEqual('1 * 10p coin', self.calculator.calculate('0.10'))
def test_complex_amount(self):
self.assertEqual('3 * £20 note, 1 * £2 coin, 1 * £1 coin, 1 * 50p coin, 1 * 5p coin, 1 * 2p coin, 1 * 1p coin',
self.calculator.calculate(63.58))
if __name__ == '__main__':
unittest.main()
I will reveal what I managed to write in the comments later on.
Leave Code Challenge #1 The Coin Calculator to:
Read more #codechallenge posts
Best Posts From Adam
We have not curated any of munchell's posts yet. But you can encourage our curation team to review posts by visiting them regularly and by referring other readers. Because we give priority to frequently read content.
More Posts From Adam
- Forget your troubles and be free - An Inspiring Motorcycle Tale.
- Don't f*ck with another man's v̶e̶h̶i̶c̶l̶e̶ bicycle
- Fight the good fight! 👊
- Tales from another cyclist - The cocktail runs
- All weather cyclist problems...
- Roared at a motorist last night
- ✨ ✨ The magical self healing car! ✨ ✨
- Sunset contest in St Andrews Scotland #sunsetcontest
- Tales of a cyclist - Part 9: Man vs. Machine
- Sometimes you just need to learn to not take life too seriously!