使用Python解决简单问题
自主编写并运行代码,按照模板要求撰写实验报告
本次实验共有5题:
import itertools
count = 0
for arr in itertools.permutations('1234', 3):
# print(arr)
print(int(arr[0]) * 100 + int(arr[1]) * 10 + int(arr[2]))
count = count + 1
print('共有' + str(count) + '个组合')
import itertools
count = 0
for arr in itertools.permutations('2379', 3):
# print(arr)
print(int(arr[0]) * 100 + int(arr[1]) * 10 + int(arr[2]))
count = count + 1
print('共有' + str(count) + '个组合')
import math
count = 0
res = []
def check(x):
if x <= 1:
return False
for flag in range(2, int(math.sqrt(x) + 1)):
if x % flag == 0:
return False
return True
for i in range(1000, 2001):
if check(i):
res.append(i)
count = count + 1
for i in range(0, len(res)):
print(res[i], end=' ')
if ((i + 1) % 10) == 0:
print('\n')
print('\n1000~2000有素数' + str(count) + '个')
def func(x):
arr = str(x)
if res(arr) == x:
return True
else:
return False
def res(arr):
return pow(int(arr[0]), 4) + pow(int(arr[1]), 4) + pow(int(arr[2]), 4) + pow(int(arr[3]), 4)
for i in range(1000, 10000):
if func(i):
print(i)
import re
count_n = 0
count_s = 0
count_l = 0
count_o = 0
'''
string = 'Some people, when confronted with a problem, ' \
'think “I know, I’ll use regular expressions.” ' \
'Now they have two problems.'
'''
string = input()
number = re.finditer(r'\d+', string)
for match in number:
count_n = count_n + 1
print('数字的数量是' + str(count_n))
space = re.finditer(r'\s+', string)
for match in space:
count_s = count_s + 1
print('空白的数据是' + str(count_s))
letter = re.finditer(r'[a-zA-Z]', string)
for match in letter:
count_l = count_l + 1
print('字符的数量是' + str(count_l))
print('其他字符的数量是' + str(len(string) - count_s - count_l - count_n))
import numpy as np
import itertools
count = 0
form = np.empty([81, 2], int)
for arr in itertools.product('123456789', repeat=2):
form[count][0] = arr[0]
form[count][1] = arr[1]
count = count + 1
print('打印99乘法表:')
left = 0
count = 1
for i in range(0, 9):
for j in range(left, left + count):
string = str(form[j][0]) + '*' + str(form[j][1]) + '=' + str(int(form[j][0]) * int(form[j][1]))
print('%-8s' % string, end='')
print("\n")
left = left + 9
count = count + 1
Python标准库和第三库众多,功能强大。充分利用库函数来简化和加速代码、不重复造轮子能够显著简化代码提高编码效率