Code Challenge #1 The Coin Calculator

in codechallenge •  7 years ago  (edited)

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.

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  

Congratulations! This post has been upvoted from the communal account, @minnowsupport, by munchell from the Minnow Support Project. It's a witness project run by aggroed, ausbitbank, teamsteem, theprophet0, and someguy123. The goal is to help Steemit grow by supporting Minnows and creating a social network. Please find us in the Peace, Abundance, and Liberty Network (PALnet) Discord Channel. It's a completely public and open space to all members of the Steemit community who voluntarily choose to be there.

If you like what we're doing please upvote this comment so we can continue to build the community account that's supporting all members.