Skip to content

Commit 719540c

Browse files
algobytewiseshermanhui
authored andcommitted
move-files-and-2-renames (TheAlgorithms#4285)
1 parent 66e97f1 commit 719540c

11 files changed

+134
-134
lines changed
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,45 @@
1-
"""
2-
Given an array of integer elements and an integer 'k', we are required to find the
3-
maximum sum of 'k' consecutive elements in the array.
4-
5-
Instead of using a nested for loop, in a Brute force approach we will use a technique
6-
called 'Window sliding technique' where the nested loops can be converted to a single
7-
loop to reduce time complexity.
8-
"""
9-
from typing import List
10-
11-
12-
def max_sum_in_array(array: List[int], k: int) -> int:
13-
"""
14-
Returns the maximum sum of k consecutive elements
15-
>>> arr = [1, 4, 2, 10, 2, 3, 1, 0, 20]
16-
>>> k = 4
17-
>>> max_sum_in_array(arr, k)
18-
24
19-
>>> k = 10
20-
>>> max_sum_in_array(arr,k)
21-
Traceback (most recent call last):
22-
...
23-
ValueError: Invalid Input
24-
>>> arr = [1, 4, 2, 10, 2, 13, 1, 0, 2]
25-
>>> k = 4
26-
>>> max_sum_in_array(arr, k)
27-
27
28-
"""
29-
if len(array) < k or k < 0:
30-
raise ValueError("Invalid Input")
31-
max_sum = current_sum = sum(array[:k])
32-
for i in range(len(array) - k):
33-
current_sum = current_sum - array[i] + array[i + k]
34-
max_sum = max(max_sum, current_sum)
35-
return max_sum
36-
37-
38-
if __name__ == "__main__":
39-
from doctest import testmod
40-
from random import randint
41-
42-
testmod()
43-
array = [randint(-1000, 1000) for i in range(100)]
44-
k = randint(0, 110)
45-
print(f"The maximum sum of {k} consecutive elements is {max_sum_in_array(array,k)}")
1+
"""
2+
Given an array of integer elements and an integer 'k', we are required to find the
3+
maximum sum of 'k' consecutive elements in the array.
4+
5+
Instead of using a nested for loop, in a Brute force approach we will use a technique
6+
called 'Window sliding technique' where the nested loops can be converted to a single
7+
loop to reduce time complexity.
8+
"""
9+
from typing import List
10+
11+
12+
def max_sum_in_array(array: List[int], k: int) -> int:
13+
"""
14+
Returns the maximum sum of k consecutive elements
15+
>>> arr = [1, 4, 2, 10, 2, 3, 1, 0, 20]
16+
>>> k = 4
17+
>>> max_sum_in_array(arr, k)
18+
24
19+
>>> k = 10
20+
>>> max_sum_in_array(arr,k)
21+
Traceback (most recent call last):
22+
...
23+
ValueError: Invalid Input
24+
>>> arr = [1, 4, 2, 10, 2, 13, 1, 0, 2]
25+
>>> k = 4
26+
>>> max_sum_in_array(arr, k)
27+
27
28+
"""
29+
if len(array) < k or k < 0:
30+
raise ValueError("Invalid Input")
31+
max_sum = current_sum = sum(array[:k])
32+
for i in range(len(array) - k):
33+
current_sum = current_sum - array[i] + array[i + k]
34+
max_sum = max(max_sum, current_sum)
35+
return max_sum
36+
37+
38+
if __name__ == "__main__":
39+
from doctest import testmod
40+
from random import randint
41+
42+
testmod()
43+
array = [randint(-1000, 1000) for i in range(100)]
44+
k = randint(0, 110)
45+
print(f"The maximum sum of {k} consecutive elements is {max_sum_in_array(array,k)}")
File renamed without changes.
File renamed without changes.
Original file line numberDiff line numberDiff line change
@@ -1,89 +1,89 @@
1-
"""
2-
Given an array of integers and another integer target,
3-
we are required to find a triplet from the array such that it's sum is equal to
4-
the target.
5-
"""
6-
from __future__ import annotations
7-
8-
from itertools import permutations
9-
from random import randint
10-
from timeit import repeat
11-
12-
13-
def make_dataset() -> tuple[list[int], int]:
14-
arr = [randint(-1000, 1000) for i in range(10)]
15-
r = randint(-5000, 5000)
16-
return (arr, r)
17-
18-
19-
dataset = make_dataset()
20-
21-
22-
def triplet_sum1(arr: list[int], target: int) -> tuple[int, int, int]:
23-
"""
24-
Returns a triplet in the array with sum equal to target,
25-
else (0, 0, 0).
26-
>>> triplet_sum1([13, 29, 7, 23, 5], 35)
27-
(5, 7, 23)
28-
>>> triplet_sum1([37, 9, 19, 50, 44], 65)
29-
(9, 19, 37)
30-
>>> arr = [6, 47, 27, 1, 15]
31-
>>> target = 11
32-
>>> triplet_sum1(arr, target)
33-
(0, 0, 0)
34-
"""
35-
for triplet in permutations(arr, 3):
36-
if sum(triplet) == target:
37-
return tuple(sorted(triplet))
38-
return (0, 0, 0)
39-
40-
41-
def triplet_sum2(arr: list[int], target: int) -> tuple[int, int, int]:
42-
"""
43-
Returns a triplet in the array with sum equal to target,
44-
else (0, 0, 0).
45-
>>> triplet_sum2([13, 29, 7, 23, 5], 35)
46-
(5, 7, 23)
47-
>>> triplet_sum2([37, 9, 19, 50, 44], 65)
48-
(9, 19, 37)
49-
>>> arr = [6, 47, 27, 1, 15]
50-
>>> target = 11
51-
>>> triplet_sum2(arr, target)
52-
(0, 0, 0)
53-
"""
54-
arr.sort()
55-
n = len(arr)
56-
for i in range(n - 1):
57-
left, right = i + 1, n - 1
58-
while left < right:
59-
if arr[i] + arr[left] + arr[right] == target:
60-
return (arr[i], arr[left], arr[right])
61-
elif arr[i] + arr[left] + arr[right] < target:
62-
left += 1
63-
elif arr[i] + arr[left] + arr[right] > target:
64-
right -= 1
65-
return (0, 0, 0)
66-
67-
68-
def solution_times() -> tuple[float, float]:
69-
setup_code = """
70-
from __main__ import dataset, triplet_sum1, triplet_sum2
71-
"""
72-
test_code1 = """
73-
triplet_sum1(*dataset)
74-
"""
75-
test_code2 = """
76-
triplet_sum2(*dataset)
77-
"""
78-
times1 = repeat(setup=setup_code, stmt=test_code1, repeat=5, number=10000)
79-
times2 = repeat(setup=setup_code, stmt=test_code2, repeat=5, number=10000)
80-
return (min(times1), min(times2))
81-
82-
83-
if __name__ == "__main__":
84-
from doctest import testmod
85-
86-
testmod()
87-
times = solution_times()
88-
print(f"The time for naive implementation is {times[0]}.")
89-
print(f"The time for optimized implementation is {times[1]}.")
1+
"""
2+
Given an array of integers and another integer target,
3+
we are required to find a triplet from the array such that it's sum is equal to
4+
the target.
5+
"""
6+
from __future__ import annotations
7+
8+
from itertools import permutations
9+
from random import randint
10+
from timeit import repeat
11+
12+
13+
def make_dataset() -> tuple[list[int], int]:
14+
arr = [randint(-1000, 1000) for i in range(10)]
15+
r = randint(-5000, 5000)
16+
return (arr, r)
17+
18+
19+
dataset = make_dataset()
20+
21+
22+
def triplet_sum1(arr: list[int], target: int) -> tuple[int, int, int]:
23+
"""
24+
Returns a triplet in the array with sum equal to target,
25+
else (0, 0, 0).
26+
>>> triplet_sum1([13, 29, 7, 23, 5], 35)
27+
(5, 7, 23)
28+
>>> triplet_sum1([37, 9, 19, 50, 44], 65)
29+
(9, 19, 37)
30+
>>> arr = [6, 47, 27, 1, 15]
31+
>>> target = 11
32+
>>> triplet_sum1(arr, target)
33+
(0, 0, 0)
34+
"""
35+
for triplet in permutations(arr, 3):
36+
if sum(triplet) == target:
37+
return tuple(sorted(triplet))
38+
return (0, 0, 0)
39+
40+
41+
def triplet_sum2(arr: list[int], target: int) -> tuple[int, int, int]:
42+
"""
43+
Returns a triplet in the array with sum equal to target,
44+
else (0, 0, 0).
45+
>>> triplet_sum2([13, 29, 7, 23, 5], 35)
46+
(5, 7, 23)
47+
>>> triplet_sum2([37, 9, 19, 50, 44], 65)
48+
(9, 19, 37)
49+
>>> arr = [6, 47, 27, 1, 15]
50+
>>> target = 11
51+
>>> triplet_sum2(arr, target)
52+
(0, 0, 0)
53+
"""
54+
arr.sort()
55+
n = len(arr)
56+
for i in range(n - 1):
57+
left, right = i + 1, n - 1
58+
while left < right:
59+
if arr[i] + arr[left] + arr[right] == target:
60+
return (arr[i], arr[left], arr[right])
61+
elif arr[i] + arr[left] + arr[right] < target:
62+
left += 1
63+
elif arr[i] + arr[left] + arr[right] > target:
64+
right -= 1
65+
return (0, 0, 0)
66+
67+
68+
def solution_times() -> tuple[float, float]:
69+
setup_code = """
70+
from __main__ import dataset, triplet_sum1, triplet_sum2
71+
"""
72+
test_code1 = """
73+
triplet_sum1(*dataset)
74+
"""
75+
test_code2 = """
76+
triplet_sum2(*dataset)
77+
"""
78+
times1 = repeat(setup=setup_code, stmt=test_code1, repeat=5, number=10000)
79+
times2 = repeat(setup=setup_code, stmt=test_code2, repeat=5, number=10000)
80+
return (min(times1), min(times2))
81+
82+
83+
if __name__ == "__main__":
84+
from doctest import testmod
85+
86+
testmod()
87+
times = solution_times()
88+
print(f"The time for naive implementation is {times[0]}.")
89+
print(f"The time for optimized implementation is {times[1]}.")
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)