Skip to content

Commit cdbb74f

Browse files
committed
Modify the q1 and prime_number file bugs.
Signed-off-by: Jackey <smjjackey@sina.com>
1 parent 00ec288 commit cdbb74f

File tree

2 files changed

+20
-8
lines changed

2 files changed

+20
-8
lines changed

Huawei/2019/q1.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,25 @@
11
"""
22
Accept a line of string set split by comma, the number of string <1000, pick the un-repeated prime number,
33
sort ascending,output them; if no prime number,output "empty"
4+
e.g.
5+
1,2,3,3,5,7,9
46
"""
57
import math
68
def fun():
79
input_num_list=list(map(int,input().strip().split(',')))
810
out=set()
911
for num in input_num_list:
10-
if(all(num%i !=0 for i in range(2,int(math.sqrt(num))+1))):
12+
if(num !=1 and all(num%i !=0 for i in range(2,int(math.sqrt(num))+1))):
1113
##### Return True if all elements of the iterable are true (or if the iterable is empty)
12-
##### So, if the input_num_list is {1,2}, the result is wrong
14+
##### So, if the input_num_list contain {1}, the result is wrong
1315
out.add(num)
16+
# flag= True
17+
# for i in range(2,int(math.sqrt(num))+1):
18+
# if (num%i):
19+
# flag= False
20+
# break
21+
# if(flag and num!=1): out.add(num)
22+
1423
out=sorted(out)
1524
if(len(out)==0):
1625
print("empty")
@@ -22,7 +31,7 @@ def fun2():
2231
input_num_list=list(map(int,input().strip().split(',')))
2332
out=[]
2433
for num in input_num_list:
25-
if(all(num%i !=0 for i in range(2,int(math.sqrt(num))+1))):
34+
if(num !=1 and all(num%i !=0 for i in range(2,int(math.sqrt(num))+1))):
2635
out.append(num)
2736
out=sorted(set(out))
2837
if(len(out)==0):

prime_number.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,25 @@ def generate_prime():
33
n=100
44
pri_num_list=[]
55
for num in range(2, n + 1): # the range of candidate numbers
6-
if all(num % j != 0 for j in range(2, int(math.sqrt(num))+1)): # Try divide 2 to sqrt(num)
6+
if all(num % j != 0 for j in range(2, int(math.sqrt(num))+1)):
7+
# Try divide 2 to sqrt(num)
8+
##### Actually, for num=2 or num=3, inner loop does not do, the all returns true
9+
##### because if the iterable is empty, all still return true,but 2,3 are prime
710
pri_num_list.append(num)
811

912
print(len(pri_num_list),pri_num_list)
1013

1114
def generate_prime1():
1215
n=100
13-
pri_num_list = [num for num in range(2, n + 1) if all(num % j != 0 for j in range(2, int(math.sqrt(num)) + 1))]
16+
pri_num_list = [num for num in range(2, n + 1) if all(num % j != 0 for j in range(2, int(math.sqrt(num)+0.5) + 1))]
1417
print(len(pri_num_list), pri_num_list)
1518

1619
def generate_prime2():
1720
n = 100
1821
pri_num_list = []
1922
for num in range(2,n+1): # the range of candidate numbers
2023
flag = True
21-
for j in range(2, int(math.sqrt(num)+1)): # Try divide 2 to sqrt(num)
24+
for j in range(2, int(math.sqrt(num)+0.5)+1): # Try divide 2 to sqrt(num)
2225
if(num%j==0):
2326
flag=False
2427
break
@@ -32,15 +35,15 @@ def generate_prime3():
3235
for num in range(2, n + 1):
3336
flag1 = True
3437
for j in range(2, int(math.sqrt(num) + 1)): # Try divide 2 to sqrt(num)
35-
flag2=True
38+
flag2= True
3639
### check whether j is a prime, if j is not a prime, continue,
3740
# because if the number can be divided by a not prime, it must can be divided by the prime
3841
# number between 2~10 i.e. 2,3,5,7###
3942
for k in range(2, int(math.sqrt(j) + 1)):
4043
if (j % k == 0):
4144
flag2 = False # j is not a prime
4245
break
43-
if(flag2==False):
46+
if(not flag2):
4447
continue
4548
### check whether j is a prime ###
4649

0 commit comments

Comments
 (0)