Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- from random import randint
- from random import choice as randchoice
- N = 4
- def get_count(smth1, smth2):
- bulls = cows = 0
- for i, value in enumerate(smth1):
- try:
- if i == smth2.index(value):
- bulls += 1
- else:
- cows += 1
- except ValueError:
- pass
- return bulls, cows
- is_correct = lambda x: x[0] != '0' and len(set(x)) == len(x)
- ask = lambda x: tuple(map(int, raw_input('%s: ' % ''.join(map(str, x))).split()[:2]))
- rules = []
- possible_numbers = [str(i) for i in xrange(10**(N-1), 10**N-1) if is_correct(str(i))]
- while len(possible_numbers) > 1:
- tmp = randchoice(possible_numbers)
- rules.append((tmp, ask(tmp)))
- tmp = []
- for number in possible_numbers:
- is_possible = True
- for rule in rules:
- if get_count(number, rule[0]) != rule[1]:
- is_possible = False
- break
- if is_possible:
- tmp.append(number)
- possible_numbers = tmp
- if possible_numbers:
- print 'Result: %s' % ''.join(map(str, possible_numbers[0]))
- else:
- print 'Incorrect!'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement