Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
TopCoder SRM 677 Div 2
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
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