Codewars算法題(7)

第23題:(Convert string to camel case)

問題:

Complete the method/function so that it converts dash/underscore delimited words into camel casing. The first word within the output should be capitalized only if the original word was capitalized.

Examples:

# returns "theStealthWarrior"
to_camel_case("the-stealth-warrior") 

# returns "TheStealthWarrior"
to_camel_case("The_Stealth_Warrior")
問題描述:就是將給定字符串變爲:第一個單詞不變。之後的每個單詞的首字母大寫。類似於駝峯命名

自己代碼:

def to_camel_case(text):
    new1 = text.replace("-", "_")
    new2 = new1.split("_")
    new=''
    if text=='':
        return ''
    else:
        for s in new2:
            new += s[:1].upper() + s[1:].lower()
        return text[0]+''.join(new)[1:]
還是一如既往的運用最簡單的if else for語句~~~~~~~真是讓人腦仁疼!!

評分較高代碼:

(1)

def to_camel_case(s):
    return s[0] + s.title().translate(None, "-_")[1:] if s else s
title()的作用是將每個單詞的首字母大寫;而capitalize()的作用是將首單詞的首字母大寫;

(2)

def to_camel_case(text):
    return text[0] + ''.join([w[0].upper() + w[1:] for w in text.replace("_", "-").split("-")])[1:] if text else ''
我感覺這位仁兄的想法和我的差不多,只不過他寫成了一個合着的語句。

(3)

import re
def to_camel_case(text):
    return reduce(lambda p, n: p + n[0].upper() + n[1:], re.split('[-_]', text))

第24題:(Bouncing Balls)

問題:

A child plays with a ball on the nth floor of a big building. The height of this floor is known:

(float parameter "h" in meters, h > 0) .

He lets out the ball. The ball rebounds for example to two-thirds:

(float parameter "bounce", 0 < bounce < 1)

of its height.

His mother looks out of a window that is 1.5 meters from the ground:

(float parameters window < h).

How many times will the mother see the ball either falling or bouncing in front of the window

(return a positive integer unless conditions are not fulfilled in which case return -1) ?

Note

You will admit that the ball can only be seen if the height of the rebouncing ball is stricty greater than the window parameter.

Example:

h = 3, bounce = 0.66, window = 1.5, result is 3

h = 3, bounce = 1, window = 1.5, result is -1

問題描述:給定樓層數(從該樓層將球扔出),給定彈跳消減係數,給定窗戶高度(當球的高度>窗戶高度時纔可看                     到球),求當球從某層扔出後,能看到球的次數?

自己代碼:

def bouncingBall(h, bounce, window):
    n=0
    if bounce>=1 or bounce<=0:
        return -1
    while (h>window):
        h=h*bounce
        n+=1
    return 1+(n-1)*2

第25題:(Write Number in Expanded Form)(以擴展形式寫數字)

問題:

You will be given a number and you will need to return it as a string in Expanded Form. For example:

expanded_form(12) # Should return '10 + 2'
expanded_form(42) # Should return '40 + 2'
expanded_form(70304) # Should return '70000 + 300 + 4'
自己代碼:

def expanded_form(num):
    s=[]
    lnum1=map(int,str(num))
    lnum2=list(lnum1)
    for i in range(0,len(lnum2)):
        if lnum2[-(i+1)]!=0:
            s.insert(0,str(lnum2[-(i+1)]*10**(i)))
    return ' + '.join(s)

代碼解析:

 首先將利用map()函數將整數轉化爲列表,其實在這裏lnum1已經是列表了,但是在程序執行過程中仍報錯,所以我又轉化了一下。其實我是逆向將數字插入到列表中s中的,比如:1234,我先處理4,再處理3(通過insert()函數將處理的結果30插入4之前),再處理2,再處理1。最終再將列表轉化爲字符串形式。

評分較高代碼:

def expanded_form(n):
    result = []
    for a in range(len(str(n)) - 1, -1, -1):
        current = 10 ** a
        quo, n = divmod(n, current)
        if quo:
            result.append(str(quo * current))
    return ' + '.join(result)
代碼解析:通過divmod()函數,求得商和餘數(實現了一個小循環,上次的餘數,當做本次的除數)



201707271623 持續更新~~~~~


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章