fork download
  1. # The important bit:
  2.  
  3. def reduce_fraction(n, d):
  4. f_iter = range(1, min(n, d) + 1)
  5. n_list = [x for x in f_iter if n % x == 0]
  6. d_list = [y for y in f_iter if d % y == 0]
  7. f_list = [f for f in n_list if f in d_list]
  8. gcf = max(f_list)
  9. return str(n//gcf) + ' ' + str(d//gcf)
  10.  
  11. # Parsing input and printing output:
  12.  
  13. def parse_input(s):
  14. output = []
  15. for line in s.splitlines():
  16. line = line.strip()
  17. if line:
  18. line = tuple(map(int, line.split()))
  19. output += [line]
  20. return output
  21.  
  22. def main():
  23. numbers = '''
  24. 4 8
  25. 1536 78360
  26. 51478 5536
  27. 46410 119340
  28. 7673 4729
  29. 4096 1024
  30. '''
  31.  
  32. tup_list = parse_input(numbers)
  33.  
  34. for x, y in tup_list:
  35. print(reduce_fraction(x, y))
  36.  
  37. if __name__ == '__main__': main()
Success #stdin #stdout 0.06s 9984KB
stdin
Standard input is empty
stdout
1 2
64 3265
25739 2768
7 18
7673 4729
4 1