Guest User

Untitled

a guest
Jul 13th, 2025
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.18 KB | None | 0 0
  1. import random
  2.  
  3. data = (
  4.     (tuple('19428367'), 7, 1),
  5.     (tuple('11111111'), 1, 1),
  6.     (tuple('56589023'), 5, 1),
  7.     (tuple('40921067'), None, 4),
  8.     (tuple('21975655'), 4, 2),
  9.     (tuple('25073841'), 7, 0),
  10.     (tuple('31826018'), None, 4),
  11. )
  12.  
  13. def f(s1):
  14.     r = 0
  15.    
  16.     for s2, correct, correct_place in data:
  17.         if correct is not None:
  18.             s = s1.copy()
  19.             found = 0
  20.             for c in s2:
  21.                 try:
  22.                     i = s.index(c)
  23.                 except ValueError:
  24.                     pass
  25.                 else:
  26.                     s[i] = None
  27.                     found += 1
  28.             r += abs(found - correct)
  29.        
  30.         n = 0
  31.         for i in range(8):
  32.             n += s1[i] == s2[i]
  33.         r += abs(n - correct_place)
  34.    
  35.     return r
  36.  
  37. get_chance = random.random
  38. randint = random.randint
  39. def get_random():
  40.     return chr(randint(48, 57))
  41.  
  42. class C:
  43.     def __init__(self, s = None):
  44.         self.s = s
  45.         self.r = f(s)
  46.    
  47.     def get_child(self):
  48.         n = self.s.copy()
  49.        
  50.         for i in range(8):
  51.             if get_chance() < 0.125:
  52.                 n[i] = get_random()
  53.        
  54.         while get_chance() < 0.25:
  55.             a = randint(0, 7)
  56.             b = randint(0, 7)
  57.             n[a], n[b] = n[b], n[a]
  58.        
  59.         return C(n)
  60.  
  61. c = C([get_random() for i in range(8)])
  62.  
  63. while c.r:
  64.     n = c.get_child()
  65.     if n.r < c.r:
  66.         print('%s, error: %s' % (' '.join(n.s), n.r))
  67.         c = n
  68.  
Advertisement
Add Comment
Please, Sign In to add comment