Monday, January 23, 2012

Facebook Hacker Cup 2012 Alphabet Soup Problem



Facebook Hacker Cup 2012 Alphabet Soup Problem



#!/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 string: ').strip('\r')
  list=inp.split(' ')
  ty=''
  
  for x in list:
    ty+=x
  
  lis=[]  
  for x in ty:
     lis.append(x)
  if lis[len(lis)-1]=='\n':   
   lis=lis[:-1]
  h=[]
  a=[]
  c=[]
  k=[]
  e=[]
  r=[]
  u=[]
  p=[]
  
  for x in 'HACKERUP':
    
      first=-1
      try:
       while first < (len(ty)-1):
        first=ty.index(x,first+1)
        if x=='H':
            h.append(x)
        elif x=='A':
            a.append(x)
        elif x=='C':
            c.append(x)
        elif x=='K':
            k.append(x)
        elif x=='E':
            e.append(x)
        elif x=='R':
            r.append(x)
        elif x=='U':
            u.append(x)
        elif x=='P':
            p.append(x)
        
        
        
      except ValueError:
        continue

  if len(c)%2==0:
    c=c[0:int(len(c)/2)]
    
  elif len(c)%2!=0:
    c=c[0:-1]
    c=c[0:int(len(c)/2)]
    
  
  ans=min(len(h),len(a),len(c),len(k),len(e),len(r),len(u),len(p))
  
  res='Case #'
  res+=str(we)+': '
  res+=str(ans)
  finale.append(res)
for x in finale:
  print(x)


Facebook Hacker Cup 2012 BillBoard Problem

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)