Skip to content

Latest commit

 

History

History
53 lines (34 loc) · 923 Bytes

Recursion_101.md

File metadata and controls

53 lines (34 loc) · 923 Bytes

CodeWars Python Solutions


Recursion 101

In this Kata, you will be given two positive integers a and b and your task will be to apply the following operations:

i) If a = 0 or b = 0, return [a,b]. Otherwise, go to step (ii);
ii) If a ≥ 2*b, set a = a - 2*b, and repeat step (i). Otherwise, go to step (iii);
iii) If b ≥ 2*a, set b = b - 2*a, and repeat step (i). Otherwise, return [a,b].

a and b will both be lower than 10E8.

More examples in tests cases. Good luck!


Given Code

def solve(a,b):
    pass

Solution

def solve(a,b):
    if a == 0 or b == 0:
        return [a,b]
    elif a >= 2*b:
        a = a-2*b
        return solve(a,b)
    elif b >= 2*a:
        b = b-2*a
        return solve(a,b)
    else:
        return [a,b]

See on CodeWars.com