finish 01-homework

dev
SJ2050 4 years ago
parent 1a2d27dc09
commit 001839394c

41
.gitignore vendored

@ -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/

@ -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.

@ -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}')

@ -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}行。')

@ -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)

@ -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.')

@ -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')
# 使用制表符来进行分割可以让乘法表格显示更加清楚

@ -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}')

@ -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)

@ -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))
# 算法说明:
# 从矩阵左下角,先排除不符合条件的列,再排除不符合条件的行,将范围一直往右上角缩小

@ -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)
# 激活码是用来授予用户相关权限的凭证,一般由一组数字和字母组成,其组合非常多,随机性较强,且能保证唯一,难以被外部破解。

@ -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')))
Loading…
Cancel
Save