python | codewars

Reversed String

def solution(string):
    return string[::-1]

点赞显示机制

def likes(names):
    n = len(names)
    return {
        0: 'no one likes this',
        1: '{} likes this', 
        2: '{} and {} like this', 
        3: '{}, {} and {} like this', 
        4: '{}, {} and {others} others like this'
    }[min(4, n)].format(*names[:3], others=n-2)

通过字典来实现!太骚了

一句话每一个单词的第一个字母大写

def to_jaden_case(string):
    return " ".join(w.capitalize() for w in string.split())

string.split() 把字符串分割成列表
w.capitalize()起到大写的作用

把一个数的每个数位平方后拼在一起

def square_digits(num):
    ret = ''
    for s in [int(x)**2 for x in str(num)]:
        ret += str(s)
    return int(ret)

你可能感兴趣的:(python刷题)