字符串-数字-列表(转换)

 
  
  1. # -*- coding: utf-8 -*-
  2. #1. 编写函数,要求输入x与y,返回x和y的平方差
  3. # 方法一:
  4. def funa(x,y):
  5. a=x*x-y*y
  6. return a
  7. print funa(5,9)
  8. # 方法二:
  9. import math
  10. def funb(x,y):
  11. a=pow(x,2) - pow(y,2)
  12. return a
  13. print funb(5,9)
  14. #2. 计算1到100的平方的和
  15. # 方法一:
  16. b=0
  17. for i in range(0, 100):
  18. b +=pow(i,2)
  19. else:
  20. print 'The for loop is over'
  21. print b
  22. # 方法二:
  23. def f(x):
  24. return x * (x+1) * (2*x+1) / 6
  25. print f(100)
  26. # 方法三:
  27. print reduce(lambda x,y: x+y**2,range(1,101))
  28. # 方法四:
  29. #python中[... for ... in ...]的建list方式比较好用,配合能接收list的函数,能大大化简程序。如本例
  30. print sum([i**2 for i in range(101)])
  31. #3. 编写函数,若输入为小于100的数,返回TRUE,大于100的数,返回FALSE
  32. def func(x):
  33. if x>100:
  34. return 'false'
  35. if x<100:
  36. return 'true'
  37. else:
  38. return 'The number is 100!'
  39. print func(99.9)
  40. print func(189.1)
  41. print func(100)
  42. print func('a')
  43. #4. 某个公司采用公用电话传递数据,数据是四位的整数,在传递过程中是加密的,加密规则如下:
  44. #每位数字都加上5,然后用和除以10的余数代替该数字,再将第一位和第四位交换,第二位和第三位交换。
  45. def encrypt(x):
  46. if x.isdigit(): #判断字符串包含的是否都为数字
  47. if len(x)==4: #判断字符串是否是四位
  48. import string
  49. string.atoi(x[0]) #将字符串转化为数字
  50. a1=string.atoi(x[0])
  51. a2=string.atoi(x[1])
  52. a3=string.atoi(x[2])
  53. a4=string.atoi(x[3])
  54. a11=(a1+5)%10
  55. a22=(a2+5)%10
  56. a33=(a3+5)%10
  57. a44=(a4+5)%10
  58. r1='%d' %a11 #将数字转化为字符串
  59. r2='%d' %a22
  60. r3='%d' %a33
  61. r4='%d' %a44
  62. result=r4+r3+r2+r1
  63. return result
  64. else:
  65. return '请输入四位数字!'
  66. else:
  67. return '包含非数字,请输入四位数字!'
  68. print encrypt('0986')
  69. def decrypt(x):
  70. if x.isdigit(): #判断字符串包含的是否都为数字
  71. if len(x)==4: #判断字符串是否是四位
  72. import string
  73. string.atoi(x[0]) #将字符串转化为数字
  74. a1=string.atoi(x[0])
  75. a2=string.atoi(x[1])
  76. a3=string.atoi(x[2])
  77. a4=string.atoi(x[3])
  78. a11=(a1-5)%10
  79. a22=(a2-5)%10
  80. a33=(a3-5)%10
  81. a44=(a4-5)%10
  82. r1='%d' %a11 #将数字转化为字符串
  83. r2='%d' %a22
  84. r3='%d' %a33
  85. r4='%d' %a44
  86. result=r4+r3+r2+r1
  87. return result
  88. else:
  89. return '请输入四位数字!'
  90. else:
  91. return '包含非数字,请输入四位数字!'
  92. print decrypt('1345')
  93. #进化方法:
  94. def encrypt(x):
  95. if x.isdigit(): #判断字符串包含的是否都为数字
  96. if len(x)==4: #判断字符串是否是四位
  97. s = ''
  98. for i in range(len(x)):
  99. s += '%d' %((int(x[i]) + 5) % 10) #将数字转化为字符串
  100. return s[::-1] #切片,倒序
  101. else:
  102. return '请输入四位数字!'
  103. else:
  104. return '包含非数字,请输入四位数字!'
  105. print encrypt('0986')
  106. def decrypt(x):
  107. if x.isdigit(): #判断字符串包含的是否都为数字
  108. if len(x)==4: #判断字符串是否是四位
  109. s = ''
  110. for i in range(len(x)):
  111. s += '%d' %((int(x[i]) - 5) % 10) #将数字转化为字符串
  112. return s[::-1] #切片,倒序
  113. else:
  114. return '请输入四位数字!'
  115. else:
  116. return '包含非数字,请输入四位数字!'
  117. print decrypt('0986')





转载于:https://www.cnblogs.com/wumingxiaoyao/p/7027324.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值