Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>For fun and interest, I've rewritten the problem using OO. See what you think:</p> <pre><code>import collections import sys import random usage = """ Usage: sequence fname expected Where fname: name of file containing fragments expected: result-string which should be obtained by chaining from first fragment. """ class Frag(str): MATCHLEN = 7 def __new__(cls, s=''): return str.__new__(cls, s.strip()) def head(self): return Frag(self[:Frag.MATCHLEN]) def tail(self): return Frag(self[Frag.MATCHLEN:]) def nexthead(self): return Frag(self[-Frag.MATCHLEN:]) def check(self, s): return self.__eq__(s) def __add__(self, s): return Frag(str(self).__add__(s)) class Fraglist(list): @classmethod def fromFile(cls, fname): with open(fname, "r") as inf: lst = [Frag(ln) for ln in inf] return cls(lst) def shuffle(self): random.shuffle(self) class Sequencer(object): def __init__(self, seq=None): super(Sequencer, self).__init__() self.sequences = collections.defaultdict(list) if seq is not None: for frag in seq: self.sequences[frag.head()].append(frag.tail()) def build(self, frag): res = [frag] match = frag.nexthead() while match in self.sequences: next = random.choice(self.sequences[match]) res.append(next) match = (match + next).nexthead() return Frag(''.join(res)) def main(): if len(sys.argv) != 3: print usage sys.exit(-1) else: fname = sys.argv[1] expected = sys.argv[2] frags = Fraglist.fromFile(fname) frag1 = frags.pop(0) frags.shuffle() seq = Sequencer(frags) result = seq.build(frag1) if result.check(expected): print "Match!" else: print "No match" if __name__=="__main__": main() </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload