Skip to content

Commit 4b077c0

Browse files
kangaroo-eating-carrotsJeonghyun Min
andauthored
feat: create straight_line_depreciation (TheAlgorithms#12377)
Co-authored-by: Jeonghyun Min <20122791@tafe.wa.edu.au>
1 parent d0d7f0b commit 4b077c0

File tree

1 file changed

+103
-0
lines changed

1 file changed

+103
-0
lines changed
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
"""
2+
In accounting, depreciation refers to the decreases in the value
3+
of a fixed asset during the asset's useful life.
4+
When an organization purchases a fixed asset,
5+
the purchase expenditure is not recognized as an expense immediately.
6+
Instead, the decreases in the asset's value are recognized as expenses
7+
over the years during which the asset is used.
8+
9+
The following methods are widely used
10+
for depreciation calculation in accounting:
11+
- Straight-line method
12+
- Diminishing balance method
13+
- Units-of-production method
14+
15+
The straight-line method is the simplest and most widely used.
16+
This method calculates depreciation by spreading the cost evenly
17+
over the asset's useful life.
18+
19+
The following formula shows how to calculate the yearly depreciation expense:
20+
21+
- annual depreciation expense =
22+
(purchase cost of asset - residual value) / useful life of asset(years)
23+
24+
Further information on:
25+
https://en.wikipedia.org/wiki/Depreciation
26+
27+
The function, straight_line_depreciation, returns a list of
28+
the depreciation expenses over the given period.
29+
"""
30+
31+
32+
def straight_line_depreciation(
33+
useful_years: int,
34+
purchase_value: float,
35+
residual_value: float = 0.0,
36+
) -> list[float]:
37+
"""
38+
Calculate the depreciation expenses over the given period
39+
:param useful_years: Number of years the asset will be used
40+
:param purchase_value: Purchase expenditure for the asset
41+
:param residual_value: Residual value of the asset at the end of its useful life
42+
:return: A list of annual depreciation expenses over the asset's useful life
43+
>>> straight_line_depreciation(10, 1100.0, 100.0)
44+
[100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0]
45+
>>> straight_line_depreciation(6, 1250.0, 50.0)
46+
[200.0, 200.0, 200.0, 200.0, 200.0, 200.0]
47+
>>> straight_line_depreciation(4, 1001.0)
48+
[250.25, 250.25, 250.25, 250.25]
49+
>>> straight_line_depreciation(11, 380.0, 50.0)
50+
[30.0, 30.0, 30.0, 30.0, 30.0, 30.0, 30.0, 30.0, 30.0, 30.0, 30.0]
51+
>>> straight_line_depreciation(1, 4985, 100)
52+
[4885.0]
53+
"""
54+
55+
if not isinstance(useful_years, int):
56+
raise TypeError("Useful years must be an integer")
57+
58+
if useful_years < 1:
59+
raise ValueError("Useful years cannot be less than 1")
60+
61+
if not isinstance(purchase_value, (float, int)):
62+
raise TypeError("Purchase value must be numeric")
63+
64+
if not isinstance(residual_value, (float, int)):
65+
raise TypeError("Residual value must be numeric")
66+
67+
if purchase_value < 0.0:
68+
raise ValueError("Purchase value cannot be less than zero")
69+
70+
if purchase_value < residual_value:
71+
raise ValueError("Purchase value cannot be less than residual value")
72+
73+
# Calculate annual depreciation expense
74+
depreciable_cost = purchase_value - residual_value
75+
annual_depreciation_expense = depreciable_cost / useful_years
76+
77+
# List of annual depreciation expenses
78+
list_of_depreciation_expenses = []
79+
accumulated_depreciation_expense = 0.0
80+
for period in range(useful_years):
81+
if period != useful_years - 1:
82+
accumulated_depreciation_expense += annual_depreciation_expense
83+
list_of_depreciation_expenses.append(annual_depreciation_expense)
84+
else:
85+
depreciation_expense_in_end_year = (
86+
depreciable_cost - accumulated_depreciation_expense
87+
)
88+
list_of_depreciation_expenses.append(depreciation_expense_in_end_year)
89+
90+
return list_of_depreciation_expenses
91+
92+
93+
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+
)

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy