aboutsummaryrefslogtreecommitdiff
path: root/challenge-146/paulo-custodio/python/ch-2.py
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2024-09-25 09:29:21 +0100
committerGitHub <noreply@github.com>2024-09-25 09:29:21 +0100
commit261da1d2c54ab8faab88537076b4248d5abd00f7 (patch)
treec60a0327997c7d9759f275323ae120d735726784 /challenge-146/paulo-custodio/python/ch-2.py
parent9e56f497ae79225e5a6e041a963741885335d0d0 (diff)
parent206c2fce8db1de9b7f82f04a3276005a284b3c40 (diff)
downloadperlweeklychallenge-club-261da1d2c54ab8faab88537076b4248d5abd00f7.tar.gz
perlweeklychallenge-club-261da1d2c54ab8faab88537076b4248d5abd00f7.tar.bz2
perlweeklychallenge-club-261da1d2c54ab8faab88537076b4248d5abd00f7.zip
Merge pull request #10906 from pauloscustodio/master
Add Python solutions
Diffstat (limited to 'challenge-146/paulo-custodio/python/ch-2.py')
-rw-r--r--challenge-146/paulo-custodio/python/ch-2.py42
1 files changed, 42 insertions, 0 deletions
diff --git a/challenge-146/paulo-custodio/python/ch-2.py b/challenge-146/paulo-custodio/python/ch-2.py
new file mode 100644
index 0000000000..a355bd9517
--- /dev/null
+++ b/challenge-146/paulo-custodio/python/ch-2.py
@@ -0,0 +1,42 @@
+#!/usr/bin/env python3
+
+# Challenge 146
+#
+# Consider the following Curious Fraction Tree:
+#
+#
+# Curious Fraction Tree
+#
+#
+# You are given a fraction, member of the tree created similar to the above sample.
+#
+# Write a script to find out the parent and grandparent of the given member.
+#
+# Example 1:
+# Input: $member = '3/5';
+# Output: parent = '3/2' and grandparent = '1/2'
+# Example 2:
+# Input: $member = '4/3';
+# Output: parent = '1/3' and grandparent = '1/2'
+#
+# Solution:
+# for node a/b, the children are a/(a+b) and (a+b)/b
+
+import sys
+
+def parent(a, b):
+ if a / b < 1: # a/(a+b)
+ parent_a = a
+ parent_b = abs(b - a)
+ else: # (a+b)/b
+ parent_a = abs(b - a)
+ parent_b = b
+ return parent_a, parent_b
+
+input_value = sys.argv[1]
+a, b = map(int, input_value.split('/'))
+print(f"{a}/{b} -> ", end="")
+a, b = parent(a, b)
+print(f"{a}/{b} -> ", end="")
+a, b = parent(a, b)
+print(f"{a}/{b}")