CheckIO Element

题目1:Multiply(intro)

在这里插入图片描述
创建一个输入类型为int的a,b的函数返回其乘积。
回答:

def mult_two(a:int, b:int):
    # your code here
    return a*b

题目2:Easy Unpack

在这里插入图片描述
定义一个函数,使得给定一个元组返回其中的第一个,第三个,倒数第二个元素。
回答:

def easy_unpack(elements: tuple) -> tuple:
    """
        returns a tuple with 3 elements - first, third and second to the last
    """
    # your code here
    elements = (elements[0],elements[2],elements[-2])
    return elements

题目3:First Word (simplified)

在这里插入图片描述
找到一句话中的第一个单词
回答:

def first_word(text: str) -> str:
    """
        returns the first word in a given text.
    """

    return text.split(' ')[0]


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!")

其他人的回答:

first_word = lambda txt: txt[:(txt+' ').index(' ')]

题目4:Acceptable Password I

在这里插入图片描述
判断字符串个数是否大于6
回答:

def is_acceptable_password(password: str) -> bool:
    # your code here
    
    return len(password) > 6


if __name__ == '__main__':
    print("Example:")
    print(is_acceptable_password('short'))

    # These "asserts" are used for self-checking and not for an auto-testing
    assert is_acceptable_password('short') == False
    assert is_acceptable_password('muchlonger') == True
    assert is_acceptable_password('ashort') == False
    print("Coding complete? Click 'Check' to earn cool rewards!")

题目5:Number Length

在这里插入图片描述
确定正整数的位数
回答:

def number_length(a: int) -> int:
    # your code here
    
    return len(str(a))

题目6:End Zeros

在这里插入图片描述
确定给定数字末尾的0的个数
回答:

def end_zeros(num: int) -> int:
    num = str(num)[::-1]
    lenght = 0
    for x in num:
        if x is '0':
            lenght += 1
        else:
            break
    return lenght

其他人的回答:

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

Python中的strip用于去除字符串的首尾字符,同理,lstrip用于去除左边的字符,rstrip用于去除右边的字符。

题目7:Backward String

在这里插入图片描述
倒序输出字符串
回答:

def backward_string(val: str) -> str:
    # your code here
    return val[::-1]

题目8:Remove All Before

在这里插入图片描述
删除给定数字前的所有元素,若该列表中没用给定元素则列表保持不变
回答:

from typing import Iterable

def remove_all_before(items: list, border: int) -> Iterable:
    # your code here
    if border in items:
        items = items[items.index(border):]
    return items

其他人的回答:

def remove_all_before(items, border):
    try:
        return items[items.index(border):]
    except ValueError:
        return items

题目9:All Upper I

在这里插入图片描述
判断所给字符串是否全是大写
回答:

def is_all_upper(text: str) -> bool:
    # your code here
    if text.isupper():
        return True
    else:
        if text.replace(' ','').isdigit():
           return True
        else:
            if text.strip(' ') == '':
                return True
            else:
                return False

其他人的回答:

def is_all_upper(text: str) -> bool:
    # your code here
    return text.upper() == text

题目10:Replace First

在这里插入图片描述
将列表中的第一个元素放置最后
回答:

from typing import Iterable

def replace_first(items: list) -> Iterable:
    # your code here
    try:
        return items[1:]+items[:1]
    except:
        return items

题目11:Max Digit

在这里插入图片描述
给定某一int数,找出其中最大的值
回答:

def max_digit(number: int) -> int:
    # your code here
    number = list(str(number))
    number = [eval(x) for x in number]
    return max(number)

题目12:Split Pairs

在这里插入图片描述
把给定字符串切分成两个字符为一组,如果字符个数为奇数将拆分后的第二个部分最后添加下划线(_)
回答:

def split_pairs(a):
    # your code here
    num = []
    sum = ''
    i = 2
    if len(a)%2 != 0:
        a = a+'_'
    
    for x in a:
        sum = sum + x
        i -= 1
        if i == 0:
            i = 2
            num.append(sum)
            sum = ''
    return num  

其他人的回答:

def split_pairs(a):
    word = a + '_' if len(a) % 2 else a
    return [word[i:i + 2] for i in range(0, len(word), 2)]
def split_pairs(a):
    return [ch1+ch2 for ch1,ch2 in zip(a[::2],a[1::2]+'_')]

题目13:Beginning Zeros

在这里插入图片描述
找出一串字符串中开头数字零的个数
回答:

def beginning_zeros(number: str) -> int:
    # your code here
    return len(number) - len(number.lstrip('0'))

题目14:Nearest Value

在这里插入图片描述
找到给定数值在某一集合中最接近的数中的最小值
回答:

def nearest_value(values: set, one: int) -> int:
    # your code here
    return round(min({x - one+0.1 for x in values} , key =lambda x:abs(x)) + one -0.1)

其他人的回答:

def nearest_value(values: set, one: int) -> int:
    return min(values, key=lambda n: (abs(one - n), n))

min函数中的key可以有两个取值,先求第一个最小值,再求第二个最小值。以求得相同距离最小的数。

题目15:Between Markers (simplified)

在这里插入图片描述

回答:

def between_markers(text: str, begin: str, end: str) -> str:
    # your code here
    return text[text.index(begin)+1:text.index(end)]

题目16:Correct Sentence

在这里插入图片描述
修改给定句子,开头字母大写,结尾加逗点
回答:

def correct_sentence(text: str) -> str:
    """
        returns a corrected sentence which starts with a capital letter
        and ends with a dot.
    """
    # your code here
    text = text[0].upper()+text[1:]
    if text[-1] is not '.':
        text = text + '.'
    return text

其他人的回答:

def correct_sentence(text: str) -> str:
    return text[0].upper() + text[1:] + ("." if text[-1] != "." else '')

题目17:Is Even

在这里插入图片描述
建立一个偶数判断函数
回答:

def is_even(num: int) -> bool:
    # your code here
    return not bool(num%2)
  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值