🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉
Avatar of zachyang12

zachyang12's solution

to Ledger in the Python Track

Published at Apr 18 2021 · 0 comments
Instructions
Test suite
Solution

Refactor a ledger printer.

The ledger exercise is a refactoring exercise. There is code that prints a nicely formatted ledger, given a locale (American or Dutch) and a currency (US dollar or euro). The code however is rather badly written, though (somewhat surprisingly) it consistently passes the test suite.

Rewrite this code. Remember that in refactoring the trick is to make small steps that keep the tests passing. That way you can always quickly go back to a working version. Version control tools like git can help here as well.

Please keep a log of what changes you've made and make a comment on the exercise containing that log, this will help reviewers.

Exception messages

Sometimes it is necessary to raise an exception. When you do this, you should include a meaningful error message to indicate what the source of the error is. This makes your code more readable and helps significantly with debugging. Not every exercise will require you to raise an exception, but for those that do, the tests will only pass if you include a message.

To raise a message with an exception, just write it as an argument to the exception type. For example, instead of raise Exception, you should write:

raise Exception("Meaningful message indicating the source of the error")

Running the tests

To run the tests, run pytest ledger_test.py

Alternatively, you can tell Python to run the pytest module: python -m pytest ledger_test.py

Common pytest options

  • -v : enable verbose output
  • -x : stop running tests on first failure
  • --ff : run failures from previous test before running other test cases

For other options, see python -m pytest -h

Submitting Exercises

Note that, when trying to submit an exercise, make sure the solution is in the $EXERCISM_WORKSPACE/python/ledger directory.

You can find your Exercism workspace by running exercism debug and looking for the line that starts with Workspace.

For more detailed information about running tests, code style and linting, please see Running the Tests.

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

ledger_test.py

# -*- coding: utf-8 -*-
import unittest

from ledger import format_entries, create_entry


class LedgerTest(unittest.TestCase):
    maxDiff = 5000

    def test_empty_ledger(self):
        currency = 'USD'
        locale = 'en_US'
        entries = []
        expected = 'Date       | Description               | Change       '
        self.assertEqual(format_entries(currency, locale, entries), expected)

    def test_one_entry(self):
        currency = 'USD'
        locale = 'en_US'
        entries = [
            create_entry('2015-01-01', 'Buy present', -1000),
        ]
        expected = '\n'.join([
            'Date       | Description               | Change       ',
            '01/01/2015 | Buy present               |      ($10.00)',
        ])
        self.assertEqual(format_entries(currency, locale, entries), expected)

    def test_credit_and_debit(self):
        currency = 'USD'
        locale = 'en_US'
        entries = [
            create_entry('2015-01-02', 'Get present', 1000),
            create_entry('2015-01-01', 'Buy present', -1000),
        ]
        expected = '\n'.join([
            'Date       | Description               | Change       ',
            '01/01/2015 | Buy present               |      ($10.00)',
            '01/02/2015 | Get present               |       $10.00 ',
        ])
        self.assertEqual(format_entries(currency, locale, entries), expected)

    def test_multiple_entries_on_same_date_ordered_by_description(self):
        currency = 'USD'
        locale = 'en_US'
        entries = [
            create_entry('2015-01-02', 'Get present', 1000),
            create_entry('2015-01-01', 'Buy present', -1000),
        ]
        expected = '\n'.join([
            'Date       | Description               | Change       ',
            '01/01/2015 | Buy present               |      ($10.00)',
            '01/02/2015 | Get present               |       $10.00 ',
        ])
        self.assertEqual(format_entries(currency, locale, entries), expected)

    def test_final_order_tie_breaker_is_change(self):
        currency = 'USD'
        locale = 'en_US'
        entries = [
            create_entry('2015-01-01', 'Something', 0),
            create_entry('2015-01-01', 'Something', -1),
            create_entry('2015-01-01', 'Something', 1),
        ]
        expected = '\n'.join([
            'Date       | Description               | Change       ',
            '01/01/2015 | Something                 |       ($0.01)',
            '01/01/2015 | Something                 |        $0.00 ',
            '01/01/2015 | Something                 |        $0.01 ',
        ])
        self.assertEqual(format_entries(currency, locale, entries), expected)

    def test_overlong_description(self):
        currency = 'USD'
        locale = 'en_US'
        entries = [
            create_entry('2015-01-01', 'Freude schoner Gotterfunken', -123456),
        ]
        expected = '\n'.join([
            'Date       | Description               | Change       ',
            '01/01/2015 | Freude schoner Gotterf... |   ($1,234.56)',
        ])
        self.assertEqual(format_entries(currency, locale, entries), expected)

    def test_euros(self):
        currency = 'EUR'
        locale = 'en_US'
        entries = [
            create_entry('2015-01-01', 'Buy present', -1000),
        ]
        expected = '\n'.join([
            'Date       | Description               | Change       ',
            u'01/01/2015 | Buy present               |      (€10.00)',
        ])
        self.assertEqual(format_entries(currency, locale, entries), expected)

    def test_dutch_locale(self):
        currency = 'USD'
        locale = 'nl_NL'
        entries = [
            create_entry('2015-03-12', 'Buy present', 123456),
        ]
        expected = '\n'.join([
            'Datum      | Omschrijving              | Verandering  ',
            '12-03-2015 | Buy present               |   $ 1.234,56 ',
        ])
        self.assertEqual(format_entries(currency, locale, entries), expected)

    def test_dutch_locale_and_euros(self):
        currency = 'EUR'
        locale = 'nl_NL'
        entries = [
            create_entry('2015-03-12', 'Buy present', 123456),
        ]
        expected = '\n'.join([
            'Datum      | Omschrijving              | Verandering  ',
            u'12-03-2015 | Buy present               |   € 1.234,56 ',
        ])
        self.assertEqual(format_entries(currency, locale, entries), expected)

    def test_dutch_negative_number_with_3_digits_before_decimal_point(self):
        currency = 'USD'
        locale = 'nl_NL'
        entries = [
            create_entry('2015-03-12', 'Buy present', -12345),
        ]
        expected = '\n'.join([
            'Datum      | Omschrijving              | Verandering  ',
            '12-03-2015 | Buy present               |    $ -123,45 ',
        ])
        self.assertEqual(format_entries(currency, locale, entries), expected)

    def test_american_negative_number_with_3_digits_before_decimal_point(self):
        currency = 'USD'
        locale = 'en_US'
        entries = [
            create_entry('2015-03-12', 'Buy present', -12345),
        ]
        expected = '\n'.join([
            'Date       | Description               | Change       ',
            '03/12/2015 | Buy present               |     ($123.45)',
        ])
        self.assertEqual(format_entries(currency, locale, entries), expected)


