Last active
December 27, 2015 03:05
-
-
Save whatalnk/af05aff96b75c0abdde9 to your computer and use it in GitHub Desktop.
TopCoder SRM 677 Div 2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import math | |
class PalindromePrime: | |
def isPrime(self, n): | |
if n == 1: | |
return False | |
elif n == 2: | |
return True | |
else: | |
lim = int(math.sqrt(n)) + 1 | |
for i in range(2, lim): | |
if n % i == 0: | |
return False | |
return True | |
def count(self, L, R): | |
res = 0 | |
for n in range(L, R+1): | |
if self.isPrime(n): | |
nstr = str(n) | |
if nstr == nstr[::-1]: | |
res += 1 | |
return res |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import itertools | |
class FourStrings: | |
def cnct(self, s1, s2): | |
if s2 in s1: | |
return s1 | |
else: | |
for i in range(1, len(s2)): | |
if s1.endswith(s2[:(i * -1):]): | |
return s1 + s2[(len(s2) - i)::] | |
return s1 + s2 | |
def shortestLength(self, a, b, c, d): | |
res = len(a + b + c + d) | |
for strlist in itertools.permutations([a, b, c, d]): | |
ss = strlist[0] | |
for i in range(1, 4): | |
ss = self.cnct(ss, strlist[i]) | |
res = min(res, len(ss)) | |
return res |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment