Skip to content

Commit c912c11

Browse files
committed
1. Modify nq2 and nq3(float map for input) of Newcoder.
2. Add Espressif 2020_Algorithm and 2020 Soft. Signed-off-by: Jackey <smjjackey@sina.com>
1 parent 9c1d8ee commit c912c11

File tree

6 files changed

+348
-3
lines changed

6 files changed

+348
-3
lines changed

Espressif/2020_Algorithm/e1.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
"""
2+
在证明数素无穷性时,使用了一个表达式 N=2*3*5*7*11…….*P + 1,其中 P 为一个素数,N 是 2 到 P 中所有素数的乘积加 1,
3+
若 P 为最大的素数,可以反证出 N 也是素数,从而证明素数是无穷多的。
4+
但有人因此认为,所有的 N 都是素数。如N0 = 3 为 素数,N1 = 7 为素数,N2 = 31 为素数。请判断第 i 个 N 是否为素数。
5+
6+
In: 每组输入只有一行,包含一个整数i(0 <= i <= 14),表示要检查的是第i个N。
7+
Out: 输出只有一行,若Ni为素数,打印“Ni is a prime”,否则打印“Ni is not a prime”。
8+
9+
e.g.
10+
In: 1
11+
Out: 7 is a prime
12+
"""
13+
import math
14+
def _odd_iter():
15+
n = 1
16+
while True:
17+
n = n + 2
18+
yield n
19+
20+
def _not_divisible(n):
21+
"""
22+
:param n: known number, x is the element of the sequence
23+
:return: bool
24+
"""
25+
return lambda x: x % n > 0
26+
27+
def primes():
28+
yield 2
29+
it = _odd_iter()
30+
while True:
31+
n = next(it)
32+
yield n
33+
it = filter(_not_divisible(n), it)
34+
35+
def fun(i):
36+
# prime_list=iter(primes())[:i]
37+
prime_list=[]
38+
for num in primes():
39+
prime_list.append(num)
40+
if(len(prime_list)==i+1): break
41+
# print(prime_list)
42+
prod=1
43+
for num in prime_list:
44+
prod=prod*num
45+
n=prod+1
46+
flag=False
47+
for j in range(2,int(math.sqrt(n))+1):
48+
if(n%j==0):
49+
flag=True
50+
break
51+
if(flag): print(str(n)+" is not a prime")
52+
else: print(str(n)+ " is a prime")
53+
54+
if __name__ == "__main__":
55+
i= int(input().strip())
56+
fun(i)

