import string

from utils import clean_word

# {'A': 1, 'B': 2, 'C': 3, 'D': 4, 'E': 5, 'F': 6, 'G': 7, 'H': 8, 'I': 9,
#  'J': 10, 'K': 11, 'L': 12, 'M': 13, 'N': 14, 'O': 15, 'P': 16, 'Q': 17, 'R': 18,
#  'S': 19, 'T': 20, 'U': 21, 'V': 22, 'W': 23, 'X': 24, 'Y': 25, 'Z': 26, ' ': 0}
letter_to_value = {letter: index for index, letter in enumerate(string.ascii_uppercase, start=1)}
letter_to_value[' '] = 0

# {1: 'A', 2: 'B', 3: 'C', 4: 'D', 5: 'E', 6: 'F', 7: 'G', 8: 'H', 9: 'I',
#  10: 'J', 11: 'K', 12: 'L', 13: 'M', 14: 'N', 15: 'O', 16: 'P', 17: 'Q', 18: 'R',
#  19: 'S', 20: 'T', 21: 'U', 22: 'V', 23: 'W', 24: 'X', 25: 'Y', 26: 'Z', 0: ' '}
value_to_letter = {value: key for key, value in letter_to_value.items()}


def word_to_value(word: str) -> int:
    """
    Return the letter value for the given word

    >>> word_to_value('KWANTUMCOMPUTER')
    214
    >>> word_to_value('Hello World')
    124
    """

    word = clean_word(word)
    return sum(letter_to_value[letter] for letter in word)


def get_letter_for_value(value: int, zero_as_space: bool = False) -> str:
    """
    Return the letter that is associated with the value

    :param value: Any integer (even < 0 or > 26)
    :param zero_as_space: Flag to indicate whether a zero indicates a space or not
    :return: A single uppercase letter

    >>> get_letter_for_value(-28, zero_as_space=False)
    X
    >>> get_letter_for_value(-2, zero_as_space=False)
    X
    >>> get_letter_for_value(0, zero_as_space=False)
    Z
    >>> get_letter_for_value(2, zero_as_space=False)
    B
    >>> get_letter_for_value(28, zero_as_space=False)
    B
    >>> get_letter_for_value(-28, zero_as_space=True)
    Z
    >>> get_letter_for_value(-2, zero_as_space=True)
    Y
    >>> get_letter_for_value(0, zero_as_space=True)

    >>> get_letter_for_value(2, zero_as_space=True)
    B
    >>> get_letter_for_value(28, zero_as_space=True)
    A
    """

    if zero_as_space:
        value = value % 27
    else:
        value = value % 26 or 26
    return value_to_letter[value]


if __name__ == '__main__':
    print(letter_to_value)
    print(value_to_letter)
    print(word_to_value('Hello World'))

    for flag_zero_as_space in (True, False):
        print('----')
        print(get_letter_for_value(-28, flag_zero_as_space))
        print(get_letter_for_value(-2, flag_zero_as_space))
        print(get_letter_for_value(-0, flag_zero_as_space))
        print(get_letter_for_value(2, flag_zero_as_space))
        print(get_letter_for_value(28, flag_zero_as_space))

import string

from utils import clean_word

# {'A': 1, 'B': 2, 'C': 3, 'D': 4, 'E': 5, 'F': 6, 'G': 7, 'H': 8, 'I': 9,
#  'J': 10, 'K': 11, 'L': 12, 'M': 13, 'N': 14, 'O': 15, 'P': 16, 'Q': 17, 'R': 18,
#  'S': 19, 'T': 20, 'U': 21, 'V': 22, 'W': 23, 'X': 24, 'Y': 25, 'Z': 26, ' ': 0}
letter_to_value = {letter: index for index, letter in enumerate(string.ascii_uppercase, start=1)}
letter_to_value[' '] = 0

# {1: 'A', 2: 'B', 3: 'C', 4: 'D', 5: 'E', 6: 'F', 7: 'G', 8: 'H', 9: 'I',
#  10: 'J', 11: 'K', 12: 'L', 13: 'M', 14: 'N', 15: 'O', 16: 'P', 17: 'Q', 18: 'R',
#  19: 'S', 20: 'T', 21: 'U', 22: 'V', 23: 'W', 24: 'X', 25: 'Y', 26: 'Z', 0: ' '}
value_to_letter = {value: key for key, value in letter_to_value.items()}


def word_to_value(word: str) -> int:
    """
    Return the letter value for the given word

    >>> word_to_value('KWANTUMCOMPUTER')
    214
    >>> word_to_value('Hello World')
    124
    """

    word = clean_word(word)
    return sum(letter_to_value[letter] for letter in word)


def get_letter_for_value(value: int, zero_as_space: bool = False) -> str:
    """
    Return the letter that is associated with the value

    :param value: Any integer (even < 0 or > 26)
    :param zero_as_space: Flag to indicate whether a zero indicates a space or not
    :return: A single uppercase letter

    >>> get_letter_for_value(-28, zero_as_space=False)
    X
    >>> get_letter_for_value(-2, zero_as_space=False)
    X
    >>> get_letter_for_value(0, zero_as_space=False)
    Z
    >>> get_letter_for_value(2, zero_as_space=False)
    B
    >>> get_letter_for_value(28, zero_as_space=False)
    B
    >>> get_letter_for_value(-28, zero_as_space=True)
    Z
    >>> get_letter_for_value(-2, zero_as_space=True)
    Y
    >>> get_letter_for_value(0, zero_as_space=True)

    >>> get_letter_for_value(2, zero_as_space=True)
    B
    >>> get_letter_for_value(28, zero_as_space=True)
    A
    """

    if zero_as_space:
        value = value % 27
    else:
        value = value % 26 or 26
    return value_to_letter[value]


if __name__ == '__main__':
    print(letter_to_value)
    print(value_to_letter)
    print(word_to_value('Hello World'))

    for flag_zero_as_space in (True, False):
        print('----')
        print(get_letter_for_value(-28, flag_zero_as_space))
        print(get_letter_for_value(-2, flag_zero_as_space))
        print(get_letter_for_value(-0, flag_zero_as_space))
        print(get_letter_for_value(2, flag_zero_as_space))
        print(get_letter_for_value(28, flag_zero_as_space))

# {'A': 1, 'B': 2, 'C': 3, 'D': 4, 'E': 5, 'F': 6, 'G': 7, 'H': 8, 'I': 9, #  'J': 10, 'K': 11, 'L': 12, 'M': 13, 'N': 14, 'O': 15, 'P': 16, 'Q': 17, 'R': 18, #  'S': 19, 'T': 20, 'U': 21, 'V': 22, 'W': 23, 'X': 24, 'Y': 25, 'Z': 26, ' ': 0} # {1: 'A', 2: 'B', 3: 'C', 4: 'D', 5: 'E', 6: 'F', 7: 'G', 8: 'H', 9: 'I', #  10: 'J', 11: 'K', 12: 'L', 13: 'M', 14: 'N', 15: 'O', 16: 'P', 17: 'Q', 18: 'R', #  19: 'S', 20: 'T', 21: 'U', 22: 'V', 23: 'W', 24: 'X', 25: 'Y', 26: 'Z', 0: ' '} Return the letter value for the given word      >>> word_to_value('KWANTUMCOMPUTER')     214     >>> word_to_value('Hello World')     124 Return the letter that is associated with the value      :param value: Any integer (even < 0 or > 26)     :param zero_as_space: Flag to indicate whether a zero indicates a space or not     :return: A single uppercase letter      >>> get_letter_for_value(-28, zero_as_space=False)     X     >>> get_letter_for_value(-2, zero_as_space=False)     X     >>> get_letter_for_value(0, zero_as_space=False)     Z     >>> get_letter_for_value(2, zero_as_space=False)     B     >>> get_letter_for_value(28, zero_as_space=False)     B     >>> get_letter_for_value(-28, zero_as_space=True)     Z     >>> get_letter_for_value(-2, zero_as_space=True)     Y     >>> get_letter_for_value(0, zero_as_space=True)      >>> get_letter_for_value(2, zero_as_space=True)     B     >>> get_letter_for_value(28, zero_as_space=True)     A