aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2024-09-11 20:37:50 +0100
committerGitHub <noreply@github.com>2024-09-11 20:37:50 +0100
commit2b1abf52154959807ae50fbd9266a2de475c519a (patch)
tree3412387fc1eb6522deb0726ba6b0fb375bc94356
parentcbf28e6e924b7cd24d1614a5ad28d801f0e85dea (diff)
parent96f4b7c0a062a00bf2aba6098d8b703991510458 (diff)
downloadperlweeklychallenge-club-2b1abf52154959807ae50fbd9266a2de475c519a.tar.gz
perlweeklychallenge-club-2b1abf52154959807ae50fbd9266a2de475c519a.tar.bz2
perlweeklychallenge-club-2b1abf52154959807ae50fbd9266a2de475c519a.zip
Merge pull request #10823 from pauloscustodio/master
Add Python solutions
-rw-r--r--challenge-056/paulo-custodio/python/ch-1.py29
-rw-r--r--challenge-056/paulo-custodio/python/ch-2.py81
2 files changed, 110 insertions, 0 deletions
diff --git a/challenge-056/paulo-custodio/python/ch-1.py b/challenge-056/paulo-custodio/python/ch-1.py
new file mode 100644
index 0000000000..38a1c8f8b8
--- /dev/null
+++ b/challenge-056/paulo-custodio/python/ch-1.py
@@ -0,0 +1,29 @@
+#!/usr/bin/env python3
+
+# Challenge 056
+#
+# TASK #1
+# Diff-K
+# You are given an array @N of positive integers (sorted) and another non
+# negative integer k.
+#
+# Write a script to find if there exists 2 indices i and j such that
+# A[i] - A[j] = k and i != j.
+#
+# It should print the pairs of indices, if any such pairs exist.
+#
+# Example:
+#
+# @N = (2, 7, 9)
+# $k = 2
+# Output : 2,1
+
+import sys
+
+k = int(sys.argv[1])
+n = [int(x) for x in sys.argv[2:]]
+
+for i in range(0, len(n)-1):
+ for j in range(i+1, len(n)):
+ if abs(n[i]-n[j]) == k:
+ print(str(i)+","+str(j))
diff --git a/challenge-056/paulo-custodio/python/ch-2.py b/challenge-056/paulo-custodio/python/ch-2.py
new file mode 100644
index 0000000000..5ce29d26b9
--- /dev/null
+++ b/challenge-056/paulo-custodio/python/ch-2.py
@@ -0,0 +1,81 @@
+#!/usr/bin/env python3
+
+# Challenge 056
+#
+# TASK #2
+# Path Sum
+# You are given a binary tree and a sum, write a script to find if the tree has
+# a path such that adding up all the values along the path equals the given sum.
+# Only complete paths (from root to leaf node) may be considered for a sum.
+#
+# Example
+# Given the below binary tree and sum = 22,
+#
+# 5
+# / \
+# 4 8
+# / / \
+# 11 13 9
+# / \ \
+# 7 2 1
+# For the given binary tree, the partial path sum 5 ? 8 ? 9 = 22 is not valid.
+#
+# The script should return the path 5 ? 4 ? 11 ? 2 whose sum is 22.
+
+import fileinput
+import re
+import sys
+
+class Node:
+ def __init__(self, value):
+ self.value = value
+ self.left = None
+ self.right = None
+
+ def __repr__(self):
+ return "Node(value: {}, left: {}, right: {})" \
+ .format(self.value, self.left, self.right)
+
+def read_input():
+ lines = []
+ for line in fileinput.input():
+ lines.append(line)
+ return lines
+
+def parse_subtree(lines, row, col):
+ def ch(row, col):
+ if row < 0 or row >= len(lines) or \
+ col < 0 or col >= len(lines[row]):
+ return ' '
+ else:
+ return lines[row][col]
+
+ tree = Node(int(lines[row][col]))
+ if ch(row + 1, col - 1) == '/':
+ tree.left = parse_subtree(lines, row + 2, col - 2)
+ if ch(row + 1, col + 1) == '\\':
+ tree.right = parse_subtree(lines, row + 2, col + 2)
+
+ return tree
+
+def parse_tree(lines):
+ found = re.search(r'^[ ]+\d', lines[0])
+ col = found.span()[1] - 1
+ return parse_subtree(lines, 0, col)
+
+
+def path_sum(path, sum1, tree):
+ path1 = path+[tree.value]
+ if not tree.left and not tree.right:
+ if sum(path1) == sum1:
+ print(" ".join([str(x) for x in path1]))
+ else:
+ if tree.left:
+ path_sum(path1, sum1, tree.left)
+ if tree.right:
+ path_sum(path1, sum1, tree.right)
+
+sum1 = int(sys.argv[1])
+sys.argv.pop()
+tree = parse_tree(read_input())
+path_sum([], sum1, tree)