mirror of
https://github.com/Xevion/exercism.git
synced 2025-12-13 00:11:36 -06:00
ledger exercise pre-locale migration
This commit is contained in:
@@ -6,90 +6,44 @@ class LedgerEntry(object):
|
|||||||
def __init__(self, date, description, change):
|
def __init__(self, date, description, change):
|
||||||
self.date, self.description, self.change = date, description, change
|
self.date, self.description, self.change = date, description, change
|
||||||
|
|
||||||
|
locales = {
|
||||||
|
'en_US' : {
|
||||||
|
'date' : 'Date',
|
||||||
|
'desc' : 'Description',
|
||||||
|
'change' : 'Change'
|
||||||
|
},
|
||||||
|
'nl_NL' : {
|
||||||
|
'date' : 'Datum',
|
||||||
|
'desc' : 'Omschrijving',
|
||||||
|
'change' : 'Verandering'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
currencies = {
|
||||||
|
'USD' : '$',
|
||||||
|
'EUR' : '€'
|
||||||
|
}
|
||||||
|
|
||||||
def create_entry(date, description, change):
|
def create_entry(date, description, change):
|
||||||
return LedgerEntry(datetime.strptime(date, '%Y-%m-%d'), description, change)
|
return LedgerEntry(datetime.strptime(date, '%Y-%m-%d'), description, change)
|
||||||
|
|
||||||
|
|
||||||
def format_entries(currency, locale, entries):
|
def format_entries(currency, locale, entries):
|
||||||
if locale == 'en_US':
|
if locale == 'en_US':
|
||||||
# Generate Header Row
|
# Generate Header Row
|
||||||
table = 'Date'
|
table = ' | '.join([locales[locale]['date'].ljust(10), locales[locale]['desc'].ljust(25), locales[locale]['change'].ljust(13)])
|
||||||
for _ in range(7):
|
|
||||||
table += ' '
|
entries.sort(key=lambda item : (item.date, item.change))
|
||||||
table += '| Description'
|
|
||||||
for _ in range(15):
|
|
||||||
table += ' '
|
|
||||||
table += '| Change'
|
|
||||||
for _ in range(7):
|
|
||||||
table += ' '
|
|
||||||
|
|
||||||
while len(entries) > 0:
|
while len(entries) > 0:
|
||||||
table += '\n'
|
table += '\n'
|
||||||
|
entry = entries.pop(0)
|
||||||
|
|
||||||
# Find next entry in order
|
date_str = "{}/{}/{}".format(str(entry.date.month).zfill(2), str(entry.date.day).zfill(2), str(entry.date.year).zfill(4))
|
||||||
min_entry_index = -1
|
table += date_str + ' | '
|
||||||
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
|
# Write entry description to table
|
||||||
# Truncate if necessary
|
# Truncate if necessary
|
||||||
if len(entry.description) > 25:
|
table += (entry.description[:22] + '...' if len(entry.description) > 25 else entry.description).ljust(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 += ' | '
|
table += ' | '
|
||||||
|
|
||||||
# Write entry change to table
|
# Write entry change to table
|
||||||
@@ -99,6 +53,7 @@ def format_entries(currency, locale, entries):
|
|||||||
change_str = '('
|
change_str = '('
|
||||||
change_str += '$'
|
change_str += '$'
|
||||||
change_dollar = abs(int(entry.change / 100.0))
|
change_dollar = abs(int(entry.change / 100.0))
|
||||||
|
|
||||||
dollar_parts = []
|
dollar_parts = []
|
||||||
while change_dollar > 0:
|
while change_dollar > 0:
|
||||||
dollar_parts.insert(0, str(change_dollar % 1000))
|
dollar_parts.insert(0, str(change_dollar % 1000))
|
||||||
@@ -159,6 +114,7 @@ def format_entries(currency, locale, entries):
|
|||||||
table += change_str
|
table += change_str
|
||||||
return table
|
return table
|
||||||
|
|
||||||
|
|
||||||
elif locale == 'nl_NL':
|
elif locale == 'nl_NL':
|
||||||
# Generate Header Row
|
# Generate Header Row
|
||||||
table = 'Datum'
|
table = 'Datum'
|
||||||
@@ -294,3 +250,13 @@ def format_entries(currency, locale, entries):
|
|||||||
change_str = ' ' + change_str
|
change_str = ' ' + change_str
|
||||||
table += change_str
|
table += change_str
|
||||||
return table
|
return table
|
||||||
|
|
||||||
|
entries = [
|
||||||
|
create_entry('2015-01-02', 'Something', 0),
|
||||||
|
create_entry('2015-01-02', 'Something', -1),
|
||||||
|
create_entry('2015-01-02', 'Something', 1),
|
||||||
|
create_entry('2015-01-01', 'Something', 0),
|
||||||
|
create_entry('2015-01-01', 'Something', -1),
|
||||||
|
create_entry('2015-01-01', 'Something', 1)
|
||||||
|
]
|
||||||
|
print(format_entries('USD', 'en_US', entries))
|
||||||
Reference in New Issue
Block a user