1.写一个匿名函数,判断指定的年是否是闰年
is_leap_year = lambda year: print(True) if (year % 4 ==0 and year % 400 != 0) or year % 400 == 0 else print(False)
2.写一个函数将一个指定的列表中的元素逆序( 如[1, 2, 3] -> [3, 2, 1])(注意:不要使用列表自带的逆序函数)
def reverse_order(list1: list):
list2 = [i for i in list1[::-1]]
return list2
3.写一个函数,获取指定列表中指定元素的下标(如果指定元素有多个,将每个元素的下标都返回)
例如: 列表是:[1, 3, 4, 1] ,元素是1, 返回:0,3
def return_index(list1: list, char):
length = len(list1)
list2 = [i for i in range(length) if list1[i] == char]
return list2
4.编写一个函数,计算一个整数的各位数的平方和
例如: sum1(12) -> 5(1的平方加上2的平方) sum1(123) -> 14
def sum_of_squares(num_int: int):
length = len(str(num_int))
sumOfNum = 0
for i in range(length):
sumOfNum += (num_int % 10) ** 2
num_int //= 10
return sumOfNum
5.求列表 nums 中绝对值最大的元素
例如:nums = [-23, 100, 89, -56, -234, 123], 最大值是:-234
def max_in_nums(list1: list):
max_index = 0
count_index = 0
length = len(list1)
while True:
if list1[max_index] >= 0: # 数为整数
for a in range(length):
if count_index == length - 1:
return list1[max_index]
count_index += 1
if list1[max_index] + list1[count_index] < 0: # 有数比我大
max_index = count_index
break
else:
for b in range(length):
if count_index == length-1:
return list1[max_index]
count_index += 1
if list1[max_index] + list1[count_index] > 0: # 有数比我大
max_index = count_index
break
if count_index == length-1:
break
return list1[max_index]
6.已经列表points中保存的是每个点的坐标(坐标是用元组表示的,第一个值是x坐标,第二个值是y坐标)
points = [
(10, 20), (0, 100), (20, 30), (-10, 20), (30, -100)
]
1)获取列表中y坐标最大的点
2)获取列表中x坐标最小的点
3)获取列表中距离原点最远的点
4)将点按照点到x轴的距离大小从大到小排序
1)
def max_y(list1: list):
a = max([i[1] for i in list1])
b = [j for j in list1 if j[1] == a]
return b
2)def max_y(list1: list):
a = min([abs(i[0]) for i in list1])
b = [j for j in list1 if j[0] == a]
return b
3)def max_y(list1: list):
length = len(list1)
a = [list1[i][1]**2+list1[i][0]**2 for i in range(length)]
b = [list1[j] for j in range(length) if a[j] == max(a)]
return b
4)def sorted_x(list1: list):
a = sorted(list1)
return a