Espressif/2020_Algorithm/e2.py

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
"""
2+
求矩阵像素的最大连通域,像素为1的pixel周围8*8的像素为1的pixel都是它的邻接点,连接性具有传递性,pixel a与
3+
pixel b连通,b与c连通,则a与c连通,所以找到邻接点后再找邻接点的邻接点。
4+
试找出一个二值矩阵的所有连通域(8邻接),并给出每个连通域的面积(邻接点的个数)和重心。
5+
6+
每组输入包括 M+1 行,第一行输入2个整数 M (1<M<100), N (1<N<100),其中M是矩阵的行数,N是矩阵的列数。
7+
第2至M+1行,每行 N 个整数,表示在矩阵N列的像素值(已二值化为 0 和 1, 连通域为 1 表示的区域)。
8+
9+
输出 K+1 行,第一行输出连通域个数K,第2至 K+1 行,每行输出3个数,依次表示为连通域的面积值和重心的坐标值(保留2位小数点),
10+
按照连通域起始点顺序输出。
11+
e.g. Input:
12+
4 4
13+
0 1 0 0
14+
0 0 0 1
15+
0 0 0 1
16+
1 0 0 0
17+
Output:
18+
3
19+
1 1.00 0.00
20+
2 3.00 1.50
21+
1 0.00 3.00
22+
"""
23+
24+
"""
25+
# wrong understanding
26+
import operator
27+
def fun():
28+
result=[]
29+
dirs=[(-1,0),(1,0),(0,-1),(0,1),(-1,-1),(1,-1),(-1,1),(1,1)]
30+
for i in range(1,row-1):
31+
for j in range(1,col-1):
32+
cor=[]
33+
for dx,dy in dirs:
34+
if (num_list[i+dx][j+dy] == 1):
35+
cor.append((i+dx,j+dy))
36+
# area=sum(list(num_list[i-1,j],num_list[i+1,j],
37+
# num_list[i, j - 1],num_list[i, j+1],
38+
# num_list[i - 1, j - 1],num_list[i - 1, j+1],
39+
# num_list[i + 1, j - 1],num_list[i + 1, j+1]))
40+
# print("cor",cor)
41+
if len(cor)==1 or all(is_connect(cor,t1) for t1 in cor):
42+
x_sum=0
43+
y_sum=0
44+
for t in cor:
45+
x,y=t
46+
x_sum+=x
47+
y_sum+=y
48+
# mid_x = round(x_sum /len(cor),2)
49+
# mid_y = round(y_sum / len(cor),2)
50+
mid_x = '%.2f'% (x_sum / len(cor))
51+
mid_y = '%.2f'% (y_sum / len(cor))
52+
result.append((len(cor),mid_y,mid_x))
53+
54+
print(len(result))
55+
for ele in result:
56+
# area,mid_x,mid_y=ele
57+
print(ele[0],ele[1],ele[2])
58+
59+
def is_connect(cor,t1):
60+
x1,y1=t1
61+
for t in cor:
62+
if(not operator.eq(t,t1)):
63+
x,y=t
64+
if(x1==x or y1==y):
65+
return True
66+
break
67+
return False
68+
"""
69+
70+
def fun2():
71+
count=0
72+
result=[]
73+
for i in range(row):
74+
for j in range(col):
75+
if(num_list[i][j]):
76+
x_sum,y_sum,num=dfs(i,j,used,0,0,0)
77+
count+=1
78+
result.append((num,y_sum/num,x_sum/num))
79+
print(count)
80+
for res in result:
81+
# print("%d %.2f %.2f" % (res[0],res[1],res[2]))
82+
print("{} {:.2f} {:.2f}".format(res[0],res[1],res[2]))
83+
84+
dirs = [(-1, 0), (1, 0), (0, -1), (0, 1), (-1, -1), (1, -1), (-1, 1), (1, 1)]
85+
86+
def dfs(i, j, used,x_sum, y_sum, num):
87+
num_list[i][j]=0 ###### if belongs to other's adjacency, don't need to count its adjacency
88+
x_sum, y_sum= x_sum + i, y_sum + j
89+
num+=1
90+
for ix,iy in dirs:
91+
if(0<=i+ix<row and 0<=j+iy<col and num_list[i+ix][j+iy] and not used[i+ix][j+iy]):
92+
used[i+ix][j+iy]=1
93+
x_sum, y_sum, num=dfs(i + ix, j + iy, used,x_sum, y_sum, num) #### or add (i+ix,y+iy) to a list
94+
return x_sum, y_sum, num
95+
96+
97+
if __name__ == "__main__":
98+
row,col= map(int,input().strip().split())
99+
num_list=[]
100+
for i in range(row):
101+
num_list.append(list(map(int,input().strip().split())))
102+
# print(num_list)
103+
used = [[0 for j in range(col)] for i in range(row)]
104+
fun2()

Espressif/2020_Soft/es1.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import math
2+
def root(n,num):
3+
incre=0.005
4+
i=1
5+
while(i<num):
6+
prod=1
7+
for j in range(n): # i multiply n times
8+
prod=prod*i
9+
if(prod>=num):
10+
n_root=i-incre
11+
break
12+
i+=incre
13+
print(n_root)
14+
15+
def root2(n,num):
16+
n_root=math.exp(1/n * math.log(num))
17+
print(n_root)
18+
19+
def root3(n,num):
20+
acc=0.001
21+
# if(num>=1):
22+
# low=1
23+
# high=num
24+
# else:
25+
# low=num
26+
# high=1
27+
low=0
28+
high=num
29+
while(abs(low-high)>=10**-16):
30+
mid=(low+high)/2.0
31+
# prod=1
32+
# for j in range(n): # mid multiply n times
33+
# prod=prod*mid
34+
prod=mid**n
35+
if(abs(prod-num)<=acc):
36+
break
37+
if(prod>=num): high=mid
38+
else: low=mid
39+
40+
print("%.12f"%mid)
41+
42+
if __name__ == "__main__":
43+
num,n=map(int,input().strip().split())
44+
root3(n,num)

