floor函数
floor 下取整函数。
import math
math.floor( x )
字符串分割
字符串分割,分割后是列表,默认以空格
def first_word(text: str) -> str:
"""
returns the first word in a given text.
"""
aa=text.split(' ')[0]
return aa
if __name__ == "__main__":
print("Example:")
print(first_word("Hello world"))
# These "asserts" are used for self-checking and not for an auto-testing
assert first_word("Hello world") == "Hello"
assert first_word("a word") == "a"
assert first_word("hi") == "hi"
print("Coding complete? Click 'Check' to earn cool rewards!")
分割。a.split(' ')[0]读取字符串以空格分割的第一个元素。
strip()
去除字符串两边字符,strip('0')去除字符串两边的零。
def end_zeros(num: int) -> int:
b=str(num)
j=0
return len(b)-len(b.strip('0'))
if __name__ == "__main__":
print("Example:")
print(end_zeros(0))
# These "asserts" are used for self-checking and not for an auto-testing
assert end_zeros(0) == 1
assert end_zeros(1) == 0
assert end_zeros(10) == 1
assert end_zeros(101) == 0
assert end_zeros(245) == 0
assert end_zeros(100100) == 2
print("Coding complete? Click 'Check' to earn cool rewards!")
列表转化为字符串形式。用join函数。
def solve_it(a):
'''
pythontip oj不同于传统oj,代码里面直接使用变量,无需要提前声明,免去复杂的输入解析
life is short, so i user python~
you can use variables a
'''
b=[]
for i in a[0::2]:
b.append(i)
return(''.join(b)) #列表转化为字符串方式
a='12456'
print(solve_it(a)) # 答案需要输出
列表逆序输出
def solve_it(a):
'''
pythontip oj不同于传统oj,代码里面直接使用变量,无需要提前声明,免去复杂的输入解析
life is short, so i user python~
'''
b=[]
for i in a[::-1]:
b.append(i)
return b
a='1234'
print(solve_it(a)) # 答案需要输出