Created
May 15, 2014 03:36
-
-
Save nickgarvey/7c6a93920bf95204a2e7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# http://www.csee.umbc.edu/courses/undergraduate/471/fall09/mastermind/mm-projects.html | |
import sys | |
import itertools | |
# {'q': 2, 'k': 4, 'n': 4, | |
# 'test_cases': [{'num_correct': 0, 'guess_list': [2, 1, 2, 2]}, | |
# {'num_correct': 1, 'guess_list': [2, 2, 1, 1]}]} | |
# {'q': 2, 'k': 4, 'n': 4, | |
# 'test_cases': [{'num_correct': 4, 'guess_list': [1, 2, 3, 4]}, | |
# {'num_correct': 1, 'guess_list': [4, 3, 2, 1]}]} | |
def get_tests(test_lines, num_tests): | |
tests = list() | |
while len(tests) < num_tests: | |
test = dict() | |
test["n"], test["k"], test["q"] = map(int, test_lines[0].split(" ")) | |
test["test_cases"] = list() | |
for test_line in test_lines[1:1+test["q"]]: | |
test_line_ints = map(int, test_line.split(" ")) | |
test_case = dict() | |
test_case["guess_list"] = test_line_ints[:-1] | |
test_case["num_correct"] = test_line_ints[-1] | |
test["test_cases"].append(test_case) | |
test_lines = test_lines[test["q"]+1:] | |
tests.append(test) | |
return tests | |
def run_on_file(input_file): | |
lines = input_file.readlines() | |
num_tests = int(lines[0]) | |
if not num_tests: | |
return | |
test_lines = lines[1:] | |
tests = get_tests(test_lines, num_tests) | |
def case_is_correct(secret_key, test_case): | |
return test_case["num_correct"] == \ | |
sum(map(lambda tup: int(tup[0] == tup[1]), | |
zip(secret_key, test_case["guess_list"]))) | |
for test in tests: | |
for secret_key in itertools.product(range(1, test["n"] + 1), repeat = test["k"]): | |
for test_case in test["test_cases"]: | |
if not case_is_correct(secret_key, test_case): | |
break | |
else: | |
print "Yes" | |
break | |
else: | |
print "No" | |
if __name__ == "__main__": | |
run_on_file(sys.stdin) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment