# 【python】py课后作业程序题4「PTA」

63 篇文章 96 订阅
18 篇文章 4 订阅

#### 7-1 输出星期名缩写

1 Mon
2 Tue
3 Wed
4 Thu
5 Fri
6 Sat
7 Sun

1

Mon

n = int(input())
weeks = {1:"Mon", 2:"Tue", 3:"Wed", 4:"Thu", 5:"Fri", 6:"Sat", 7:"Sun"}
print(weeks[n])

#### 7-2 图的字典表示

{‘O’:{‘A’:2,‘B’:5,‘C’:4}}。用eval函数处理输入，eval函数具体用法见第六章内置函数。

4
{‘a’:{‘b’:10,‘c’:6}}
{‘b’:{‘c’:2,‘d’:7}}
{‘c’:{‘d’:10}}
{‘d’:{}}

4 5 35

n = int(input())
bian = 0
summ = 0
for i in range(n):
dic = eval(input())
for j in dic:
t = dic[j]
for k in t:
bian += 1
summ += t[k]
print(n, bian, summ)

#### 7-3 四则运算（用字典实现）

7
/
3

2.33

10
/
0

divided by zero

num1 = float(input())
c = input()
num2 = float(input())
ch = {}
ch["+"] = "{:.2f}".format(num1 + num2)
ch["-"] = "{:.2f}".format(num1 - num2)
ch["*"] = "{:.2f}".format(num1 * num2)
if c == '/' and num2 == 0:
print("divided by zero")
else:
ch["/"] = "{:.2f}".format(num1 / num2)
print(ch[c])

#### 7-4 分析活动投票情况

1,5,9,3,9,1,1,7,5,7,7,3,3,1,5,7,4,4,5,4,9,5,10,9

6 8

(关于全部赋值为0这个操作其实可以使用字典的get方法来简化的,下面的题目中有运用到)

devote = [int(n) for n in input().split(',')]
dic = {}
f = True
for i in range(1, 11):
dic[i] = 0
for i in devote:
dic[i] += 1
for i in range(6, 11):
if dic[i] == 0:
if f:
print(i, end = '')
f = False
else:
print(f" {i}", end = '')

#### 7-5 统计字符出现次数

programming is More fun!
m

2

string = input()
c = input()
dic = {}
for i in string:
dic[i] =dic.get(i, 0) + 1
if c not in dic:
print("0")
else:
print(dic[c])

#### 7-6 统计工龄

5
），即员工总人数；随后给出N个整数，即每个员工的工龄，范围在[0, 50]。

8
10 2 0 5 7 2 5 2

0:1
2:3
5:2
7:1
10:1

n = int(input())
dic = {}
work_year = [int(year) for year in input().split()]
for year in work_year:
dic[year] = dic.get(year, 0) + 1
year = set(work_year)
for i in year:
print(f"{i}:{dic[i]}")

#### 7-7 列表去重

[4,7,5,6,8,6,9,5]

4 7 5 6 8 9

nums = eval(input())
nums = list(set(nums))
nums.sort(key=nums.index)
f = True
for num in nums:
if f:
print(num, end = '')
f = False
else:
print(f" {num}", end = '')

#### 7-8 能被3,5和7整除的数的个数（用集合实现）

10 100

0

1000 100000

943

l, h = [int(num) for num in input().split()]
set3 = set()
set5 = set()
set7 = set()
for i in range(l, h + 1):
if i % 3 == 0:
if i % 5 == 0:
if i % 7 == 0:
true_set = set3 & set5 & set7
print(len(true_set))

#### 7-9 求矩阵鞍点的个数

4
1 7 4 1
4 8 3 6
1 6 1 2
0 7 8 9

1

2
1 7
4 1

0

3
4 7 8
1 3 3
2 3 1

2

n = int(input())
a = [[0 for _ in range(n)] for _ in range(n)]
r = []
c = []
for i in range(n):
j = 0
nums = [int(num) for num in input().split()]
for m in nums:
a[i][j] = m
j += 1
for i in range(n):
num1 = [a[i][num] for num in range(n)]
num1 = max(num1)
num2 = [a[num][i] for num in range(n)]
num2 = min(num2)
r += [(i, j) for j in range(n) if a[i][j] == num1]
c += [(k, i) for k in range(n) if a[k][i] == num2]
set1 = set(r) & set(c)
print(len(set1))

#### 7-10 两数之和

2,7,11,15
9

0 1

3,6,9
10

nums = [int(num) for num in input().split(',')]
n = int(input())
dic = {}
f = 1
for i in range(len(nums)):
num2 = n - nums[i]
if dic.get(num2) != None:
print(dic.get(num2), i)
f = 0
dic[nums[i]] = i
if f:

#### 7-11 字典合并

“1” 的 ASCII 码为 49，大于 1，排序时 1 在前，“1” 在后。其它的字符同理。

{1:3,2:5}
{1:5,3:7}

{1:8,2:5,3:7}

{“1”:3,1:4}
{“a”:5,“1”:6}

{1:4,“1”:9,“a”:5}

dic1 = eval(input())
dic2 = eval(input())
for i in dic2:
dic1[i] = dic1.get(i, 0) + dic2[i]
dic1 = list(dic1.items())
dic1.sort(key=lambda k: ord(k[0]) if type(k[0]) == str else k[0])
dic1 = str(dict(dic1))
dic1 = dic1.replace("'", '"').replace(' ', '')
print(dic1)

• 8
点赞
• 61
收藏
觉得还不错? 一键收藏
• 打赏
• 1
评论
11-14 2750
08-09
06-07 256
01-21 7871
05-30 746
07-06 1107
05-23 701
04-26 666

Khalil三省

¥1 ¥2 ¥4 ¥6 ¥10 ¥20

1.余额是钱包充值的虚拟货币，按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载，可以购买VIP、付费专栏及课程。