练习1029

"""
题目描述:一个整数,它加上100后是一个完全平方数,再加上168又是一个完全平方数,请问该数是多少?
"""
i = []
j = []
for num in range(10000):
i.append(num**2-100)
j.append(num**2-268)
res = [x for x in i if x in j]
print(res)

结果:

[-99, 21, 261, 1581]

 

# 冒泡排序法正序

q = [10, 88, 36, 8, 90, 7, 75, 64]
for i in range(len(q)):
for j in range(len(q) - i - 1):
if q[j] > q[j + 1]:
temp = q[j + 1]
q[j + 1] = q[j]
q[j] = temp
print(q)

 

"""
题目描述:斐波那契数列,指的是这样一个数列:1、1、2、3、5、8、13、21、34、
"""
n=10
res = [0,1]
for i in range(n):
res.append(res[-1]+res[-2])
print(res)

结果:

[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]

 

"""
题目描述:输出 9*9 乘法口诀表
1x1=1
1x2=2 2x2=4
1x3=3 2x3=6 3x3=9
"""
x = list(range(10))
del x[0]
print(x)
for i in x:
for j in x[:i]:
print('%dx%d=%d' % (j, i, i * j), end=" ")
print('\n')

 

 

"""
题目描述:判断101-200之间有多少个素数,并输出所有素数
1,3,7,11,13,17,19....
"""

res = []
for i in range(101, 201):
flag = True
for j in range(2, i):
if i % j == 0:
flag = False
break
if flag:
res.append(i)
print('总共有素数:%d 个' % len(res))
print(res)

结果:

总共有素数:21 个

[101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199]

 

 

"""
题目描述:打印出所有的“水仙花数”,所谓“水仙花数”是指一个三位数,其各位数字立方和等于该数本身。
例如:153是一个“水仙花数”,因为153=1的三次方+5的三次方+3的三次方。
"""
res = []
for n in range(100, 1000):
# if (int(n / 100))**3 + (int((n % 100) / 10))**3 + ((n % 100) % 10)**3 == n:
# res.append(n)

#充分利用了转化为列表可以分片的功能,简单
i = str(n)
if int(i[0]) ** 3 + int(i[1]) ** 3 + int(i[2]) ** 3 == n:
res.append(n)
print(res)

结果:

 [153, 370, 371, 407]

 

 

"""
题目描述:输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。
"""
import string
x = 'jeht924tj9 &*)&^2435 ehtERh897*(+oedf周曼'
res = {'letter': [], 'digits': [], 'whitespace': [], 'punctuation': [], 'zh': []}
for i in x:
if i in string.ascii_letters:
res['letter'].append(i)
if i in string.digits:
res['digits'].append(i)
if i in string.whitespace:
res['whitespace'].append(i)
if i in string.punctuation:
res['punctuation'].append(i)
if '\u4e00' <= i <= '\u9fff':
res['zh'].append(i)

for key, value in res.items():
print(value, '%s count is: %d ' % (key, len(value)))

或:

for i in x:
if ord(i) in range(65, 91) or ord(i) in range(97, 123):
res['letter'].append(i)
elif ord(i) in range(48, 58):
res['digits'].append(i)
elif ord(i) is 32:
res['whitespace'].append(i)
else:
res['punctuation'].append(i)

结果:

['j', 'e', 'h', 't', 't', 'j', 'e', 'h', 't', 'E', 'R', 'h', 'o', 'e', 'd', 'f'] letter count is: 16
['9', '2', '4', '9', '2', '4', '3', '5', '8', '9', '7'] digits count is: 11
[' ', ' '] whitespace count is: 2
['&', '*', ')', '&', '^', '*', '(', '+'] punctuation count is: 8
['周', '曼'] zh count is: 2

 

"""
题目描述:有一分数序列:2/1,3/2,5/3,8/5,13/8,21/13…求出这个数列的前20项之和
(2+1)/2=3/2 (3+2)/3 (5+3)/5 (8+5)/8 (13+8)/13...
"""

x = 2
y = 1
res = 0
for i in range(20):
# y = x
# x=x+y
res += x / y
print('%d/%d' % (x, y))
x, y = x + y, x
print(res)

 

 

"""
题目描述:利用递归函数调用方式,将所输入的5个字符,以相反顺序打印出来。
"""


def r(s):
if len(s) < 2:
return s
return s[-1] + r(s[:-1])


print(r('tehks'))

结果:

skhet

 

"""
题目描述:去掉字符串中的非数字字母。
"""

 

import re
s = 'Onboard_/I210 -PXE'
res = re.sub(r'[^a-zA-Z\d]', '', s)
print(res)

结果:

OnboardI210PXE

 

"""
题目描述:判断字符串中没有数字,有捕获异常。
""

结果:

test error ...
<class 'AssertionError'>
***8888
('test error ...',)

 

posted @ 2022-03-04 10:29  zmm521  阅读(52)  评论(0)    收藏  举报