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,66 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
'''
|
|
3
|
+
请只在 "
|
|
4
|
+
##########start下面可以改动
|
|
5
|
+
|
|
6
|
+
##########end上面可以改动 "
|
|
7
|
+
|
|
8
|
+
中间部分作答,作答行数自由调整
|
|
9
|
+
|
|
10
|
+
请不要使用 input() 函数
|
|
11
|
+
|
|
12
|
+
此题在期中考试考察范围内,作业需提交,注意期中考试有卷面分,请认真作答。
|
|
13
|
+
|
|
14
|
+
题目:
|
|
15
|
+
你是一个专业的小偷,计划偷窃沿街的房屋,每间房内都藏有一定的现金。
|
|
16
|
+
这个地方所有的房屋都 围成一圈 ,这意味着第一个房屋和最后一个房屋是紧挨着的。
|
|
17
|
+
同时,相邻的房屋装有相互连通的防盗系统,如果两间相邻的房屋在同一晚上被小偷闯入,系统会自动报警 。
|
|
18
|
+
|
|
19
|
+
给定一个代表每个房屋存放金额的非负整数数组,计算你 在不触动警报装置的情况下 (只去偷1个晚上),能够偷窃到的最高金额。
|
|
20
|
+
若 nums 为空,返回 0。
|
|
21
|
+
|
|
22
|
+
示例1:
|
|
23
|
+
|
|
24
|
+
输入:nums = [2,3,2]
|
|
25
|
+
输出:3
|
|
26
|
+
解释:你不能先偷窃 1 号房屋(金额 = 2),然后偷窃 3 号房屋(金额 = 2), 因为他们是相邻的。
|
|
27
|
+
示例 2:
|
|
28
|
+
|
|
29
|
+
输入:nums = [1,2,3,1]
|
|
30
|
+
输出:4
|
|
31
|
+
解释:你可以先偷窃 1 号房屋(金额 = 1),然后偷窃 3 号房屋(金额 = 3)。
|
|
32
|
+
偷窃到的最高金额 = 1 + 3 = 4 。
|
|
33
|
+
示例 3:
|
|
34
|
+
|
|
35
|
+
输入:nums = [0]
|
|
36
|
+
输出:0
|
|
37
|
+
'''
|
|
38
|
+
|
|
39
|
+
# 参考答案
|
|
40
|
+
def TEST_DO_NOT_CHANGE(nums):
|
|
41
|
+
result = -1
|
|
42
|
+
##########start下面可以改动
|
|
43
|
+
|
|
44
|
+
def best_in_range(start, end):
|
|
45
|
+
previous = best = 0
|
|
46
|
+
for i in range(start, end):
|
|
47
|
+
previous, best = best, max(best, previous + nums[i])
|
|
48
|
+
return best
|
|
49
|
+
|
|
50
|
+
if len(nums) <= 1:
|
|
51
|
+
result = sum(nums)
|
|
52
|
+
else:
|
|
53
|
+
result = max(best_in_range(0, len(nums) - 1), best_in_range(1, len(nums)))
|
|
54
|
+
|
|
55
|
+
##########end上面可以改动
|
|
56
|
+
return result
|
|
57
|
+
|
|
58
|
+
if __name__ == "__main__":
|
|
59
|
+
###########下面可以修改
|
|
60
|
+
nums = [1,2,3,1]
|
|
61
|
+
print(TEST_DO_NOT_CHANGE(nums))
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
# 题号:hwc406
|
|
@@ -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
|
+
给定一个字符串,请你找出其中不含有重复字符的 最长子串的长度。
|
|
16
|
+
(子串:字符串中连续的一段,例如 "abc" 是 "abcabcbb" 的子串。)
|
|
17
|
+
|
|
18
|
+
示例:
|
|
19
|
+
输入: s = "abcabcbb"
|
|
20
|
+
输出: 3
|
|
21
|
+
解释: 因为无重复字符的最长子串是 "abc",所以其长度为 3。
|
|
22
|
+
示例 2:
|
|
23
|
+
|
|
24
|
+
输入: s = "bbbbb"
|
|
25
|
+
输出: 1
|
|
26
|
+
解释: 因为无重复字符的最长子串是 "b",所以其长度为 1。
|
|
27
|
+
'''
|
|
28
|
+
|
|
29
|
+
# 参考答案
|
|
30
|
+
|
|
31
|
+
def TEST_DO_NOT_CHANGE(s):
|
|
32
|
+
rlt = None
|
|
33
|
+
##########start下面可以改动
|
|
34
|
+
|
|
35
|
+
left = rlt = 0
|
|
36
|
+
last_index = {}
|
|
37
|
+
for right, char in enumerate(s):
|
|
38
|
+
left = max(left, last_index.get(char, -1) + 1)
|
|
39
|
+
rlt = max(rlt, right - left + 1)
|
|
40
|
+
last_index[char] = right
|
|
41
|
+
|
|
42
|
+
##########end上面可以改动
|
|
43
|
+
return rlt
|
|
44
|
+
|
|
45
|
+
if __name__ == "__main__":
|
|
46
|
+
###########下面可以修改
|
|
47
|
+
print(TEST_DO_NOT_CHANGE("abcabcbb"))
|
|
48
|
+
print(TEST_DO_NOT_CHANGE("bbbbb"))
|
|
49
|
+
print(TEST_DO_NOT_CHANGE("pwwkew")) # 应输出 3
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
'''
|
|
3
|
+
请只在 "
|
|
4
|
+
##########start下面可以改动
|
|
5
|
+
|
|
6
|
+
##########end上面可以改动 "
|
|
7
|
+
|
|
8
|
+
中间部分作答,作答行数自由调整
|
|
9
|
+
|
|
10
|
+
请不要使用 input() 函数
|
|
11
|
+
|
|
12
|
+
此题在期中考试考察范围内,作业需提交,注意期中考试有卷面分,请认真作答。
|
|
13
|
+
|
|
14
|
+
题目:
|
|
15
|
+
找出两个字符串之间的最长公共子字符串。
|
|
16
|
+
例如:现有2个字符串,s1,s2;
|
|
17
|
+
s1 = "ABABC"
|
|
18
|
+
s2 = "BABCA"
|
|
19
|
+
s1和s2之间的最长公共子字符串即为"BABC"
|
|
20
|
+
在result中只返回字符串: "BABC"。
|
|
21
|
+
|
|
22
|
+
现有以下3组字符串,请找出每组字符串的最长公共子字符串。
|
|
23
|
+
a1 = "ABABCGRFGGJDJFKLSJDFKLJSLG"
|
|
24
|
+
a2 = "BABCACGJISEJGIOSDJGIOSDGJG"
|
|
25
|
+
|
|
26
|
+
b1 = "SDASQWERTYUIOPVJDKKVHNJKDN"
|
|
27
|
+
b2 = "QWERTYUIOPFDSFKSJFFNJSDFFF"
|
|
28
|
+
|
|
29
|
+
c1 = "RERFZXCVBNMJKLJKLJKLJKLJKL"
|
|
30
|
+
c2 = "WZXCVBNMFDUSHFUSDHFFHJDSFH"
|
|
31
|
+
'''
|
|
32
|
+
|
|
33
|
+
# 参考答案
|
|
34
|
+
|
|
35
|
+
def TEST_DO_NOT_CHANGE(s1,s2):
|
|
36
|
+
|
|
37
|
+
print(s1,s2)
|
|
38
|
+
result = None
|
|
39
|
+
|
|
40
|
+
##########start下面可以改动
|
|
41
|
+
|
|
42
|
+
lengths = [0] * (len(s2) + 1)
|
|
43
|
+
best_length = end = 0
|
|
44
|
+
for i, first in enumerate(s1, 1):
|
|
45
|
+
for j in range(len(s2), 0, -1):
|
|
46
|
+
lengths[j] = lengths[j - 1] + 1 if first == s2[j - 1] else 0
|
|
47
|
+
if lengths[j] > best_length:
|
|
48
|
+
best_length, end = lengths[j], i
|
|
49
|
+
result = s1[end - best_length:end]
|
|
50
|
+
|
|
51
|
+
##########end上面可以改动
|
|
52
|
+
return result
|
|
53
|
+
if __name__ == "__main__":
|
|
54
|
+
###########下面可以修改
|
|
55
|
+
|
|
56
|
+
a1 = "ABABCGRFGGJDJFKLSJDFKLJSLG"
|
|
57
|
+
a2 = "BABCACGJISEJGIOSDJGIOSDGJG"
|
|
58
|
+
|
|
59
|
+
b1 = "SDASQWERTYUIOPVJDKKVHNJKDN"
|
|
60
|
+
b2 = "QWERTYUIOPFDSFKSJFFNJSDFFF"
|
|
61
|
+
|
|
62
|
+
c1 = "RERFZXCVBNMJKLJKLJKLJKLJKL"
|
|
63
|
+
c2 = "WZXCVBNMFDUSHFUSDHFFHJDSFH"
|
|
64
|
+
print(TEST_DO_NOT_CHANGE(a1,a2))
|
|
65
|
+
print(TEST_DO_NOT_CHANGE(b1,b2))
|
|
66
|
+
print(TEST_DO_NOT_CHANGE(c1,c2))
|
|
67
|
+
# 题号:hwc408
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
'''
|
|
3
|
+
请只在 "
|
|
4
|
+
##########start下面可以改动
|
|
5
|
+
|
|
6
|
+
##########end上面可以改动 "
|
|
7
|
+
|
|
8
|
+
中间部分作答,作答行数自由调整
|
|
9
|
+
|
|
10
|
+
请不要使用 input() 函数
|
|
11
|
+
|
|
12
|
+
此题在期中考试考察范围内,作业需提交,注意期中考试有卷面分,请认真作答。
|
|
13
|
+
|
|
14
|
+
题目:找出最具竞争力的子序列
|
|
15
|
+
给你一个整数数组nums和一个正整数k,返回长度为k且最具竞争力的nums子序列。
|
|
16
|
+
数组的子序列是从数组中删除一些元素(可能不删除元素)得到的序列。
|
|
17
|
+
在子序列a和子序列b第一个不相同的位置上,如果a中的数字小于b中对应的数字,那么我们称子序列a比子序列b(相同长度下)更具竞争力。
|
|
18
|
+
例如,[1,3,4]比[1,3,5]更具竞争力,在第一个不相同的位置,也就是最后一个位置上,4小于5。
|
|
19
|
+
|
|
20
|
+
示例:
|
|
21
|
+
输入:nums = [2,4,3,3,5,4,9,6], k = 4
|
|
22
|
+
输出:[2,3,3,4]
|
|
23
|
+
|
|
24
|
+
提示:
|
|
25
|
+
1 <= nums.length <= 105
|
|
26
|
+
0 <= nums[i] <= 109
|
|
27
|
+
1 <= k <= nums.length
|
|
28
|
+
'''
|
|
29
|
+
|
|
30
|
+
# 参考答案
|
|
31
|
+
|
|
32
|
+
def TEST_DO_NOT_CHANGE(num,k):
|
|
33
|
+
print(num, k)
|
|
34
|
+
lst_rlt = []
|
|
35
|
+
##########start下面可以改动
|
|
36
|
+
|
|
37
|
+
remove = len(num) - k
|
|
38
|
+
for value in num:
|
|
39
|
+
while remove and lst_rlt and lst_rlt[-1] > value:
|
|
40
|
+
lst_rlt.pop()
|
|
41
|
+
remove -= 1
|
|
42
|
+
lst_rlt.append(value)
|
|
43
|
+
lst_rlt = lst_rlt[:k]
|
|
44
|
+
|
|
45
|
+
##########end上面可以改动
|
|
46
|
+
return lst_rlt
|
|
47
|
+
|
|
48
|
+
if __name__ == "__main__":
|
|
49
|
+
###########下面可以修改
|
|
50
|
+
print(TEST_DO_NOT_CHANGE( [2,4,3,3,5,4,9,6],4))
|
|
51
|
+
# 题号:hwc409
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
'''
|
|
2
|
+
请只在 "
|
|
3
|
+
##########start下面可以改动
|
|
4
|
+
|
|
5
|
+
##########end上面可以改动 "
|
|
6
|
+
|
|
7
|
+
中间部分作答,作答行数自由调整
|
|
8
|
+
|
|
9
|
+
请不要使用 input() 函数
|
|
10
|
+
|
|
11
|
+
此题在期中考试考察范围内,作业需提交,注意期中考试有卷面分,请认真作答。
|
|
12
|
+
|
|
13
|
+
题目:单词拆分
|
|
14
|
+
|
|
15
|
+
给定一个包含非空单词的字典 wordDict,编写函数 TEST_DO_NOT_CHANGE(s, wordDict),判断一个非空字符串 s 是否可以被空格拆分成一个或多个字典中出现的单词。
|
|
16
|
+
|
|
17
|
+
假设字典为:wordDict = ["cats", "dog", "sand", "and", "cat"]
|
|
18
|
+
|
|
19
|
+
对于以下输入:
|
|
20
|
+
|
|
21
|
+
s = "catsandog"
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
应该返回 False,因为 "catsandog" 无法被拆分成字典中的单词。
|
|
25
|
+
'''
|
|
26
|
+
|
|
27
|
+
# 参考答案
|
|
28
|
+
|
|
29
|
+
wordDict = ["cats", "dog", "sand", "and", "cat"]
|
|
30
|
+
def TEST_DO_NOT_CHANGE(s, wordDict):
|
|
31
|
+
output = None
|
|
32
|
+
##########start下面可以改动
|
|
33
|
+
|
|
34
|
+
words = set(wordDict)
|
|
35
|
+
lengths = {len(word) for word in words}
|
|
36
|
+
reachable = [True] + [False] * len(s)
|
|
37
|
+
for end in range(1, len(s) + 1):
|
|
38
|
+
reachable[end] = any(
|
|
39
|
+
length <= end and reachable[end - length] and s[end - length:end] in words
|
|
40
|
+
for length in lengths
|
|
41
|
+
)
|
|
42
|
+
output = reachable[-1]
|
|
43
|
+
|
|
44
|
+
##########end上面可以改动
|
|
45
|
+
return output
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
if __name__ == "__main__":
|
|
49
|
+
###########下面可以修改
|
|
50
|
+
print(TEST_DO_NOT_CHANGE("catsandog", wordDict)) # False
|
|
51
|
+
print(TEST_DO_NOT_CHANGE("catsanddog", wordDict)) # True
|
|
52
|
+
# 题号:hwc410
|
|
@@ -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
|
+
|
|
16
|
+
给定两个整数 n 和 k,返回范围 [1, n] 中所有可能的 k 个数的组合。
|
|
17
|
+
你可以按 任何顺序 返回答案。
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
示例 1:
|
|
24
|
+
输入: n = 4, k = 2
|
|
25
|
+
输出:
|
|
26
|
+
[[2,4],
|
|
27
|
+
[3,4],
|
|
28
|
+
[2,3],
|
|
29
|
+
[1,2],
|
|
30
|
+
[1,3],
|
|
31
|
+
[1,4],
|
|
32
|
+
]
|
|
33
|
+
|
|
34
|
+
示例 2:
|
|
35
|
+
输入: n = 1, k = 1
|
|
36
|
+
输出: [[1]]
|
|
37
|
+
'''
|
|
38
|
+
|
|
39
|
+
# 参考答案
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
def TEST_DO_NOT_CHANGE(n, k):
|
|
44
|
+
rlt = None
|
|
45
|
+
##########start下面可以改动
|
|
46
|
+
|
|
47
|
+
from itertools import combinations
|
|
48
|
+
|
|
49
|
+
rlt = [list(group) for group in combinations(range(1, n + 1), k)]
|
|
50
|
+
|
|
51
|
+
##########end上面可以改动
|
|
52
|
+
return rlt
|
|
53
|
+
|
|
54
|
+
if __name__ == "__main__":
|
|
55
|
+
###########下面可以修改
|
|
56
|
+
print(TEST_DO_NOT_CHANGE(4,2))
|
|
57
|
+
print(TEST_DO_NOT_CHANGE(1,1))
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
'''
|
|
3
|
+
请只在 "
|
|
4
|
+
##########start下面可以改动
|
|
5
|
+
|
|
6
|
+
##########end上面可以改动 "
|
|
7
|
+
|
|
8
|
+
中间部分作答,作答行数自由调整
|
|
9
|
+
|
|
10
|
+
请不要使用 input() 函数
|
|
11
|
+
|
|
12
|
+
此题在期中考试考察范围内,作业需提交,注意期中考试有卷面分,请认真作答。
|
|
13
|
+
|
|
14
|
+
题目:
|
|
15
|
+
|
|
16
|
+
给定一个三角形 triangle ,找出自顶向下的最小路径和。
|
|
17
|
+
|
|
18
|
+
每一步只能移动到下一行中相邻的结点上。
|
|
19
|
+
相邻的结点在这里指的是下标与上一层结点下标相同或者等于上一层结点下标 + 1 的两个结点。
|
|
20
|
+
也就是说,如果正位于当前行的下标 i ,那么下一步可以移动到下一行的下标 i 或 i + 1 。
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
示例 1:
|
|
24
|
+
|
|
25
|
+
输入: triangle = [[2],[3,4],[6,5,7],[4,1,8,3]]
|
|
26
|
+
输出: 11
|
|
27
|
+
解释: 如下面简图所示:自顶向下的最小路径和为 11(即,2 + 3 + 5 + 1 = 11)。
|
|
28
|
+
[2]
|
|
29
|
+
[3,4]
|
|
30
|
+
[6,5,7]
|
|
31
|
+
[4,1,8,3]
|
|
32
|
+
'''
|
|
33
|
+
|
|
34
|
+
# 参考答案
|
|
35
|
+
|
|
36
|
+
def TEST_DO_NOT_CHANGE(nums):
|
|
37
|
+
rlt = None
|
|
38
|
+
##########start下面可以改动
|
|
39
|
+
|
|
40
|
+
costs = [0] * (len(nums) + 1)
|
|
41
|
+
for row in reversed(nums):
|
|
42
|
+
for i, value in enumerate(row):
|
|
43
|
+
costs[i] = value + min(costs[i], costs[i + 1])
|
|
44
|
+
rlt = costs[0]
|
|
45
|
+
|
|
46
|
+
##########end上面可以改动
|
|
47
|
+
return rlt
|
|
48
|
+
|
|
49
|
+
if __name__ == "__main__":
|
|
50
|
+
###########下面可以修改
|
|
51
|
+
print(TEST_DO_NOT_CHANGE([[2],[3,4],[6,5,7],[4,1,8,3]]))
|
|
52
|
+
print(TEST_DO_NOT_CHANGE([[-10]]))
|
|
53
|
+
# 题号:hwc412
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
'''
|
|
3
|
+
请只在 "
|
|
4
|
+
##########start下面可以改动
|
|
5
|
+
|
|
6
|
+
##########end上面可以改动 "
|
|
7
|
+
|
|
8
|
+
中间部分作答,作答行数自由调整
|
|
9
|
+
|
|
10
|
+
请不要使用 input() 函数
|
|
11
|
+
|
|
12
|
+
此题在期中考试考察范围内,作业需提交,注意期中考试有卷面分,请认真作答。
|
|
13
|
+
|
|
14
|
+
题目
|
|
15
|
+
加油站问题:
|
|
16
|
+
在一条环路上有 n 个加油站,其中第 i 个加油站有汽油 gas[i] 升。
|
|
17
|
+
你有一辆油箱容量无限的的汽车,从第 i 个加油站开往第 i+1 个加油站需要消耗汽油 cost[i] 升。你从其中的一个加油站出发,开始时油箱为空。
|
|
18
|
+
给定两个整数数组 gas 和 cost ,如果你可以按顺序绕环路行驶一周,则返回出发时加油站的编号,否则返回 -1 。
|
|
19
|
+
例如:gas = [1,2,3,4,5],cost = [3,4,5,1,2]
|
|
20
|
+
从第0个位置开始,汽油不足以到达第1个位置。
|
|
21
|
+
从第1个位置开始,汽油不足以到达第2个位置。
|
|
22
|
+
从第2个位置开始,汽油不足以到达第3个位置。
|
|
23
|
+
从第3个位置开始,可以绕圈一周。
|
|
24
|
+
结果:从第3个位置开始。
|
|
25
|
+
所以result返回值为:3
|
|
26
|
+
|
|
27
|
+
有以下加油站配比组合,请按照示例返回出发时加油站的编号。
|
|
28
|
+
gas1 = [2,3,4,5,1], cost1 = [5,1,2,3,4]
|
|
29
|
+
gas2 = [3,3,4,0,2], cost2 = [2,3,4,0,1]
|
|
30
|
+
gas3 = [5,1,2,3,4], cost3 = [4,4,1,5,1]
|
|
31
|
+
'''
|
|
32
|
+
|
|
33
|
+
# 参考答案
|
|
34
|
+
|
|
35
|
+
def TEST_DO_NOT_CHANGE(gas,cost):
|
|
36
|
+
result = None
|
|
37
|
+
|
|
38
|
+
##########start下面可以改动
|
|
39
|
+
|
|
40
|
+
start = total = tank = 0
|
|
41
|
+
for i, (available, needed) in enumerate(zip(gas, cost)):
|
|
42
|
+
difference = available - needed
|
|
43
|
+
total += difference
|
|
44
|
+
tank += difference
|
|
45
|
+
if tank < 0:
|
|
46
|
+
start, tank = i + 1, 0
|
|
47
|
+
result = start if gas and total >= 0 else -1
|
|
48
|
+
|
|
49
|
+
##########end上面可以改动
|
|
50
|
+
return result
|
|
51
|
+
if __name__ == "__main__":
|
|
52
|
+
###########下面可以修改
|
|
53
|
+
gas1 = [2,3,4,5,1]
|
|
54
|
+
cost1 = [5,1,2,3,4]
|
|
55
|
+
|
|
56
|
+
gas2 = [3,3,4,0,2]
|
|
57
|
+
cost2 = [2,3,4,0,1]
|
|
58
|
+
|
|
59
|
+
gas3 = [5,1,2,3,4]
|
|
60
|
+
cost3 = [4,4,1,5,1]
|
|
61
|
+
|
|
62
|
+
print(TEST_DO_NOT_CHANGE(gas1,cost1))
|
|
63
|
+
print(TEST_DO_NOT_CHANGE(gas2,cost2))
|
|
64
|
+
print(TEST_DO_NOT_CHANGE(gas3,cost3))
|
|
65
|
+
# 题号:hwc413
|
|
@@ -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
|
+
给定一个字符串S,通过将字符串S中的每个字母转变大小写,我们可以获得一个新的字符串。返回所有可能得到的字符串集合。
|
|
16
|
+
|
|
17
|
+
示例 1:
|
|
18
|
+
输入: S = "a1b2"
|
|
19
|
+
输出: ["a1b2", "a1B2", "A1b2", "A1B2"]
|
|
20
|
+
|
|
21
|
+
示例 2:
|
|
22
|
+
输入: S = "3z4"
|
|
23
|
+
输出: ["3z4", "3Z4"]
|
|
24
|
+
|
|
25
|
+
示例 3:
|
|
26
|
+
输入: S = "12345"
|
|
27
|
+
输出: ["12345"]
|
|
28
|
+
|
|
29
|
+
返回集合中的字符串顺序任意;判定时只比较结果集合,不要求固定顺序。
|
|
30
|
+
'''
|
|
31
|
+
|
|
32
|
+
# 参考答案
|
|
33
|
+
def TEST_DO_NOT_CHANGE(s):
|
|
34
|
+
rlt = None
|
|
35
|
+
##########start下面可以改动
|
|
36
|
+
|
|
37
|
+
from itertools import product
|
|
38
|
+
|
|
39
|
+
choices = [tuple(dict.fromkeys((char, char.lower(), char.upper()))) for char in s]
|
|
40
|
+
rlt = ["".join(chars) for chars in product(*choices)]
|
|
41
|
+
|
|
42
|
+
##########end上面可以改动
|
|
43
|
+
return rlt
|
|
44
|
+
|
|
45
|
+
if __name__ == "__main__":
|
|
46
|
+
###########下面可以修改
|
|
47
|
+
print(TEST_DO_NOT_CHANGE("a1b2"))
|
|
48
|
+
print(TEST_DO_NOT_CHANGE("3z4"))
|
|
49
|
+
print(TEST_DO_NOT_CHANGE("12345"))
|
|
50
|
+
# 题号:hwc414
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
'''
|
|
3
|
+
请只在 "
|
|
4
|
+
##########start下面可以改动
|
|
5
|
+
|
|
6
|
+
##########end上面可以改动 "
|
|
7
|
+
|
|
8
|
+
中间部分作答,作答行数自由调整
|
|
9
|
+
|
|
10
|
+
请不要使用 input() 函数
|
|
11
|
+
|
|
12
|
+
此题在期中考试考察范围内,作业需提交,注意期中考试有卷面分,请认真作答。
|
|
13
|
+
|
|
14
|
+
题目:
|
|
15
|
+
给定一个包含非负整数的 m x n 网格 grid ,请找出一条从左上角到右下角的路径,使得路径上的数字总和为最小。
|
|
16
|
+
说明:每次只能向下或者向右移动一步。
|
|
17
|
+
|
|
18
|
+
示例:grid = [
|
|
19
|
+
[1,3,1,5],
|
|
20
|
+
[1,5,1,3],
|
|
21
|
+
[4,2,1,5],
|
|
22
|
+
[2,5,3,1]
|
|
23
|
+
],求得的最短路径数字总和为11。(1-3-1-1-1-3-1)
|
|
24
|
+
因此,result输出值为 11
|
|
25
|
+
|
|
26
|
+
有以下几个网格数据,请分别求出最短路径数字总和:
|
|
27
|
+
grid1 = [[2,3,1,4],[4,5,1,5],[1,7,4,9],[4,7,2,1]]
|
|
28
|
+
grid2 = [[1,4,3,2],[1,5,1,5],[2,1,3,1],[1,7,3,8]]
|
|
29
|
+
grid3 = [[3,1,1,2],[6,5,2,1],[5,7,1,2],[6,2,2,3]]
|
|
30
|
+
'''
|
|
31
|
+
|
|
32
|
+
# 参考答案
|
|
33
|
+
def TEST_DO_NOT_CHANGE(grid):
|
|
34
|
+
result = None
|
|
35
|
+
##########start下面可以改动
|
|
36
|
+
|
|
37
|
+
if not grid or not grid[0]:
|
|
38
|
+
return 0
|
|
39
|
+
costs = [0] * len(grid[0])
|
|
40
|
+
for i, row in enumerate(grid):
|
|
41
|
+
for j, value in enumerate(row):
|
|
42
|
+
if i and j:
|
|
43
|
+
costs[j] = value + min(costs[j], costs[j - 1])
|
|
44
|
+
elif j:
|
|
45
|
+
costs[j] = value + costs[j - 1]
|
|
46
|
+
else:
|
|
47
|
+
costs[j] += value
|
|
48
|
+
result = costs[-1]
|
|
49
|
+
|
|
50
|
+
##########end上面可以改动
|
|
51
|
+
return result
|
|
52
|
+
|
|
53
|
+
if __name__ == "__main__":
|
|
54
|
+
###########下面可以修改
|
|
55
|
+
grid1 = [[2,3,1,4],[4,5,1,5],[1,7,4,9],[4,7,2,1]]
|
|
56
|
+
grid2 = [[1,4,3,2],[1,5,1,5],[2,1,3,1],[1,7,3,8]]
|
|
57
|
+
grid3 = [[3,1,1,2],[6,5,2,1],[5,7,1,2],[6,2,2,3]]
|
|
58
|
+
print(TEST_DO_NOT_CHANGE(grid1))
|
|
59
|
+
print(TEST_DO_NOT_CHANGE(grid2))
|
|
60
|
+
print(TEST_DO_NOT_CHANGE(grid3))
|
|
61
|
+
# 题号:hwc415
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
'''
|
|
3
|
+
请只在 "
|
|
4
|
+
##########start下面可以改动
|
|
5
|
+
|
|
6
|
+
##########end上面可以改动 "
|
|
7
|
+
|
|
8
|
+
中间部分作答,作答行数自由调整
|
|
9
|
+
|
|
10
|
+
请不要使用 input() 函数
|
|
11
|
+
|
|
12
|
+
此题在期中考试考察范围内,作业需提交,注意期中考试有卷面分,请认真作答。
|
|
13
|
+
|
|
14
|
+
题目:使字符串总不同字符的数目相等
|
|
15
|
+
给你两个下标从 0 开始的字符串 word1 和 word2 。
|
|
16
|
+
|
|
17
|
+
一次 移动 由以下两个步骤组成:
|
|
18
|
+
1.选中两个下标 i 和 j ,分别满足 0 <= i < word1.length 和 0 <= j < word2.length ,
|
|
19
|
+
2.交换 word1[i] 和 word2[j] 。
|
|
20
|
+
3.如果可以通过 恰好一次 移动,使 word1 和 word2 中不同字符的数目相等,则返回 True ;否则,返回 False 。
|
|
21
|
+
|
|
22
|
+
示例 1:
|
|
23
|
+
|
|
24
|
+
输入:word1 = "ac", word2 = "b"
|
|
25
|
+
输出:False
|
|
26
|
+
解释:交换任何一组下标都会导致第一个字符串中有 2 个不同的字符,而在第二个字符串中只有 1 个不同字符。
|
|
27
|
+
|
|
28
|
+
示例 2:
|
|
29
|
+
|
|
30
|
+
输入:word1 = "abcc", word2 = "aab"
|
|
31
|
+
输出:True
|
|
32
|
+
解释:交换第一个字符串的下标 2 和第二个字符串的下标 0 。之后得到 word1 = "abac" 和 word2 = "cab" ,各有 3 个不同字符。
|
|
33
|
+
|
|
34
|
+
示例 3:
|
|
35
|
+
|
|
36
|
+
输入:word1 = "abcde", word2 = "fghij"
|
|
37
|
+
输出:True
|
|
38
|
+
解释:无论交换哪一组下标,两个字符串中都会有 5 个不同字符。
|
|
39
|
+
'''
|
|
40
|
+
|
|
41
|
+
# 参考答案
|
|
42
|
+
def TEST_DO_NOT_CHANGE(word1,word2):
|
|
43
|
+
result = False
|
|
44
|
+
##########start下面可以改动
|
|
45
|
+
|
|
46
|
+
from collections import Counter
|
|
47
|
+
|
|
48
|
+
first, second = Counter(word1), Counter(word2)
|
|
49
|
+
for a, count_a in first.items():
|
|
50
|
+
for b, count_b in second.items():
|
|
51
|
+
if a == b:
|
|
52
|
+
if len(first) == len(second):
|
|
53
|
+
return True
|
|
54
|
+
continue
|
|
55
|
+
distinct_a = len(first) - (count_a == 1) + (b not in first)
|
|
56
|
+
distinct_b = len(second) - (count_b == 1) + (a not in second)
|
|
57
|
+
if distinct_a == distinct_b:
|
|
58
|
+
return True
|
|
59
|
+
|
|
60
|
+
##########end上面可以改动
|
|
61
|
+
return result
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
if __name__ == "__main__":
|
|
65
|
+
###########下面可以修改
|
|
66
|
+
print(TEST_DO_NOT_CHANGE("ac","b"))
|
|
67
|
+
print(TEST_DO_NOT_CHANGE("abcc","aab"))
|
|
68
|
+
print(TEST_DO_NOT_CHANGE("abcde","fghij"))
|
|
69
|
+
# 题号:hwc416
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
'''
|
|
3
|
+
请只在 "
|
|
4
|
+
##########start下面可以改动
|
|
5
|
+
|
|
6
|
+
##########end上面可以改动 "
|
|
7
|
+
|
|
8
|
+
中间部分作答,作答行数自由调整
|
|
9
|
+
|
|
10
|
+
请不要使用 input() 函数
|
|
11
|
+
|
|
12
|
+
此题在期中考试考察范围内,作业需提交,注意期中考试有卷面分,请认真作答。
|
|
13
|
+
|
|
14
|
+
题目
|
|
15
|
+
给定一个没有重复数字的序列,返回其所有可能的全排列。
|
|
16
|
+
|
|
17
|
+
示例:
|
|
18
|
+
输入: [1,2,3]
|
|
19
|
+
输出:
|
|
20
|
+
[
|
|
21
|
+
[1,2,3],
|
|
22
|
+
[1,3,2],
|
|
23
|
+
[2,1,3],
|
|
24
|
+
[2,3,1],
|
|
25
|
+
[3,1,2],
|
|
26
|
+
[3,2,1]
|
|
27
|
+
]
|
|
28
|
+
'''
|
|
29
|
+
|
|
30
|
+
# 参考答案
|
|
31
|
+
|
|
32
|
+
def TEST_DO_NOT_CHANGE(nums):
|
|
33
|
+
print(nums)
|
|
34
|
+
permutations = None
|
|
35
|
+
##########start下面可以改动
|
|
36
|
+
|
|
37
|
+
from itertools import permutations as permute
|
|
38
|
+
|
|
39
|
+
permutations = [list(order) for order in permute(nums)]
|
|
40
|
+
|
|
41
|
+
##########end上面可以改动
|
|
42
|
+
return permutations
|
|
43
|
+
|
|
44
|
+
if __name__ == "__main__":
|
|
45
|
+
###########下面可以修改
|
|
46
|
+
print(TEST_DO_NOT_CHANGE([1,2,3]))
|
|
47
|
+
# 题号:hwc418
|