checkio(一)

Multiply(Intro)

Write a function that will receive 2 numbers as input and it should return the multiplication of these 2 numbers.
Input: Two arguments. Both are of type int.
Output: Int.
Example:

mult_two(2, 3) == 6
mult_two(1, 0) == 0

code:

def mult_two(a,b):
    return (a*b)

Number Length

You have a positive integer. Try to find out how many digits it has?
Input: A positive Int
Output: An Int.
Example:

number_length(10) == 2
number_length(0) == 1

code:

def number_length(a:int) ->int:
    return len(str(a))

str()

将其他格式的内容转换为字符串格式的内容

len()

计算元素个数

First Word(simplified)

You are given a string and you have to find its first word.
Input: A string.
Output: A string.
Example:

first_word("Hello world") == "Hello"

code:

def first_word(text: str) -> str:
        return text.split()[0]

str.split(sep=None,maxsplit=-1)

sep: sep为指定的分隔符,将除sep外的内容以sep为隔板拆分(sep可由多个字符组成)。如果 sep 未指定或为 None,连续的空格会被视为单个分隔符, 因此,使用 None 拆分空字符串或仅包含空格的字符串将返回 []。

maxsplit: smaxsplit表示最大分拆分,若maxsplit=1,则最多会有 maxsplit+1 个元素,若未指定或为 -1,则不限制拆分次数(进行所有可能的拆分)。
例:

>>> '1 2 3'.split()                ## 进行所有可能的拆分
['1', '2', '3']
>>> '1 2 3'.split(maxsplit=1)      ## 最多拆分为2个元素 
['1', '2 3']
>>> '   1   2   3   '.split()      ## 连续空格视为单个空格,进行所有可能的拆分 
['1', '2', '3']
>>>' '.split()                     ## 拆分空字符串或仅包含空格的字符串将返回 []
[]
>>> '1,2,3'.split(',')             ## 以“,”为拆分的标志,进行所有可能的拆分
['1', '2', '3']
>>> '1,2,3'.split(',', maxsplit=1) ## 以“,”为拆分的标志,最多分为2个元素的拆分
['1', '2,3']
>>> '1,2,,3,'.split(',')           ## 以“,”为拆分的标志,空格也是一个元素
['1', '2', '', '3', '']

s[i]

s[i] 表示s的第i

End Zeros

Try to find out how many zeros a given number has at the end.
Input: A positive Int
Output: An Int.
Example:

end_zeros(0) == 1
end_zeros(1) == 0
end_zeros(10) == 1
end_zeros(101) == 0

code:

def end_zeros(num: int) -> int:
    n=str(num)
    return len(n)-len(n.strip('0'))   

str.strip([chars])

用于移除字符串头尾指定的字符(默认为空格或换行符)或字符序列。chars表示移除字符串头尾指定的字符序列。

Backward String

You should return a given string in reverse order.
Input: A string.
Output: A string.
Example:

backward_string('val') == 'lav'
backward_string('') == ''
backward_string('ohho') == 'ohho'
backward_string('123456789') == '987654321'

code

def backward_string(val: str) -> str:
    a=reversed(val)
    return ''.join(a)
    ## return val[::-1]

reversed(seq)

reversed函数返回一个反转的迭代器。
seq表示需要转换的序列,可以是突破了,string,list或range。

str.join(iterable)

str表示字符串,iterable表示一个成员(只能有一个)可以是字符串,列表,元组,字典。str为空表示将iterable的元素拼接起来。

>>>','.join('abc')
a,b,c
>>>';'.join([a,b c])
a;b;c

字符串切片

str[::-1]
str表示需要反转的字符串

上一篇:反射机制(获取Class对象的三种方式)


下一篇:仿口袋助理源码 uniapp三端通用