if __name__ == '__main__':
    unittest.main()
# -*- coding: utf-8 -*-
from datetime import datetime


class LedgerEntry:
    def __init__(self):
        self.date = None
        self.description = None
        self.change = None


def create_entry(date, description, change):
    entry = LedgerEntry()
    entry.date = datetime.strptime(date, '%Y-%m-%d')
    entry.description = description
    entry.change = change
    return entry


def format_entries(currency, locale, entries):
    if locale == 'en_US':
        # Generate Header Row
        table = 'Date'
        for _ in range(7):
            table += ' '
        table += '| Description'
        for _ in range(15):
            table += ' '
        table += '| Change'
        for _ in range(7):
            table += ' '

        while len(entries) > 0:
            table += '\n'

            # Find next entry in order
            min_entry_index = -1
            for i in range(len(entries)):
                entry = entries[i]
                if min_entry_index < 0:
                    min_entry_index = i
                    continue
                min_entry = entries[min_entry_index]
                if entry.date < min_entry.date:
                    min_entry_index = i
                    continue
                if (
                    entry.date == min_entry.date and
                    entry.change < min_entry.change
                ):
                    min_entry_index = i
                    continue
                if (
                    entry.date == min_entry.date and
                    entry.change == min_entry.change and
                    entry.description < min_entry.description
                ):
                    min_entry_index = i
                    continue
            entry = entries[min_entry_index]
            entries.pop(min_entry_index)

            # Write entry date to table
            month = entry.date.month
            month = str(month)
            if len(month) < 2:
                month = '0' + month
            date_str = month
            date_str += '/'
            day = entry.date.day
            day = str(day)
            if len(day) < 2:
                day = '0' + day
            date_str += day
            date_str += '/'
            year = entry.date.year
            year = str(year)
            while len(year) < 4:
                year = '0' + year
            date_str += year
            table += date_str
            table += ' | '

            # Write entry description to table
            # Truncate if necessary
            if len(entry.description) > 25:
                for i in range(22):
                    table += entry.description[i]
                table += '...'
            else:
                for i in range(25):
                    if len(entry.description) > i:
                        table += entry.description[i]
                    else:
                        table += ' '
            table += ' | '

            # Write entry change to table
            if currency == 'USD':
                change_str = ''
                if entry.change < 0:
                    change_str = '('
                change_str += '$'
                change_dollar = abs(int(entry.change / 100.0))
                dollar_parts = []
                while change_dollar > 0:
                    dollar_parts.insert(0, str(change_dollar % 1000))
                    change_dollar = change_dollar // 1000
                if len(dollar_parts) == 0:
                    change_str += '0'
                else:
                    while True:
                        change_str += dollar_parts[0]
                        dollar_parts.pop(0)
                        if len(dollar_parts) == 0:
                            break
                        change_str += ','
                change_str += '.'
                change_cents = abs(entry.change) % 100
                change_cents = str(change_cents)
                if len(change_cents) < 2:
                    change_cents = '0' + change_cents
                change_str += change_cents
                if entry.change < 0:
                    change_str += ')'
                else:
                    change_str += ' '
                while len(change_str) < 13:
                    change_str = ' ' + change_str
                table += change_str
            elif currency == 'EUR':
                change_str = ''
                if entry.change < 0:
                    change_str = '('
                change_str += u'€'
                change_euro = abs(int(entry.change / 100.0))
                euro_parts = []
                while change_euro > 0:
                    euro_parts.insert(0, str(change_euro % 1000))
                    change_euro = change_euro // 1000
                if len(euro_parts) == 0:
                    change_str += '0'
                else:
                    while True:
                        change_str += euro_parts[0]
                        euro_parts.pop(0)
                        if len(euro_parts) == 0:
                            break
                        change_str += ','
                change_str += '.'
                change_cents = abs(entry.change) % 100
                change_cents = str(change_cents)
                if len(change_cents) < 2:
                    change_cents = '0' + change_cents
                change_str += change_cents
                if entry.change < 0:
                    change_str += ')'
                else:
                    change_str += ' '
                while len(change_str) < 13:
                    change_str = ' ' + change_str
                table += change_str
        return table
    elif locale == 'nl_NL':
        # Generate Header Row
        table = 'Datum'
        for _ in range(6):
            table += ' '
        table += '| Omschrijving'
        for _ in range(14):
            table += ' '
        table += '| Verandering'
        for _ in range(2):
            table += ' '

        while len(entries) > 0:
            table += '\n'

            # Find next entry in order
            min_entry_index = -1
            for i in range(len(entries)):
                entry = entries[i]
                if min_entry_index < 0:
                    min_entry_index = i
                    continue
                min_entry = entries[min_entry_index]
                if entry.date < min_entry.date:
                    min_entry_index = i
                    continue
                if (
                    entry.date == min_entry.date and
                    entry.change < min_entry.change
                ):
                    min_entry_index = i
                    continue
                if (
                    entry.date == min_entry.date and
                    entry.change == min_entry.change and
                    entry.description < min_entry.description
                ):
                    min_entry_index = i
                    continue
            entry = entries[min_entry_index]
            entries.pop(min_entry_index)

            # Write entry date to table
            day = entry.date.day
            day = str(day)
            if len(day) < 2:
                day = '0' + day
            date_str = day
            date_str += '-'
            month = entry.date.month
            month = str(month)
            if len(month) < 2:
                month = '0' + month
            date_str += month
            date_str += '-'
            year = entry.date.year
            year = str(year)
            while len(year) < 4:
                year = '0' + year
            date_str += year
            table += date_str
            table += ' | '

            # Write entry description to table
            # Truncate if necessary
            if len(entry.description) > 25:
                for i in range(22):
                    table += entry.description[i]
                table += '...'
            else:
                for i in range(25):
                    if len(entry.description) > i:
                        table += entry.description[i]
                    else:
                        table += ' '
            table += ' | '

            # Write entry change to table
            if currency == 'USD':
                change_str = '$ '
                if entry.change < 0:
                    change_str += '-'
                change_dollar = abs(int(entry.change / 100.0))
                dollar_parts = []
                while change_dollar > 0:
                    dollar_parts.insert(0, str(change_dollar % 1000))
                    change_dollar = change_dollar // 1000
                if len(dollar_parts) == 0:
                    change_str += '0'
                else:
                    while True:
                        change_str += dollar_parts[0]
                        dollar_parts.pop(0)
                        if len(dollar_parts) == 0:
                            break
                        change_str += '.'
                change_str += ','
                change_cents = abs(entry.change) % 100
                change_cents = str(change_cents)
                if len(change_cents) < 2:
                    change_cents = '0' + change_cents
                change_str += change_cents
                change_str += ' '
                while len(change_str) < 13:
                    change_str = ' ' + change_str
                table += change_str
            elif currency == 'EUR':
                change_str = u'€ '
                if entry.change < 0:
                    change_str += '-'
                change_euro = abs(int(entry.change / 100.0))
                euro_parts = []
                while change_euro > 0:
                    euro_parts.insert(0, str(change_euro % 1000))
                    change_euro = change_euro // 1000
                if len(euro_parts) == 0:
                    change_str += '0'
                else:
                    while True:
                        change_str += euro_parts[0]
                        euro_parts.pop(0)
                        if len(euro_parts) == 0:
                            break
                        change_str += '.'
                change_str += ','
                change_cents = abs(entry.change) % 100
                change_cents = str(change_cents)
                if len(change_cents) < 2:
                    change_cents = '0' + change_cents
                change_str += change_cents
                change_str += ' '
                while len(change_str) < 13:
                    change_str = ' ' + change_str
                table += change_str
        return table

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?