发布时间:2019-09-21 11:04:12编辑:auto阅读(1597)
prime = [2]
for i in range(3,100000,2):
flag = False
up = int(i**0.5)+1
for j in prime:
if i % j == 0:
flag = False
break
if j >= up:
flag = True
break
if flag:
prime.append(i)
print(len(prime))
n=6
pre=[[1]]
for i in range(n):
line=pre[i]
temp=[1]
for j in range(i):
temp.append(line[j]+line[j+1])
temp.append(1)
pre.append(temp)
print(pre)
n = 6
pre = [1]
for i in range(n):
temp = [1]
for j in range(i):
temp.append(pre[j] + pre[j + 1])
temp.append(1)
print(pre)
pre = temp
n=6
pre=[1]
for i in range(1,n):
pre.append(0)
newline=[]
for j in range(i+1):
var=pre[j-1]+pre[j]
newline.append(var)
print(newline)
pre=newline
n=6
triangle=[]
for i in range(n):
temp=[1]*(i+1)
for j in range(1,i//2+1):
var=triangle[j-1]+triangle[j]
temp[j]=var
if i!=2*j:
temp[-j-1]=var
triangle=temp
print(temp)
n = 6
triangle = []
for i in range(n):
temp = [1] * (i + 1)
triangle.append(temp)
for j in range(1,i // 2 + 1):
var = triangle[i - 1][j - 1] + triangle[i - 1][j]
temp[j] = var
if i != 2 * j:
temp[- j - 1] = var
print(temp)
pre = temp
n = 6
triangle = [1] * n
for i in range(n):
temp = 1
for j in range(1,i // 2 + 1):
var = temp + triangle[j]
temp = triangle[j]
triangle[j] = var
if i != j * 2:
triangle[- j - (n - i)] = var
print(triangle[: i + 1])
lst=[9,8,7,6,5,4,3,2,1]
cnt=0
cnt_swap=0
for i in range(len(lst)-1):
for j in range(len(lst)-i-1):
cnt+=1
if lst[j]>lst[j+1]:
lst[j],lst[j+1] = lst[j+1],lst[j]
cnt_swap+=1
print(lst,cnt,cnt_swap)
#lst = [9,8,7,6,5,4,3,2,1]
#lst = [1,2,3,4,6,5,7,8,9]
lst = [2,1,3,4,5,6,7,8,9]
cnt = 0
cnt_swap = 0
for i in range(len(lst) - 1):
flag = False
for j in range(len(lst) - i - 1):
cnt += 1
if lst[j] > lst[j + 1]:
lst[j],lst[j + 1] = lst[j + 1],lst[j]
flag = True
cnt_swap += 1
if not flag:
break
print(lst,cnt,cnt_swap)
lst = [1, (2, 3, 4), 5]
_, (*_, a), _ = lst
print(a)
str = 'JAVA_HOME=usr/bin'
name, path = str.split('=')
print(name, path)
a = {'A', 'B', 'C'}
b = {'C', 'B', 'D'}
print(a & b)
XXX_friends = {'A', 'B', 'C'}
b = {'E', 'F', 'D'}
if not XXX_friends & b:
print('Warning! :XXX')
问该API
sys_auth = {'A', 'B', 'C'}
user_auth = {'B', 'C', 'D'}
if sys_auth <= user_auth : #sys_auth是B的子集
print('Pass!')
else :
print('Insuffitient auth!')
够访问该API
sys_auth = {'A', 'B', 'C'}
user_auth = {'B', 'C', 'D'}
if sys_auth & user_auth:
print('Pass!')
else :
print('Insuffitient auth!')
mission = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'}
completed_mission = {'C', 'E', 'G'}
print(mission - completed_mission)
from random import randint
a = []
b = []
for i in range(20):
if i > 9:
a.append(randint(10, 20))
else:
b.append(randint(10, 20))
print(a, b)
set_a = set(a)
set_b = set(b)
print('set_union: ', set_a | set_b)
print('set_minus: ', set_a & set_b)
print('set_intersect: ', set_a ^ set_b)
a = int(input('a')) - 1
b = int(input('b'))
mul_a = 1
mul_c = 1
for i in range(b - 1):
mul_a *= a
a -= 1
for j in range(1, b):
mul_c *= j
print(mul_a, mul_c, mul_a / mul_c)
n = int(input('>>>')) - 1
m = int(input('>>>')) - 1
r = n - m
l=[]
mul=1
for i in range(1, n + 1):
mul *= i
if i == m:
l.append(mul)
if i == r:
l.append(mul)
if i == n:
l.append(mul)
print(l[2] // (l[0] * l[1]))
matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
new_matrix = []
for i in range(len(matrix[0])):
temp = []
for j in range(len(matrix)):
temp.append(matrix[j][i])
new_matrix.append(temp)
print(new_matrix)
matrix = [[1, 2, 3], [4, 5, 6]]
tm = []
for row in matrix:
for i ,col in enumerate(row):
if len(tm) < i + 1:
tm.append([])
tm[i].append(col)
print(tm)
对于大矩阵速度更快
matrix = [[1, 2, 3], [4, 5, 6]]
tm = [[0 for col in range(len(matrix))] for row in range(len(matrix[0]))]
for i,row in enumerate(tm):
for j, col in enumerate(row):
tm[i][j] = matrix[j][i]
print(tm)
import random
lst = []
rep_lst = []
uniq_lst = []
for i in range(10):
temp = random.randint(1, 20)
if temp in lst and temp not in rep_lst:
rep_lst.append(temp)
lst.append(temp)
for j in lst:
if j not in rep_lst:
uniq_lst.append(j)
print(lst)
print(len(rep_lst), ': ', rep_lst)
print(len(uniq_lst), ': ', uniq_lst)
import random
nums = []
for _ in range(10):
nums.append(random.randrange(21))
print(nums)
length=len(nums)
samenums = []
diffnums = []
states=[0] * length
for i in range(length):
flag = False
if states[i] == 1:
continue
for j in range(i + 1, length):
if states[j] == 1:
continue
if nums[i] == nums[j]:
flag = True
states[j] = 1
if flag:
samenums.append(nums[i])
states[i] = 1
else:
diffnums.append(nums[i])
print('{}\n{}'.format(samenums, diffnums))
上一篇: Makefile入门(3)
下一篇: GNS3的使用心得
47838
46382
37270
34724
29310
25967
24898
19945
19536
18017
5787°
6407°
5923°
5958°
7061°
5906°
5938°
6434°
6401°
7773°