Guest User

Robots

a guest
May 10th, 2019
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.50 KB | None | 0 0
  1.  
  2. """
  3. def delete_multi(c,idx):
  4.    popped_count = 0
  5.    for i in range(0,len(idx)):
  6.        pop_index = idx[i] - popped_count
  7.        c.pop(pop_index)
  8.        popped_count +=1
  9.    return c
  10.  
  11.  
  12.    This is very slow.
  13.    first you have to iterate through each index that you want to remove O(n)
  14.    then for each of those, you pop an element out, which is also linear time (a new list has to be created)
  15.    so this ends up taking O(n^2)
  16.    avoid poping elements out of lists- if you are, use a different data structure
  17.  
  18. """
  19. T = int(input())
  20.  
  21.  
  22. #for some reason you looped through all of the inputs, stored, then output. you do not need to do this- you can output at anytime
  23. for i in range(T): #range function can be called with one var, 0 is implied
  24.     N = int(input())
  25.     c = [] #give your variables more meaningful names
  26.     #max_len = 0 you use this to determine how many itereations to use, but it is better to just check if you have won
  27.     for j in range(N):
  28.         C = input()
  29.         c.append(C)
  30.  
  31.    
  32.     #output = '' strings are immutable in python, so a new string is made when you add elements
  33.     output = []# much better to use an array
  34.  
  35.  
  36.     #loop until break
  37.     index = 0 # posistion in arrays
  38.     while True:
  39.  
  40.         #if(len(c)==0): this check is not needed. you break if only one type was played last round
  41.  
  42.         #this_column= []
  43.         #you are using this as a set, initialize a set
  44.         this_column = set()
  45.  
  46.         counter = ''
  47.        
  48.         # r_index, s_index, p_index = [],[],[] you store indicies, why not store the whole move
  49.         r, s, p = [],[],[]
  50.  
  51.  
  52.  
  53.         #for k in range(0,len(c)):  This is a c style loop
  54.             #series = c[k]
  55.  
  56.         #if you need the index, use
  57.         #for i, series in enumerate(c):
  58.  
  59.  
  60.         for series in c:
  61.             ele = index % len(series) #this is only used once, could be compacted
  62.             play = series[ele]
  63.             if(play=='R'):
  64.                 #r_index.append(k) ah this is why you wanted the index
  65.                 r.append(series)
  66.             elif(play=='P'):
  67.                 p.append(series)
  68.             elif(play=='S'):
  69.                 s.append(series)
  70.  
  71.             this_column.add(play) #add element to set
  72.        
  73.         # x = list(set(this_column))
  74.            
  75.        
  76.        
  77.         if(len(this_column)>2):
  78.             output = "IMPOSSIBLE"
  79.             break
  80.         elif(len(this_column)==1):
  81.             if('R' in this_column):
  82.                 counter = 'P'
  83.                 # c = delete_multi(c,r_index)
  84.                 # there is no need to delete these, you already won
  85.             if('S' in this_column):
  86.                 counter = 'R'
  87.             if('P' in this_column):
  88.                 counter = 'S'
  89.             output.append(counter)
  90.             # you won, so break out
  91.             break
  92.                
  93.         else:
  94.             if(('R' in this_column) and ('S' in this_column)):
  95.                 counter = 'R'
  96.                 # c = delete_multi(c,s_index)
  97.                 # instead of deleting unwanted el, why not just keep the ones we used
  98.                 c = r
  99.             elif(('P' in this_column) and ('S' in this_column)):
  100.                 counter = 'S'
  101.                 c = s
  102.             elif(('R' in this_column) and ('P' in this_column)):
  103.                 counter = 'P'
  104.                 c = p
  105.  
  106.         output.append(counter)
  107.         index += 1 # move forawrd
  108.  
  109.     print("Case #%i: %s"%((i+1),''.join(output)))
Advertisement
Add Comment
Please, Sign In to add comment