Advertisement
Guest User

Untitled

a guest
May 3rd, 2015
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.65 KB | None | 0 0
  1. def TrainsRecursive(totalN):
  2.     trains = 0;
  3.     initNode = {'num':1,'cut':False,'parent':0}
  4.     frontier = [initNode]
  5.     trainArray = []
  6.  
  7.     while(len(frontier) > 0):
  8.         node = frontier.pop(len(frontier)-1)
  9.         #check it off the frontier
  10.         if(node["num"] < totalN):
  11.             nodeCut = {}; nodeCut["num"] = node["num"] + 1; nodeCut["cut"] = True; nodeCut["parent"] = node;
  12.             frontier.append(nodeCut)
  13.             nodeSpare = {}; nodeSpare["num"] = node["num"] + 1; nodeSpare["cut"] = False;  nodeSpare["parent"] = node;
  14.             frontier.append(nodeSpare)
  15.         else:
  16.             fullList = []
  17.             cutArray = []
  18.             cuts = ""
  19.             while(node['parent'] != 0):
  20.                 if(node['cut']):
  21.                     cuts = cuts + "1"
  22.                     cutArray.append(1)
  23.                 else:
  24.                     cuts = cuts + "0"
  25.                     cutArray.append(0)
  26.                 fullList.append(node['parent'])
  27.                 node = node['parent']
  28.                
  29.  
  30.             #print(cuts)
  31.             #translate cuts to rods
  32.  
  33.             rodLength = 1;
  34.             rodArray = []
  35.             for i in range(len(cutArray)):
  36.                 cut = cutArray[i];
  37.                 if(cut == 1):
  38.                     rodArray.append(rodLength)
  39.                     rodLength = 1
  40.                 else:
  41.                     rodLength = rodLength + 1;
  42.  
  43.             rodArray.append(rodLength)
  44.             trainArray.append(rodArray)
  45.  
  46.             trains +=1;
  47.  
  48.     #sort them
  49.     sortedRodArray = []
  50.     num = 1;
  51.     while(True):
  52.        
  53.         found = False;
  54.         for i in range(len(trainArray)):
  55.             if(len(trainArray[i]) == num):
  56.                 sortedRodArray.append(trainArray[i])
  57.                 found = True;
  58.  
  59.         if(found == False):
  60.             break;
  61.         num += 1;
  62.  
  63.     #pretty print
  64.     noOnes = 0;
  65.     for i in range(len(sortedRodArray)):
  66.         foundOne = False;
  67.         for j in range(len(sortedRodArray[i])):
  68.             if(sortedRodArray[i][j] == 1):
  69.                 foundOne = True;
  70.         if(foundOne == False):
  71.             noOnes += 1;
  72.  
  73.     return sortedRodArray
  74.  
  75. def TrainsWithParts(trains,k):
  76.     #takes an array of trains, and an integer k, removes all trains whose len() is more than k
  77.     newTrains = []
  78.     for i in range(len(trains)):
  79.         arr = trains[i];
  80.         if(len(arr) <= k):
  81.             newTrains.append(arr)
  82.  
  83.     return newTrains;
  84.  
  85. def TrainsWithAtMost(trains,k):
  86.     #takes an array of trains, and an integer k. Removes all trains that contain a rod with a length greater than k
  87.     newTrains = []
  88.     for i in range(len(trains)):
  89.         arr = trains[i];
  90.         Found = True;
  91.         for j in range(len(arr)):
  92.             if(arr[j] > k):
  93.                 Found = False;
  94.  
  95.         if(Found):
  96.             newTrains.append(arr)
  97.  
  98.     return newTrains;
  99.  
  100. def Unordered(trains):
  101.     #Removes duplicates ex: [1,4] and [4,1] are considered to be the same
  102.  
  103.     for i in range(len(trains)):
  104.         trains[i].sort();
  105.  
  106.     hashed = {}
  107.  
  108.     newTrains = []
  109.     for arr in trains:
  110.         signature = ""
  111.         for j in arr:
  112.             signature += str(j)
  113.         if(not(signature in hashed)):
  114.             hashed[signature] = True;
  115.             newTrains.append(arr)
  116.  
  117.     return newTrains
  118.  
  119. def Triangles(trains):
  120.     #Takes a q_3(n-3), adds missing pieces and returns all the valid triangle
  121.     #A valid triangle is such that:
  122.     #a < b + c
  123.     #a > b > c > 0
  124.     return 0.
  125.  
  126. def Distinct(trains):
  127.     #This function removes all trains that have non-unique parts
  128.  
  129.     newTrains = []
  130.  
  131.     for train in trains:
  132.         FoundDuplicate = False;
  133.         numHash = {};
  134.         for j in train:
  135.             if(j in numHash):
  136.                 FoundDuplicate = True;
  137.                 break;
  138.             numHash[j] = True;
  139.         if(not FoundDuplicate):
  140.             newTrains.append(train)
  141.  
  142.     return newTrains;
  143.  
  144.  
  145. TRAIN_LENGTH = 9;
  146. PARTS = 3;
  147.  
  148. allTrains = TrainsRecursive(TRAIN_LENGTH);
  149.  
  150. print("Total " + str(len(allTrains)))
  151.  
  152. # newPartsTrains = TrainsWithParts(allTrains,PARTS);
  153. # newPartsTrains = Unordered(newPartsTrains);
  154.  
  155. # print(" ")
  156. # print("q_" + str(PARTS) + "(" + str(TRAIN_LENGTH) + ") = " +  str(len(newPartsTrains)))
  157. # print(" ")
  158.  
  159. # for train in newPartsTrains:
  160. #   print(train)
  161.  
  162. # newTrains = TrainsWithAtMost(allTrains,PARTS);
  163. # newTrains = Unordered(newTrains);
  164.  
  165. # print(" ")
  166. # print("p_" + str(PARTS) + "(" + str(TRAIN_LENGTH) + ") = " +  str(len(newTrains)))
  167. # print(" ")
  168.  
  169. # for train in newTrains:
  170. #   print(train)
  171.  
  172. # uTrains = Distinct(newTrains)
  173.  
  174. # print(" ")
  175. # print("Unique = " + str(len(uTrains)))
  176. # print(" ")
  177.  
  178.  
  179.  
  180. # for train in uTrains:
  181. #   print(train)
  182.  
  183. n = 11;
  184.  
  185. # For k = 3, the differences between PN and UN were 3 3, 4 4, 5 5, 6 6 etc..
  186.  
  187. # for i in range(3,15):
  188. #   print("--------------- n = " + str(i) + " k = " + str(k))
  189. #   totalTrains = TrainsRecursive(i);
  190. #   pTrains = TrainsWithParts(totalTrains,k)
  191. #   pTrains = Unordered(pTrains)
  192.  
  193. #   uTrains = Distinct(pTrains)
  194.  
  195. #   print("PN = " + str(len(pTrains)))
  196. #   print("UN = " +  str(len(uTrains)))
  197. #   print("PN - UN = " + str(len(pTrains) - len(uTrains)))
  198.  
  199. totalTrains = TrainsRecursive(n)
  200. pTrains = TrainsWithParts(totalTrains,5)
  201. pTrains = Unordered(pTrains)
  202.  
  203. uTrains = Distinct(pTrains)
  204.  
  205. for train in pTrains:
  206.     print(train)
  207.  
  208. print("==============")
  209.  
  210. for train in uTrains:
  211.     print(train)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement