from typing import Tuple
from typing import Callable
import re
import operator


def parse_condition(condition: str) -> Tuple[Callable[[int, int], bool], int]:
    """
    Parse a string representing a comparison like 'x < 1', returning
    a function representing the operation, and the int value 1,
    in the example case.

    If a string like '1 > x' is used, it will be converted to 'x < 1' first.
    """
    condition_regex = re.compile(r'^(x|\d+) ?(<|>|<=|>=|==|!=) ?(x|\d+)')
    matches = condition_regex.match(str(condition))
    if not matches:
        raise ValueError('Unrecognized condition string')

    if matches[1] == 'x':
        op, val = matches[2], int(matches[3])
    else:
        op, val = matches[2], int(matches[1])

    op_dict = {
        '<': operator.lt,
        '<=': operator.le,
        '==': operator.eq,
        '!=': operator.ne,
        '>=': operator.ge,
        '>': operator.gt,
    }

    opposite_op = {'<': '>', '<=': '>=', '==': '==',
                   '>': '<', '>=': '<=', '!=': '!='}

    try:
        if matches[1] == 'x':
            real_op = op_dict[op]
        else:
            real_op = op_dict[opposite_op[op]]
    except KeyError:
        raise ValueError('Unrecognized operation "%s"' % op)

    return real_op, val