Skip to content

Commit d70cc4d

Browse files
Merge branch 'TheAlgorithms:master' into master
2 parents b100e66 + c3d4b9e commit d70cc4d

File tree

5 files changed

+144
-22
lines changed

5 files changed

+144
-22
lines changed

.github/workflows/directory_writer.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
name: directory_writer
44
on: [push]
55
jobs:
6-
build:
6+
directory_writer:
77
runs-on: ubuntu-latest
88
steps:
99
- uses: actions/checkout@v4

.pre-commit-config.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ repos:
1616
- id: auto-walrus
1717

1818
- repo: https://github.com/astral-sh/ruff-pre-commit
19-
rev: v0.11.9
19+
rev: v0.11.11
2020
hooks:
2121
- id: ruff
2222
- id: ruff-format
@@ -29,7 +29,7 @@ repos:
2929
- tomli
3030

3131
- repo: https://github.com/tox-dev/pyproject-fmt
32-
rev: "v2.5.1"
32+
rev: "v2.6.0"
3333
hooks:
3434
- id: pyproject-fmt
3535

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+
)

maths/radix2_fft.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,13 @@ class FFT:
4040
4141
Print product
4242
>>> x.product # 2x + 3x^2 + 8x^3 + 4x^4 + 6x^5
43-
[(-0+0j), (2+0j), (3+0j), (8+0j), (6+0j), (8+0j)]
43+
[(-0-0j), (2+0j), (3-0j), (8-0j), (6+0j), (8+0j)]
4444
4545
__str__ test
4646
>>> print(x)
4747
A = 0*x^0 + 1*x^1 + 2*x^0 + 3*x^2
4848
B = 0*x^2 + 1*x^3 + 2*x^4
49-
A*B = 0*x^(-0+0j) + 1*x^(2+0j) + 2*x^(3+0j) + 3*x^(8+0j) + 4*x^(6+0j) + 5*x^(8+0j)
49+
A*B = 0*x^(-0-0j) + 1*x^(2+0j) + 2*x^(3-0j) + 3*x^(8-0j) + 4*x^(6+0j) + 5*x^(8+0j)
5050
"""
5151

5252
def __init__(self, poly_a=None, poly_b=None):
@@ -147,7 +147,9 @@ def __multiply(self):
147147
inverce_c = new_inverse_c
148148
next_ncol *= 2
149149
# Unpack
150-
inverce_c = [round(x[0].real, 8) + round(x[0].imag, 8) * 1j for x in inverce_c]
150+
inverce_c = [
151+
complex(round(x[0].real, 8), round(x[0].imag, 8)) for x in inverce_c
152+
]
151153

152154
# Remove leading 0's
153155
while inverce_c[-1] == 0:

strings/boyer_moore_search.py

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,30 +11,42 @@
1111
a shift is proposed that moves the entirety of Pattern past
1212
the point of mismatch in the text.
1313
14-
If there no mismatch then the pattern matches with text block.
14+
If there is no mismatch then the pattern matches with text block.
1515
1616
Time Complexity : O(n/m)
1717
n=length of main string
1818
m=length of pattern string
1919
"""
2020

21-
from __future__ import annotations
22-
2321

2422
class BoyerMooreSearch:
23+
"""
24+
Example usage:
25+
26+
bms = BoyerMooreSearch(text="ABAABA", pattern="AB")
27+
positions = bms.bad_character_heuristic()
28+
29+
where 'positions' contain the locations where the pattern was matched.
30+
"""
31+
2532
def __init__(self, text: str, pattern: str):
2633
self.text, self.pattern = text, pattern
2734
self.textLen, self.patLen = len(text), len(pattern)
2835

2936
def match_in_pattern(self, char: str) -> int:
30-
"""finds the index of char in pattern in reverse order
37+
"""
38+
Finds the index of char in pattern in reverse order.
3139
3240
Parameters :
3341
char (chr): character to be searched
3442
3543
Returns :
3644
i (int): index of char from last in pattern
3745
-1 (int): if char is not found in pattern
46+
47+
>>> bms = BoyerMooreSearch(text="ABAABA", pattern="AB")
48+
>>> bms.match_in_pattern("B")
49+
1
3850
"""
3951

4052
for i in range(self.patLen - 1, -1, -1):
@@ -44,15 +56,19 @@ def match_in_pattern(self, char: str) -> int:
4456

4557
def mismatch_in_text(self, current_pos: int) -> int:
4658
"""
47-
find the index of mis-matched character in text when compared with pattern
48-
from last
59+
Find the index of mis-matched character in text when compared with pattern
60+
from last.
4961
5062
Parameters :
5163
current_pos (int): current index position of text
5264
5365
Returns :
5466
i (int): index of mismatched char from last in text
5567
-1 (int): if there is no mismatch between pattern and text block
68+
69+
>>> bms = BoyerMooreSearch(text="ABAABA", pattern="AB")
70+
>>> bms.mismatch_in_text(2)
71+
3
5672
"""
5773

5874
for i in range(self.patLen - 1, -1, -1):
@@ -61,7 +77,14 @@ def mismatch_in_text(self, current_pos: int) -> int:
6177
return -1
6278

6379
def bad_character_heuristic(self) -> list[int]:
64-
# searches pattern in text and returns index positions
80+
"""
81+
Finds the positions of the pattern location.
82+
83+
>>> bms = BoyerMooreSearch(text="ABAABA", pattern="AB")
84+
>>> bms.bad_character_heuristic()
85+
[0, 3]
86+
"""
87+
6588
positions = []
6689
for i in range(self.textLen - self.patLen + 1):
6790
mismatch_index = self.mismatch_in_text(i)
@@ -75,13 +98,7 @@ def bad_character_heuristic(self) -> list[int]:
7598
return positions
7699

77100

78-
text = "ABAABA"
79-
pattern = "AB"
80-
bms = BoyerMooreSearch(text, pattern)
81-
positions = bms.bad_character_heuristic()
101+
if __name__ == "__main__":
102+
import doctest
82103

83-
if len(positions) == 0:
84-
print("No match found")
85-
else:
86-
print("Pattern found in following positions: ")
87-
print(positions)
104+
doctest.testmod()

0 commit comments

Comments
 (0)