最近找到了一个python闯关游戏,写了第一关的几道题目
def num(int_num):
return (len(str(int_num)))
print(num(1))
def reversed(string):
str1 = str(string)
return str1[::-1]
print(reversed(123))
print(reversed("123"))
print(reversed('i am yor dad'))
321
321
dad roy ma i
def end_zeros(number):
n = str(number)
return len(n) - len(n.strip('0'))
print(end_zeros(1000))
print(end_zeros(1001))
print(end_zeros(10001200))
3
0
3
from scipy import stats
def mode(x):
Mode = stats.mode(x, keepdims=False)
return Mode[0], Mode[1] # 分别是众数和出现的频率
speed = [99, 86, 87, 88, 111, 86, 103, 87, 94, 78, 77, 85, 86]
print(mode(speed))
(86, 3)
def easy_unpack(elements):
return (elements[0], elements[2], elements[-2])
a = (1, 2, 34, 54, 6, 5, 2, 32)
print(easy_unpack(a))
(1, 34, 2)
要求如下:
assert is_all_upper("ALL UPPER") == True
assert is_all_upper("all lower") == False
assert is_all_upper("mixed UPPER and lower") == False
assert is_all_upper("") == True
assert is_all_upper("444") == True
assert is_all_upper("55 55 5 ") == True
def is_all_upper(x) :
x=x.replace(" ",'')
if x.isupper()==True or x.isdigit()==True or x=="":
return True
else:
return False
def replace_first(x) :
if len(x)>0:
a=x[0]
x.remove(x[0])
x.append(a)
return x
else:
return x
示例:
assert beginning_zeros('100') == 0
assert beginning_zeros('001') == 2
assert beginning_zeros('100100') == 0
assert beginning_zeros('001001') == 2
解:
def beginning_zeros(x) :
y=str(x)
if int(x)==0:
return len(y)
else:
return len(y)-len(str(int(x)))
示例代码:
assert list(split_pairs("abcd")) == ["ab", "cd"]
assert list(split_pairs("abc")) == ["ab", "c_"]
assert list(split_pairs("abcdf")) == ["ab", "cd", "f_"]
assert list(split_pairs("a")) == ["a_"]
解:
def split_pairs(a):
b=[]
p=0
if len(a)%2==0:
for i in range(len(a)//2):
b.append(a[p:p+2])
p=p+2
return b
else:
c=a[-1]+"_"
for i in range(len(a)//2):
b.append(a[p:p+2])
p=p+2
b.append(c)
return(b)
示例:
assert correct_sentence("greetings, friends") == "Greetings, friends."
assert correct_sentence("Greetings, friends") == "Greetings, friends."
assert correct_sentence("Greetings, friends.") == "Greetings, friends."
assert correct_sentence("greetings, friends.") == "Greetings, friends."
这个代码我写的有点麻烦
def nearest_value(set, int):
try:
a=[]
for i in set:
a.append(int-i)
positive=min([i for i in a if i>0])
negative=max([i for i in a if i<=0])
if positive>abs(negative):
return int+abs(negative)
else:
return int-positive
except ValueError :
list=[int-i for i in set]
if min(list)<0:
return int-max(list)
else:
return int-min(list)
示例:
assert remove_all_before([1, 2, 3, 4, 5], 3) == [3, 4, 5]
assert remove_all_before([1, 1, 2, 2, 3, 3], 2) == [2, 2, 3, 3]
assert remove_all_before([1, 1, 2, 4, 2, 3, 4], 2) == [2, 4, 2, 3, 4]
assert remove_all_before([1, 1, 5, 6, 7], 2) == [1, 1, 5, 6, 7]
Example:
assert between_markers('What is >apple<', '>', '<') == 'apple'
assert between_markers('What is [apple]', '[', ']') == 'apple'
assert between_markers('What is ><', '>', '<') == ''
assert between_markers('[an apple]', '[', ']') == 'an apple'
代码如下:正则表达式非常简单,注意这里用\防止转义
import re def between_markers(text,start,end): print(start,end,text) a=re.findall(f"\{start}(.*?)\{end}",text) return a[0]
'运行