-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
executable file
·137 lines (111 loc) · 3.46 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#!/usr/bin/env python
from typing import List, Tuple
def get_monthly_payment(
annual_interest: float,
loan_amount: int,
years: int,
) -> float:
monthly_interest_payment = annual_interest / 12
number_of_payments = years * 12
n = number_of_payments
i = monthly_interest_payment
group = ((1 + i)**n)
top = i * group
bottom = group - 1
formula = top / bottom
return loan_amount * formula
def get_principle_payment(
total_monthly_payment: float,
outstanding_loan_balance: float,
annual_interest: float,
) -> float:
monthly_interest_rate = annual_interest / 12
return total_monthly_payment - (
outstanding_loan_balance * monthly_interest_rate
)
def get_interest_and_principle_from_payment(
total_monthly_payment: float,
outstanding_loan_balance: float,
annual_interest: float,
) -> Tuple[float, float]:
principle_payment = get_principle_payment(
total_monthly_payment,
outstanding_loan_balance,
annual_interest,
)
return (
total_monthly_payment - principle_payment,
principle_payment,
)
def split_into_chunks(value: str, chunk_size: int) -> List[str]:
start = 0
end = len(value)
chunks = []
for i in range(start, end, chunk_size):
chunks.append(
value[i:i+chunk_size]
)
return chunks
def add_commas(value: str) -> str:
rev = value[::-1]
chunks = split_into_chunks(rev, 3)
joined = ','.join(chunks)
return joined[::-1]
def align_left(value: str, width: int) -> str:
space_needed = width - len(value)
space = ' ' * space_needed
return f'{value}{space}'
def justify(left: str, right: str, width: int) -> str:
space_needed = width - len(right) - len(left)
space = ' ' * space_needed
return f'{left}{space}{right}'
def currency(value: float, width: int) -> str:
with_commas = add_commas(str(int(value)))
return justify('$', with_commas, width)
def print_col(
value: float,
width: int,
) -> None:
prefix = '\t'
print(f'{prefix}{currency(value, width)}', end='')
header_padding = 4
def print_header(columns: List[str]) -> None:
for i, c in enumerate(columns):
prefix = i != 0 and '\t' or ''
print(f'{prefix}{align_left(c, len(c) + header_padding)}', end='')
print('')
def main():
annual_interest_rate = 0.07
loan_amount = 500000
years = 30
total_payment = (
get_monthly_payment(annual_interest_rate, loan_amount, years)
)
loan_balance = loan_amount
columns = [
'period',
'beginning balance',
'total payment',
'interest',
'principle',
'next balance',
]
print_header(columns)
for period in range(years * 12):
print(f'{align_left(str(period + 1), len(columns[0]) + header_padding)}', end='')
print_col(loan_balance, len(columns[1]) + header_padding)
print_col(total_payment, len(columns[2]) + header_padding)
interest, principle = get_interest_and_principle_from_payment(
total_payment,
loan_balance,
annual_interest_rate,
)
print_col(interest, len(columns[3]) + header_padding)
print_col(principle, len(columns[4]) + header_padding)
next_balance = loan_balance - principle
print_col(next_balance, len(columns[5]) + header_padding)
print('')
loan_balance = next_balance
print_header(columns)
if __name__ == '__main__':
main()