Skip to content

Commit 6d242b2

Browse files
Expand depreciation
1 parent c3d4b9e commit 6d242b2

1 file changed

Lines changed: 88 additions & 11 deletions

File tree

Lines changed: 88 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def straight_line_depreciation(
3535
residual_value: float = 0.0,
3636
) -> list[float]:
3737
"""
38-
Calculate the depreciation expenses over the given period
38+
Calculate the depreciation expenses over the given period using the straight-line method
3939
:param useful_years: Number of years the asset will be used
4040
:param purchase_value: Purchase expenditure for the asset
4141
:param residual_value: Residual value of the asset at the end of its useful life
@@ -89,15 +89,92 @@ def straight_line_depreciation(
8989

9090
return list_of_depreciation_expenses
9191

92+
def declining_balance_depreciation(useful_years: int,
93+
purchase_value: float,
94+
residual_value: float = 0.0,):
95+
"""
96+
Calculate the depreciation expenses over the given period using the declining balance method
97+
:param useful_years: Number of years the asset will be used
98+
:param purchase_value: Purchase expenditure for the asset
99+
:param residual_value: Residual value of the asset at the end of its useful life
100+
:return: A list of annual depreciation expenses over the asset's useful life
101+
"""
102+
103+
if not isinstance(useful_years, int):
104+
raise TypeError("Useful years must be an integer")
105+
106+
if useful_years < 1:
107+
raise ValueError("Useful years cannot be less than 1")
108+
109+
if not isinstance(purchase_value, (float, int)):
110+
raise TypeError("Purchase value must be numeric")
111+
112+
if not isinstance(residual_value, (float, int)):
113+
raise TypeError("Residual value must be numeric")
114+
115+
if purchase_value < 0.0:
116+
raise ValueError("Purchase value cannot be less than zero")
117+
118+
if purchase_value < residual_value:
119+
raise ValueError("Purchase value cannot be less than residual value")
120+
121+
depreciation_rate = 1 - (residual_value / purchase_value) ** (1 / useful_years)
122+
book_value = purchase_value
123+
124+
list_of_depreciation_expenses = []
125+
126+
for i in range(1, useful_years+1):
127+
new_book_value = purchase_value * ((1 - depreciation_rate) ** i)
128+
list_of_depreciation_expenses.append(book_value - new_book_value)
129+
book_value = new_book_value
130+
131+
return list_of_depreciation_expenses
132+
133+
def sum_of_years_digits_depreciation(useful_years: int,
134+
purchase_value: float,
135+
residual_value: float = 0.0,):
136+
"""
137+
Calculate the depreciation expenses over the given period using the sum of years' digits method
138+
:param useful_years: Number of years the asset will be used
139+
:param purchase_value: Purchase expenditure for the asset
140+
:param residual_value: Residual value of the asset at the end of its useful life
141+
:return: A list of annual depreciation expenses over the asset's useful life
142+
"""
143+
144+
if not isinstance(useful_years, int):
145+
raise TypeError("Useful years must be an integer")
146+
147+
if useful_years < 1:
148+
raise ValueError("Useful years cannot be less than 1")
149+
150+
if not isinstance(purchase_value, (float, int)):
151+
raise TypeError("Purchase value must be numeric")
152+
153+
if not isinstance(residual_value, (float, int)):
154+
raise TypeError("Residual value must be numeric")
155+
156+
if purchase_value < 0.0:
157+
raise ValueError("Purchase value cannot be less than zero")
158+
159+
if purchase_value < residual_value:
160+
raise ValueError("Purchase value cannot be less than residual value")
161+
162+
digits_sum = useful_years * (useful_years + 1) // 2
163+
164+
list_of_depreciation_expenses = []
165+
166+
for i in range(1, useful_years+1):
167+
depreciation_value = (useful_years - (i - 1)) / digits_sum * (purchase_value - residual_value)
168+
list_of_depreciation_expenses.append(depreciation_value)
169+
170+
171+
return list_of_depreciation_expenses
172+
173+
174+
175+
92176

93177
if __name__ == "__main__":
94-
user_input_useful_years = int(input("Please Enter Useful Years:\n > "))
95-
user_input_purchase_value = float(input("Please Enter Purchase Value:\n > "))
96-
user_input_residual_value = float(input("Please Enter Residual Value:\n > "))
97-
print(
98-
straight_line_depreciation(
99-
user_input_useful_years,
100-
user_input_purchase_value,
101-
user_input_residual_value,
102-
)
103-
)
178+
import doctest
179+
180+
doctest.testmod()

0 commit comments

Comments
 (0)