CodeWars打卡(09)

Details:

Rectangle into Squares

Description:

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.

  sqInRect(5, 3) should return [3, 2, 1, 1]
  sqInRect(3, 5) should return [3, 2, 1, 1]

Notes:

  • lng == wdth as a starting case would be an entirely different problem and the drawing is planned to be interpreted with lng != wdth. (See kata, Square into Squares. Protect trees! http://www.codewars.com/kata/54eb33e5bc1a25440d000891 for this problem). When the initial parameters are so that lng == wdth, the solution [lng] would be the most obvious but not in the spirit of this kata so, in that case, return None/nil/null/Nothing. Return {} with C++. In that case the returned structure of C will have its sz component equal to 0. Return the string "nil" with Bash and Fortran.
  • You can see more examples in "RUN SAMPLE TESTS".

中文大概含義:

看圖就明白了,沒必要說明~~

我自己的代碼如下:

def sqInRect(lng, wdth):
    num = [lng,wdth]
    Min = min(num)
    if lng==wdth:
        return None
    squre = []
    while Min!=0:
        squre.append(Min)
        num = [num[0]-Min,num[1]] if num[1]==Min else [num[0],num[1]-Min]
        Min = min(num)
    # your code
    return squre

第一名代碼:

def sqInRect(lng, wdth):
    if lng == wdth:
        return None
    if lng < wdth:
        wdth, lng = lng, wdth
    res = []
    while lng != wdth:
        res.append(wdth)
        lng = lng - wdth
        if lng < wdth:
            wdth, lng = lng, wdth
    res.append(wdth)
    return res

第二名代碼:

# 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)
  1. 我的方法和第一名思路類似,很容易明白.
  2. 第二名代碼使用了遞歸的方式,效率沒測試怎么樣,但是代碼風(fēng)格值的學(xué)習(xí).recur是一個標志位,為了得到第一個None.相當(dāng)于我的第一個IF判斷.
  • 這道題難度系數(shù)六級
  • 天外有天,人外有人~~
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末赃蛛,一起剝皮案震驚了整個濱河市信粮,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌北戏,老刑警劉巖师郑,帶你破解...
    沈念sama閱讀 218,525評論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件环葵,死亡現(xiàn)場離奇詭異,居然都是意外死亡宝冕,警方通過查閱死者的電腦和手機张遭,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,203評論 3 395
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來地梨,“玉大人菊卷,你說我怎么就攤上這事”ζ剩” “怎么了的烁?”我有些...
    開封第一講書人閱讀 164,862評論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長诈闺。 經(jīng)常有香客問我渴庆,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,728評論 1 294
  • 正文 為了忘掉前任襟雷,我火速辦了婚禮刃滓,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘耸弄。我一直安慰自己咧虎,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 67,743評論 6 392
  • 文/花漫 我一把揭開白布计呈。 她就那樣靜靜地躺著砰诵,像睡著了一般。 火紅的嫁衣襯著肌膚如雪捌显。 梳的紋絲不亂的頭發(fā)上茁彭,一...
    開封第一講書人閱讀 51,590評論 1 305
  • 那天,我揣著相機與錄音扶歪,去河邊找鬼理肺。 笑死,一個胖子當(dāng)著我的面吹牛善镰,可吹牛的內(nèi)容都是我干的妹萨。 我是一名探鬼主播,決...
    沈念sama閱讀 40,330評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼炫欺,長吁一口氣:“原來是場噩夢啊……” “哼乎完!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起品洛,我...
    開封第一講書人閱讀 39,244評論 0 276
  • 序言:老撾萬榮一對情侶失蹤囱怕,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后毫别,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體娃弓,經(jīng)...
    沈念sama閱讀 45,693評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,885評論 3 336
  • 正文 我和宋清朗相戀三年岛宦,在試婚紗的時候發(fā)現(xiàn)自己被綠了台丛。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,001評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡砾肺,死狀恐怖挽霉,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情变汪,我是刑警寧澤侠坎,帶...
    沈念sama閱讀 35,723評論 5 346
  • 正文 年R本政府宣布,位于F島的核電站裙盾,受9級特大地震影響实胸,放射性物質(zhì)發(fā)生泄漏他嫡。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,343評論 3 330
  • 文/蒙蒙 一庐完、第九天 我趴在偏房一處隱蔽的房頂上張望钢属。 院中可真熱鬧,春花似錦门躯、人聲如沸淆党。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,919評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽染乌。三九已至,卻和暖如春懂讯,著一層夾襖步出監(jiān)牢的瞬間荷憋,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,042評論 1 270
  • 我被黑心中介騙來泰國打工域醇, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人蓉媳。 一個月前我還...
    沈念sama閱讀 48,191評論 3 370
  • 正文 我出身青樓譬挚,卻偏偏與公主長得像,于是被迫代替她去往敵國和親酪呻。 傳聞我的和親對象是個殘疾皇子减宣,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,955評論 2 355

推薦閱讀更多精彩內(nèi)容