Created
April 4, 2013 15:32
-
-
Save danilobellini/5311427 to your computer and use it in GitHub Desktop.
Mastermind guess description
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 py.test | |
# -*- coding: utf-8 -*- | |
# Mastermind guess description | |
# Created on Thu Mar 14 2013 | |
# Danilo de Jesus da Silva Bellini | |
def mastermind(guess, secret): | |
""" | |
Mastermind guess description extractor. | |
:param guess: Iterable with hashable contents. | |
:param secret: Iterable with hashable contents. | |
:returns: A ``(ok, mok)`` tuple where ``ok`` is the amount of items | |
in ``guess`` that maps equally to ``secret`` (keeping the same | |
index), and ``mok`` is the same for a guess permutation that | |
maximizes the result. | |
""" | |
return sum(1 for g, s in zip(guess, secret) if g == s), \ | |
sum(min(guess.count(x), secret.count(x)) for x in set(secret)) | |
def test_empty(): | |
assert mastermind("", "") == (0, 0) | |
def test_1_symbol(): | |
assert mastermind("1", "1") == (1, 1) | |
assert mastermind("11", "11") == (2, 2) | |
assert mastermind("111", "111") == (3, 3) | |
def test_2_symbols(): | |
assert mastermind("12", "12") == (2, 2) | |
assert mastermind("21", "12") == (0, 2) | |
assert mastermind("112", "112") == (3, 3) | |
assert mastermind("112", "121") == (1, 3) | |
assert mastermind("122", "212") == (1, 3) | |
assert mastermind("1212", "2121") == (0, 4) | |
def test_7_symbols(): | |
assert mastermind("1234567", "7654321") == (1, 7) | |
assert mastermind("1234567", "1234567") == (7, 7) | |
assert mastermind("4444444", "1231567") == (0, 0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment