Advertisement
RonWeber

q4.py

Feb 14th, 2020
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.79 KB | None | 0 0
  1. #!/usr/bin/env python3
  2.  
  3. # This problem is tricky.  
  4. # We were told that our output has to be both random and
  5. # different each time we run.
  6. #
  7. # It will be difficult to make sure that our output is one
  8. # that has never occurred before.
  9.  
  10. # If we use the "random" module, a determined attacker could
  11. # set things up so that we get the same output as a previous run.
  12. # We need cryptography-strength RNG.
  13. import secrets
  14.  
  15. NUM_SEQUENCES = 5
  16. SEQUENCE_LENGTH = 10
  17. POSSIBLE_LETTERS = ["A", "C", "G", "T"]
  18.  
  19. # In order to truly ensure that we get a result different
  20. # from all other times we've run, we need to keep a set of
  21. # all prior results.
  22. all_prior_results = set([
  23. #AUTOGENERATED_CODE
  24.     ('CCGAGTCCTT', 'AAGGGGACGG', 'CGATATTTTC', 'GCCACTGCAT', 'ATCGCTAGGG'),
  25.     ('CTTTGTCCCC', 'CTGAATGGTT', 'CTATCAGAAG', 'CCCCGGCTAC', 'CCATGGTTAG'),
  26.     ('ACTTCTATTT', 'TATCTGAGAA', 'GTGTGGTTTG', 'AGTCTTAGTT', 'GGATGTAAAG'),
  27.     ('CGGGCGCGGT', 'ATCCCAGGGA', 'AAACTCAGTG', 'GGGCGAAGGC', 'AGGGCATGAA'),
  28. #END_AUTOGENERATED_CODE
  29. ])
  30.  
  31. seqs = []
  32. # Keep trying until we get a completely new sequence.
  33. while True:
  34.     # Create a new list of sequences
  35.     for _ in range(NUM_SEQUENCES):
  36.         seq = "".join(secrets.choice(POSSIBLE_LETTERS) for _ in range(SEQUENCE_LENGTH))
  37.         seqs.append(seq)
  38.     if not tuple(seqs) in all_prior_results:
  39.         break
  40.  
  41. # Add our new entry to the results.
  42. # Read this script
  43. with open(__file__, "r") as f:
  44.     lines = f.readlines()
  45.  
  46. # Write the script back, adding our new line.
  47. with open(__file__, "w") as f:
  48.     for l in lines:
  49.         f.write(l)
  50.         # Look for the magic string.
  51.         if l == "#AUTOGENERATED_CODE\n":
  52.             f.write("    %s,\n" % str(tuple(seqs)))
  53.  
  54. # Print
  55. for (i, s) in enumerate(seqs):
  56.     print("Random sequence %d: %s" % (i, s))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement