Adam avatar

Code Challenge #1 The Coin Calculator

munchell

Published: 04 Jul 2017 › Updated: 04 Jul 2017Code Challenge #1 The Coin Calculator

Code Challenge #1 The Coin Calculator

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:

Written by

The cycling scottish software engineer with a stunning family. I love anything with wheels.

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