diff options
| author | 冯昶 <fengchang@novel-supertv.com> | 2023-12-11 18:11:32 +0800 |
|---|---|---|
| committer | 冯昶 <fengchang@novel-supertv.com> | 2023-12-11 18:11:32 +0800 |
| commit | e94f52525bf99cded9ba76e696b691c7a657500b (patch) | |
| tree | 4e614b7233580b0aee9c58e23e87eff5e9331aee /challenge-246/packy-anderson/python/ch-2.py | |
| parent | 4ac02978fd0732a7fcc61a9def95e1e08100e4c9 (diff) | |
| parent | 98df168725aa587bd4db1e24018dae1ca77b29da (diff) | |
| download | perlweeklychallenge-club-e94f52525bf99cded9ba76e696b691c7a657500b.tar.gz perlweeklychallenge-club-e94f52525bf99cded9ba76e696b691c7a657500b.tar.bz2 perlweeklychallenge-club-e94f52525bf99cded9ba76e696b691c7a657500b.zip | |
Merge remote-tracking branch 'upstream/master'
Diffstat (limited to 'challenge-246/packy-anderson/python/ch-2.py')
| -rwxr-xr-x | challenge-246/packy-anderson/python/ch-2.py | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/challenge-246/packy-anderson/python/ch-2.py b/challenge-246/packy-anderson/python/ch-2.py new file mode 100755 index 0000000000..0ac4fae9eb --- /dev/null +++ b/challenge-246/packy-anderson/python/ch-2.py @@ -0,0 +1,50 @@ +#!/usr/bin/env python + +def findPandQ(a): + p = ( + (a[2] * a[2] - a[1] * a[3]) + / + (a[0] * a[2] - a[1] * a[1]) + ) + q = ( + (a[0] * a[3] - a[2] * a[1]) + / + (a[0] * a[2] - a[1] * a[1]) + ) + return(p, q) + + +def isLinearRecurranceOfSecondOrder(a): + (p1, q1) = findPandQ(a[0:4]) + (p2, q2) = findPandQ(a[1:5]) + if p1 != p2 or q1 != q2: + print(f'Values for P ({p1}, {p2}) ', end='') + print(f'and Q ({q1}, {q2}) ', end='') + print(f'are not consistent across all five elements') + return False + if p1 != int(p1) or q1 != int(q1): + print(f'Values for P ({p1}) ', end='') + print(f'and Q ({q1}) ', end='') + print(f'for first four elements are not integers') + return False + + print(f'Found integer values for P ({int(p1)}) ', end='') + print(f'and Q ({int(q1)})') + return True + +def comma_join(a): + return ', '.join(map(str, a)) + +def solution(a): + print(f'Input: @a = ({comma_join(a)})') + result = isLinearRecurranceOfSecondOrder(a) + print(f'Output: {result}') + +print('Example 1:') +solution([1, 1, 2, 3, 5]) + +print('\nExample 2:') +solution([4, 2, 4, 5, 7]) + +print('\nExample 3:') +solution([4, 1, 2, -3, 8])
\ No newline at end of file |
