Skip to content

Commit c93cdff

Browse files
authored
Merge pull request #13 from rlishtaba/feature/various
adding raw solutions
2 parents 4ff7d07 + 4a868ed commit c93cdff

File tree

7 files changed

+102
-0
lines changed

7 files changed

+102
-0
lines changed

py_algorithms/challenges/various/quarantine/__init__.py

Whitespace-only changes.

py_algorithms/challenges/various/quarantine/arrays/__init__.py

Whitespace-only changes.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
from typing import List
2+
from typing import Union
3+
4+
5+
def get_target(array1, array2):
6+
sum1 = sum(array1)
7+
sum2 = sum(array2)
8+
9+
if (sum1 - sum2) % 2 != 0:
10+
return None
11+
return (sum1 - sum2) // 2
12+
13+
14+
def algorithm(array1, array2) -> Union[None, List[tuple]]:
15+
target = get_target(array1, array2)
16+
if target is None:
17+
return None
18+
solutions = []
19+
for x in array1:
20+
for y in array2:
21+
if x - y == target:
22+
pair = (x, y,)
23+
if pair not in solutions:
24+
solutions.append(pair)
25+
return solutions
26+
27+
28+
if __name__ == '__main__':
29+
assert algorithm([4, 1, 2, 1, 1, 2], [3, 6, 3, 3]) == [(4, 6), (1, 3)]

py_algorithms/challenges/various/quarantine/dp/__init__.py

Whitespace-only changes.
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
from typing import Any
2+
3+
4+
class CountTransformations:
5+
def __call__(self, a: str, b: str) -> Any:
6+
n = len(a)
7+
m = len(b)
8+
9+
if m == 0:
10+
return 1
11+
12+
dp = [[0 for _ in range(n)] for _ in range(m)]
13+
14+
for i in range(0, m):
15+
for j in range(i, n):
16+
if i == 0:
17+
if a[j] == b[i] and j == 0:
18+
dp[i][j] = 1
19+
elif a[j] == b[i]:
20+
dp[i][j] = dp[i][j - 1] + 1
21+
else:
22+
dp[i][j] = dp[i][j - 1]
23+
else:
24+
if a[j] == b[i]:
25+
dp[i][j] = dp[i][j - 1] + dp[i - 1][j - 1]
26+
else:
27+
dp[i][j] = dp[i][j - 1]
28+
return dp[m - 1][n - 1]
29+
30+
31+
class TestCountTransformations:
32+
def test_algorithm(self):
33+
f = CountTransformations()
34+
assert f(a="abcccdf", b="abccdf") == 3
35+
assert f(a="aabba", b="ab") == 4
36+
37+
38+
if __name__ == '__main__':
39+
runner = TestCountTransformations()
40+
runner.test_algorithm()
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
from typing import List
2+
3+
4+
def _make_change(amount: int, denoms: List[int], index: int, cache) -> int:
5+
if amount in cache:
6+
if index in cache[amount]:
7+
return cache[amount][index]
8+
9+
if index >= len(denoms) - 1:
10+
return 1
11+
12+
denom_amount = denoms[index]
13+
ways = 0
14+
i = 1
15+
while i * denom_amount <= amount:
16+
remaining = amount - i * denom_amount
17+
ways += _make_change(remaining, denoms, index + 1, cache)
18+
i += 1
19+
if amount not in cache:
20+
cache[amount] = {}
21+
cache[amount][index] = ways
22+
23+
return ways
24+
25+
26+
def make_change(amount, denoms: List[int]):
27+
cache = {}
28+
ways = _make_change(amount, denoms, 0, cache)
29+
return ways
30+
31+
32+
if __name__ == '__main__':
33+
assert make_change(100, [1, 5, 10, 25]) == 2625

tests/py_algorithms/challenges/db/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)