python: 考试两题

题目截图

Problem 1.

Write a function is_sorted_evenodd(L) that takes as input a list L of integers. The function returns (not print!) the Boolean value True if the list L is sorted as follows: the even elements appear in ascending order, and the odd elements appear in descending order. Otherwise, the function returns False.

Example:
A list with elements 13,−18,6,9,9,10,−17 is sorted as requested because the odd elements 13,9,9,−17 are descending, while the even elements −18,6,10 are ascending:Numbers 13 -18 6 9 9 10 -17, Parity odd even even odd odd even odd.

Your function should not use the in-build sort or sorted commands: checking whether or not a list is sorted does not require any sorting.

Problem 2.

Assume that L is a list of integers. Write a function even_slice(L, a, b) that returns a new list containing all the even numbers among the elments of L between indices a and b (including both of them) in their original order. The original list L should remain unchanged. You can assume that 0 <= a <= b < len(L).

Example:
even_slice([11, 12, 2, 17, 20, 22, 2, 19, 7, 42, 18, 20], 2, 10) should return [2,20,22,2,42,18]

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值