From 455cbc85ee6f2a4a3408f5e75e5d5f813d8b48fa Mon Sep 17 00:00:00 2001 From: Lubos Kolouch Date: Sat, 8 Jul 2023 12:46:34 +0200 Subject: feat(challenge-146/lubos-kolouch/perl,python/): Challenge 146 LK Perl Python --- challenge-146/lubos-kolouch/python/ch-2.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 challenge-146/lubos-kolouch/python/ch-2.py (limited to 'challenge-146/lubos-kolouch/python/ch-2.py') 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') -- cgit