CheckIO-Element
- 题目1:Multiply(intro)
- 题目2:Easy Unpack
- 题目3:First Word (simplified)
- 题目4:Acceptable Password I
- 题目5:Number Length
- 题目6:End Zeros
- 题目7:Backward String
- 题目8:Remove All Before
- 题目9:All Upper I
- 题目10:Replace First
- 题目11:Max Digit
- 题目12:Split Pairs
- 题目13:Beginning Zeros
- 题目14:Nearest Value
- 题目15:Between Markers (simplified)
- 题目16:Correct Sentence
- 题目17:Is Even
题目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)