Advertisement
Tarferi

OPT Solution

Dec 27th, 2019
417
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.82 KB | None | 0 0
  1. with open ("input.txt", "r") as myfile:
  2.     data = myfile.readlines()
  3. data = map(lambda line: map(lambda num: int(num, 16), line.replace('\n', '').split("|")), data)
  4.  
  5. head = data[0]
  6. data = data[1:]
  7.  
  8. def getSeq(data, firstDataIndex, sequenceIndex, expectedValue, nextExpectedValues):
  9.     last = len(nextExpectedValues) == 0
  10.    
  11.     for index in range(firstDataIndex, len(data)):
  12.         seq = data[index]
  13.         if seq[sequenceIndex] == expectedValue:
  14.             if last:
  15.                 yield [index]
  16.             else:
  17.                 for nextItem in getSeq(data, index + 1, sequenceIndex, nextExpectedValues[0], nextExpectedValues[1:]):
  18.                     yield [index] + nextItem
  19.  
  20. maxL = lambda mx, item: mx if mx > item else item
  21. longestSequence = 0
  22. for seq in data:
  23.     longestSequence = maxL(reduce(maxL, seq, 0), longestSequence)
  24.    
  25. with open("output.txt","w") as file:
  26.     for valueIndex in range(0, longestSequence):  # For each index
  27.        
  28.         # Collect sequences of such length
  29.         seqs = []
  30.         rev = []
  31.        
  32.         for i in range(len(data)):
  33.             seq = data[i]
  34.             if len(seq) > valueIndex:
  35.                 seqs.append(seq)
  36.                 rev.append(i)
  37.                
  38.         print("Seeking at index {0}/{1} in {2} total sequences".format(valueIndex + 1, longestSequence, len(seqs)))
  39.        
  40.         # Take every sequence as if it could be the first
  41.         for sequenceIndex in range(len(seqs)):
  42.             val = seqs[sequenceIndex][valueIndex]
  43.             for sequences in getSeq(seqs, sequenceIndex, valueIndex, val + head[0], map(lambda var: var + val, head[1:])):
  44.                 result = ", ".join(map(lambda val: "sequences[{0}][{1}] == {2}".format(rev[val], valueIndex, seqs[val][valueIndex]), sequences))
  45.                 file.write(result);
  46.  
  47. pass
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement