Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- # This problem is tricky.
- # We were told that our output has to be both random and
- # different each time we run.
- #
- # It will be difficult to make sure that our output is one
- # that has never occurred before.
- # If we use the "random" module, a determined attacker could
- # set things up so that we get the same output as a previous run.
- # We need cryptography-strength RNG.
- import secrets
- NUM_SEQUENCES = 5
- SEQUENCE_LENGTH = 10
- POSSIBLE_LETTERS = ["A", "C", "G", "T"]
- # In order to truly ensure that we get a result different
- # from all other times we've run, we need to keep a set of
- # all prior results.
- all_prior_results = set([
- #AUTOGENERATED_CODE
- ('CCGAGTCCTT', 'AAGGGGACGG', 'CGATATTTTC', 'GCCACTGCAT', 'ATCGCTAGGG'),
- ('CTTTGTCCCC', 'CTGAATGGTT', 'CTATCAGAAG', 'CCCCGGCTAC', 'CCATGGTTAG'),
- ('ACTTCTATTT', 'TATCTGAGAA', 'GTGTGGTTTG', 'AGTCTTAGTT', 'GGATGTAAAG'),
- ('CGGGCGCGGT', 'ATCCCAGGGA', 'AAACTCAGTG', 'GGGCGAAGGC', 'AGGGCATGAA'),
- #END_AUTOGENERATED_CODE
- ])
- seqs = []
- # Keep trying until we get a completely new sequence.
- while True:
- # Create a new list of sequences
- for _ in range(NUM_SEQUENCES):
- seq = "".join(secrets.choice(POSSIBLE_LETTERS) for _ in range(SEQUENCE_LENGTH))
- seqs.append(seq)
- if not tuple(seqs) in all_prior_results:
- break
- # Add our new entry to the results.
- # Read this script
- with open(__file__, "r") as f:
- lines = f.readlines()
- # Write the script back, adding our new line.
- with open(__file__, "w") as f:
- for l in lines:
- f.write(l)
- # Look for the magic string.
- if l == "#AUTOGENERATED_CODE\n":
- f.write(" %s,\n" % str(tuple(seqs)))
- # Print
- for (i, s) in enumerate(seqs):
- print("Random sequence %d: %s" % (i, s))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement