Skip to content

Instantly share code, notes, and snippets.

@harmo
Last active February 10, 2017 11:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save harmo/c6cfc78d773446f50019d386ee2860e6 to your computer and use it in GitHub Desktop.
Save harmo/c6cfc78d773446f50019d386ee2860e6 to your computer and use it in GitHub Desktop.

The drawing below gives an idea of how to cut a given "true" rectangle into squares ("true" rectangle meaning that the two dimensions are different).

Can you translate this drawing into an algorithm?

You will be given two dimensions

a positive integer length (parameter named lng) a positive integer width (parameter named wdth) You will return an array with the size of each of the squares.

    sqInRect(5, 3) should return [3, 2, 1, 1]
    sqInRect(3, 5) should return [3, 2, 1, 1]
    sqInRect(5, 5) should return None/null
@harmo
Copy link
Author

harmo commented Feb 10, 2017

def sqInRect(x, y):
    if x == y:
        return None
        
    result = []
    while y != x:
        if y > x:
            y -= x
            result.append(x)
        elif x > y:
            x -= y
            result.append(y)

    result.append(y)
    
    return result

OR

def sqInRect(x, y):
    r = lambda x, y: [x] if x==y else (([y] + r(x-y, y)) if x > y else ([x] + r(x, y-x)))
    if x != y:
        return r(x, y)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment