Skip to content

Commit 8f1ba26

Browse files
authored
Merge branch 'TheAlgorithms:master' into master
2 parents b3a9dd4 + a13e9c2 commit 8f1ba26

6 files changed

+97
-6
lines changed

.pre-commit-config.yaml

+2-2
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.1.3
19+
rev: v0.1.4
2020
hooks:
2121
- id: ruff
2222

@@ -33,7 +33,7 @@ repos:
3333
- tomli
3434

3535
- repo: https://github.com/tox-dev/pyproject-fmt
36-
rev: "1.3.0"
36+
rev: "1.4.1"
3737
hooks:
3838
- id: pyproject-fmt
3939

DIRECTORY.md

+2
Original file line numberDiff line numberDiff line change
@@ -725,6 +725,7 @@
725725
* [Carmichael Number](maths/special_numbers/carmichael_number.py)
726726
* [Catalan Number](maths/special_numbers/catalan_number.py)
727727
* [Hamming Numbers](maths/special_numbers/hamming_numbers.py)
728+
* [Happy Number](maths/special_numbers/happy_number.py)
728729
* [Harshad Numbers](maths/special_numbers/harshad_numbers.py)
729730
* [Hexagonal Number](maths/special_numbers/hexagonal_number.py)
730731
* [Krishnamurthy Number](maths/special_numbers/krishnamurthy_number.py)
@@ -1310,6 +1311,7 @@
13101311
* [Get Amazon Product Data](web_programming/get_amazon_product_data.py)
13111312
* [Get Imdb Top 250 Movies Csv](web_programming/get_imdb_top_250_movies_csv.py)
13121313
* [Get Imdbtop](web_programming/get_imdbtop.py)
1314+
* [Get Ip Geolocation](web_programming/get_ip_geolocation.py)
13131315
* [Get Top Billionaires](web_programming/get_top_billionaires.py)
13141316
* [Get Top Hn Posts](web_programming/get_top_hn_posts.py)
13151317
* [Get User Tweets](web_programming/get_user_tweets.py)

data_structures/arrays/find_triplets_with_0_sum.py

+63
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,66 @@ def find_triplets_with_0_sum(nums: list[int]) -> list[list[int]]:
2222
list(x)
2323
for x in sorted({abc for abc in combinations(sorted(nums), 3) if not sum(abc)})
2424
]
25+
26+
27+
def find_triplets_with_0_sum_hashing(arr: list[int]) -> list[list[int]]:
28+
"""
29+
Function for finding the triplets with a given sum in the array using hashing.
30+
31+
Given a list of integers, return elements a, b, c such that a + b + c = 0.
32+
33+
Args:
34+
nums: list of integers
35+
Returns:
36+
list of lists of integers where sum(each_list) == 0
37+
Examples:
38+
>>> find_triplets_with_0_sum_hashing([-1, 0, 1, 2, -1, -4])
39+
[[-1, 0, 1], [-1, -1, 2]]
40+
>>> find_triplets_with_0_sum_hashing([])
41+
[]
42+
>>> find_triplets_with_0_sum_hashing([0, 0, 0])
43+
[[0, 0, 0]]
44+
>>> find_triplets_with_0_sum_hashing([1, 2, 3, 0, -1, -2, -3])
45+
[[-1, 0, 1], [-3, 1, 2], [-2, 0, 2], [-2, -1, 3], [-3, 0, 3]]
46+
47+
Time complexity: O(N^2)
48+
Auxiliary Space: O(N)
49+
50+
"""
51+
target_sum = 0
52+
53+
# Initialize the final output array with blank.
54+
output_arr = []
55+
56+
# Set the initial element as arr[i].
57+
for index, item in enumerate(arr[:-2]):
58+
# to store second elements that can complement the final sum.
59+
set_initialize = set()
60+
61+
# current sum needed for reaching the target sum
62+
current_sum = target_sum - item
63+
64+
# Traverse the subarray arr[i+1:].
65+
for other_item in arr[index + 1 :]:
66+
# required value for the second element
67+
required_value = current_sum - other_item
68+
69+
# Verify if the desired value exists in the set.
70+
if required_value in set_initialize:
71+
# finding triplet elements combination.
72+
combination_array = sorted([item, other_item, required_value])
73+
if combination_array not in output_arr:
74+
output_arr.append(combination_array)
75+
76+
# Include the current element in the set
77+
# for subsequent complement verification.
78+
set_initialize.add(other_item)
79+
80+
# Return all the triplet combinations.
81+
return output_arr
82+
83+
84+
if __name__ == "__main__":
85+
from doctest import testmod
86+
87+
testmod()

