1. 列表奇偶拆分
【问题描述】 输入一个列表,包含若干个整数(允许为空),然后将其中的奇数和偶数单独放置在一个列表中,保持原有顺序
【输入形式】
【输出形式】
分两行输出,第一行输出偶数序列,第二行输出奇数序列
【样例输入1】
[48,82,47,54,55,57,27,73,86,14]
【样例输出1】
48, 82, 54, 86, 14
47, 55, 57, 27, 73
【样例输入2】
[10, 22, 40] 【
样例输出2】
10, 22, 40
NONE
【样例说明】
如果奇偶拆分后,奇数列表,或者偶数列表为空,请直接输出NONE表示
【代码】
x = input() x1 = x.strip('[]') x2 = x1.split(",") a = [] b = [] for i in x2: if int(i) % 2 == 0: a.append(i) else: b.append(i) if a == []: print("NONE") else: print(a) if b == []: print("NONE") else: print(b)
输入输出:
48,82,47,54,55,57,27,73,86,14
['48', '82', '54', '86', '14']
['47', '55', '57', '27', '73']
==============================
10, 22, 40
['10', ' 22', ' 40']
NONE
2. 随机生成车牌号
某市随机生成车辆号牌的规则是:号牌字头为"某A-","某B-"等(字母为除了C以外的A~H范围内的大写字母),字头后面由5位字符组成,第1位必须是数字;第2、3、4、5位可以是任意数字或不含字母"O"的大写英文字母。
程序功能为:调用自己设计的函数license_plate(),随机生成5个车辆号牌,等待输入一个心仪号码的序号选择号牌,并将其打印输出。
程序运行结果如下图所示:
https://edu.csdn.net/practice/24062022
import random def genrndchar(metachar): return metachar[int(random.random() * len(metachar))] def license_plate(): s = "某" s = s + genrndchar(['A', 'B', 'C', 'D', 'E', 'H']) s = s + '-' s = s + genrndchar(['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']) for i in range(4): s = s + genrndchar(['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']) return s lst = [] for i in range(5): plate = license_plate() lst.append(plate) print(str(i + 1) + ":" + plate) x = int(input("请输入您心仪的号牌序号:")) - 1 print("您选中的号牌为:" + lst[x])
输入输出:
1:某C-913SM
2:某C-287BI
3:某C-4IY0T
4:某H-71TCD
5:某E-83E5D
请输入您心仪的号牌序号:5
您选中的号牌为:某E-83E5D
附:
推荐用推导式替代原题中的字符列表:
>>> [chr(i) for i in range(48,58)]
['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
>>> [chr(i) for i in range(65,91)]
['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
3. 接雨水
给定 n 个非负整数表示每个宽度为 1 的柱子的高度图,计算按此排列的柱子,下雨之后能接多少雨水。
示例 1:
输入:height = [0,1,0,2,1,0,1,3,2,1,2,1]
输出:6
解释:上面是由数组 [0,1,0,2,1,0,1,3,2,1,2,1] 表示的高度图,在这种情况下,可以接 6 个单位的雨水(蓝色部分表示雨水)。
示例 2:
输入:height = [4,2,0,3,2,5]
输出:9
提示:
n == height.length
0 <= n <= 3 * 10^4
0 <= height[i] <= 10^5
代码1: 原题代码
class Solution(object): def trap(self, height): ls = len(height) if ls == 0: return 0 res, left = 0, 0 while left < ls and height[left] == 0: left += 1 pos = left + 1 while pos < ls: if height[pos] >= height[left]: res += self.rain_water(height, left, pos) left = pos pos += 1 elif pos == ls - 1: max_value, max_index = 0, pos for index in range(left + 1, ls): if height[index] > max_value: max_value = height[index] max_index = index res += self.rain_water(height, left, max_index) left = max_index pos = left + 1 else: pos += 1 return res def rain_water(self, height, start, end): if end - start <= 1: return 0 min_m = min(height[start], height[end]) res = min_m * (end - start - 1) step = 0 for index in range(start + 1, end): if height[index] > 0: step += height[index] return res - step if __name__ == '__main__': s = Solution() print (s.trap([2,6,3,8,2,7,2,5,0])) print (s.trap([0,1,0,2,1,0,1,3,2,1,2,1])) print (s.trap([4,2,0,3,2,5]))
输出:
11
6
9
代码2: 遍历累加
from typing import List class Solution: def trap(self, height: List[int]) -> int: n = len(height) ans = 0 for i in range(1, n - 1): left_max = max(height[:i]) right_max = max(height[i + 1:]) if min(left_max, right_max) > height[i]: ans += min(left_max, right_max) - height[i] return ans if __name__ == '__main__': s = Solution() print (s.trap([2,6,3,8,2,7,2,5,0])) print (s.trap([0,1,0,2,1,0,1,3,2,1,2,1])) print (s.trap([4,2,0,3,2,5]))
代码3: 动态规划
from typing import List class Solution: def trap(self, height: List[int]) -> int: n = len(height) left_max = [0] * n right_max = [0] * n left_max[0] = height[0] right_max[n - 1] = height[n - 1] for i in range(1, n): left_max[i] = max(left_max[i - 1], height[i]) for i in range(n - 2, -1, -1): right_max[i] = max(right_max[i + 1], height[i]) ans = 0 for i in range(n): if min(left_max[i], right_max[i]) > height[i]: ans += min(left_max[i], right_max[i]) - height[i] return ans if __name__ == '__main__': s = Solution() print (s.trap([2,6,3,8,2,7,2,5,0])) print (s.trap([0,1,0,2,1,0,1,3,2,1,2,1])) print (s.trap([4,2,0,3,2,5]))
代码4: 双指针
from typing import List class Solution: def trap(self, height: List[int]) -> int: n = len(height) left = 0 right = n - 1 left_max = 0 right_max = 0 ans = 0 while left < right: if height[left] < height[right]: if height[left] >= left_max: left_max = height[left] else: ans += left_max - height[left] left += 1 else: if height[right] >= right_max: right_max = height[right] else: ans += right_max - height[right] right -= 1 return ans if __name__ == '__main__': s = Solution() print (s.trap([2,6,3,8,2,7,2,5,0])) print (s.trap([0,1,0,2,1,0,1,3,2,1,2,1])) print (s.trap([4,2,0,3,2,5]))
代码5: 单调栈
from typing import List class Solution: def trap(self, height: List[int]) -> int: n = len(height) stack = [] ans = 0 for i in range(n): while stack and height[i] > height[stack[-1]]: top = stack.pop() if not stack: break left = stack[-1] distance = i - left - 1 h = min(height[left], height[i]) - height[top] ans += distance * h stack.append(i) return ans if __name__ == '__main__': s = Solution() print (s.trap([2,6,3,8,2,7,2,5,0])) print (s.trap([0,1,0,2,1,0,1,3,2,1,2,1])) print (s.trap([4,2,0,3,2,5]))