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
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)) 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).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).rstrip() for line in string.splitlines()) “apples, pears # and # bananas\ngrapes\nbananas !apples”

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

1. Laquisha说道：

That’s a skillful answer to a difulcfit question