CodeWars刷题记录20180907-Python

1、判断一个整数是否为完全平方数

我的解答:

import math

def is_square(n): 
    if n < 0:
        return False
    else:
        a = int(math.sqrt(n))
        return a*a == n
    

漂亮解答:

import math
def is_square(n):
    return n > -1 and math.sqrt(n) % 1 == 0;

2、Mumbling

Examples:

accum("abcd")    # "A-Bb-Ccc-Dddd"
accum("RqaEzty") # "R-Qq-Aaa-Eeee-Zzzzz-Tttttt-Yyyyyyy"
accum("cwAt")    # "C-Ww-Aaa-Tttt"

The parameter of accum is a string which includes only letters from a..z and A..Z.

我的解答:

def accum(s):   
    if len(s) == 1:
        return s.upper()
    lowerStr = s.lower()
    res = lowerStr[0]
    for i in xrange(1,len(s)):
        res += '-' + lowerStr[i]*(i+1)
    return res.title()

漂亮解答:

def accum(s):
    return '-'.join(c.upper() + c.lower() * i for i, c in enumerate(s))

3、Strip Comments删除注释

Complete the solution so that it strips all text that follows any of a set of comment markers passed in. Any whitespace at the end of the line should also be stripped out.

Example:

Given an input string of:

apples, pears # and bananas
grapes
bananas !apples

The output expected would be:

apples, pears
grapes
bananas

The code would be called like so:

result = solution("apples, pears # and bananas\ngrapes\nbananas !apples", ["#", "!"])
# result should == "apples, pears\ngrapes\nbananas"

我的解答:

def solution(string, markers):
    strList = string.split('\n')
    for s in markers:
        strList = [v.split(s)[0].rstrip() for v in strList]
    return '\n'.join(strList)

一种正则表示解法:

from re import split, escape


def solution(string, markers):
    if markers:
        pattern = "[" + escape("".join(markers)) + "]"
    else:
        pattern = ''
    return '\n'.join(split(pattern, line)[0].rstrip() for line in string.splitlines())

备注:这题最需要关注的是注释中包含注释的情况,例如:

“apples, pears # and # bananas\ngrapes\nbananas !apples”

关于 “CodeWars刷题记录20180907-Python” 的 1 个意见

评论关闭。