checkio 过关手记 | 持续更新

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()
  1. iterable: 元素可数,可遍历的对象。string,list,tuple,dictionary,set都是。可用iter( ). next()       https://www.w3schools.com/python/python_iterators.asp
  2. list分深度复制和浅层复制,和变量的赋值方式要有所区分
  3. list.assert(loc,val)
  4. lst1.append(lst2 )
  5. append,assert是操作,不可以放在return,原因暂未知
  6. 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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值