当前位置:   article > 正文

《Python程序设计》 第五章 编程题题解_7-5 分析活动投票情况

7-5 分析活动投票情况

目录

7-1 输出星期名缩写

7-2 图的字典表示

7-3 四则运算(用字典实现)

7-4 分析活动投票情况

7-5 统计字符出现次数

7-6 统计工龄

7-7 列表去重

7-8 能被3,5和7整除的数的个数(用集合实现)

7-9 求矩阵鞍点的个数

7-10 两数之和

7-11 字典合并

 


7-1 输出星期名缩写

  1. box = {1: 'Mon', 2: 'Tue', 3: 'Wed', 4: 'Thu', 5: 'Fri', 6: 'Sat', 7: 'Sun'}
  2. key = int(input())
  3. print(box[key])

7-2 图的字典表示

  1. N = int(input())
  2. roadNum = 0
  3. sumLength = 0
  4. for index in range(0, N):
  5. dict1 = eval(input()) # 读取第一层字典
  6. for z in dict1:
  7. dict2 = dict1[z] # 读取第二层字典
  8. for z1 in dict2: # 遍历字典
  9. # print("%c %d" % (z1,dict2[z1]))
  10. sumLength += dict2[z1]
  11. roadNum += 1
  12. print("%d %d %d" % (N, roadNum, sumLength))

7-3 四则运算(用字典实现)

  1. dict1 = {'+': 'a+b', '-': 'a-b', '*': 'a*b', '/': 'a/b'}
  2. a = int(input())
  3. op = input()
  4. b = int(input())
  5. if op == '/' and b == 0:
  6. print("divided by zero")
  7. else:
  8. result = eval(dict1[op])
  9. print("%.2f" % result)

7-4 分析活动投票情况

  1. nums = [int(x) for x in input().split(',')]
  2. ticket = [0 for i in range(0, 11)]
  3. for x in nums:
  4. ticket[x] += 1
  5. output = 0
  6. for index in range(6, 11):
  7. if ticket[index] == 0:
  8. if output == 1:
  9. print(' ', end='')
  10. output = 1
  11. print(index, end='')

7-5 统计字符出现次数

  1. str1 = input()
  2. key = input()
  3. result = 0
  4. for index in str1:
  5. if index == key:
  6. result += 1
  7. print(result)

7-6 统计工龄

  1. N = int(input())
  2. key = [0 for z in range(0, 51)]
  3. nums = [int(x) for x in input().split()]
  4. for index in nums:
  5. key[index] += 1
  6. for index in range(0, 51):
  7. if key[index] > 0:
  8. print("%d:%d" % (index, key[index]))

7-7 列表去重

  1. list1 = eval(input())
  2. dict1 = {}
  3. for index in list1:
  4. if index not in dict1:
  5. dict1[index] = 0
  6. output = 0
  7. for index in dict1:
  8. if output == 1:
  9. print(' ', end='')
  10. print(index, end='')
  11. output = 1

7-8 能被3,5和7整除的数的个数(用集合实现)

  1. n, m = map(int, input().split())
  2. print(int(m/105)-int(n/105))
  1. a, b = map(int, input().split())
  2. f3 = set()
  3. f5 = set()
  4. f7 = set()
  5. for num in range(a, b + 1):
  6. if num % 3 == 0:
  7. f3.add(num)
  8. if num % 5 == 0:
  9. f5.add(num)
  10. if num % 7 == 0:
  11. f7.add(num)
  12. print(len(f3 & f5 & f7))

7-9 求矩阵鞍点的个数

  1. n = int(input())
  2. box = []
  3. for z in range(0, n):
  4. line = [int(x) for x in input().split()]
  5. box.append(line)
  6. # 找到改行最大的数
  7. def lineMAX(lineNum):
  8. maxNum = 0
  9. for index1 in range(1, n):
  10. if box[lineNum][index1] > box[lineNum][maxNum]:
  11. maxNum = index1
  12. return box[lineNum][maxNum]
  13. # 找到该列最小的数
  14. def rankMin(rankNum):
  15. minNum = 0
  16. for index2 in range(1, n):
  17. if box[index2][rankNum] < box[minNum][rankNum]:
  18. minNum = index2
  19. return box[minNum][rankNum]
  20. result = 0
  21. for z1 in range(0, n):
  22. for z2 in range(0, n):
  23. if box[z1][z2] == lineMAX(z1) == rankMin(z2):
  24. result += 1
  25. print(result)

7-10 两数之和

  1. nums = [int(x) for x in input().split(',')]
  2. key = int(input())
  3. flag = 0
  4. for z in range(0,len(nums)):
  5. for z1 in range(z,len(nums)):
  6. if nums[z] + nums[z1] == key:
  7. print("%d %d" % (z,z1))
  8. flag = 1
  9. if flag == 0:
  10. print("no answer")

7-11 字典合并

  1. dict1 = eval(input())
  2. dict2 = eval(input())
  3. for index in dict2:
  4. if index in dict1:
  5. dict1[index] += dict2[index]
  6. else:
  7. dict1[index] = dict2[index]
  8. result = sorted(dict1.items(), key=lambda item: item[0] if type(item[0]) == int else ord(item[0]))
  9. print(str(dict(result)).replace(' ', '').replace("'", '"'))
  10. # 网上找到的说明,说实话这题如果用队列+字典模拟这个过程实在是太麻烦了
  11. # for key,value in b.items():
  12. # a[key] = a.get(key,0)+value
  13. # #get方法语法:dict.get(key,default=None),key-字典中要查找的值,default-如果指定键的值不存在时,返回该默认值
  14. # d = sorted(a.items(),key = lambda item:item[0] if type(item[0])==int else ord(item[0]))
  15. # #lambda是匿名函数,lambda函数实现的主要功能是:如果是数字正常比较合并即可,如果是字母的话要转ascii码值然后再比较
  16. # out = str(dict(d)).replace(' ','').replace("'",'"')
  17. # #将得到的字典d按照指定格式进行改造
  18. # print(out)

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/553684
推荐阅读
相关标签
  

闽ICP备14008679号