简体   繁体   中英

Does anyone have any tips on how to solve this Codewars Kata to find squares from rectangle?

I am doing the code wars rectangular into squares Kata. I am very new to python, just trying to learn.

The aim is to split a given length and width into squares: https://www.codewars.com/kata/55466989aeecab5aac00003e/train/python

This is what I have so far. I cannot figure out how to determine whether the square will fit in the rectangle. Any tips will be appreciated.


def sqInRect(lng, wdth):
    solution = []
    b =  (lng * wdth) - (wdth**2)
    solution.append(wdth)
    print(b)
    if lng == wdth:
        return None
    else:
        for i in range(wdth,0,-1):
            print("i is " + str(i))
            if b - (i**2) > 0:
                if wdth + i <= lng:
                    solution.append(i)
                    b = b- i**2
                    print("Yes!")
                    print(b)
                    if b-(i**2) >= 0:
                        solution.append(i)
                        b = b - (i**2)
                        print("DOne")
                        print(b)
        
        
    return solution
                    
            
     

Just do it methodically, step by step.

def sqInRect(length, width):
    sizes = []
    while length and width:
        side = min(length,width)
        sizes.append(side)
        print( "%dx%d" % (side,side) )
        if length == side:
            width -= side
        else:
            length -= side
    return sizes

print(sqInRect(5,3))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM