codewars题目

1.Your task is to write a function that takes a string and return a new string with all vowels removed.For example, the string "This website is for losers LOL!" would become "Ths wbst s fr lsrs LL!". Note: for this kata y isn't considered a vowel.

def disemvowel(string):
    s2=[]
    for ii in string:
        if ii.lower() in ['e','a','i','o','u']:
            continue
        else:
            s2.append(ii)
    string=''.join(s2)
        
    return string
测试程序

2.Your task is to write a function maskify, which changes all but the last four characters into '#'.

def maskify(cc):
    return ('#'*(len(cc)-4)+cc[-4:] if len(cc)>4 else cc)
test

 3.Implement a function that adds two numbers together and returns their sum in binary. The conversion can be done before, or after the addition.The binary number returned should be a string.

def add_binary(a,b):
    return bin(a+b).lstrip('0b')
add_binary

 4.Well met with Fibonacci bigger brother, AKA Tribonacci.As the name may already reveal, it works basically like a Fibonacci, but summing the last 3 (instead of 2) numbers of the sequence to generate the next. And, worse part of it, regrettably I won't get to hear non-native Italian speakers trying to pronounce it :(So, if we are to start our Tribonacci sequence with [1, 1, 1] as a starting input (AKA signature), we have this sequence:[1, 1 ,1, 3, 5, 9, 17, 31, ...]

But what if we started with [0, 0, 1] as a signature? As starting with [0, 1] instead of [1, 1] basically shifts the common Fibonacci sequence by once place, you may be tempted to think that we would get the same sequence shifted by 2 places, but that is not the case and we would get:[0, 0, 1, 1, 2, 4, 7, 13, 24, ...]

Well, you may have guessed it by now, but to be clear: you need to create a fibonacci function that given a signature array/list, returns the first n elements - signature included of the so seeded sequence.

Signature will always contain 3 numbers; n will always be a non-negative number; if n == 0, then return an empty array (except in C return NULL) and be ready for anything else which is not clearly specified ;)

def tribonacci(signature, n):
    for i in range(3,n): # 3 到 n-1循环
        signature.append(signature[i-1] + signature[i-2] + signature[i-3]) # 根据算法写公式
    return signature[0:n] 
tribonacci

 5.Write a function, which takes a non-negative integer (seconds) as input and returns the time in a human-readable format (HH:MM:SS)

  • HH = hours, padded to 2 digits, range: 00 - 99
  • MM = minutes, padded to 2 digits, range: 00 - 59
  • SS = seconds, padded to 2 digits, range: 00 - 59

The maximum time never exceeds 359999 (99:59:59)

def make_readable(seconds):
    minutes, seconds = divmod(seconds, 60)
    hours, minutes = divmod(minutes, 60)
    res = '%02d:%02d:%02d' %(hours, minutes, seconds)
    return res
make_readable

6.Count the number of Duplicates

Write a function that will return the count of distinct case-insensitive alphabetic characters and numeric digits that occur more than once in the input string. The input string can be assumed to contain only alphabets (both uppercase and lowercase) and numeric digits.

Example

"abcde" -> 0 # no characters repeats more than once
"aabbcde" -> 2 # 'a' and 'b'
"aabBcde" -> 2 # 'a' occurs twice and 'b' twice (`b` and `B`)
"indivisibility" -> 1 # 'i' occurs six times
"Indivisibilities" -> 2 # 'i' occurs seven times and 's' occurs twice
"aA11" -> 2 # 'a' and '1'
"ABBA" -> 2 # 'A' and 'B' each occur twice

def duplicate_count(text):
    dicts = {}
    text = text.lower()
    for i in text:
        if text.count(i) > 1:
            dicts[i] = text.count(i)
    return len(dicts)
duplicate_count

 7.The city provides its citizens with a Walk Generating App on their phones -- everytime you press the button it sends you an array of one-letter strings representing directions to walk (eg. ['n', 's', 'w', 'e']). You always walk only a single block in a direction and you know it takes you one minute to traverse one city block, so create a function that will return true if the walk the app gives you will take you exactly ten minutes (you don't want to be early or late!) and will, of course, return you to your starting point. Return false otherwise.Note: you will always receive a valid array containing a random assortment of direction letters ('n', 's', 'e', or 'w' only). It will never give you an empty array (that's not a walk, that's standing still!).

def isValidWalk(walk):
    if len(walk) != 10:
        return False
    if walk.count('n') == walk.count('s') and walk.count('w') == walk.count('e'):
        return True
    return False
isValidWalk

 8.Write a function named first_non_repeating_letter that takes a string input, and returns the first character that is not repeated anywhere in the string.For example, if given the input 'stress', the function should return 't', since the letter t only occurs once in the string, and occurs first in the string.As an added challenge, upper- and lowercase letters are considered the same character, but the function should return the correct case for the initial letter. For example, the input 'sTreSS' should return 'T'.If a string contains all repeating characters, it should return an empty string ("") or None -- see sample tests.

def first_non_repeating_letter(string):
    string_lower = string.lower()
    for i, letter in enumerate(string_lower):
        if string_lower.count(letter) == 1:
            return string[i]
            
    return ""
first_non_repeating_letter

 9.阶乘的三种实现方式(递归|双循环|单循环)

# 第一种
n = int(input('输入的数:'))
sum = 0


def func(n):
    if n == 1:
        return 1
    else:
        return n * func(n - 1)

for i in range(1, n + 1):
    sum += func(i)
print(sum)
# 第二种,双循环
sum = 0
for i in range(1, n + 1):
    temp = 1
    for j in range(1, i + 1):
        temp *= j
    sum += temp
print(sum)
# 第三种,单循环
sum, temp = 0, 1
for j in range(1, n + 1):
    temp = temp * j
    sum += temp
print(sum)

 

posted on 2020-08-05 15:36  眨眼星舟_Eric  阅读(291)  评论(0编辑  收藏  举报

导航