Facebook Hacker Cup 2012 BillBoard Problem
" facebook hacker cup 2012 :billboard"
#!/usr/bin/python
noi=input('how many string you wanna check :').strip('\r')
noi=int(noi)
finale=[]
for we in range(1,noi+1):
inp=input('enter the test string:').strip('\r')
inp=inp.split(' ')
list=[]
for x in inp:
list.append(x)
lt=int(list[0])
ht=int(list[1])
list=list[2:]
list[-1]=list[-1].strip('\n')
len_list=[]
for x in range(0,len(list)):
len_list.append(len(list[x]))
mx=max(len_list)
superFont=int(lt/mx)
i=superFont
def hooray(answer):
res='Case #'
res+=str(we)+': '
res+=str(answer)
finale.append(res)
cat=list
try:
while i>1 and len(list)!=0:
list=cat
x=1
rows=int(ht/i)
cols=int(lt/i)
while x<=rows:
occ=0
avail=cols
while avail>0:
if occ==0: # i.e if the current row is empty
#print(len(list[0]))
occ=len(list[0])+1
list=list[1:]
avail=cols-occ
if(len(list))==0:
hooray(i)
elif len(list[0])>avail: #if the word is longer than the space available
if(len(list))==0:
hooray(i)
avail=0
elif len(list[0])<avail: #if the word is shorter than the available space
occ=occ+len(list[0])+1
list=list[1:]
if(len(list))==0:
hooray(i)
avail=cols-occ
elif len(list[0])==avail:#if the word's length is equal to the space available
list=list[1:]
if(len(list))==0:
hooray(i)
avail=0
x+=1 ## move to the next row
if(len(list))!=0:
i-=1
except IndexError:
print('')
if i<=1:
res='Case #'
res+=str(we)+': '
res+=str(1)
finale.append(res)
for tu in finale:
print(tu)
No comments:
Post a Comment