diff options
Diffstat (limited to 'challenge-246/pokgopun/python')
| -rw-r--r-- | challenge-246/pokgopun/python/ch-1.py | 48 | ||||
| -rw-r--r-- | challenge-246/pokgopun/python/ch-2.py | 90 |
2 files changed, 138 insertions, 0 deletions
diff --git a/challenge-246/pokgopun/python/ch-1.py b/challenge-246/pokgopun/python/ch-1.py new file mode 100644 index 0000000000..ad2228bc13 --- /dev/null +++ b/challenge-246/pokgopun/python/ch-1.py @@ -0,0 +1,48 @@ +### https://theweeklychallenge.org/blog/perl-weekly-challenge-246/ +""" + +Task 1: 6 out of 49 + +Submitted by: [46]Andreas Voegele + __________________________________________________________________ + + 6 out of 49 is a German lottery. + + Write a script that outputs six unique random integers from the range 1 + to 49. + +Output + +3 +10 +11 +22 +38 +49 + +Task 2: Linear Recurrence of Second Order +""" +### solution by pokgopun@gmail.com + +from random import sample + +def GmLot(): + return sample(range(1,50),k=6) + +import unittest + +class TestGmLot(unittest.TestCase): + def test1(self): + self.assertEqual(len(GmLot()), 6, f"member count incorrect") + def test2(self): + for e in GmLot(): self.assertEqual( e in range(1,50), True, f"member not in range") + def test3(self): + self.assertEqual(len(set(GmLot())), 6, f"non-unique member found") + def test4(self): + lots = list() + for i in range(100): + lot = GmLot() + self.assertEqual(lot not in lots, True, f"subsequent lot duplicated") + lots.append(lot) + +unittest.main() diff --git a/challenge-246/pokgopun/python/ch-2.py b/challenge-246/pokgopun/python/ch-2.py new file mode 100644 index 0000000000..8c158b8dbf --- /dev/null +++ b/challenge-246/pokgopun/python/ch-2.py @@ -0,0 +1,90 @@ +### https://theweeklychallenge.org/blog/perl-weekly-challenge-246/ +""" + +Task 2: Linear Recurrence of Second Order + +Submitted by: [47]Jorg Sommrey + __________________________________________________________________ + + You are given an array @a of five integers. + + Write a script to decide whether the given integers form a linear + recurrence of second order with integer factors. + + A linear recurrence of second order has the form +a[n] = p * a[n-2] + q * a[n-1] with n > 1 + +where p and q must be integers. + +Example 1 + +Input: @a = (1, 1, 2, 3, 5) +Output: true + +@a is the initial part of the Fibonacci sequence a[n] = a[n-2] + a[n-1] +with a[0] = 1 and a[1] = 1. + +Example 2 + +Input: @a = (4, 2, 4, 5, 7) +Output: false + +a[1] and a[2] are even. Any linear combination of two even numbers with integer +factors is even, too. +Because a[3] is odd, the given numbers cannot form a linear recurrence of second + order with integer factors. + +Example 3 + +Input: @a = (4, 1, 2, -3, 8) +Output: true + +a[n] = a[n-2] - 2 * a[n-1] + __________________________________________________________________ + + Last date to submit the solution 23:59 (UK Time) Sunday 10th December + 2023. + __________________________________________________________________ + +SO WHAT DO YOU THINK ? +""" +### solution by pokgopun@gmail.com + +class Seq3: + def __init__(self, ints :tuple): + ## a*p + b*q = c + self.a, self.b, self.c = ( ints[i:i+3] for i in range(3) ) + self.is_lroso = None + self.ans = set() + self.process() + def set_lroso(self, b :bool): + if self.is_lroso is None: + self.is_lroso = b + def process(self): + for i,j in ( (i,i+1) for i in range(2) ): ## process pair of seq3 + ## eliminate p from equation to get q = dvd/dvs + dvd, dvs = self.a[j]*self.c[i] - self.a[i]*self.c[j], self.a[j]*self.b[i] - self.a[i]*self.b[j] + if dvd % dvs != 0: ## if q is not int, set is_lroso to false + self.set_lroso(False) + q = dvd/dvs + ## replace q in equation to get p = dvd/dvs + dvd, dvs = self.c[i] - self.b[i]*q, self.a[i] + if dvd % dvs != 0: ## if p is not int, set is_lroso to false + self.set_lroso(False) + p = dvd/dvs + self.ans.add((p,q)) ## store p,q to ans + if len(self.ans) > 1: ## if there multiple answers, set is_lroso to false + self.set_lroso(False) + self.set_lroso(True) ## otherwise, set is_lroso to true + +import unittest + +class TestSeq3(unittest.TestCase): + def test1(self): + self.assertEqual(Seq3((1, 1, 2, 3, 5)).is_lroso, True,"Ex1") + def test2(self): + self.assertEqual(Seq3((4, 2, 4, 5, 7)).is_lroso, False,"Ex2") + def test3(self): + self.assertEqual(Seq3((4, 1, 2, -3, 8)).is_lroso, True,"Ex3") + +unittest.main() |
