From 765044c7bb56a171d8fd340d36f51c9d7f7ec836 Mon Sep 17 00:00:00 2001 From: chirvasitua Date: Mon, 17 May 2021 19:45:52 -0400 Subject: 1st commit on 113_python --- challenge-113/stuart-little/python/ch-2.py | 51 ++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100755 challenge-113/stuart-little/python/ch-2.py (limited to 'challenge-113/stuart-little/python/ch-2.py') 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