aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorchirvasitua <stuart-little@users.noreply.github.com>2021-05-17 19:45:52 -0400
committerchirvasitua <stuart-little@users.noreply.github.com>2021-05-17 19:45:52 -0400
commit765044c7bb56a171d8fd340d36f51c9d7f7ec836 (patch)
tree94c99087daad1a4bcd2f3f7831cfa6123d842d1e
parentc3cd45087006d3f63b05219b8280a25dc1ea7ba9 (diff)
downloadperlweeklychallenge-club-765044c7bb56a171d8fd340d36f51c9d7f7ec836.tar.gz
perlweeklychallenge-club-765044c7bb56a171d8fd340d36f51c9d7f7ec836.tar.bz2
perlweeklychallenge-club-765044c7bb56a171d8fd340d36f51c9d7f7ec836.zip
1st commit on 113_python
-rwxr-xr-xchallenge-113/stuart-little/python/ch-1.py18
-rwxr-xr-xchallenge-113/stuart-little/python/ch-2.py51
2 files changed, 69 insertions, 0 deletions
diff --git a/challenge-113/stuart-little/python/ch-1.py b/challenge-113/stuart-little/python/ch-1.py
new file mode 100755
index 0000000000..bbaaf80337
--- /dev/null
+++ b/challenge-113/stuart-little/python/ch-1.py
@@ -0,0 +1,18 @@
+#!/usr/bin/env python
+
+# run <script> <number> <digit>
+
+import sys
+
+def lastDigSumm(nr, dig, nrSummands):
+ return ((nr - nrSummands * dig) % 10 == 0) and (nrSummands * dig <= nr) and (nrSummands * ((dig -1) * 10 + dig) >= nr)
+
+def lastDig(nr,dig):
+ return bool(list(filter(lambda x: lastDigSumm(nr,dig,x), range(1,10))))
+
+def sol(nr,dig):
+ if (dig == 0):
+ return (nr >= 101 or (nr % 10 == 0))
+ return ((nr >= dig * 11) or lastDig(nr,dig));
+
+print(int(sol(*map(int,sys.argv[1:]))))
diff --git a/challenge-113/stuart-little/python/ch-2.py b/challenge-113/stuart-little/python/ch-2.py
new file mode 100755
index 0000000000..593c4cc9a7
--- /dev/null
+++ b/challenge-113/stuart-little/python/ch-2.py
@@ -0,0 +1,51 @@
+#!/usr/bin/env python
+
+import sys
+from print_tree import print_tree
+
+class Node(object):
+ def __init__(self, value):
+ self.value = value
+ self.children = []
+
+class print_custom_tree(print_tree):
+ def get_children(self, node):
+ return node.children
+ def get_node_str(self, node):
+ return str(node.value)
+
+def moreDots(lst):
+ return lambda i: 2*len(list(filter(lambda x: x == ".",lst[1:i+1]))) > i
+
+def list2node(lst):
+ if len(lst) == 0 or lst[0] == '.':
+ return Node("")
+ ix = next(filter(moreDots(lst), range(0,len(lst))))
+ root=Node(lst[0])
+ lft=list2node(lst[1:ix+1])
+ rght=list2node(lst[ix+1:])
+ root.children=[lft,rght]
+ return root
+
+INPUT = sys.argv[1:] if len(sys.argv) >= 2 else ["1", "2", "4", ".", "7", ".", ".", ".", "3", "5", ".", ".", "6", ".", "."]
+sm = sum(map(int, filter(lambda x: x.isdigit(), INPUT)))
+outTreeList=list(map(lambda x: str(sm - int(x)) if x.isdigit() else x, INPUT))
+print_custom_tree(list2node(outTreeList))
+
+"""
+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
+"""