Remove all before
新知识:
- index(): lst.index(“ele”) 只会pop出在列表里第一次出现的对应元素的Index。 or the first occurance of the matching element( https://www.programiz.com/python-programming/methods/list/index)
- bool: a in lst ; a not in lst 也是bool型指标
- 切片操作. 和昨天的例子联系起来。val[::-1]= . reverse() ,其实是切片操作。
- 简述下切片的大致语法就是 lst [start : end : jump]
return items[items.index(border):] if border in items else items
- 好的答案,用到 val[ index: ]
All Upper |
定义一个用来判断字符串中是否全是大写字母的函数,我的答案
def is_all_upper(text: str) -> bool:
lc='abcdefghijklmnopqrstuvwxyz'
for elem in lc:
if elem in text:
a=False
break
else:
a=True
return a
哭了,第一的答案超级简洁啊,用了一个判断语句就结束了
def is_all_upper(text: str) -> bool:
return text.upper()==text
Solve Replace First
# for variable
a=1
b=a
a=2
>>>a,b
>>>2,1
list=[1,2,3]
lc=list
list[2]=4
>>>list,lc
>>>[1,2,4] [1,2,4]
# 正确的方法
lc.copy()
- iterable: 元素可数,可遍历的对象。string,list,tuple,dictionary,set都是。可用iter( ). next() https://www.w3schools.com/python/python_iterators.asp
- list分深度复制和浅层复制,和变量的赋值方式要有所区分
- list.assert(loc,val)
- lst1.append(lst2 )
- append,assert是操作,不可以放在return,原因暂未知
- len(list)<2是一个很好的判断是否为一个元素或空列表
好的例子
def replace_first(items: list) -> list:
if items:
items.append(items.pop(0))
return items
- [ ]=False ,[elem]=True
- append只在末尾加元素
- body段逻辑很棒,lst.pop,对列表进行操作的同时取出了这个元素,现在的items已经去除了第一个元素,再用append加上。
max digit
step 1:将number映射为digit list
- map(int,str(number))
- [int(i) for i in str(number)]
str( )是因为这样才可以遍历
step 2:得到最大值,max就可以解决. 排名第一的答案用lambda函数十分简洁
max_digit=lambda number: int(max(str(number)))
split pairs
mission info:Split the string into pairs of two characters. If the string contains an odd number of characters, then the missing second character of the final pair should be replaced with an underscore ('_').
比较好的答案:
from textwrap import wrap
def split_pairs(a):
a = a + '_' if len(a) % 2 else a
return wrap(a, 2)
我的
def split_pairs(a:str):
# your code here
lst=[]
if len(a)%2:
b=a+'_'
for i in range(0,int(len(b)/2)):
piece=b[2*i:2*i+2]
lst.append(piece)
else:
for i in range(0,int(len(a)/2)):
piece=a[2*i:2*i+2]
lst.append(piece)
return lst