後輩がｘを求めたがってたので、僕も求めてみた。

## プログラム

```py:AskX.py
"""
後輩がｘを求めたがってたので、僕も求めてみた。
"""

import re
import sys

def ask_case_of_left(formula):
    answer    = int(formula[-1])
    tilt      = int(re.search("\d+", formula[0]).group(0)) if re.search("\d+", formula[0]) else 1
    right_num = int(formula[2])
    operator  = formula[1]
    if   operator == "+": return (answer - right_num) / tilt
    elif operator == "-": return (answer + right_num) / tilt
    elif operator == "*": return (answer / right_num) / tilt
    elif operator == "/": return (answer * right_num) / tilt

def ask_case_of_right(formula):
    answer    = int(formula[-1])
    left_num  = int(formula[0])
    tilt      = int(re.search("\d+", formula[2]).group(0)) if re.search("\d+", formula[2]) else 1
    operator  = formula[1]
    if   operator == "+": return (answer - left_num) / tilt
    elif operator == "-": return ((answer - left_num) * -1) / tilt
    elif operator == "*": return (answer / left_num) / tilt
    elif operator == "/": return (left_num / answer) / tilt

def ask_case_of_answer(formula):
    tilt      = int(re.search("\d+", formula[-1]).group(0)) if re.search("\d+", formula[-1]) else 1
    left_num  = int(formula[0])
    right_num = int(formula[2])
    operator  = formula[1]
    if   operator == "+": return (left_num + right_num) / tilt
    elif operator == "-": return (left_num - right_num) / tilt
    elif operator == "*": return (left_num * right_num) / tilt
    elif operator == "/": return (left_num / right_num) / tilt

def ask(formula):
    formula = formula.split(" ")
    if   "x" in formula[0]:  return ask_case_of_left(formula)
    elif "x" in formula[2]:  return ask_case_of_right(formula)
    elif "x" in formula[-1]: return ask_case_of_answer(formula)
    else: return " ".join(formula)

print(ask(sys.argv[1]))
```

適当に組んだけど、右辺パターンあってるのかな。。。

## 実行結果

指数部とか基数部とかあるけど、`2x`の`2`って傾き？係数？なに？（無能）

### パターン１：ｘなし

```sh
$ python AskX.py "2 + 8   = 10"
2 + 8 = 10

$ python AskX.py "2 - -8  = 10"
2 - -8 = 10

$ python AskX.py "2 * 5   = 10"
2 * 5 = 10

$ python AskX.py "2 / 0.1 = 10"
2 / 0.1 = 10
```

### パターン２：左辺にｘ（傾きなし）

```sh
$ python AskX.py "x + 2 = 10"
8.0

$ python AskX.py "x - 2 = 10"
12.0

$ python AskX.py "x * 2 = 10"
5.0

$ python AskX.py "x / 2 = 10"
20.0

$ python AskX.py "x + 3 = 12"
9.0

$ python AskX.py "x - 3 = 12"
15.0

$ python AskX.py "x * 3 = 12"
4.0

$ python AskX.py "x / 3 = 12"
36.0
```

### パターン３：右辺にｘ（傾きなし）

```sh
$ python AskX.py "2 + x = 10"
8.0

$ python AskX.py "2 - x = 10"
-8.0

$ python AskX.py "2 * x = 10"
5.0

$ python AskX.py "2 / x = 10"
0.2

$ python AskX.py "3 + x = 12"
9.0

$ python AskX.py "3 - x = 12"
-9.0

$ python AskX.py "3 * x = 12"
4.0

$ python AskX.py "3 / x = 12"
0.25
```

### パターン４：答えにｘ（傾きなし）

```sh
$ python AskX.py "2 + 10 = x"
12.0

$ python AskX.py "2 - 10 = x"
-8.0

$ python AskX.py "2 * 10 = x"
20.0

$ python AskX.py "2 / 10 = x"
0.2

$ python AskX.py "3 + 10 = x"
13.0

$ python AskX.py "3 - 10 = x"
-7.0

$ python AskX.py "3 * 10 = x"
30.0

$ python AskX.py "3 / 10 = x"
0.3
```

### パターン５：左辺にｘ（傾きあり）

```sh
$  AskX.py "2x + 2 = 10"
4.0

$  AskX.py "2x - 2 = 10"
6.0

$  AskX.py "2x * 2 = 10"
2.5

$  AskX.py "2x / 2 = 10"
10.0

$  AskX.py "3x + 3 = 12"
3.0

$  AskX.py "3x - 3 = 12"
5.0

$  AskX.py "3x * 3 = 12"
1.3333333333333333

$  AskX.py "3x / 3 = 12"
12.0
```

### パターン６：右辺にｘ（傾きあり）

```sh
$  AskX.py "2 + 2x = 10"
4.0

$  AskX.py "2 - 2x = 10"
-4.0

$  AskX.py "2 * 2x = 10"
2.5

$  AskX.py "2 / 2x = 10"
0.1

$  AskX.py "3 + 3x = 12"
3.0

$  AskX.py "3 - 3x = 12"
-3.0

$  AskX.py "3 * 3x = 12"
1.3333333333333333

$  AskX.py "3 / 3x = 12"
0.08333333333333333
```

### パターン７：答えにｘ（傾きあり）

```sh
$  AskX.py "2 + 10 = 2x"
6.0

$  AskX.py "2 - 10 = 2x"
-4.0

$  AskX.py "2 * 10 = 2x"
10.0

$  AskX.py "2 / 10 = 2x"
0.1

$  AskX.py "3 + 10 = 3x"
4.333333333333333

$  AskX.py "3 - 10 = 3x"
-2.3333333333333335

$  AskX.py "3 * 10 = 3x"
10.0

$  AskX.py "3 / 10 = 3x"
0.09999999999999999
```

## 最後に

もっとシンプルにできる気がする。

指数とかマイナス値も考慮したい。