aboutsummaryrefslogtreecommitdiff
path: root/challenge-146/lubos-kolouch/python/ch-2.py
diff options
context:
space:
mode:
authorLubos Kolouch <lubos@kolouch.net>2023-07-08 12:46:34 +0200
committerLubos Kolouch <lubos@kolouch.net>2023-07-08 12:46:34 +0200
commit455cbc85ee6f2a4a3408f5e75e5d5f813d8b48fa (patch)
tree4acd93e8aebb7219028a8ad297a4889d4a7b9357 /challenge-146/lubos-kolouch/python/ch-2.py
parent393d8f7b2cc5cb6fa63fd185b730a61173e452a4 (diff)
downloadperlweeklychallenge-club-455cbc85ee6f2a4a3408f5e75e5d5f813d8b48fa.tar.gz
perlweeklychallenge-club-455cbc85ee6f2a4a3408f5e75e5d5f813d8b48fa.tar.bz2
perlweeklychallenge-club-455cbc85ee6f2a4a3408f5e75e5d5f813d8b48fa.zip
feat(challenge-146/lubos-kolouch/perl,python/): Challenge 146 LK Perl Python
Diffstat (limited to 'challenge-146/lubos-kolouch/python/ch-2.py')
-rw-r--r--challenge-146/lubos-kolouch/python/ch-2.py19
1 files changed, 19 insertions, 0 deletions
diff --git a/challenge-146/lubos-kolouch/python/ch-2.py b/challenge-146/lubos-kolouch/python/ch-2.py
new file mode 100644
index 0000000000..0cfdb31445
--- /dev/null
+++ b/challenge-146/lubos-kolouch/python/ch-2.py
@@ -0,0 +1,19 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+from fractions import Fraction
+
+
+def find_ancestors(fraction):
+ num, denom = map(int, fraction.split("/"))
+ parent = Fraction(num, denom - num) if denom > num else Fraction(num - denom, denom)
+ grandparent = (
+ Fraction(parent.numerator, parent.denominator - parent.numerator)
+ if parent.denominator > parent.numerator
+ else Fraction(parent.numerator - parent.denominator, parent.denominator)
+ )
+ return str(parent), str(grandparent)
+
+
+print(find_ancestors("3/5")) # Output: ('3/2', '1/2')
+print(find_ancestors("4/3")) # Output: ('1/3', '1/2')