andrejpodzimek

počet testů [odporná hrubá síla]

Mar 26th, 2020
271
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.25 KB | None | 0 0
  1. #!/bin/env python3.8
  2.  
  3. import fractions
  4.  
  5. def subsets(n, k):
  6.   if k == 0:
  7.     yield ()
  8.   elif k < n:
  9.     for s in subsets(n - 1, k):
  10.       yield s
  11.     for s in subsets(n - 1, k - 1):
  12.       yield s + (n - 1,)
  13.   elif n == k:
  14.     yield tuple(range(n))
  15.  
  16. class NumberOfTests:
  17.   def __init__(self, depth):
  18.     self.patients = 2 ** depth
  19.  
  20.   def report(self, tested, sample):
  21.     sample += self.patients
  22.     while sample > 1:
  23.       if sample in tested:
  24.         break
  25.       tested.add(sample)  # current vertex
  26.       tested.add(sample + (-1 if sample % 2 else 1))  # sibling
  27.       sample //= 2  # path to the root
  28.  
  29.   def averageTests(self, infected):
  30.     infected_subsets = 0
  31.     all_tests_done = 0
  32.     for subset in subsets(self.patients, infected):
  33.       tested = {1}
  34.       infected_subsets += 1
  35.       for sample in subset:
  36.         self.report(tested, sample)
  37.       all_tests_done += len(tested)
  38.     return fractions.Fraction(all_tests_done, infected_subsets)
  39.  
  40. DEPTH = 6
  41. MAX_INFECTED = 5  # too slow for more
  42. MIN_INFECTED = 59  # too slow for less
  43.  
  44. nt = NumberOfTests(DEPTH)
  45. for r in (range(MAX_INFECTED + 1), range(MIN_INFECTED, 2 ** DEPTH + 1)):
  46.   for i in r:
  47.     blah = nt.averageTests(i)
  48.     print(f'{i:02}: {float(blah)} ({str(blah)})')
Add Comment
Please, Sign In to add comment