当前位置:   article > 正文

【头歌-Python】Python第五章作业(初级)_头歌-python】python第五章作业(初级)

头歌-python】python第五章作业(初级)

第1关:回文素数

  1. def is_prime(num):
  2. if num < 2:
  3. return False
  4. for i in range(2, int(num**0.5) + 1):
  5. if num % i == 0:
  6. return False
  7. return True
  8. def is_palindrome(num):
  9. return str(num) == str(num)[::-1]
  10. def generate_palindrome_primes(n):
  11. palindrome_primes = []
  12. num = 2
  13. while len(palindrome_primes) < n:
  14. if is_prime(num) and is_palindrome(num):
  15. palindrome_primes.append(num)
  16. num += 1
  17. return palindrome_primes
  18. if __name__ == "__main__":
  19. n = int(input())
  20. result = generate_palindrome_primes(n)
  21. print(" ".join(map(str, result)))

第2关:反素数

  1. def is_prime(num):
  2. if num < 2:
  3. return False
  4. for i in range(2, int(num**0.5) + 1):
  5. if num % i == 0:
  6. return False
  7. return True
  8. def is_palindrome(num):
  9. if str(num) == str(num)[::-1]:
  10. return False
  11. return True
  12. def generate_palindrome_primes(n):
  13. palindrome_primes = []
  14. num = 2
  15. while len(palindrome_primes) < n:
  16. num1 = int(str(num)[::-1])
  17. if is_prime(num) and is_palindrome(num)and is_prime(num1):
  18. palindrome_primes.append(num)
  19. num += 1
  20. return palindrome_primes
  21. if __name__ == "__main__":
  22. n = int(input())
  23. result = generate_palindrome_primes(n)
  24. print(" ".join(map(str, result)))

第3关:哥德巴赫猜想

  1. def is_prime(n):
  2. """判断素数的函数,接收一个正整数为参数,参数是素数时返回True,否则返回False"""
  3. if n < 2:
  4. return False
  5. for i in range(2, int(n ** 0.5) + 1):
  6. if n % i == 0:
  7. return False
  8. return True
  9. def goldbach_conjecture(num):
  10. """ 哥德巴赫猜想, 接收一个不小于4的正整数为参数。
  11. 当参数为不小于4的偶数时,将其分解为两个素数的加和,按小数+数的格式输出。
  12. 有多种组合时全部输出,但不输出重复的组合,例如输出8=3+5,不输出8=5+3。
  13. 参数为奇数或小于4时,输出'Data error!'
  14. """
  15. if num % 2 != 0 or num < 4:
  16. print('Data error!')
  17. a = int(num/2)+1
  18. for i in range(1, a):
  19. if is_prime(i) and is_prime(num - i):
  20. print('{}={}+{}'.format(num, i, num - i))
  21. if __name__ == '__main__':
  22. positive_even = int(input()) # 输入一个正数
  23. goldbach_conjecture(positive_even)

第4关:货币转换

  1. a = input()
  2. b = float(input())
  3. str1=''
  4. if a[- 1] != '¥' and a[- 1] != '$' or b < 0.0:
  5. print('Data error!')
  6. elif a[- 1] == '$':
  7. for i in a[:-1]:
  8. if i != ' ':
  9. str1 = str1 + i
  10. str1 = float(str1)
  11. money = float(b * str1)
  12. print('{:.2f}¥'.format(money))
  13. elif a[- 1] == '¥':
  14. for i in a[:-1]:
  15. if i != ' ':
  16. str1 = str1 + i
  17. str1 = float(str1)
  18. money = float(str1 / b)
  19. print('{:.2f}$'.format(money))

第5关:个人信息提取

  1. n=input()
  2. t=n.split(" ")
  3. print('姓名:'+t[1])
  4. print('班级:'+t[2])
  5. j = 0
  6. for i in t[4]:
  7. if i=='年':
  8. break
  9. j = j+1
  10. print('出生:'+t[4][j-4:j+1])

第6关:身份证号基本信息

  1. a = input()
  2. str1 = a[6:10]
  3. str2 = a[10:12]
  4. str3 = a[12:14]
  5. num = int(a[16])
  6. print('出生:'+str1+'年'+str2+'月'+str3+'日')
  7. if num%2==0:
  8. print('性别:女')
  9. else:
  10. print('性别:男')

第7关:各位数字之和为5的数

  1. n = int(input())
  2. for i in range(0, n + 1):
  3. k = i
  4. a = k % 10 + int(k / 10) % 10 + int(k / 100) % 10
  5. if a == 5:
  6. print(i, end=" ")

第8关:字符串长度

print(len(input()))

第9关:字符串加密

  1. import string
  2. a = input()
  3. lower = string.ascii_lowercase
  4. upper = string.ascii_uppercase
  5. before = string.ascii_letters
  6. after = lower[3:] + lower[:3] + upper[5:] + upper[:5]
  7. table = "".maketrans(before, after)
  8. print(a.translate(table))

第10关:输出单词

  1. a = input()
  2. t = a.split(" ")
  3. for i in t:
  4. print(i)

第11关:字符大小写转换

  1. n =input()
  2. str1 = n.swapcase()
  3. print(str1)

第12关:查找指定字符

  1. a = input()
  2. b =input()
  3. index = b.find(a)
  4. if index == -1:
  5. print('Not Found')
  6. else:
  7. print('index = {}'.format(index))

第13关:随机密码生成器

  1. import random
  2. import string
  3. n,s = map(int,input().split(','))
  4. random.seed(s)
  5. all_chars = string.digits + string.ascii_letters + '!"#$%&\\()*+,-.'
  6. password = ''.join(random.choice(all_chars) for _ in range(n))
  7. print(password)

第14关:模拟生成微软序列号

  1. import random
  2. import string
  3. n = int(input())
  4. s = int(input())
  5. random.seed(s)
  6. all_chars = r'BCEFGHJKMPQRTVWXY2346789'
  7. for i in range(n):
  8. password = ''.join(random.choice(all_chars) for _ in range(5)) + '-' + ''.join(
  9. random.choice(all_chars) for _ in range(5)) + '-' + ''.join(
  10. random.choice(all_chars) for _ in range(5)) + '-' + ''.join(
  11. random.choice(all_chars) for _ in range(5)) + '-' + ''.join(random.choice(all_chars) for _ in range(5))
  12. print(password)

第15关:数列求和

  1. a = int(input())
  2. b = int(input())
  3. if a>=1 and a<=9 and b>=0:
  4. sum = 0
  5. k = a
  6. if b ==0:
  7. print('0')
  8. else:
  9. for i in range(1,b+1):
  10. sum = sum + k
  11. k=k*10+a
  12. print(sum)
  13. else:
  14. print('data error')

第16关:单词个数

  1. a = input()
  2. num=0
  3. for i in a:
  4. if i==" ":
  5. num = num+1
  6. print(num+1)

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

闽ICP备14008679号