Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- data = (
- (tuple('19428367'), 7, 1),
- (tuple('11111111'), 1, 1),
- (tuple('56589023'), 5, 1),
- (tuple('40921067'), None, 4),
- (tuple('21975655'), 4, 2),
- (tuple('25073841'), 7, 0),
- (tuple('31826018'), None, 4),
- )
- def f(s1):
- r = 0
- for s2, correct, correct_place in data:
- if correct is not None:
- s = s1.copy()
- found = 0
- for c in s2:
- try:
- i = s.index(c)
- except ValueError:
- pass
- else:
- s[i] = None
- found += 1
- r += abs(found - correct)
- n = 0
- for i in range(8):
- n += s1[i] == s2[i]
- r += abs(n - correct_place)
- return r
- get_chance = random.random
- randint = random.randint
- def get_random():
- return chr(randint(48, 57))
- class C:
- def __init__(self, s = None):
- self.s = s
- self.r = f(s)
- def get_child(self):
- n = self.s.copy()
- for i in range(8):
- if get_chance() < 0.125:
- n[i] = get_random()
- while get_chance() < 0.25:
- a = randint(0, 7)
- b = randint(0, 7)
- n[a], n[b] = n[b], n[a]
- return C(n)
- c = C([get_random() for i in range(8)])
- while c.r:
- n = c.get_child()
- if n.r < c.r:
- print('%s, error: %s' % (' '.join(n.s), n.r))
- c = n
Advertisement
Add Comment
Please, Sign In to add comment