Codewars--Rectangle into Squares

Rectangle into Squares

problem describtion

在这里插入图片描述

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).

在这里插入图片描述

alternative text

Can you translate this drawing into an algorithm?

You will be given two dimensions
1 、a positive integer length (parameter named lng)
2 、a positive integer width (parameter named wdth)

You will return an array or a string (depending on the language; Shell bash and Fortran return a string) with the size of each of the squares.

the version of python:

Methon:

methon1:

def sqInRect(lng, wdth):
    # your code
    import math
    if lng==wdth:
        return None
    list=[]
    if lng<wdth:
            lng,wdth=wdth,lng 
    while lng!=wdth:
        list.append(wdth)
        lng=lng-wdth
        if lng<wdth:
            lng,wdth=wdth,lng
    list.append(wdth)
    return list

The first time,I use “list[]” instead of “list=[]”. You should take care of the version of python.

methon2:

# Recursive solution
def sqInRect(lng, wdth, recur = 0):
    if lng == wdth:
        return (None, [lng])[recur]            # If this is original function call, return None for equal sides (per kata requirement);
                                               # if this is recursion call, we reached the smallest square, so get out of recursion.
    lesser = min(lng, wdth)
    return [lesser] + sqInRect(lesser, abs(lng - wdth), recur = 1)

It uses recursive solution.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值