c
codeye
V1
2022/10/23阅读:17主题:默认主题
真正的矩形
class Rectangle:
@staticmethod
def rect_into_rects(lngth, wdth):
#Put your code here.
s = []
out = ''
l,w = lngth, wdth
while w >= 1 and l >= 1:
s.extend([w]*(l//w))
l,w = w,l % w
return s
但要满足按着题目要求的输出格式和顺序,仅依靠👆子程序还是有困难!
有高手做了如下修改,添加子函数 adder()整个代码看起来松耦合且 便于阅读。
class Rectangle:
@staticmethod
def rect_into_rects(w, h):
if w<h: w,h = h,w
def adder(h,w,n):
lst.extend( ('({}*{})'.format(w,h),) * n )
lst = []
while w!=h and h>0:
nSQ, nextH = divmod(w,h)
for n in range(2,nSQ+1):
adder(h, n*h, nSQ+1-n)
for n in range(1,(nextH>0)*nSQ+1):
adder(h, n*h+nextH, 1)
w,h = h,nextH
return lst if lst else None
作者介绍
c
codeye
V1