blob: e10180e83f4d456516206a1d3872ebd6c9f54fd6 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
#!/usr/bin/env python3
# Challenge 113
#
# TASK #2 - Recreate Binary Tree
# Submitted by: Mohammad S Anwar
# You are given a Binary Tree.
#
# Write a script to replace each node of the tree with the sum of all the
# remaining nodes.
#
# Example
# Input Binary Tree
# 1
# / \
# 2 3
# / / \
# 4 5 6
# \
# 7
# Output Binary Tree
# 27
# / \
# 26 25
# / / \
# 24 23 22
# \
# 21
import sys
import re
import fileinput
def read_input():
lines = []
for line in fileinput.input():
lines.append(line)
return lines
tree = "".join(read_input())
nums = [int(x) for x in re.findall(r"\d+", tree)]
sum = sum(nums)
for num in nums:
tree = re.sub(r"\b"+str(num)+r"\b ?", str(sum-num), tree)
tree = re.sub(r"\n$", "", tree)
print(tree)
|