From 720fbf72fa5bcf577e392bbee9e5571ed5872611 Mon Sep 17 00:00:00 2001 From: wanghaoyuan <1596364995@qq.com> Date: Mon, 14 Nov 2022 13:51:05 +0000 Subject: [PATCH] 2022260481_homework_01_python 2022260481_homework_01_python Signed-off-by: wanghaoyuan <1596364995@qq.com> --- homework_01_python/01_01.py | 27 +++++++++++++++++++++++++++ homework_01_python/01_02.py | 15 +++++++++++++++ homework_01_python/01_03.py | 26 ++++++++++++++++++++++++++ homework_01_python/01_04.py | 6 ++++++ homework_01_python/01_05.py | 14 ++++++++++++++ homework_01_python/01_06.py | 13 +++++++++++++ homework_01_python/01_07.py | 23 +++++++++++++++++++++++ homework_01_python/01_08.py | 23 +++++++++++++++++++++++ homework_01_python/01_09.py | 24 ++++++++++++++++++++++++ homework_01_python/01_10.py | 23 +++++++++++++++++++++++ homework_01_python/01_11.py | 16 ++++++++++++++++ homework_01_python/01_12.py | 28 ++++++++++++++++++++++++++++ homework_01_python/name.txt | 2 ++ 13 files changed, 240 insertions(+) create mode 100644 homework_01_python/01_01.py create mode 100644 homework_01_python/01_02.py create mode 100644 homework_01_python/01_03.py create mode 100644 homework_01_python/01_04.py create mode 100644 homework_01_python/01_05.py create mode 100644 homework_01_python/01_06.py create mode 100644 homework_01_python/01_07.py create mode 100644 homework_01_python/01_08.py create mode 100644 homework_01_python/01_09.py create mode 100644 homework_01_python/01_10.py create mode 100644 homework_01_python/01_11.py create mode 100644 homework_01_python/01_12.py create mode 100644 homework_01_python/name.txt diff --git a/homework_01_python/01_01.py b/homework_01_python/01_01.py new file mode 100644 index 0000000..83ff007 --- /dev/null +++ b/homework_01_python/01_01.py @@ -0,0 +1,27 @@ +#1.1 字符串:给定一个文章,找出每个单词的出现次数。例如给定下面的一篇短文,进行操作。 + +str = "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. " +n = len(str) +#跳过列表 +skip_list = [' ','.','\n','\t'] +#记录单词和出现次数 +word_dict = dict() +i = 0 +while i < n-1: + if str[i] in skip_list: + i += 1 + #读取一个单词 + word = '' + while str[i] not in skip_list: + word = word + str[i] + i += 1 + #更新单词列表 + if word in word_dict: + word_dict[word] += 1 + else: + word_dict[word] = 1 +#删除空元素 +del word_dict[''] + +print(word_dict) diff --git a/homework_01_python/01_02.py b/homework_01_python/01_02.py new file mode 100644 index 0000000..264a71d --- /dev/null +++ b/homework_01_python/01_02.py @@ -0,0 +1,15 @@ +#1.2 组合:有 1、2、3、4 个数字,能组成多少个互不相同且无重复数字的三位数?都是多少? + +#数字源 +num_list = [1,2,3,4] +#不重复三位数列表 +three_digit_list = [] +for i in range(len(num_list)): + for j in range(len(num_list)): + for k in range(len(num_list)): + #生成三位数 + three_digit = 100*num_list[i] + 10*num_list[j] + 1*num_list[k] + # 更新三位数列表 + if three_digit not in three_digit_list: + three_digit_list.append(three_digit) +print(three_digit_list) \ No newline at end of file diff --git a/homework_01_python/01_03.py b/homework_01_python/01_03.py new file mode 100644 index 0000000..d6d7b58 --- /dev/null +++ b/homework_01_python/01_03.py @@ -0,0 +1,26 @@ +#1.3 判断: +''' +企业发放的奖金根据利润提成。利润(I): +低于或等于 10 万元时,奖金可提 10%; +高于 10 万元,低于 20 万元时,低于 10 万元的部分按 10%提成,高于 10 万元的部分,可提成 7.5%; +20 万到 40 万之间时,高于 20 万元的部分,可提成 5%; +40 万到 60 万之间时,高于 40 万元的部分,可提成 3%; +60 万到 100 万之间时,高于 60 万元的部分,可提成 1.5%, +高于 100 万元时, 超过 100 万元的部分按 1%提成, 从键盘输入当月利润 I,求应发放奖金总数? +''' + +#利润I(万元) +I = 15.0 +if I <= 10: + bonus = 10.0/100 * I +elif I > 10 and I <= 20: + bonus = 10.0/100 * 10 + 7.5/100 * (I-10) +elif I > 20 and I <= 40: + bonus = 10.0/100 * 10 + 7.5/100 * 10 + 5.0/100 * (I-20) +elif I > 40 and I <= 60: + bonus = 10.0/100 * 10 + 7.5/100 * 10 + 5.0/100 * 20 + 3.0/100 * (I-40) +elif I > 60 and I <= 100: + bonus = 10.0/100 * 10 + 7.5/100 * 10 + 5.0/100 * 20 + 3.0/100 * 20 + 3.0/100 * (I-60) +else: + bonus = 10.0/100 * 10 + 7.5/100 * 10 + 5.0/100 * 20 + 3.0/100 * 20 + 3.0/100 * 40 + 1.0/100 * (I-100) +print(bonus) \ No newline at end of file diff --git a/homework_01_python/01_04.py b/homework_01_python/01_04.py new file mode 100644 index 0000000..2e2f023 --- /dev/null +++ b/homework_01_python/01_04.py @@ -0,0 +1,6 @@ +#1.4 循环:输出9x9的乘法口诀表 + +for i in range(1,10): + for j in range(1, i+1): + print('{:1d}x{:1d} = {:<2d}'.format(i,j,i*j), end=" ") #5位宽度,小数点后取3位 + print('') \ No newline at end of file diff --git a/homework_01_python/01_05.py b/homework_01_python/01_05.py new file mode 100644 index 0000000..5ee289a --- /dev/null +++ b/homework_01_python/01_05.py @@ -0,0 +1,14 @@ +#1.5 使用while循环实现输出2-3+4-5+6.....+100的和 + +#方法1 +i = 2 +sum1 = 0 +while i <= 100: + sum1 += (-1)**i * i + i += 1 + +#方法2 +sum2 = 0 +for j in range(2,101):sum2 += (-1)**j * j + +print(sum1,sum2) \ No newline at end of file diff --git a/homework_01_python/01_06.py b/homework_01_python/01_06.py new file mode 100644 index 0000000..fca13f0 --- /dev/null +++ b/homework_01_python/01_06.py @@ -0,0 +1,13 @@ +#1.6 排序算法:给一个数字列表,将其按照由大到小的顺序排列,同时输出排序后元素在原始列表中的下标 + +data = [1, 10, 4, 2, 9, 2, 34, 5, 9, 8, 5, 0]#数据源 +result_num = data#排序后数据 +subscript = list(range(len(data)))#排序后下标 +for i in range(len(data)): + for j in range(len(data)-i-1): + if result_num[j] > result_num[j+1]: + #冒泡交换 + result_num[j],result_num[j + 1] = result_num[j + 1],result_num[j] + subscript[j],subscript[j+1] = subscript[j+1],subscript[j] +print(result_num) +print(subscript) diff --git a/homework_01_python/01_07.py b/homework_01_python/01_07.py new file mode 100644 index 0000000..7a85ef4 --- /dev/null +++ b/homework_01_python/01_07.py @@ -0,0 +1,23 @@ +#1.7 矩阵搜索:编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target。该矩阵具有以下特性: +#每行的元素从左到右升序排列。 +#每列的元素从上到下升序排列。 + +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 = 17 +result = 'false' + +#获取矩阵尺寸 +m = len(matrix) +n = len(matrix[0]) +for i in range(m): + if matrix[i][0] <= target and matrix[i][n-1] >= target: + for j in range(n): + if matrix[i][j] == target: + result = 'true' +print(result) diff --git a/homework_01_python/01_08.py b/homework_01_python/01_08.py new file mode 100644 index 0000000..c21f76c --- /dev/null +++ b/homework_01_python/01_08.py @@ -0,0 +1,23 @@ +#1.8 完数计算:找出1000以内的所有完数,并打印输出。 +#什么是完数? 完全数,又被称作完美数或完备数,是一些特殊的自然数。 它所有的真因子(即除了自身以外的约数)的和,恰好等于它本身。如果一个数恰好等于它的因子之和,则称该数为“完全数”。 + +perfect_number = [] +for num in range(3,1000+1): + factor_list = []#存储该数所有因子 + + #寻找因子 + if num % 2 == 0: + factor_list.append(2) + for factor in range(3,num): + #找约数 + if num % factor == 0: + factor_list.append(factor) + #print(num,factor_list) + + sum = 1 + for i in range(len(factor_list)): + sum += factor_list[i] + if sum == num: + perfect_number.append(num) + +print(perfect_number) \ No newline at end of file diff --git a/homework_01_python/01_09.py b/homework_01_python/01_09.py new file mode 100644 index 0000000..43126c4 --- /dev/null +++ b/homework_01_python/01_09.py @@ -0,0 +1,24 @@ +#1.9 快乐数:编写一个算法来判断一个数 n 是不是快乐数。如果 n 是快乐数打印True ;不是,则打印输出False。 +#「快乐数」定义为:对于一个正整数,每一次将该数替换为它每个位置上的数字的平方和。然后重复这个过程直到这个数变为 1,也可能是 无限循环 但始终变不到 1。如果 可以变为 1,那么这个数就是快乐数。 + +num = 19 +#结果历史,判断是否陷入无限循环 +oper_list = [num] +while 1: + oper = 0 + for i in range(1,len(str(num))+1): + oper += (num//10**(i-1)%10) ** 2#从右至左第i位上数字的平方 + num = oper + + if num == 1: + print('true') + oper_list.append(oper) + break + elif num in oper_list: + print('false') + oper_list.append(oper) + break + else: + oper_list.append(oper) + +print(oper_list) \ No newline at end of file diff --git a/homework_01_python/01_10.py b/homework_01_python/01_10.py new file mode 100644 index 0000000..d5d6fcd --- /dev/null +++ b/homework_01_python/01_10.py @@ -0,0 +1,23 @@ +#1.10 连续的子数组和: +''' +给你一个整数数组 nums 和一个整数 k ,编写一个函数来判断该数组是否含有同时满足下述条件的连续子数组: +子数组大小 至少为 2 ,且 +子数组元素总和为 k 的倍数。 +如果存在,返回 True ;否则,返回 False 。 +如果存在一个整数 n ,令整数 x 符合 x = n * k ,则称 x 是 k 的一个倍数。0 始终视为 k 的一个倍数。 +''' + +nums = [23,2,4,6,7] +k = 6 +if k == 0: + print("true") + exit(0) +for lengthi in range(2,len(nums)+1): + for i in range(len(nums)-lengthi): + sum = 0 + for j in range(i,i + lengthi): + sum += nums[j] + if sum == k: + print("true") + exit(0) +print("false") \ No newline at end of file diff --git a/homework_01_python/01_11.py b/homework_01_python/01_11.py new file mode 100644 index 0000000..4957814 --- /dev/null +++ b/homework_01_python/01_11.py @@ -0,0 +1,16 @@ +#1.11 确定字符串是否包含唯一字符: +#实现一个算法:识别一个字符串中,是否包含唯一的字符。 +#如果字符串中的字符都是唯一的,则返回 True,如 '123';如果字符串中的字符有重复,则返回 False,如 '1223'。 + +str = "1223" +n = len(str) +#字符列表 +str_list = [] +for i in range(len(str)): + #字符列表 + if str[i] not in str_list: + str_list.append(str[i]) + else: + print('False') + exit(0) +print('True') diff --git a/homework_01_python/01_12.py b/homework_01_python/01_12.py new file mode 100644 index 0000000..7c278d0 --- /dev/null +++ b/homework_01_python/01_12.py @@ -0,0 +1,28 @@ +#1.12 能够拼成多少个单词: +#给出一个由小写字母组成的字符串 s,使用 s 中的字符来拼凑单词 'balloon'(气球)。字符串 s 中的每个字符最多只能被使用一次,求出 s 中的字符最多可以拼凑出多少个单词 'balloon'。 + +str = "ballloonbalxballpoonopq" +n = len(str) +#字符列表:记录b,a,l,o,n每个字母出现的次数 +target_list = [1,1,2,2,1] +#字符列表:记录b,a,l,o,n每个字母出现的次数 +letter_count = [0,0,0,0,0] +for i in range(len(str)): + #更新字符列表 + if str[i] == 'b': + letter_count[0] += 1 + if str[i] == 'a': + letter_count[1] += 1 + if str[i] == 'l': + letter_count[2] += 1 + if str[i] == 'o': + letter_count[3] += 1 + if str[i] == 'n': + letter_count[4] += 1 +for i in range(len(letter_count)): + letter_count[i] = letter_count[i]//target_list[i] +count_min = letter_count[0] +for i in range(1,len(letter_count)): + if count_min > letter_count[i]: + count_min = letter_count[i] +print(count_min) diff --git a/homework_01_python/name.txt b/homework_01_python/name.txt new file mode 100644 index 0000000..5422fcc --- /dev/null +++ b/homework_01_python/name.txt @@ -0,0 +1,2 @@ +王浩渊 +2022260481 \ No newline at end of file -- Gitee