pyproject.toml

+4-4
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,10 @@ max-branches = 20 # default: 12
117117
max-returns = 8 # default: 6
118118
max-statements = 88 # default: 50
119119

120+
[tool.codespell]
121+
ignore-words-list = "3rt,ans,bitap,crate,damon,fo,followings,hist,iff,kwanza,manuel,mater,secant,som,sur,tim,toi,zar"
122+
skip = "./.*,*.json,ciphers/prehistoric_men.txt,project_euler/problem_022/p022_names.txt,pyproject.toml,strings/dictionary.txt,strings/words.txt"
123+
120124
[tool.pytest.ini_options]
121125
markers = [
122126
"mat_ops: mark a test as utilizing matrix operations.",
@@ -133,7 +137,3 @@ omit = [
133137
"project_euler/*"
134138
]
135139
sort = "Cover"
136-
137-
[tool.codespell]
138-
ignore-words-list = "3rt,ans,bitap,crate,damon,fo,followings,hist,iff,kwanza,manuel,mater,secant,som,sur,tim,toi,zar"
139-
skip = "./.*,*.json,ciphers/prehistoric_men.txt,project_euler/problem_022/p022_names.txt,pyproject.toml,strings/dictionary.txt,strings/words.txt"

strings/detecting_english_programmatically.py

+12
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,18 @@ def get_english_count(message: str) -> float:
2525

2626

2727
def remove_non_letters(message: str) -> str:
28+
"""
29+
>>> remove_non_letters("Hi! how are you?")
30+
'Hi how are you'
31+
>>> remove_non_letters("P^y%t)h@o*n")
32+
'Python'
33+
>>> remove_non_letters("1+1=2")
34+
''
35+
>>> remove_non_letters("www.google.com/")
36+
'wwwgooglecom'
37+
>>> remove_non_letters("")
38+
''
39+
"""
2840
return "".join(symbol for symbol in message if symbol in LETTERS_AND_SPACE)
2941

3042

strings/string_switch_case.py

+14
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@ def to_simple_case(str_: str) -> str:
2828
"""
2929
>>> to_simple_case("one two 31235three4four")
3030
'OneTwo31235three4four'
31+
>>> to_simple_case("This should be combined")
32+
'ThisShouldBeCombined'
33+
>>> to_simple_case("The first letters are capitalized, then string is merged")
34+
'TheFirstLettersAreCapitalizedThenStringIsMerged'
35+
>>> to_simple_case("special characters :, ', %, ^, $, are ignored")
36+
'SpecialCharactersAreIgnored'
3137
"""
3238
string_split = split_input(str_)
3339
return "".join(
@@ -37,6 +43,14 @@ def to_simple_case(str_: str) -> str:
3743

3844
def to_complex_case(text: str, upper: bool, separator: str) -> str:
3945
"""
46+
Returns the string concatenated with the delimiter we provide.
47+
48+
Parameters:
49+
@text: The string on which we want to perform operation
50+
@upper: Boolean value to determine whether we want capitalized result or not
51+
@separator: The delimiter with which we want to concatenate words
52+
53+
Examples:
4054
>>> to_complex_case("one two 31235three4four", True, "_")
4155
'ONE_TWO_31235THREE4FOUR'
4256
>>> to_complex_case("one two 31235three4four", False, "-")

0 commit comments

Comments
 (0)