diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2021-08-11 21:52:01 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-08-11 21:52:01 +0100 |
| commit | 7be549d83be67ccd5e60a7031a0b222a63a02671 (patch) | |
| tree | fae2e72991eaba179c60fb35d84a785e586e2099 | |
| parent | 3e6718dd8ff06074bec334a291b74b31b8db9305 (diff) | |
| parent | 913bd2198fa356651e23e90e9552a5b78e378b50 (diff) | |
| download | perlweeklychallenge-club-7be549d83be67ccd5e60a7031a0b222a63a02671.tar.gz perlweeklychallenge-club-7be549d83be67ccd5e60a7031a0b222a63a02671.tar.bz2 perlweeklychallenge-club-7be549d83be67ccd5e60a7031a0b222a63a02671.zip | |
Merge pull request #4693 from stuart-little/stuart-little_125_python
1st commit on 125_python
| -rwxr-xr-x | challenge-125/stuart-little/python/ch-1.py | 18 | ||||
| -rwxr-xr-x | challenge-125/stuart-little/python/ch-2.py | 57 |
2 files changed, 75 insertions, 0 deletions
diff --git a/challenge-125/stuart-little/python/ch-1.py b/challenge-125/stuart-little/python/ch-1.py new file mode 100755 index 0000000000..f35cf52624 --- /dev/null +++ b/challenge-125/stuart-little/python/ch-1.py @@ -0,0 +1,18 @@ +#!/usr/bin/env python + +# run <script> <number> + +import math +import sys + +def trps(n): + triples=[] + triples.extend([[x, math.floor(math.sqrt(n**2-x**2)), n] for x in range(1,math.ceil((n+1)/2)+1) if x<n and x**2 + math.floor(math.sqrt(n**2-x**2))**2 == n**2 ]) + triples.extend([ [math.floor((math.floor(n**2/x)-x)/2),n,math.floor((math.floor(n**2/x)+x)/2)] for x in [i for i in range(1,n) if n**2 % i == 0 and i%2==(math.floor(n**2/i))%2]]) + return triples + +sol = trps(int(sys.argv[1])) +if len(sol)>0: + [print(i) for i in sol] + sys.exit() +print(-1) diff --git a/challenge-125/stuart-little/python/ch-2.py b/challenge-125/stuart-little/python/ch-2.py new file mode 100755 index 0000000000..9b5b75aa84 --- /dev/null +++ b/challenge-125/stuart-little/python/ch-2.py @@ -0,0 +1,57 @@ +#!/usr/bin/env python + +import sys + +def lr(tree): + if (len(tree) < 3 or tree[0] == '.'): + return [[],[]] + if len(tree) == 3: + return [["."],["."]] + left=[] + sm,ix = 0,1 + while sm != -1: + left.append(tree[ix]) + sm+=(-1 if tree[ix] == '.' else 1) + ix+=1 + right = tree[len(left)+1:] + return [left,right] + +def lrLongPath(tree): + if tree[0] == '.': + return [[],[]] + if len(tree) == 3: + return [[tree[0]],[tree[0]]] + left,right = lr(tree) + lLongPath = [tree[0]] + max(lrLongPath(left), key=len) + rLongPath = [tree[0]] + max(lrLongPath(right), key=len) + return [lLongPath,rLongPath] + +def biLongPath(tree): + if len(tree) < 3 or tree[0] == '.': + return [] + if len(tree) == 3: + return [tree[0]] + lPath,rPath = lrLongPath(tree) + path = list(reversed(lPath[1:])) + rPath + left,right = lr(tree) + return max([path]+list(map(biLongPath, [left,right])), key=len) + +print(" ".join(biLongPath(sys.argv[1:]))) + +""" +run <script> <tree in preorder form with '.' for empty nodes, entered as space-separated values> + +ref: https://stackoverflow.com/a/2676849/11064961 + +e.g. 1 2 4 . 7 . . . 3 5 . . 6 . . represents the tree + + 1 + / \ + 2 3 + / / \ + 4 5 6 + \ + 7 + +given as an example in the problem formulation at https://perlweeklychallenge.org/blog/perl-weekly-challenge-113/#TASK2 +""" |
