Hi,
I am having runtime error with my code. can somebody help figure out what's
wrong?
Thanks in advance
def main():
for i in range(1, int(input()) + 1):
R,C,H,V = [int(s) for s in input().split(" ")]
ls = []
for k in range(R):
ls.append(input())
ans =solution(ls,R,C,H,V)
print('Case #{}: {}'.format(i, ans))
def solution(ls,R,C,H,V):
cookie = count_total_cookie(ls)
if cookie%(H+1):
return 'IMPOSSIBLE'
elif cookie%(V+1):
return 'IMPOSSIBLE'
elif cookie%((V+1)*(H+1)):
return 'IMPOSSIBLE'
h_cuts = possible(ls,cookie//(H+1))
if h_cuts == 'IMPOSSIBLE':
return 'IMPOSSIBLE'
v_cuts = possible(transpose(ls),cookie//(V+1))
if v_cuts == 'IMPOSSIBLE':
return 'IMPOSSIBLE'
a = split(ls,h_cuts,v_cuts,cookie//((V+1)*(H+1)),R,C)
if a == 'IMPOSSIBLE':
return 'IMPOSSIBLE'
return 'POSSIBLE'
def split(ls,h_cuts,v_cuts,each_bin,R,C):
curr_h_ind = 0
curr_v_ind = 0
d = {}
for i in range(R):
if i>h_cuts[curr_h_ind]:
for keys in d:
if d[keys]!=each_bin:
return 'IMPOSSIBLE'
d = {}
curr_h_ind +=1
curr_v_ind = 0
for j in range(C):
if j>v_cuts[curr_v_ind]:
curr_v_ind +=1
if ls[i][j] == "@":
d[curr_v_ind] = d.get(curr_v_ind,0)+1
#print(d)
return 'POSSIBLE'
def transpose(ls):
ls2 = []
t_matrix = zip(*ls)
for row in t_matrix:
ls2.append(row)
return ls2
def possible(ls,each):
bins = 0
cuts = []
i = 0
for s in ls:
c = s.count('@')
bins+=c
if bins>each:
return 'IMPOSSIBLE'
elif bins==each:
bins=0
cuts.append(i)
i+=1
return cuts
#cut is after index, if 0, means cut between 0 and 1
def count_total_cookie(ls):
count = 0
for s in ls:
count+= s.count('@')
return count
main()
--
You received this message because you are subscribed to the Google Groups
"Google Code Jam" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/google-code/13a8327f-a68f-4f65-be59-9e3a3bd1b0fd%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.