Espressif/2020_Soft/es2.py

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
"""
2+
8 7 3 6
3+
0 1
4+
1 2
5+
2 3
6+
0 3
7+
4 5
8+
5 6
9+
4 7
10+
"""
11+
class Graph():
12+
def __init__(self,nodes,sides):
13+
'''
14+
nodes 表示点, list
15+
sides 表示边(两个点组成的边), a list of tuple
16+
'''
17+
# self.sequence是字典,key是node,value是与key相连接的点(是list)
18+
self.sequence = {}
19+
# self.side是临时变量,主要用于保存与指定点相连接的点
20+
self.side=[]
21+
for node in nodes:
22+
for side_tuple in sides: ## side_tuple is a tuple
23+
u,v=side_tuple
24+
# 指定点与另一个点在同一个边中,则说明这个点与指定点是相连接的点,则需要将这个点放到self.side中
25+
if node == u:
26+
self.side.append(v)
27+
elif node == v:
28+
self.side.append(u)
29+
self.sequence[node] = self.side
30+
self.side=[]
31+
32+
# def count(self):
33+
# """
34+
# wrong method, which might contains the duplicated node
35+
# :return:
36+
# """
37+
# sides_list=[]
38+
# print(self.sequence)
39+
# combine_list=[]
40+
# temp=[]
41+
# for k, v in self.sequence.items():
42+
# combine_list=v.copy() ######
43+
# combine_list.append(k)
44+
# flag=False
45+
# for i in range(len(sides_list)):
46+
# # if k in sides_list[i]:
47+
# # flag=True
48+
# # break
49+
# if(any(ele in sides_list[i] for ele in combine_list)):
50+
# flag=True
51+
# break
52+
# if(flag):
53+
# # temp=list(sides_list[i]).extend(combine_list) ###### temp=None
54+
# # print("temp",temp)
55+
# temp=list(sides_list[i])
56+
# temp.extend(combine_list)
57+
# print("temp",temp)
58+
# sides_list[i]=temp.copy()
59+
# print("sides_list[i]",sides_list[i])
60+
# sides_list[i]=set(sides_list[i])
61+
# print("sides_list[i]_2",sides_list[i])
62+
# else:
63+
# sides_list.append(combine_list)
64+
# # v.append(k)
65+
# # sides_list.append(v)
66+
# # if(set(v) & set(sides_list[i])!=None):
67+
# # v+sides_list[i]
68+
# # i=+1
69+
#
70+
# sides_t_list=[]
71+
# split_list=[]
72+
# for sides in sides_list:
73+
# sides_t= set(sides)
74+
# if(len(sides_t)==1):
75+
# split_list.append(sides_t)
76+
# else:
77+
# sides_t_list.append(sides_t)
78+
# print(sides_t_list)
79+
# print(split_list)
80+
# cost=2*b+b*(len(sides_t_list)+len(split_list)-1)+a*len(sides_t_list)
81+
# return cost
82+
83+
def DFS(self, node0):
84+
# queue本质上是栈,用来存放需要进行遍历的数据
85+
# order里面存放的是具体的访问路径
86+
queue, order = [], []
87+
# 首先将初始遍历的节点放到queue中,表示将要从这个点开始遍历
88+
queue.append(node0)
89+
while queue:
90+
# 从queue中pop出点v,然后从v点开始遍历了,所以可以将这个点pop出,然后将其放入order中
91+
# 这里才是最有用的地方,pop()表示弹出栈顶,由于下面的for循环不断的访问子节点,并将子节点压入堆栈,
92+
# 也就保证了每次的栈顶弹出的顺序是下面的节点
93+
v = queue.pop() #####LIFO
94+
order.append(v)
95+
# 这里开始遍历v的子节点
96+
for w in self.sequence[v]: #####
97+
# w既不属于queue也不属于order,意味着这个点没被访问过,所以将其放到queue中,然后后续进行访问
98+
if w not in order and w not in queue:
99+
queue.append(w)
100+
return order
101+
102+
if __name__ == "__main__":
103+
n,m,a,b = map(int,input().strip().split())
104+
nodes = [i for i in range(n)]
105+
# print(nodes)
106+
sides=[]
107+
for i in range(m):
108+
t= set(map(int,input().strip().split()))
109+
sides.append(t)
110+
# print(sides)
111+
112+
g=Graph(nodes,sides)
113+
# cost=g.count()
114+
# print(cost)
115+
side_list=[]
116+
side_list_all=[]
117+
for node in nodes:
118+
side_list=g.DFS(node)
119+
# print(side_list)
120+
side_list_all.append(tuple(sorted(side_list)))
121+
122+
# print(side_list_all)
123+
cnt=1
124+
dict_con={}
125+
for i in range(len(side_list_all)-1):
126+
if(side_list_all[i] not in dict_con):
127+
dict_con.update({side_list_all[i]:1})
128+
else:
129+
dict_con[side_list_all[i]]+=1
130+
cnt=len(dict_con.keys())
131+
print(len(side_list_all),cnt)
132+
cost = 2 * b + b * (cnt-1) + a * cnt
133+
print(cost)
134+
135+
136+
137+
138+
139+
140+
141+

Newcoder/nq2.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,5 +55,5 @@ def fun2(values):
5555
if __name__ == "__main__":
5656
n = int(sys.stdin.readline().strip())
5757
line = sys.stdin.readline().strip()
58-
values = list(map(int, line.split()))
58+
values = list(map(float, line.split())) ###float
5959
fun(values)

Newcoder/nq3.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
Output: The number of longest increasing subsequence
66
In: 6
77
7 2 3 1 5 6
8-
Output: 5
8+
Output: 5 - {2 3 1 5 6}
99
1010
In: 5
1111
10 3 10 5 7
12-
Output: 4
12+
Output: 4 - {3 10 5 7}
1313
"""
1414
import sys
1515
def fun(values):

0 commit comments

Comments
 (0)