最近发现了一个有趣的网站:https://py.checkio.org/ 是一个编程挑战的网站 支持python和js
下面我们要挑战的 题目是这个岛屿,还剩几道题目没有挑战
先从第一题开始好了
- 第一题 给定一个函数,函数接收2个整数位参数,返回两个参数的乘积
小白题目 没有难度
def mult_two(a, b):
# your code here
return a * b
if __name__ == '__main__':
print("Example:")
print(mult_two(3, 2))
# These "asserts" are used for self-checking and not for an auto-testing
assert mult_two(3, 2) == 6
assert mult_two(1, 0) == 0
print("Coding complete? Click 'Check' to earn cool rewards!")
2.你的任务是编写一个根据给出的属性参数来介绍一个人的函数
输入: 两个参数。一个字符串(str)和一个正整数(int)。
输出: 字符串(str)。
范例:
say_hi(“Alex”, 32) == “Hi. My name is Alex and I’m 32 years old”
say_hi(“Frank”, 68) == “Hi. My name is Frank and I’m 68 years old”
这道题考察的是字符串的拼接 没有难度
# 1. on CheckiO your solution should be a function
# 2. the function should return the right answer, not print it.
def say_hi(name: str, age: int) -> str:
"""
Hi!
"""
# your code here
return "Hi. My name is " + name + " and I'm " + str(age) + " years old"
if __name__ == '__main__':
#These "asserts" using only for self-checking and not necessary for auto-testing
assert say_hi("Alex", 32) == "Hi. My name is Alex and I'm 32 years old", "First"
assert say_hi("Frank", 68) == "Hi. My name is Frank and I'm 68 years old", "Second"
print('Done. Time to Check.')
3:在这里你的任务是创建得到一个元组,并返回一个包含三个元素(第一,第三和倒数第二的给定元组)的元组与的功能。
输入: 一个不少于三个元素的元组
输出: 一个元组.
举个栗子:
easy_unpack((1, 2, 3, 4, 5, 6, 7, 9)) == (1, 3, 7)
easy_unpack((1, 1, 1, 1)) == (1, 1, 1)
easy_unpack((6, 3, 7)) == (6, 7, 3)
考察如何返回一个元组,没有难度
def easy_unpack(elements: tuple) -> tuple:
"""
returns a tuple with 3 elements - first, third and second to the last
"""
# your code here
return (elements[0],elements[2],elements[-2],)
if __name__ == '__main__':
print('Examples:')
print(easy_unpack((1, 2, 3, 4, 5, 6, 7, 9)))
#These "asserts" using only for self-checking and not necessary for auto-testing
assert easy_unpack((1, 2, 3, 4, 5, 6, 7, 9)) == (1, 3, 7)
assert easy_unpack((1, 1, 1, 1)) == (1, 1, 1)
assert easy_unpack((6, 3, 7)) == (6, 7, 3)
print('Done! Go Check!')
- 您将获得一个具有正数和数字N的数组。您应该在索引为N的数组中找到元素的N次方。如果N在数组之外,则返回-1。不要忘记第一个元素的索引为0。
让我们看几个例子:
-array = [1、2、3、4]且N = 2,则结果为3 2 == 9;
-数组= [1、2、3],并且N = 3,但是N在数组之外,因此结果为-1。
输入:两个参数。一个数组,它是一个整数列表,一个数字是一个整数。
输出:结果为整数。
前提条件: 0 <len(array)≤10
0≤N
all(对于x in数组0≤x≤100)
def index_power(array: list, n: int) -> int:
"""
Find Nth power of the element with index N.
"""
if len(array) <= n:
return -1
else:
return array[n] ** n
if __name__ == '__main__':
print('Example:')
print(index_power([1, 2, 3, 4], 2))
#These "asserts" using only for self-checking and not necessary for auto-testing
assert index_power([1, 2, 3, 4], 2) == 9, "Square"
assert index_power([1, 3, 10, 100], 3) == 1000000, "Cube"
assert index_power([0, 1], 0) == 1, "Zero power"
assert index_power([1, 2], 3) == -1, "IndexError"
print("Coding complete? Click 'Check' to review your tests and earn cool rewards!")
5:给你一个正整数,请你写一个函数来实现:传入正整数的每一位(不包括00)的乘积
例如:给你 123405, 你应该这样处理 12345=120(别忘了把0丢掉)
输入: 一个正整数.
输出: 正整数的每一位的乘积
举个栗子:
checkio(123405) == 120
checkio(999) == 729
checkio(1000) == 1
checkio(1111) == 1
1
2
3
4
你将学到: 在这个任务中你将学会一些简