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 持续更新~~~~~


你可能感兴趣的:(codewars)