Task:
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
- a positive integer width
You will return a collection or a string (depending on the language; Shell bash, PowerShell, Pascal and Fortran return a string) with the size of each of the squares.
Examples in general form:
(depending on the language)
sqInRect(5, 3) should return [3, 2, 1, 1]
sqInRect(3, 5) should return [3, 2, 1, 1]
You can see examples for your language in **"SAMPLE TESTS".**
Solution:
def sqInRect(a, b):
if a == b:
return None
res = []
while b:
b, a = sorted([a, b])
res += [b]
a, b = b, a-b
return res
print(sqInRect(5,3))
Rectangle into Squares Solutions | Codewars
No comments :
Post a Comment