Skip to content

Instantly share code, notes, and snippets.

@parthpower
Last active March 9, 2016 20:29
Show Gist options
  • Save parthpower/80d0a2ba740142e873a4 to your computer and use it in GitHub Desktop.
Save parthpower/80d0a2ba740142e873a4 to your computer and use it in GitHub Desktop.
TCS CodeVita Solutions I don't remember problem statements:p
#Copyright (c) 2016 Parth V. Parikh
#Freely distributable under MIT lincese.
__author__ = 'Parth'
def alternate(num, target):
if int(num)<3:
return False
found = False
occ = []
current = -1
for i in range(len(num)):
current = num.find(target, current + 1)
if current == -1:
break
occ.append(current)
for i in range(len(occ)):
for j in range(len(occ)):
if occ[i] - occ[j] ==2:
found = True
return found
def Consecutive(num, target_freq, target):
if int(num) < int(target_freq):
return False
occ = []
current = -1
freq = 1
for i in range(len(num)):
current = num.find(target, current + 1)
if current == -1:
break
occ.append(current)
for i in range(len(occ)):
for j in range(len(occ)):
if occ[i] - occ[j] == 1:
freq += 1
if freq >= int(target_freq):
return True
else:
return False
def Wildcard(num, pattern):
if len(pattern)>len(num):
return False
prev = -1
current = 0
num = list(num)
while True:
current = pattern.find('W', prev + 1)
if current == -1:
break
num[current] = 'W'
prev = current
if num == list(pattern):
return True
else:
return False
N = int(input())
rang = input()
case = []
ans = []
for i in range(N):
tm = input()
case.append(tm)
ans.append(0)
L, U = rang.split()
for i in range(N):
for j in range(int(L), int(U)):
if case[i][0] == 'A':
if alternate(str(j), case[i][1::]):
ans[i] = ans[i] + 1
elif case[i][0] == 'C':
if Consecutive(str(j), case[i][1:len(case[i])-1:], case[i][len(case[i])-1]):
ans[i] = ans[i] + 1
else:
if Wildcard(str(j), case[i]):
ans[i] = ans[i] + 1
for each in ans:
print(each)
#Copyright (c) 2016 Parth V. Parikh
#Freely distributable under MIT lincese.
N = int(input())
case = []
step = []
for i in range(N):
tm = input()
case.append(tm.lower())
tm = input()
step.append(int(tm))
m = 0
for each in case:
line = each.split()
txt = []
num = []
for i in line:
if i.isdecimal():
num.append(int(i))
else:
txt.append(i)
for j in range(step[m]):
if j % 2 == 0: ##sort txt
posMin = txt.index(min(txt[int(j / 2)::]))
tmp = txt[int(j / 2)]
txt[int(j / 2)] = txt[posMin]
txt[posMin] = tmp
else:
posMin = num.index(min(num[int((j - 1) / 2)::]))
tmp = num[int((j - 1) / 2)]
num[int((j - 1) / 2)] = num[posMin]
num[posMin] = tmp
ans = []
for i in range(int((len(txt) + len(num)) / 2)):
ans.append(txt[i])
ans.append(str(num[i]))
a = str(' '.join(ans))
print(a)
del txt
del num
m = m + 1
#Copyright (c) 2016 Parth V. Parikh
#Freely distributable under MIT lincese.
def count_neighbours(grid, row, col):
neighbors = [(-1, -1), (-1, 0), (-1, 1),
(0, -1), (0, 1),
(1, -1), (1, 0), (1, 1)]
grid = [tuple([0]) * (len(grid[0]) + 2)] + [tuple([0]) + i + tuple([0]) for i in grid] + [tuple([0]) * (len(grid[0]) + 2)]
return sum([grid[row + 1 + i[0]][col + 1 + i[1]] for i in neighbors])
N = int(input())
matrix = []
for i in range(N):
tm = input()
h,w = tm.split()
h = int(h)
w = int(w)
tmMat = []
for i in range(h):
tm = input()
tm = tm.replace('#','1')
tm = tm.replace('.','0')
tm = list(tm)
for i in range(len(tm)):
tm[i] = int(tm[i])
tmMat.append(tm)
matrix.append(tmMat)
for mat in matrix:
for m in range(len(mat)):
mat[m] = tuple(mat[m])
for y in range(len(mat)):
for x in range(len(mat[y])):
print(count_neighbours(tuple(mat),x,y))
#Copyright (c) 2016 Parth V. Parikh
#Freely distributable under MIT lincese.
N = int(input())
inMat = []
for i in range(N):
tm = input()
tm = tm.split()
inMat.append(tm)
for case in inMat:
x = int(case[0])
y = int(case[1])
t = int(case[2])
sizeChart = [x, y]
newsizeChart = sizeChart[:]
for i in range(t):
newsizeChart.append(sizeChart[0]+sizeChart[1])
newsizeChart.append(2*sizeChart[0] + sizeChart[1])
sizeChart = newsizeChart[2::]
print(max(sizeChart)%1000000007)
#Copyright (c) 2016 Parth V. Parikh
#Freely distributable under MIT lincese.
line = input()
curPos = line.split('/')
prevPos = curPos[:]
while True:
line = input()
if line == '-1':
break
curPos = line.split('/')
#Copyright (c) 2016 Parth V. Parikh
#Freely distributable under MIT lincese.
map = []
tm = input()
r,c = tm.split()
r = int(r)
c = int(c)
people = 0
for i in range(r):
tm = input()
map.append(list(tm.split()))
people += tm.count('s')
for s in range(people):
for x in
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment