diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..0d48645 --- /dev/null +++ b/.gitignore @@ -0,0 +1,41 @@ +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class + +# Distribution / packaging +.Python +env/ +build/ +dist/ +*.log + +# pyenv +.python-version + +# dotenv +.env + +# virtualenv +.venv/ +venv/ +ENV/ + +# VSCode settings +.vscode + +# IDEA files +.idea + +# OSX dir files +.DS_Store + +# Sublime Text settings +*.sublime-workspace +*.sublime-project + +# History +.history + +# test files +homework_01_python/**/test/ \ No newline at end of file diff --git a/homework_01_python/1-string/essay.txt b/homework_01_python/1-string/essay.txt new file mode 100644 index 0000000..80db580 --- /dev/null +++ b/homework_01_python/1-string/essay.txt @@ -0,0 +1 @@ +One is always on a strange road, watching strange scenery and listening to strange music. Then one day, you will find that the things you try hard to forget are already gone. \ No newline at end of file diff --git a/homework_01_python/1-string/homework1.py b/homework_01_python/1-string/homework1.py new file mode 100644 index 0000000..76cdcc2 --- /dev/null +++ b/homework_01_python/1-string/homework1.py @@ -0,0 +1,22 @@ +''' +Author: SJ2050 +Date: 2021-10-10 23:22:04 +LastEditTime: 2021-10-10 23:34:06 +Version: v0.0.1 +Description: Solution for homework 1. +Copyright © 2021 SJ2050 +''' + +if __name__ == '__main__': + with open('essay.txt', 'r') as fp_in: + content = fp_in.read() + word_map = {} + words = content.split() + for w in words: + if w in word_map: + word_map[w] += 1 + else: + word_map[w] = 1 + + for (k, v) in word_map.items(): + print(f'{k}: {v}') diff --git a/homework_01_python/10-application3/homework10.py b/homework_01_python/10-application3/homework10.py new file mode 100644 index 0000000..5e5f8b8 --- /dev/null +++ b/homework_01_python/10-application3/homework10.py @@ -0,0 +1,30 @@ +''' +Author: SJ2050 +Date: 2021-10-16 21:14:15 +LastEditTime: 2021-10-16 23:24:52 +Version: v0.0.1 +Description: Solution for homework10. +Copyright © 2021 SJ2050 +''' +import os + +if __name__ == '__main__': + res = {} + dir_path = './test/' + files= os.listdir(dir_path) + + for file in files: + if not os.path.isdir(file): + ext = os.path.splitext(file)[-1][1:] + with open(dir_path+"/"+file) as fp: + count = len(fp.readlines()) + if ext in res: + res[ext] += count + else: + res[ext] = count + + print('统计结果如下: ') + for k, v in res.items(): + print(f'{k}有: {v}行。') + + diff --git a/homework_01_python/2-combination/homework2.py b/homework_01_python/2-combination/homework2.py new file mode 100644 index 0000000..294b06f --- /dev/null +++ b/homework_01_python/2-combination/homework2.py @@ -0,0 +1,64 @@ +''' +Author: SJ2050 +Date: 2021-10-10 23:34:50 +LastEditTime: 2021-10-16 23:23:48 +Version: v0.0.1 +Description: Solution for homework 2. +Copyright © 2021 SJ2050 +''' +from itertools import permutations + +def combination(arr, n): + if n == 0: + return [[]] + + l =[] + for i in range(0, len(arr)): + m = arr[i] + remLst = arr[i + 1:] + + for p in combination(remLst, n-1): + l.append([m]+p) + + return l + + +def heapPermutation(arr, n, process): + if n == 1: + process(arr) + return + + for i in range(n): + heapPermutation(arr, n-1, process) + if n & 1: + arr[0], arr[n-1] = arr[n-1], arr[0] + else: + arr[i], arr[n-1] = arr[n-1], arr[i] + + + +if __name__ == '__main__': + nums = [1, 2, 3, 4] + m = 3 + count = 0 + combs = combination(nums, m) + + def printAndCount(arr): + global count + count += 1 + + for i in range(len(arr)): + print(f'{arr[i]}', end='') + + print('') + + print('3-digit nums:') + for c in combs: + heapPermutation(c, m, printAndCount) + print('-'*12) + print(f'Total count: {count}') + + # 从一列数中取出三个元素组成三位数的算法复杂度分析: + # 首先是组合分析,从一列数中取出三个数的组合个数为n*(n-1)*(n-2)/6,算法时间复杂度为O(n^3). + # 接着,对每队组合进行排列,排列算法选用的是heap算法,其时间复杂度为O(n!). + # 故总的时间复杂度为: O((n^3)*(3!)) --> O(n^3) diff --git a/homework_01_python/3-judgement/homework3.py b/homework_01_python/3-judgement/homework3.py new file mode 100644 index 0000000..f09d29c --- /dev/null +++ b/homework_01_python/3-judgement/homework3.py @@ -0,0 +1,59 @@ +''' +Author: SJ2050 +Date: 2021-10-16 22:24:48 +LastEditTime: 2021-10-16 22:44:04 +Version: v0.0.1 +Description: Solution for homework3. +Copyright © 2021 SJ2050 +''' + +def judgement(x): + s = 0 + p = 0 + + if x == 0: + return 0 + + if x <= 10: + s = 0 + p = 0.1 + elif x > 10 and x <= 20: + s = 10 + p = 0.075 + elif x > 20 and x <= 40: + s = 20 + p = 0.05 + elif x > 40 and x <= 60: + s = 40 + p = 0.03 + elif x > 60 and x <= 100: + s = 60 + p = 0.015 + else: + s = 100 + p = 0.01 + + return (x-s)*p+judgement(s) + +import bisect + +# 使用列表来实现判断 +def judgementList(x): + a = [0, 10, 20, 40, 60, 100] + p = [0.1, 0.075, 0.05, 0.03, 0.015, 0.01] + ind = bisect.bisect_left(a, x) + res = 0 + + res += (x-a[ind-1])*p[ind-1] + for i in range(1, ind): + res += (a[i]-a[i-1])*(p[i-1]) + + return res + + +if __name__ == '__main__': + x = 100 + print(f'利润为:{x}w, 发放的奖金为{judgement(x)}w.') + print('----------------------------------------') + print('以下是使用列表计算的结果:') + print(f'利润为:{x}w, 发放的奖金为{judgementList(x)}w.') diff --git a/homework_01_python/4-multiplication_table/homework4.py b/homework_01_python/4-multiplication_table/homework4.py new file mode 100644 index 0000000..a428587 --- /dev/null +++ b/homework_01_python/4-multiplication_table/homework4.py @@ -0,0 +1,16 @@ +''' +Author: SJ2050 +Date: 2021-10-16 22:21:24 +LastEditTime: 2021-10-16 23:25:56 +Version: v0.0.1 +Description: Solution for homework4. +Copyright © 2021 SJ2050 +''' + +if __name__ == '__main__': + for i in range(10): + for j in range (10-i,10): + print(f'{10-i}*{j}={(10-i)*j}', end='\t') + print('\n') + + # 使用制表符来进行分割可以让乘法表格显示更加清楚 diff --git a/homework_01_python/5-while/homework5.py b/homework_01_python/5-while/homework5.py new file mode 100644 index 0000000..fa7fa98 --- /dev/null +++ b/homework_01_python/5-while/homework5.py @@ -0,0 +1,22 @@ +''' +Author: SJ2050 +Date: 2021-10-16 22:14:07 +LastEditTime: 2021-10-16 22:19:04 +Version: v0.0.1 +Description: Solution for homework5. +Copyright © 2021 SJ2050 +''' + +if __name__ == '__main__': + i = 2 + res = 0 + while i <= 100: + res += (-1)**(i&1)*i + i += 1 + + print(f'result: {res}') + + # 可以使用列表推导式一句话写完 + res = sum([(-1)**(i&1)*i for i in range(2, 101)]) + + print(f'result: {res}') diff --git a/homework_01_python/6-sort/homework6.py b/homework_01_python/6-sort/homework6.py new file mode 100644 index 0000000..80e1458 --- /dev/null +++ b/homework_01_python/6-sort/homework6.py @@ -0,0 +1,34 @@ +''' +Author: SJ2050 +Date: 2021-10-16 21:05:23 +LastEditTime: 2021-10-16 23:25:39 +Version: v0.0.1 +Description: Solution for homework6. +Copyright © 2021 SJ2050 +''' + +def quickSort(lists, i, j): + if i >= j: + return list + pivot = lists[i] + low = i + high = j + while i < j: + while i < j and lists[j] <= pivot: + j -= 1 + lists[i]=lists[j] + while i < j and lists[i] >= pivot: + i += 1 + lists[j]=lists[i] + lists[j] = pivot + quickSort(lists,low,i-1) + quickSort(lists,i+1,high) + return lists + + +if __name__ == '__main__': + # 采用快速排序法进行列表排序 + lists = [1, 10, 4, 2, 9, 2, 34, 5, 9, 8, 5, 0] + ordered_lists = quickSort(lists, 0, len(lists)-1) + print('从大到小排列后的列表为: ') + print(ordered_lists) diff --git a/homework_01_python/7-algorithm2/homework7.py b/homework_01_python/7-algorithm2/homework7.py new file mode 100644 index 0000000..82aa4f9 --- /dev/null +++ b/homework_01_python/7-algorithm2/homework7.py @@ -0,0 +1,37 @@ +''' +Author: SJ2050 +Date: 2021-10-16 22:04:07 +LastEditTime: 2021-10-16 23:25:30 +Version: v0.0.1 +Description: Solution for homework7. +Copyright © 2021 SJ2050 +''' +def searchMatrix(matrix, target): + if (len(matrix) == 0 ): + return False + + i = len(matrix)-1 + j = 0 + while (i>=0 and j < len(matrix[0])): + if (matrix[i][j] == target): + return True + elif (matrix[i][j] < target): + j += 1 + elif(matrix[i][j] > target): + i -= 1 + + return False + +if __name__ == "__main__": + matrix = [ + [1, 4, 7, 11, 15], + [2, 5, 8, 12, 19], + [3, 6, 9, 16, 22], + [10, 13, 14, 17, 24], + [18, 21, 23, 26, 30] + ] + target = 20 + print(searchMatrix(matrix, target)) + + # 算法说明: + # 从矩阵左下角,先排除不符合条件的列,再排除不符合条件的行,将范围一直往右上角缩小 diff --git a/homework_01_python/8-application1/homework8.py b/homework_01_python/8-application1/homework8.py new file mode 100644 index 0000000..edbaaad --- /dev/null +++ b/homework_01_python/8-application1/homework8.py @@ -0,0 +1,30 @@ +''' +Author: SJ2050 +Date: 2021-10-16 21:43:32 +LastEditTime: 2021-10-16 23:25:21 +Version: v0.0.1 +Description: Solution for homework8. +Copyright © 2021 SJ2050 +''' +import random +import string + +def activationCode(count, length): + res = set() + base = string.ascii_letters + string.digits + + while len(res) < count: + res.add(''.join(random.sample(base, length))) + + return res + +if __name__ == '__main__': + n = 200 # count + l = 11 # length of each code + + res = activationCode(n, l) + print(f'生成的{n}个激活码如下: ') + for item in res: + print(item) + + # 激活码是用来授予用户相关权限的凭证,一般由一组数字和字母组成,其组合非常多,随机性较强,且能保证唯一,难以被外部破解。 diff --git a/homework_01_python/9-application2/homework9.py b/homework_01_python/9-application2/homework9.py new file mode 100644 index 0000000..ba1db2b --- /dev/null +++ b/homework_01_python/9-application2/homework9.py @@ -0,0 +1,13 @@ +''' +Author: SJ2050 +Date: 2021-10-16 21:33:05 +LastEditTime: 2021-10-16 23:25:11 +Version: v0.0.1 +Description: Solution for homework9. +Copyright © 2021 SJ2050 +''' +from pathlib import Path + +if __name__ == '__main__': + dir_path = './test/' + print(sorted(Path('.').glob('**/*.dll')))