Codewars-python 20190117

1.Descending Orders
input:324189
output:984321
知识点:’’.join
[::-1]:list倒序排列

def Descending_Order(num):
    #Bust a move right here
    num = list(str(num))
    return int(''.join(sorted(num)[::-1]))

2.Your Order,please
input:“is2 Thi1s T4est 3a”
output:“Thi1s is2 3a T4est”
方法一:

def order(sentence):
  # code here'
    sentence = sentence.split()
    Arr = ['']*len(sentence)
    for s in sentence:
        for c in s:
            if c.isdigit(): #只想到了这一步
                Arr[ord(c)-ord('1')] = s
                break
    return ' '.join(Arr)

方法二:

def order(words):
	return ' '.join(sorted(words.split(), key=lambda w:sorted(w)))

3.Mumbling
input:“abcd”
output:“A-Bb-Ccc-Dddd”
自己方法:

def accum(s):
	s = list(s)
	result = []
	reslut1 = []
	for i in range(len(s)):
    	result.append((i+1)*s[i])
	for j in result:
   		j = j.capitalize()
    	result1.append(j)
	return str('-'.join(result1))

大神做法:

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

4.Find the next perfect square!

import math
def find_next_square(sq):
    # Return the next square if sq is a square, -1 otherwise
    a = math.sqrt(sq)  
    if a - int(a) > 0:#判断是否为int类型
        return -1
    else:
        return int(pow((a+1),2))

你可能感兴趣的:(python)