#!/usr/bin/python3
# coding : utf-8

import sys
from math import factorial, pow

def display_usage(argv):
    if len(argv) == 2 and argv[1] in ("-h", "--help"):
        usage = [
            "USAGE",
            "\t{} d1 d2 d3 d4 d5 c".format(argv[0]),
            "",
            "DESCRIPTION",
            "\td1\tvalue of the first die (0 if not thrown)",
            "\td2\tvalue of the seconde die (0 if not thrown)",
            "\td3\tvalue of the third die (0 if not thrown)",
            "\td4\tvalue of the fourth die (0 if not thrown)",
            "\td5\tvalue of the fifth die (0 if not thrown)",
            "\tc\texpected combination"
        ]

        print('\n'.join(usage))
        return True
    return False

def argument_parsing_find_error(argv):
    # Checking of the presence of all arguments.
    if len(argv) != 7:
        return True

    # Checking the first five arguments,
    # if they are numbers and present in a die.
    for arg in argv[1:-1]:
        try:
            value_of_the_die = int(arg)
            if 0 <= value_of_the_die <= 6:
                pass
            else:
                return True
        except ValueError:
            return True

    # Checking the last argument,
    # if he respect the pattern of expected combination.
    patterns = (
        "pair_A",
        "three_A",
        "four_A",
        "full_A_B",
        "straight_A",
        "yams_A"
    )

    for pattern in patterns:
        idx_A = pattern.find('A')
        idx_B = pattern.find('B')

        try:
            if 0 <= int(argv[-1][idx_A]) <= 6:
                pass
            else:
                continue
            pattern = pattern.replace('A', argv[-1][idx_A])

            if idx_B != -1:
                if 0 <= int(argv[-1][idx_B]) <= 6:
                    pass
                else:
                    continue
                pattern = pattern.replace('B', argv[-1][idx_B])
        except IndexError:
            pass
        except ValueError:
            continue
        if argv[-1] == pattern:
            # Special case for straight
            if argv[-1].startswith('straight_') and argv[-1][-1] not in '56':
                return True
            return False
    return True

def combination_formatting(expected_combination, value):
    correspondences = {
        'pair': "pair",
        'three': "three-of-a-kind",
        'four': "four-of-a-kind",
        'full': "full of {}",
        'straight': "straight",
        'yams' : "yams"
    }

    if len(value) == 2 and expected_combination == 'full':
        return correspondences[expected_combination].format(value[1])
    else:
        return correspondences[expected_combination]

def a_among_b(a, b):
    return  factorial(a) // (factorial(b) * factorial(a - b))

def binomiale(n, k, p):
    """
    n -- Number of experience
    k -- Number excepted.
    p -- probability to obtained a good tirage.
    q -- probability to obtained a bad tirage. (1 - p)
    """
    return a_among_b(n, k) * pow(p, k) * pow(1 - p, n - k)

def deduce_probability(dice, expected_combination, value):
    occ = dice.count(value[0])
    translation = {'pair': 2, 'three': 3, 'four': 4, 'yams': 5}

    if occ >= translation[expected_combination]:
        return 100.0
    else:
        result, n, p = 0.0, 5 - occ, 1 / 6
        for k in range(translation[expected_combination] - occ, 5 - occ + 1):
            result += binomiale(n, k, p)
        return result * 100.0

def deduce_probability_full(dice, expected_combination, value):
    occ = [dice.count(value[0]), dice.count(value[1])]

    if occ == [3, 2]:
        return 100.0
    else:
        occ[0] = 3 if occ[0] > 3 else occ[0]
        occ[1] = 2 if occ[1] > 2 else occ[1]
        three = a_among_b(5 - occ[0] - occ[1], 3 - occ[0])
        pair = a_among_b(2 - occ[1], 2 - occ[1])
        return ((three * pair) / pow(6, 5 - occ[0] - occ[1])) * 100.0

def deduce_probability_straight(dice, expected_combination, value):
    count = 0

    for i in range(value[0] - 5 + 1, value[0] + 1):
        if i in dice:
            count += 1
    return (factorial(5 - count) / pow(6, 5 - count)) * 100.0

def main(argv):
    # Initialization.
    dice = [int(die) for die in argv[1:-1]]
    expected_combination = argv[-1][:argv[-1].index('_')]
    value = [int(c) for c in argv[-1] if c.isdigit()]

    # Computing probability.
    if expected_combination not in ('full', 'straight'):
        result = deduce_probability(dice, expected_combination, value)
    elif expected_combination == 'full':
        result = deduce_probability_full(dice, expected_combination, value)
    elif expected_combination == 'straight':
        result = deduce_probability_straight(dice, expected_combination, value)

    # Print result.
    print("chances to get a {} {}:  {:.2f}%".format(
        value[0],
        combination_formatting(expected_combination, value),
        result
    ))

if __name__ == "__main__":
    if display_usage(sys.argv):
        sys.exit(0)
    elif argument_parsing_find_error(sys.argv):
        sys.exit(84)
    else:
        main(sys.argv)
        sys.exit(0)
