bai-python-hwc 1.0.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- bai_python_hwc/__init__.py +46 -0
- bai_python_hwc/answers/hwc101.py +38 -0
- bai_python_hwc/answers/hwc103.py +52 -0
- bai_python_hwc/answers/hwc104.py +45 -0
- bai_python_hwc/answers/hwc105.py +45 -0
- bai_python_hwc/answers/hwc106.py +56 -0
- bai_python_hwc/answers/hwc107.py +49 -0
- bai_python_hwc/answers/hwc108.py +45 -0
- bai_python_hwc/answers/hwc109.py +50 -0
- bai_python_hwc/answers/hwc110.py +42 -0
- bai_python_hwc/answers/hwc111.py +47 -0
- bai_python_hwc/answers/hwc112.py +42 -0
- bai_python_hwc/answers/hwc113.py +58 -0
- bai_python_hwc/answers/hwc114.py +58 -0
- bai_python_hwc/answers/hwc115.py +38 -0
- bai_python_hwc/answers/hwc116.py +44 -0
- bai_python_hwc/answers/hwc117.py +46 -0
- bai_python_hwc/answers/hwc118.py +47 -0
- bai_python_hwc/answers/hwc119.py +44 -0
- bai_python_hwc/answers/hwc120.py +41 -0
- bai_python_hwc/answers/hwc121.py +50 -0
- bai_python_hwc/answers/hwc122.py +54 -0
- bai_python_hwc/answers/hwc123.py +43 -0
- bai_python_hwc/answers/hwc124.py +41 -0
- bai_python_hwc/answers/hwc125.py +52 -0
- bai_python_hwc/answers/hwc126.py +40 -0
- bai_python_hwc/answers/hwc127.py +41 -0
- bai_python_hwc/answers/hwc128.py +39 -0
- bai_python_hwc/answers/hwc130.py +46 -0
- bai_python_hwc/answers/hwc131.py +46 -0
- bai_python_hwc/answers/hwc132.py +38 -0
- bai_python_hwc/answers/hwc133.py +61 -0
- bai_python_hwc/answers/hwc134.py +45 -0
- bai_python_hwc/answers/hwc135.py +49 -0
- bai_python_hwc/answers/hwc136.py +43 -0
- bai_python_hwc/answers/hwc201.py +47 -0
- bai_python_hwc/answers/hwc202.py +47 -0
- bai_python_hwc/answers/hwc203.py +48 -0
- bai_python_hwc/answers/hwc204.py +61 -0
- bai_python_hwc/answers/hwc205.py +56 -0
- bai_python_hwc/answers/hwc206.py +53 -0
- bai_python_hwc/answers/hwc207.py +43 -0
- bai_python_hwc/answers/hwc208.py +57 -0
- bai_python_hwc/answers/hwc209.py +42 -0
- bai_python_hwc/answers/hwc210.py +46 -0
- bai_python_hwc/answers/hwc211.py +62 -0
- bai_python_hwc/answers/hwc212.py +58 -0
- bai_python_hwc/answers/hwc213.py +54 -0
- bai_python_hwc/answers/hwc214.py +55 -0
- bai_python_hwc/answers/hwc215.py +53 -0
- bai_python_hwc/answers/hwc216.py +50 -0
- bai_python_hwc/answers/hwc217.py +38 -0
- bai_python_hwc/answers/hwc218.py +44 -0
- bai_python_hwc/answers/hwc219.py +49 -0
- bai_python_hwc/answers/hwc220.py +58 -0
- bai_python_hwc/answers/hwc221.py +49 -0
- bai_python_hwc/answers/hwc222.py +48 -0
- bai_python_hwc/answers/hwc223.py +39 -0
- bai_python_hwc/answers/hwc224.py +47 -0
- bai_python_hwc/answers/hwc225.py +50 -0
- bai_python_hwc/answers/hwc226.py +41 -0
- bai_python_hwc/answers/hwc227.py +40 -0
- bai_python_hwc/answers/hwc228.py +41 -0
- bai_python_hwc/answers/hwc229.py +51 -0
- bai_python_hwc/answers/hwc230.py +41 -0
- bai_python_hwc/answers/hwc231.py +39 -0
- bai_python_hwc/answers/hwc232.py +44 -0
- bai_python_hwc/answers/hwc233.py +49 -0
- bai_python_hwc/answers/hwc234.py +52 -0
- bai_python_hwc/answers/hwc235.py +45 -0
- bai_python_hwc/answers/hwc236.py +53 -0
- bai_python_hwc/answers/hwc237.py +56 -0
- bai_python_hwc/answers/hwc238.py +52 -0
- bai_python_hwc/answers/hwc239.py +60 -0
- bai_python_hwc/answers/hwc240.py +45 -0
- bai_python_hwc/answers/hwc241.py +48 -0
- bai_python_hwc/answers/hwc242.py +50 -0
- bai_python_hwc/answers/hwc243.py +61 -0
- bai_python_hwc/answers/hwc244.py +61 -0
- bai_python_hwc/answers/hwc245.py +47 -0
- bai_python_hwc/answers/hwc246.py +52 -0
- bai_python_hwc/answers/hwc247.py +76 -0
- bai_python_hwc/answers/hwc301.py +44 -0
- bai_python_hwc/answers/hwc302.py +53 -0
- bai_python_hwc/answers/hwc303.py +63 -0
- bai_python_hwc/answers/hwc304.py +56 -0
- bai_python_hwc/answers/hwc305.py +59 -0
- bai_python_hwc/answers/hwc306.py +60 -0
- bai_python_hwc/answers/hwc307.py +50 -0
- bai_python_hwc/answers/hwc308.py +59 -0
- bai_python_hwc/answers/hwc309.py +48 -0
- bai_python_hwc/answers/hwc310.py +38 -0
- bai_python_hwc/answers/hwc311.py +48 -0
- bai_python_hwc/answers/hwc312.py +49 -0
- bai_python_hwc/answers/hwc313.py +54 -0
- bai_python_hwc/answers/hwc314.py +49 -0
- bai_python_hwc/answers/hwc315.py +63 -0
- bai_python_hwc/answers/hwc316.py +65 -0
- bai_python_hwc/answers/hwc317.py +57 -0
- bai_python_hwc/answers/hwc318.py +55 -0
- bai_python_hwc/answers/hwc319.py +50 -0
- bai_python_hwc/answers/hwc320.py +84 -0
- bai_python_hwc/answers/hwc321.py +49 -0
- bai_python_hwc/answers/hwc322.py +49 -0
- bai_python_hwc/answers/hwc323.py +69 -0
- bai_python_hwc/answers/hwc324.py +66 -0
- bai_python_hwc/answers/hwc325.py +49 -0
- bai_python_hwc/answers/hwc326.py +53 -0
- bai_python_hwc/answers/hwc327.py +38 -0
- bai_python_hwc/answers/hwc328.py +50 -0
- bai_python_hwc/answers/hwc329.py +59 -0
- bai_python_hwc/answers/hwc330.py +45 -0
- bai_python_hwc/answers/hwc332.py +57 -0
- bai_python_hwc/answers/hwc401.py +50 -0
- bai_python_hwc/answers/hwc402.py +43 -0
- bai_python_hwc/answers/hwc403.py +73 -0
- bai_python_hwc/answers/hwc404.py +51 -0
- bai_python_hwc/answers/hwc405.py +62 -0
- bai_python_hwc/answers/hwc406.py +66 -0
- bai_python_hwc/answers/hwc407.py +49 -0
- bai_python_hwc/answers/hwc408.py +67 -0
- bai_python_hwc/answers/hwc409.py +51 -0
- bai_python_hwc/answers/hwc410.py +52 -0
- bai_python_hwc/answers/hwc411.py +57 -0
- bai_python_hwc/answers/hwc412.py +53 -0
- bai_python_hwc/answers/hwc413.py +65 -0
- bai_python_hwc/answers/hwc414.py +50 -0
- bai_python_hwc/answers/hwc415.py +61 -0
- bai_python_hwc/answers/hwc416.py +69 -0
- bai_python_hwc/answers/hwc418.py +47 -0
- bai_python_hwc/answers/hwc419.py +62 -0
- bai_python_hwc/answers/hwc421.py +51 -0
- bai_python_hwc/answers/hwc424.py +48 -0
- bai_python_hwc/answers/hwc501.py +63 -0
- bai_python_hwc/answers/hwc502.py +69 -0
- bai_python_hwc/answers/hwc503.py +75 -0
- bai_python_hwc/answers/hwc504.py +52 -0
- bai_python_hwc/answers/hwc505.py +63 -0
- bai_python_hwc/answers/hwc506.py +61 -0
- bai_python_hwc/answers/hwc508.py +60 -0
- bai_python_hwc/answers/hwc509.py +85 -0
- bai_python_hwc/answers/hwc510.py +56 -0
- bai_python_hwc/answers/hwc511.py +52 -0
- bai_python_hwc/answers/hwc513.py +63 -0
- bai_python_hwc/answers/hwc514.py +74 -0
- bai_python_hwc/answers/hwc515.py +83 -0
- bai_python_hwc/answers/hwc516.py +67 -0
- bai_python_hwc/answers/hwc517.py +46 -0
- bai_python_hwc/answers/hwc520.py +54 -0
- bai_python_hwc/answers/hwc526.py +58 -0
- bai_python_hwc/attachments/hwc424.pdf +0 -0
- bai_python_hwc/attachments/hwc501.pdf +0 -0
- bai_python_hwc/questions/hwc101.txt +17 -0
- bai_python_hwc/questions/hwc103.txt +27 -0
- bai_python_hwc/questions/hwc104.txt +22 -0
- bai_python_hwc/questions/hwc105.txt +23 -0
- bai_python_hwc/questions/hwc106.txt +26 -0
- bai_python_hwc/questions/hwc107.txt +29 -0
- bai_python_hwc/questions/hwc108.txt +18 -0
- bai_python_hwc/questions/hwc109.txt +24 -0
- bai_python_hwc/questions/hwc110.txt +16 -0
- bai_python_hwc/questions/hwc111.txt +25 -0
- bai_python_hwc/questions/hwc112.txt +13 -0
- bai_python_hwc/questions/hwc113.txt +28 -0
- bai_python_hwc/questions/hwc114.txt +35 -0
- bai_python_hwc/questions/hwc115.txt +15 -0
- bai_python_hwc/questions/hwc116.txt +13 -0
- bai_python_hwc/questions/hwc117.txt +14 -0
- bai_python_hwc/questions/hwc118.txt +25 -0
- bai_python_hwc/questions/hwc119.txt +18 -0
- bai_python_hwc/questions/hwc120.txt +23 -0
- bai_python_hwc/questions/hwc121.txt +26 -0
- bai_python_hwc/questions/hwc122.txt +21 -0
- bai_python_hwc/questions/hwc123.txt +21 -0
- bai_python_hwc/questions/hwc124.txt +19 -0
- bai_python_hwc/questions/hwc125.txt +31 -0
- bai_python_hwc/questions/hwc126.txt +18 -0
- bai_python_hwc/questions/hwc127.txt +17 -0
- bai_python_hwc/questions/hwc128.txt +14 -0
- bai_python_hwc/questions/hwc130.txt +23 -0
- bai_python_hwc/questions/hwc131.txt +19 -0
- bai_python_hwc/questions/hwc132.txt +16 -0
- bai_python_hwc/questions/hwc133.txt +39 -0
- bai_python_hwc/questions/hwc134.txt +22 -0
- bai_python_hwc/questions/hwc135.txt +29 -0
- bai_python_hwc/questions/hwc136.txt +20 -0
- bai_python_hwc/questions/hwc201.txt +20 -0
- bai_python_hwc/questions/hwc202.txt +25 -0
- bai_python_hwc/questions/hwc203.txt +26 -0
- bai_python_hwc/questions/hwc204.txt +32 -0
- bai_python_hwc/questions/hwc205.txt +28 -0
- bai_python_hwc/questions/hwc206.txt +28 -0
- bai_python_hwc/questions/hwc207.txt +16 -0
- bai_python_hwc/questions/hwc208.txt +27 -0
- bai_python_hwc/questions/hwc209.txt +15 -0
- bai_python_hwc/questions/hwc210.txt +28 -0
- bai_python_hwc/questions/hwc211.txt +35 -0
- bai_python_hwc/questions/hwc212.txt +29 -0
- bai_python_hwc/questions/hwc213.txt +27 -0
- bai_python_hwc/questions/hwc214.txt +27 -0
- bai_python_hwc/questions/hwc215.txt +28 -0
- bai_python_hwc/questions/hwc216.txt +22 -0
- bai_python_hwc/questions/hwc217.txt +17 -0
- bai_python_hwc/questions/hwc218.txt +18 -0
- bai_python_hwc/questions/hwc219.txt +22 -0
- bai_python_hwc/questions/hwc220.txt +28 -0
- bai_python_hwc/questions/hwc221.txt +18 -0
- bai_python_hwc/questions/hwc222.txt +20 -0
- bai_python_hwc/questions/hwc223.txt +18 -0
- bai_python_hwc/questions/hwc224.txt +23 -0
- bai_python_hwc/questions/hwc225.txt +26 -0
- bai_python_hwc/questions/hwc226.txt +18 -0
- bai_python_hwc/questions/hwc227.txt +15 -0
- bai_python_hwc/questions/hwc228.txt +16 -0
- bai_python_hwc/questions/hwc229.txt +20 -0
- bai_python_hwc/questions/hwc230.txt +18 -0
- bai_python_hwc/questions/hwc231.txt +13 -0
- bai_python_hwc/questions/hwc232.txt +19 -0
- bai_python_hwc/questions/hwc233.txt +21 -0
- bai_python_hwc/questions/hwc234.txt +27 -0
- bai_python_hwc/questions/hwc235.txt +18 -0
- bai_python_hwc/questions/hwc236.txt +23 -0
- bai_python_hwc/questions/hwc237.txt +28 -0
- bai_python_hwc/questions/hwc238.txt +25 -0
- bai_python_hwc/questions/hwc239.txt +28 -0
- bai_python_hwc/questions/hwc240.txt +21 -0
- bai_python_hwc/questions/hwc241.txt +25 -0
- bai_python_hwc/questions/hwc242.txt +23 -0
- bai_python_hwc/questions/hwc243.txt +35 -0
- bai_python_hwc/questions/hwc244.txt +25 -0
- bai_python_hwc/questions/hwc245.txt +24 -0
- bai_python_hwc/questions/hwc246.txt +28 -0
- bai_python_hwc/questions/hwc247.txt +51 -0
- bai_python_hwc/questions/hwc301.txt +23 -0
- bai_python_hwc/questions/hwc302.txt +26 -0
- bai_python_hwc/questions/hwc303.txt +32 -0
- bai_python_hwc/questions/hwc304.txt +32 -0
- bai_python_hwc/questions/hwc305.txt +31 -0
- bai_python_hwc/questions/hwc306.txt +31 -0
- bai_python_hwc/questions/hwc307.txt +29 -0
- bai_python_hwc/questions/hwc308.txt +30 -0
- bai_python_hwc/questions/hwc309.txt +25 -0
- bai_python_hwc/questions/hwc310.txt +15 -0
- bai_python_hwc/questions/hwc311.txt +23 -0
- bai_python_hwc/questions/hwc312.txt +19 -0
- bai_python_hwc/questions/hwc313.txt +26 -0
- bai_python_hwc/questions/hwc314.txt +27 -0
- bai_python_hwc/questions/hwc315.txt +21 -0
- bai_python_hwc/questions/hwc316.txt +36 -0
- bai_python_hwc/questions/hwc317.txt +28 -0
- bai_python_hwc/questions/hwc318.txt +27 -0
- bai_python_hwc/questions/hwc319.txt +26 -0
- bai_python_hwc/questions/hwc320.txt +42 -0
- bai_python_hwc/questions/hwc321.txt +27 -0
- bai_python_hwc/questions/hwc322.txt +26 -0
- bai_python_hwc/questions/hwc323.txt +24 -0
- bai_python_hwc/questions/hwc324.txt +40 -0
- bai_python_hwc/questions/hwc325.txt +21 -0
- bai_python_hwc/questions/hwc326.txt +25 -0
- bai_python_hwc/questions/hwc327.txt +16 -0
- bai_python_hwc/questions/hwc328.txt +15 -0
- bai_python_hwc/questions/hwc329.txt +26 -0
- bai_python_hwc/questions/hwc330.txt +27 -0
- bai_python_hwc/questions/hwc332.txt +26 -0
- bai_python_hwc/questions/hwc401.txt +25 -0
- bai_python_hwc/questions/hwc402.txt +24 -0
- bai_python_hwc/questions/hwc403.txt +37 -0
- bai_python_hwc/questions/hwc404.txt +23 -0
- bai_python_hwc/questions/hwc405.txt +29 -0
- bai_python_hwc/questions/hwc406.txt +34 -0
- bai_python_hwc/questions/hwc407.txt +24 -0
- bai_python_hwc/questions/hwc408.txt +28 -0
- bai_python_hwc/questions/hwc409.txt +25 -0
- bai_python_hwc/questions/hwc410.txt +23 -0
- bai_python_hwc/questions/hwc411.txt +34 -0
- bai_python_hwc/questions/hwc412.txt +29 -0
- bai_python_hwc/questions/hwc413.txt +28 -0
- bai_python_hwc/questions/hwc414.txt +27 -0
- bai_python_hwc/questions/hwc415.txt +27 -0
- bai_python_hwc/questions/hwc416.txt +36 -0
- bai_python_hwc/questions/hwc418.txt +25 -0
- bai_python_hwc/questions/hwc419.txt +28 -0
- bai_python_hwc/questions/hwc421.txt +23 -0
- bai_python_hwc/questions/hwc424.txt +20 -0
- bai_python_hwc/questions/hwc501.txt +23 -0
- bai_python_hwc/questions/hwc502.txt +41 -0
- bai_python_hwc/questions/hwc503.txt +36 -0
- bai_python_hwc/questions/hwc504.txt +18 -0
- bai_python_hwc/questions/hwc505.txt +25 -0
- bai_python_hwc/questions/hwc506.txt +32 -0
- bai_python_hwc/questions/hwc508.txt +24 -0
- bai_python_hwc/questions/hwc509.txt +42 -0
- bai_python_hwc/questions/hwc510.txt +25 -0
- bai_python_hwc/questions/hwc511.txt +23 -0
- bai_python_hwc/questions/hwc513.txt +25 -0
- bai_python_hwc/questions/hwc514.txt +35 -0
- bai_python_hwc/questions/hwc515.txt +46 -0
- bai_python_hwc/questions/hwc516.txt +26 -0
- bai_python_hwc/questions/hwc517.txt +20 -0
- bai_python_hwc/questions/hwc520.txt +27 -0
- bai_python_hwc/questions/hwc526.txt +36 -0
- bai_python_hwc-1.0.0.dist-info/METADATA +41 -0
- bai_python_hwc-1.0.0.dist-info/RECORD +305 -0
- bai_python_hwc-1.0.0.dist-info/WHEEL +5 -0
- bai_python_hwc-1.0.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
'''
|
|
3
|
+
请只在 "
|
|
4
|
+
##########start下面可以改动
|
|
5
|
+
|
|
6
|
+
##########end上面可以改动 "
|
|
7
|
+
|
|
8
|
+
中间部分作答,作答行数自由调整
|
|
9
|
+
|
|
10
|
+
请不要使用 input() 函数
|
|
11
|
+
|
|
12
|
+
此题在期中考试考察范围内,作业需提交,注意期中考试有卷面分,请认真作答。
|
|
13
|
+
|
|
14
|
+
题目:跳跃游戏
|
|
15
|
+
非负整数数组 nums,你最初位于数组的 第一个下标 。数组中的每个元素代表你在该位置可以跳跃的最大长度。
|
|
16
|
+
判断你是否能够到达最后一个下标,如果可以,返回 True ;否则,返回 False 。
|
|
17
|
+
例如:nums = [3,2,1,0,4]
|
|
18
|
+
从第0个位置开始,即使可以跳跃3步至第3个位置,但第3个位置的值为0,因此不能再往后跳。
|
|
19
|
+
结果:无法到达最后一个位置,因此result返回值为:False。
|
|
20
|
+
现将以下几个数组进行跳跃游戏判断,判断你是否能够到达最后一个下标,如果可以,返回 True ;否则,返回 False 。
|
|
21
|
+
nums1 = [1,2,3,4,5]
|
|
22
|
+
nums2 = [2,3,1,1,4]
|
|
23
|
+
num3 = [1,1,1,1,0]
|
|
24
|
+
'''
|
|
25
|
+
|
|
26
|
+
# 参考答案
|
|
27
|
+
|
|
28
|
+
def TEST_DO_NOT_CHANGE(nums):
|
|
29
|
+
print(nums)
|
|
30
|
+
result = None
|
|
31
|
+
##########start下面可以改动
|
|
32
|
+
|
|
33
|
+
furthest = 0
|
|
34
|
+
for i, jump in enumerate(nums):
|
|
35
|
+
if i > furthest:
|
|
36
|
+
break
|
|
37
|
+
furthest = max(furthest, i + jump)
|
|
38
|
+
result = furthest >= len(nums) - 1
|
|
39
|
+
|
|
40
|
+
##########end上面可以改动
|
|
41
|
+
return result
|
|
42
|
+
if __name__ == "__main__":
|
|
43
|
+
###########下面可以修改
|
|
44
|
+
nums1 = [1,2,3,4,5]
|
|
45
|
+
nums2 = [2,3,1,1,4]
|
|
46
|
+
nums3 = [1,1,1,1,0]
|
|
47
|
+
print(TEST_DO_NOT_CHANGE(nums1))
|
|
48
|
+
print(TEST_DO_NOT_CHANGE(nums2))
|
|
49
|
+
print(TEST_DO_NOT_CHANGE(nums3))
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
'''
|
|
3
|
+
请只在 "
|
|
4
|
+
##########start下面可以改动
|
|
5
|
+
|
|
6
|
+
##########end上面可以改动 "
|
|
7
|
+
|
|
8
|
+
中间部分作答,作答行数自由调整
|
|
9
|
+
|
|
10
|
+
请不要使用 input() 函数
|
|
11
|
+
|
|
12
|
+
此题在期中考试考察范围内,作业需提交,注意期中考试有卷面分,请认真作答。
|
|
13
|
+
|
|
14
|
+
题目:(改编自计算机二级 Python 真题·综合应用题·枚举+素数考点)
|
|
15
|
+
在本题给定的输入范围内,按哥德巴赫猜想枚举可能的素数对;本题把该结论作为题设假定,
|
|
16
|
+
不要求证明“任何大于 2 的偶数都满足该性质”。
|
|
17
|
+
给定一个大于 2 的偶数 n,请找出所有满足 a + b = n 且 a <= b 的素数对 (a, b),
|
|
18
|
+
按 a 从小到大存入列表返回(每个元素是 (a, b))。
|
|
19
|
+
|
|
20
|
+
提示:先写一个 is_prime(x) 判断素数,再枚举 a 从 2 到 n//2。
|
|
21
|
+
|
|
22
|
+
示例:
|
|
23
|
+
输入:n = 10
|
|
24
|
+
输出:[(3, 7), (5, 5)]
|
|
25
|
+
|
|
26
|
+
输入:n = 20
|
|
27
|
+
输出:[(3, 17), (7, 13)]
|
|
28
|
+
'''
|
|
29
|
+
|
|
30
|
+
# 参考答案
|
|
31
|
+
|
|
32
|
+
def TEST_DO_NOT_CHANGE(n):
|
|
33
|
+
lst_rlt = None
|
|
34
|
+
##########start下面可以改动
|
|
35
|
+
|
|
36
|
+
from math import isqrt
|
|
37
|
+
|
|
38
|
+
def is_prime(value):
|
|
39
|
+
return value >= 2 and all(
|
|
40
|
+
value % divisor != 0 for divisor in range(2, isqrt(value) + 1)
|
|
41
|
+
)
|
|
42
|
+
|
|
43
|
+
lst_rlt = [(a, n - a) for a in range(2, n // 2 + 1) if is_prime(a) and is_prime(n - a)]
|
|
44
|
+
|
|
45
|
+
##########end上面可以改动
|
|
46
|
+
return lst_rlt
|
|
47
|
+
|
|
48
|
+
if __name__ == "__main__":
|
|
49
|
+
###########下面可以修改
|
|
50
|
+
print(TEST_DO_NOT_CHANGE(10))
|
|
51
|
+
print(TEST_DO_NOT_CHANGE(20))
|
|
52
|
+
print(TEST_DO_NOT_CHANGE(100))
|
|
53
|
+
# 题号:hwc326
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
'''
|
|
3
|
+
请只在 "
|
|
4
|
+
##########start下面可以改动
|
|
5
|
+
|
|
6
|
+
##########end上面可以改动 "
|
|
7
|
+
|
|
8
|
+
中间部分作答,作答行数自由调整
|
|
9
|
+
|
|
10
|
+
请不要使用 input() 函数
|
|
11
|
+
|
|
12
|
+
此题在期中考试考察范围内,作业需提交,注意期中考试有卷面分,请认真作答。
|
|
13
|
+
|
|
14
|
+
题目:
|
|
15
|
+
定义函数,求小于等于自然数 num 的所有完全数(Perfect number),存入 list 中并返回。
|
|
16
|
+
完全数是指所有真因子(即除了自身以外的约数)之和,恰好等于它本身。
|
|
17
|
+
例如:
|
|
18
|
+
第一个完全数是6,它有约数1、2、3、6,除去它本身6外,其余3个数相加,1+2+3=6。第二个完全数是28,它有约数1、2、4、7、14、28,除去它本身28外,其余5个数相加,1+2+4+7+14=28
|
|
19
|
+
'''
|
|
20
|
+
|
|
21
|
+
# 参考答案
|
|
22
|
+
def TEST_DO_NOT_CHANGE(num):
|
|
23
|
+
lst_rlt = []
|
|
24
|
+
##########start下面可以改动
|
|
25
|
+
|
|
26
|
+
divisor_sums = [0] * (num + 1)
|
|
27
|
+
for divisor in range(1, num // 2 + 1):
|
|
28
|
+
for multiple in range(2 * divisor, num + 1, divisor):
|
|
29
|
+
divisor_sums[multiple] += divisor
|
|
30
|
+
lst_rlt = [value for value in range(2, num + 1) if divisor_sums[value] == value]
|
|
31
|
+
|
|
32
|
+
##########end上面可以改动
|
|
33
|
+
return lst_rlt
|
|
34
|
+
|
|
35
|
+
if __name__ == "__main__":
|
|
36
|
+
###########下面可以修改
|
|
37
|
+
print(TEST_DO_NOT_CHANGE(10000))
|
|
38
|
+
print(TEST_DO_NOT_CHANGE(1000))
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
'''
|
|
3
|
+
请只在 "
|
|
4
|
+
##########start下面可以改动
|
|
5
|
+
|
|
6
|
+
##########end上面可以改动 "
|
|
7
|
+
|
|
8
|
+
中间部分作答,作答行数自由调整
|
|
9
|
+
|
|
10
|
+
请不要使用 input() 函数
|
|
11
|
+
|
|
12
|
+
此题在期中考试考察范围内,作业需提交,注意期中考试有卷面分,请认真作答。
|
|
13
|
+
|
|
14
|
+
题目:
|
|
15
|
+
给你一个正整数 n ,生成一个包含 1 到 n² 所有元素,且元素按顺时针顺序螺旋排列的 n x n 正方形矩阵 matrix 。
|
|
16
|
+
例如n=2,则结果为[[1,2],[4,3]]
|
|
17
|
+
请输出n=3、n=4、n=5时的结果。
|
|
18
|
+
'''
|
|
19
|
+
|
|
20
|
+
# 参考答案
|
|
21
|
+
|
|
22
|
+
def TEST_DO_NOT_CHANGE(n):
|
|
23
|
+
result = None
|
|
24
|
+
print(n)
|
|
25
|
+
##########start下面可以改动
|
|
26
|
+
|
|
27
|
+
result = [[0] * n for _ in range(n)]
|
|
28
|
+
directions = ((0, 1), (1, 0), (0, -1), (-1, 0))
|
|
29
|
+
row = col = direction = 0
|
|
30
|
+
for value in range(1, n * n + 1):
|
|
31
|
+
result[row][col] = value
|
|
32
|
+
dr, dc = directions[direction]
|
|
33
|
+
next_row, next_col = row + dr, col + dc
|
|
34
|
+
if not (0 <= next_row < n and 0 <= next_col < n) or result[next_row][next_col]:
|
|
35
|
+
direction = (direction + 1) % 4
|
|
36
|
+
dr, dc = directions[direction]
|
|
37
|
+
row += dr
|
|
38
|
+
col += dc
|
|
39
|
+
|
|
40
|
+
##########end上面可以改动
|
|
41
|
+
return result
|
|
42
|
+
|
|
43
|
+
if __name__ == "__main__":
|
|
44
|
+
###########下面可以修改
|
|
45
|
+
#for row in TEST_DO_NOT_CHANGE(3):
|
|
46
|
+
print(TEST_DO_NOT_CHANGE(3))
|
|
47
|
+
print(TEST_DO_NOT_CHANGE(4))
|
|
48
|
+
print(TEST_DO_NOT_CHANGE(5))
|
|
49
|
+
|
|
50
|
+
# 题号:hwc328
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
'''
|
|
3
|
+
请只在 "
|
|
4
|
+
##########start下面可以改动
|
|
5
|
+
|
|
6
|
+
##########end上面可以改动 "
|
|
7
|
+
|
|
8
|
+
中间部分作答,作答行数自由调整
|
|
9
|
+
|
|
10
|
+
请不要使用 input() 函数
|
|
11
|
+
|
|
12
|
+
此题在期中考试考察范围内,作业需提交,注意期中考试有卷面分,请认真作答。
|
|
13
|
+
|
|
14
|
+
题目:
|
|
15
|
+
|
|
16
|
+
有一幅以二维整数数组表示的图画,每一个整数表示该图画的像素值大小,数值在 0 到 65535 之间。
|
|
17
|
+
给你一个起点坐标 (sr, sc)(行、列)和一个新的颜色值 newColor,请你给这幅图像重新上色。
|
|
18
|
+
规则就像画图软件里的"油漆桶":从起点开始,把所有与它 上下左右相连、且颜色相同的格子都染成新颜色;
|
|
19
|
+
染色会一圈一圈向外扩散(新染的格子继续向四周扩散),直到没有可染的格子为止。
|
|
20
|
+
最后返回上色后的图像。
|
|
21
|
+
|
|
22
|
+
示例 1:
|
|
23
|
+
输入: image = [[1,1,1],[1,1,0],[1,0,1]], sr = 1, sc = 1, newColor = 2
|
|
24
|
+
输出: [[2,2,2],[2,2,0],[2,0,1]]
|
|
25
|
+
解释: 在图像的正中间,(坐标(sr,sc)=(1,1)),
|
|
26
|
+
在路径上所有符合条件的像素点的颜色都被更改成2。
|
|
27
|
+
注意,右下角的像素没有更改为2,
|
|
28
|
+
因为它不是在上下左右四个方向上与初始点相连的像素点。
|
|
29
|
+
'''
|
|
30
|
+
|
|
31
|
+
# 参考答案
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def TEST_DO_NOT_CHANGE(image, sr, sc, newColor):
|
|
36
|
+
rlt = None
|
|
37
|
+
##########start下面可以改动
|
|
38
|
+
|
|
39
|
+
original = image[sr][sc]
|
|
40
|
+
rlt = image
|
|
41
|
+
if original != newColor:
|
|
42
|
+
stack = [(sr, sc)]
|
|
43
|
+
image[sr][sc] = newColor
|
|
44
|
+
while stack:
|
|
45
|
+
row, col = stack.pop()
|
|
46
|
+
for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)):
|
|
47
|
+
nr, nc = row + dr, col + dc
|
|
48
|
+
if (0 <= nr < len(image) and 0 <= nc < len(image[0])
|
|
49
|
+
and image[nr][nc] == original):
|
|
50
|
+
image[nr][nc] = newColor
|
|
51
|
+
stack.append((nr, nc))
|
|
52
|
+
|
|
53
|
+
##########end上面可以改动
|
|
54
|
+
return rlt
|
|
55
|
+
|
|
56
|
+
if __name__ == "__main__":
|
|
57
|
+
###########下面可以修改
|
|
58
|
+
print(TEST_DO_NOT_CHANGE([[1,1,1],[1,1,0],[1,0,1]],1,1,2))
|
|
59
|
+
# 题号:hwc329
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
'''
|
|
3
|
+
请只在 "
|
|
4
|
+
##########start下面可以改动
|
|
5
|
+
|
|
6
|
+
##########end上面可以改动 "
|
|
7
|
+
|
|
8
|
+
中间部分作答,作答行数自由调整
|
|
9
|
+
|
|
10
|
+
请不要使用 input() 函数
|
|
11
|
+
|
|
12
|
+
此题在期中考试考察范围内,作业需提交,注意期中考试有卷面分,请认真作答。
|
|
13
|
+
|
|
14
|
+
题目: 最小操作次数使数组元素相等
|
|
15
|
+
|
|
16
|
+
给你一个长度为 n 的整数数组,现在有一种操作, 该操作不会处理选定的数, 而使其他 n-1 个元素的数值增加 1 。求返回让数组所有元素相等的最小操作次数。
|
|
17
|
+
|
|
18
|
+
示例 1:
|
|
19
|
+
|
|
20
|
+
输入:nums = [1,2,3]
|
|
21
|
+
输出:3
|
|
22
|
+
解释:
|
|
23
|
+
只需要3次操作(注意每次操作会增加两个元素的值):
|
|
24
|
+
[1,2,3] => [2,3,3] => [3,4,3] => [4,4,4]
|
|
25
|
+
|
|
26
|
+
示例 2:
|
|
27
|
+
|
|
28
|
+
输入:nums = [1,1,1]
|
|
29
|
+
输出:0
|
|
30
|
+
'''
|
|
31
|
+
|
|
32
|
+
# 参考答案
|
|
33
|
+
def TEST_DO_NOT_CHANGE(nums):
|
|
34
|
+
rlt = None
|
|
35
|
+
##########start下面可以改动
|
|
36
|
+
|
|
37
|
+
rlt = sum(nums) - len(nums) * min(nums, default=0)
|
|
38
|
+
|
|
39
|
+
##########end上面可以改动
|
|
40
|
+
return rlt
|
|
41
|
+
|
|
42
|
+
if __name__ == "__main__":
|
|
43
|
+
###########下面可以修改
|
|
44
|
+
nums = [1,2,3]
|
|
45
|
+
print(TEST_DO_NOT_CHANGE(nums)) # 应该输出 3
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
'''
|
|
3
|
+
请只在 "
|
|
4
|
+
##########start下面可以改动
|
|
5
|
+
|
|
6
|
+
##########end上面可以改动 "
|
|
7
|
+
|
|
8
|
+
中间部分作答,作答行数自由调整
|
|
9
|
+
|
|
10
|
+
请不要使用 input() 函数
|
|
11
|
+
|
|
12
|
+
此题在期中考试考察范围内,作业需提交,注意期中考试有卷面分,请认真作答。
|
|
13
|
+
|
|
14
|
+
题目:
|
|
15
|
+
给定一个包含红色、白色和蓝色,一共 n 个元素的数组,原地对它们进行排序("原地"即直接在原数组 nums 上修改,不新建等长的新数组),使得相同颜色的元素相邻,并按照红色、白色、蓝色顺序排列;函数返回修改后的 nums。
|
|
16
|
+
此题中,我们使用整数 0、1 和 2 分别表示红色、白色和蓝色。
|
|
17
|
+
|
|
18
|
+
示例 1:
|
|
19
|
+
输入:nums = [2,0,2,1,1,0]
|
|
20
|
+
输出:[0,0,1,1,2,2]
|
|
21
|
+
|
|
22
|
+
示例 2:
|
|
23
|
+
输入:nums = [2,0,1]
|
|
24
|
+
输出:[0,1,2]
|
|
25
|
+
|
|
26
|
+
示例 3:
|
|
27
|
+
输入:nums = [0]
|
|
28
|
+
输出:[0]
|
|
29
|
+
'''
|
|
30
|
+
|
|
31
|
+
# 参考答案
|
|
32
|
+
def TEST_DO_NOT_CHANGE(nums):
|
|
33
|
+
print(nums)
|
|
34
|
+
lst_rlt = nums
|
|
35
|
+
##########start下面可以改动
|
|
36
|
+
|
|
37
|
+
left, index, right = 0, 0, len(nums) - 1
|
|
38
|
+
while index <= right:
|
|
39
|
+
if nums[index] == 0:
|
|
40
|
+
nums[left], nums[index] = nums[index], nums[left]
|
|
41
|
+
left += 1
|
|
42
|
+
index += 1
|
|
43
|
+
elif nums[index] == 2:
|
|
44
|
+
nums[right], nums[index] = nums[index], nums[right]
|
|
45
|
+
right -= 1
|
|
46
|
+
else:
|
|
47
|
+
index += 1
|
|
48
|
+
|
|
49
|
+
##########end上面可以改动
|
|
50
|
+
return lst_rlt
|
|
51
|
+
|
|
52
|
+
if __name__ == "__main__":
|
|
53
|
+
###########下面可以修改
|
|
54
|
+
print(TEST_DO_NOT_CHANGE([2,0,2,1,1,0]))
|
|
55
|
+
print(TEST_DO_NOT_CHANGE([2,0,1]))
|
|
56
|
+
print(TEST_DO_NOT_CHANGE([0]))
|
|
57
|
+
# 题号:hwc332
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
'''
|
|
3
|
+
请只在 "
|
|
4
|
+
##########start下面可以改动
|
|
5
|
+
|
|
6
|
+
##########end上面可以改动 "
|
|
7
|
+
|
|
8
|
+
中间部分作答,作答行数自由调整
|
|
9
|
+
|
|
10
|
+
请不要使用 input() 函数
|
|
11
|
+
|
|
12
|
+
此题在期中考试考察范围内,作业需提交,注意期中考试有卷面分,请认真作答。
|
|
13
|
+
|
|
14
|
+
题目:找到 K 个最接近的元素
|
|
15
|
+
给定一个排序好的数组arr,两个整数k和x,从数组中找到最靠近x(两数之差最小)的k个数。返回的结果必须要是按升序排好的。
|
|
16
|
+
整数a比整数b更接近x需要满足:
|
|
17
|
+
|a - x| < |b - x|或者
|
|
18
|
+
|a - x| == |b - x|且a < b
|
|
19
|
+
|
|
20
|
+
示例:
|
|
21
|
+
输入:arr = [1,2,3,4,5], k = 4, x = 3输出:[1,2,3,4]
|
|
22
|
+
|
|
23
|
+
提示:
|
|
24
|
+
1 <= k <= len(arr)
|
|
25
|
+
1 <= len(arr) <= 10**4
|
|
26
|
+
arr按升序排列
|
|
27
|
+
-10**4 <= arr[i], x <= 10**4
|
|
28
|
+
'''
|
|
29
|
+
|
|
30
|
+
# 参考答案
|
|
31
|
+
|
|
32
|
+
def TEST_DO_NOT_CHANGE(arr, k, x):
|
|
33
|
+
lst_rlt = []
|
|
34
|
+
##########start下面可以改动
|
|
35
|
+
|
|
36
|
+
left, right = 0, len(arr) - k
|
|
37
|
+
while left < right:
|
|
38
|
+
middle = (left + right) // 2
|
|
39
|
+
if x - arr[middle] > arr[middle + k] - x:
|
|
40
|
+
left = middle + 1
|
|
41
|
+
else:
|
|
42
|
+
right = middle
|
|
43
|
+
lst_rlt = arr[left:left + k]
|
|
44
|
+
|
|
45
|
+
##########end上面可以改动
|
|
46
|
+
return lst_rlt
|
|
47
|
+
|
|
48
|
+
if __name__ == "__main__":
|
|
49
|
+
###########下面可以修改
|
|
50
|
+
print(TEST_DO_NOT_CHANGE( [1,2,3,4,5],4,3))
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
'''
|
|
3
|
+
请只在 "
|
|
4
|
+
##########start下面可以改动
|
|
5
|
+
|
|
6
|
+
##########end上面可以改动 "
|
|
7
|
+
|
|
8
|
+
中间部分作答,作答行数自由调整
|
|
9
|
+
|
|
10
|
+
请不要使用 input() 函数
|
|
11
|
+
|
|
12
|
+
此题在期中考试考察范围内,作业需提交,注意期中考试有卷面分,请认真作答。
|
|
13
|
+
|
|
14
|
+
题目:格雷编码
|
|
15
|
+
n 位格雷码序列是一个由 2^n 个整数组成的序列,其中:
|
|
16
|
+
每个整数都在范围 [0, 2^n - 1] 内(含 0 和 2^n - 1)
|
|
17
|
+
第一个整数是0
|
|
18
|
+
一个整数在序列中出现不超过一次
|
|
19
|
+
每对相邻整数的二进制表示恰好一位不同,且第一个和最后一个整数的二进制表示恰好一位不同
|
|
20
|
+
给你一个整数n,返回任一有效的n 位格雷码序列。
|
|
21
|
+
(用大白话说:把 0 到 2^n-1 这些整数排成一圈,要求相邻两个数写成二进制后只有 1 位不同,从 0 开始即可。)
|
|
22
|
+
|
|
23
|
+
示例:
|
|
24
|
+
输入:n = 2输出:[0,1,3,2]
|
|
25
|
+
|
|
26
|
+
提示:1<=n<=16
|
|
27
|
+
'''
|
|
28
|
+
|
|
29
|
+
# 参考答案
|
|
30
|
+
|
|
31
|
+
def TEST_DO_NOT_CHANGE(n):
|
|
32
|
+
lst_rlt = []
|
|
33
|
+
##########start下面可以改动
|
|
34
|
+
|
|
35
|
+
lst_rlt = [value ^ (value >> 1) for value in range(1 << n)]
|
|
36
|
+
|
|
37
|
+
##########end上面可以改动
|
|
38
|
+
return lst_rlt
|
|
39
|
+
|
|
40
|
+
if __name__ == "__main__":
|
|
41
|
+
###########下面可以修改
|
|
42
|
+
n=2
|
|
43
|
+
print(TEST_DO_NOT_CHANGE(n))
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
'''
|
|
3
|
+
请只在 "
|
|
4
|
+
##########start下面可以改动
|
|
5
|
+
|
|
6
|
+
##########end上面可以改动 "
|
|
7
|
+
|
|
8
|
+
中间部分作答,作答行数自由调整
|
|
9
|
+
|
|
10
|
+
请不要使用 input() 函数
|
|
11
|
+
|
|
12
|
+
此题在期中考试考察范围内,作业需提交,注意期中考试有卷面分,请认真作答。
|
|
13
|
+
|
|
14
|
+
题目:
|
|
15
|
+
给定一个无重复元素的数组candidates和一个目标数target,找出candidates中所有可以使数字和为target的组合。
|
|
16
|
+
|
|
17
|
+
说明:
|
|
18
|
+
1.所有数字(包括target)都是正整数。
|
|
19
|
+
2.解集不能包含重复的组合。
|
|
20
|
+
3.candidates中的数字可以无限制重复被选取,组合的顺序任意。
|
|
21
|
+
|
|
22
|
+
示例1:
|
|
23
|
+
|
|
24
|
+
输入:candidates = [2,3,6,7], target = 7,
|
|
25
|
+
所求解集为:
|
|
26
|
+
[
|
|
27
|
+
[7],
|
|
28
|
+
[2,2,3]
|
|
29
|
+
]
|
|
30
|
+
|
|
31
|
+
示例2:
|
|
32
|
+
|
|
33
|
+
输入:candidates = [2,3,5], target = 8,
|
|
34
|
+
所求解集为:
|
|
35
|
+
[
|
|
36
|
+
[2,2,2,2],
|
|
37
|
+
[2,3,3],
|
|
38
|
+
[3,5]
|
|
39
|
+
]
|
|
40
|
+
'''
|
|
41
|
+
|
|
42
|
+
# 参考答案
|
|
43
|
+
def TEST_DO_NOT_CHANGE(candidates,target):
|
|
44
|
+
lst_rlt = []
|
|
45
|
+
##########start下面可以改动
|
|
46
|
+
|
|
47
|
+
values = sorted(candidates)
|
|
48
|
+
|
|
49
|
+
def search(index, remaining, chosen):
|
|
50
|
+
if remaining == 0:
|
|
51
|
+
lst_rlt.append(chosen.copy())
|
|
52
|
+
return
|
|
53
|
+
if index == len(values) or values[index] > remaining:
|
|
54
|
+
return
|
|
55
|
+
value = values[index]
|
|
56
|
+
for count in range(remaining // value + 1):
|
|
57
|
+
search(index + 1, remaining - count * value, chosen + [value] * count)
|
|
58
|
+
|
|
59
|
+
search(0, target, [])
|
|
60
|
+
|
|
61
|
+
##########end上面可以改动
|
|
62
|
+
return lst_rlt
|
|
63
|
+
|
|
64
|
+
if __name__ == "__main__":
|
|
65
|
+
###########下面可以修改
|
|
66
|
+
candidates = [2,3,5]
|
|
67
|
+
target = 8
|
|
68
|
+
print(TEST_DO_NOT_CHANGE(candidates, target))
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
# 题号:hwc403
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
'''
|
|
3
|
+
请只在 "
|
|
4
|
+
##########start下面可以改动
|
|
5
|
+
|
|
6
|
+
##########end上面可以改动 "
|
|
7
|
+
|
|
8
|
+
中间部分作答,作答行数自由调整
|
|
9
|
+
|
|
10
|
+
请不要使用 input() 函数
|
|
11
|
+
|
|
12
|
+
此题在期中考试考察范围内,作业需提交,注意期中考试有卷面分,请认真作答。
|
|
13
|
+
|
|
14
|
+
题目:数字 n 代表生成括号的对数,请你设计一个函数,用于能够生成所有可能的并且 有效的 括号组合。
|
|
15
|
+
有效括号组合需满足:左括号必须以正确的顺序闭合。
|
|
16
|
+
|
|
17
|
+
示例 1:
|
|
18
|
+
|
|
19
|
+
输入: n = 3
|
|
20
|
+
输出: ["((()))","(()())","(())()","()(())","()()()"]
|
|
21
|
+
|
|
22
|
+
示例 2:
|
|
23
|
+
|
|
24
|
+
输入: n = 1
|
|
25
|
+
输出: ["()"]
|
|
26
|
+
'''
|
|
27
|
+
|
|
28
|
+
# 参考答案
|
|
29
|
+
from typing import List
|
|
30
|
+
def TEST_DO_NOT_CHANGE(n):
|
|
31
|
+
res = []
|
|
32
|
+
##########start下面可以改动
|
|
33
|
+
|
|
34
|
+
def generate(prefix, opened, closed):
|
|
35
|
+
if closed == n:
|
|
36
|
+
res.append(prefix)
|
|
37
|
+
return
|
|
38
|
+
if opened < n:
|
|
39
|
+
generate(prefix + "(", opened + 1, closed)
|
|
40
|
+
if closed < opened:
|
|
41
|
+
generate(prefix + ")", opened, closed + 1)
|
|
42
|
+
|
|
43
|
+
generate("", 0, 0)
|
|
44
|
+
|
|
45
|
+
##########end上面可以改动
|
|
46
|
+
return res
|
|
47
|
+
|
|
48
|
+
if __name__ == '__main__':
|
|
49
|
+
###########下面可以修改
|
|
50
|
+
print(TEST_DO_NOT_CHANGE(1))
|
|
51
|
+
print(TEST_DO_NOT_CHANGE(3))
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
'''
|
|
2
|
+
请只在 "
|
|
3
|
+
##########start下面可以改动
|
|
4
|
+
|
|
5
|
+
##########end上面可以改动 "
|
|
6
|
+
|
|
7
|
+
中间部分作答,作答行数自由调整
|
|
8
|
+
|
|
9
|
+
请不要使用 input() 函数
|
|
10
|
+
|
|
11
|
+
此题在期中考试考察范围内,作业需提交,注意期中考试有卷面分,请认真作答。
|
|
12
|
+
|
|
13
|
+
题目:解码字符串
|
|
14
|
+
给定一个经过编码的字符串,返回它解码后的字符串。
|
|
15
|
+
|
|
16
|
+
编码规则为: k[encoded_string],表示其中方括号内部的 encoded_string 正好重复 k 次。注意 k 保证为正整数。
|
|
17
|
+
|
|
18
|
+
你可以认为输入字符串总是有效的;输入字符串中没有额外的空格,且输入的方括号总是符合格式要求的。
|
|
19
|
+
|
|
20
|
+
此外,你可以认为原始数据不包含数字,所有的数字只表示重复的次数 k ,例如不会出现像 3a 或 2[4] 的输入。
|
|
21
|
+
|
|
22
|
+
示例 1:
|
|
23
|
+
|
|
24
|
+
输入:s = "3[a]2[bc]"
|
|
25
|
+
输出:"aaabcbc"
|
|
26
|
+
|
|
27
|
+
示例 2:
|
|
28
|
+
|
|
29
|
+
输入:s = "3[a2[c]]"
|
|
30
|
+
输出:"accaccacc"
|
|
31
|
+
'''
|
|
32
|
+
|
|
33
|
+
# 参考答案
|
|
34
|
+
def TEST_DO_NOT_CHANGE(encoded):
|
|
35
|
+
output = None
|
|
36
|
+
##########start下面可以改动
|
|
37
|
+
|
|
38
|
+
stack = []
|
|
39
|
+
parts = []
|
|
40
|
+
count = 0
|
|
41
|
+
for char in encoded:
|
|
42
|
+
if "0" <= char <= "9":
|
|
43
|
+
count = count * 10 + int(char)
|
|
44
|
+
elif char == "[":
|
|
45
|
+
stack.append((parts, count))
|
|
46
|
+
parts, count = [], 0
|
|
47
|
+
elif char == "]":
|
|
48
|
+
repeated = "".join(parts)
|
|
49
|
+
parts, times = stack.pop()
|
|
50
|
+
parts.append(repeated * times)
|
|
51
|
+
else:
|
|
52
|
+
parts.append(char)
|
|
53
|
+
output = "".join(parts)
|
|
54
|
+
|
|
55
|
+
##########end上面可以改动
|
|
56
|
+
return output
|
|
57
|
+
|
|
58
|
+
if __name__ == "__main__":
|
|
59
|
+
###########下面可以修改
|
|
60
|
+
print(TEST_DO_NOT_CHANGE("2[abc]3[cd]ef"))
|
|
61
|
+
print(TEST_DO_NOT_CHANGE("abc3[cd]xyz"))
|
|
62
|
+
print(TEST_DO_NOT_CHANGE("5[a]"))
|