diff --git a/homework_01_python/homework.py b/homework_01_python/homework.py new file mode 100644 index 0000000000000000000000000000000000000000..61a762339d4b6ba00ebf1b34317109b852cb925b --- /dev/null +++ b/homework_01_python/homework.py @@ -0,0 +1,163 @@ +import math +from turtle import left + +def homework1(article: str): + splited = article.split(" ") + result = {} + for w in [(word.strip(".").strip('?').strip('/')).lower() for word in splited if word != ""]: + if w in result: + result[w] += 1 + else: + result[w] = 1 + return result + +def homework2(): + l = [1, 2, 3, 4] + cnt = 0 + for i in l: + for j in l: + for k in l: + if i != j and i != k and j != k: + cnt += 1 + return cnt # P(4, 3) + +def homework3(l): + """Alternative + for cmp in [(1000000, 0.01, 39500), (600000, 0.015, 33500), (400000, 0.03, 27500), (200000, 0.05, 17500), (100000, 0.075, 10000), (0, 0.1, 0)]: + if l > cmp[0]: + return (l - cmp[0]) * cmp[1] + cmp[2] + """ + res = 0 + if l >= 1000000: + res += (l - 1000000) * 0.01 + l = 1000000 + if l >= 600000: + res += (l - 600000) * 0.015 + l = 600000 + if l >= 400000: + res += (l - 400000) * 0.03 + l = 400000 + if l >= 200000: + res += (l - 200000) * 0.05 + l = 200000 + if l >= 100000: + res += (l - 100000) * 0.075 + l = 100000 + res += l * 0.1 + return res + +def homework4(): + for i in range(9): + for j in range(i + 1): + print("{}*{}={:>2d}".format(i + 1, j + 1, (i + 1) * (j + 1)), end=" ") + print() + +def homework5(): + return sum([-i if i % 2 != 0 else i for i in range(101)]) + 1 + +def homework6(array): + for i in range(len(array) - 1): + for j in range(len(array) - i - 1): + (array[j], array[j + 1]) = (array[j + 1], array[j]) if array[j] > array[j + 1] else (array[j], array[j + 1]) + return array + +def homework7(matrix, find_x): + def bin_find(arr, x): + l, r = 0, len(arr) - 1 + while l <= r: + m = (l + r) // 2 + if arr[m] == x: + return m + if arr[m] < x: + l = m + 1 + else: + r = m - 1 + return None + for row in matrix: + if bin_find(row, find_x) is not None: + return True + return False + +def homework8(): + result = [] + for i in range(1, 1000): + d = [1] + for j in range(2, int(math.sqrt(i))+1): + if i % j == 0: + d.append(j) + d.append(i // j) + if sum(d) == i: + result.append(i) + return result + +def homework9(n): + appear = [n] + while True: + n = sum([int(c) ** 2 for c in str(n)]) + if n == 1: + return True + if n in appear: + return False + +def homework10(nums, k): + for i in range(len(nums)): + for j in range(i): + if (i - j >= 2) and (sum(nums[j:i]) % k == 0): + return True + return False + +def homework11(s): + appear = [] + for c in s: + if c in appear: + return False + else: + appear.append(c) + return True + +def homework12(s): + dict = {c: 0 for c in "abcdefghijklmnopqrstuvwxyz"} + for c in s: + dict[c] += 1 + return min(dict["b"], dict["a"], dict["l"] // 2, dict["o"] // 2, dict["n"]) + +def homework13(): + import random + return ["".join([random.choice("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789") for i in range(11)]) for j in range(20)] + +def homework14(directory): + import os + result = [] + def find(root): + for i in os.listdir(root): + if os.path.isdir(os.path.join(root, i)): + find(os.path.join(root, i)) + elif i.split('.')[-1] == "dll": + result.append(os.path.join(root, i)) + return find(directory) + +def homework15(directory): + import os + dict = {"c":0, "py":0} + for i in os.listdir(directory): + if i.split('.')[-1] in dict: + dict[i.split('.')[-1]] += open(os.path.join(directory, i), 'r').readlines().__len__() + return dict + + +if __name__ == "__main__": + # print(homework1("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. ")) + # print(homework2()) + # print(homework3(1919810)) + # homework4() + # homework5() + # print(homework6([1, 10, 4, 2, 9, 2, 34, 5, 9, 8, 5, 0])) + # print(homework7([[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]], 5)) + # print(homework8()) + # print(homework9(20)) + # print(homework10([23,2,6,4,7], 6)) + # print(homework11("1232")) + # print(homework12("loonbalxballpoon")) + # print(homework13()) + # print(homework14("/path/to/dir")) + pass \ No newline at end of file diff --git a/homework_01_python/name.txt b/homework_01_python/name.txt new file mode 100644 index 0000000000000000000000000000000000000000..c37492db97820d99be8f4694f6c19748e70d1875 --- /dev/null +++ b/homework_01_python/name.txt @@ -0,0 +1,2 @@ +孙彦升 +2021204551 \ No newline at end of file