PAT-1152 Google Recruitment 解答(with python)

1.Description:

Now you are asked to solve a more general problem: find the first K-digit prime in consecutive digits of any given L-digit number.

 

2.Example:

Input:
20 5
23654987725541023819
Output:
49877
Input:
10 3
2468024680
Output:
404

3.solutions:

 1 """
 2     created by sheepcore on 2020-03-01
 3 """
 4 
 5 
 6 def isPrime(x):
 7     if x < 2:
 8         return False
 9     for n in range(2, int(x ** 0.5 + 1)):
10         if x % n == 0:
11             return False
12             break
13     else:
14         return True
15 
16 
17 def removeHeadZeros(x):
18     idx = 0
19     while x[idx] == '0':
20         idx += 1
21     return eval(x[idx:]), idx
22 
23 
24 if __name__ == "__main__":
25     length, bits = input().split()
26     num = input()
27     i = 0
28     while i <= eval(length) - eval(bits):
29         x, zeros = removeHeadZeros(num[i:i+eval(bits)])
30         if isPrime(x) is True:
31             if zeros == 0:
32                 print(x, end="")
33             else:
34                 print("0"*zeros + str(x), end="")
35             break
36         i += 1
37     if i > eval(length) - eval(bits):
38         print("404", end="")
原文地址:https://www.cnblogs.com/sheepcore/p/12391573.html