aboutsummaryrefslogtreecommitdiff
path: root/challenge-246/lubos-kolouch/python
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-246/lubos-kolouch/python')
-rw-r--r--challenge-246/lubos-kolouch/python/ch-1.py29
-rw-r--r--challenge-246/lubos-kolouch/python/ch-2.py38
2 files changed, 67 insertions, 0 deletions
diff --git a/challenge-246/lubos-kolouch/python/ch-1.py b/challenge-246/lubos-kolouch/python/ch-1.py
new file mode 100644
index 0000000000..7428e5f573
--- /dev/null
+++ b/challenge-246/lubos-kolouch/python/ch-1.py
@@ -0,0 +1,29 @@
+#!/usr/bin/env python
+
+import random
+from typing import List
+
+
+def pick_lotto_numbers() -> list[int]:
+ return random.sample(range(1, 50), 6)
+
+
+# Test
+import unittest
+
+
+class TestLottoNumbers(unittest.TestCase):
+ def test_lotto_numbers(self):
+ result = pick_lotto_numbers()
+ self.assertEqual(len(result), 6)
+ self.assertEqual(len(set(result)), 6)
+ for num in result:
+ self.assertTrue(1 <= num <= 49)
+
+
+if __name__ == "__main__":
+ unittest.main()
+
+# Main
+for num in sorted(pick_lotto_numbers()):
+ print(num)
diff --git a/challenge-246/lubos-kolouch/python/ch-2.py b/challenge-246/lubos-kolouch/python/ch-2.py
new file mode 100644
index 0000000000..59a6157385
--- /dev/null
+++ b/challenge-246/lubos-kolouch/python/ch-2.py
@@ -0,0 +1,38 @@
+#!/usr/bin/env python
+
+import numpy as np
+
+
+def is_linear_recurrence(a):
+ if len(a) < 5:
+ return False
+
+ # Forming matrix to solve the linear equations
+ A = np.array([[a[0], a[1]], [a[1], a[2]]])
+ B = np.array([a[2], a[3]])
+
+ # Solve for p and q
+ try:
+ p, q = np.linalg.solve(A, B)
+ except np.linalg.LinAlgError:
+ return False # Cannot solve if the matrix A is singular
+
+ # Check if p and q are integers
+ if not (p.is_integer() and q.is_integer()):
+ return False
+
+ # Verify the recurrence for the rest of the array
+ for i in range(4, len(a)):
+ if a[i] != int(p) * a[i - 2] + int(q) * a[i - 1]:
+ return False
+
+ return True
+
+
+# Tests
+assert is_linear_recurrence([1, 1, 2, 3, 5]) == True, "Fibonacci sequence"
+assert is_linear_recurrence([4, 2, 4, 5, 7]) == False, "Even-odd mismatch"
+assert is_linear_recurrence([4, 1, 2, -3, 8]) == True, "Custom sequence"
+
+# Main
+print("true" if is_linear_recurrence([4, 1, 2, -3, 8]) else "false")