The answer to makeplex assignment with python for coding skill checking #makeplex

This assignment was published long ago in [ITmedia](http://www.itmedia.co.jp/enterprise/articles/1004/03/news002_2.html).
I finished the assignment about four hours.

```python
import itertools
import sys

def searchOrder(l):
    global out
    for i in range(len(l)):
        for n in range(1,8):
            if (str(n) in l) and (str(n+1) in l) and (str(n+2) in l):    
                ls = list(l)
                ls.remove(str(n))
                ls.remove(str(n+1))
                ls.remove(str(n+2))
                out.append(str(n)+str(n+1)+str(n+2))
                return ls
    return l

def searchSame(l,c=3):
    global out
    for i in range(1,10):
        if (l.count(str(i))==c):
            _out = ""
            for _i in range(c):
                _out = _out + str(i)
            if (c==3):
                out.append(_out)
            else:
                return str(i)
            ls = list(l)
            for _i in range(c):
                ls.remove(str(i))
            return ls
    return l

def defineOut(l,sl):
    global out;
    out = []
    for sm in sl:
        #print(sm)
        blen = len(l)
        if (sm == "o"):
            rl = searchOrder(l)
            if (rl==l):
                return False
            else:
                l = rl
        else:
            rl = searchSame(l)
            if (rl==l):
                return False
            else:
                l = rl
#        print(l)
    return l

p = ""
def defineHead(r):
    global out,outs,p
    out = sorted(out)
    if not (out in outs):
        outs.append(out)
        p = ""
        for o in out:
            p = p + "(" + o + ")"
        bp = p
        return bp
    return False

l = sys.argv[1]
ms = ['o', 's']
outs = []
for c in l:
    nl = list(l)
    nl.remove(c)
    for ml in list(itertools.product(ms,ms,ms,ms)):
        r = defineOut(str(nl),ml)
        if (r):
            bp = defineHead(r)
            if (bp):
                print(bp + "[" + c + "]")

for cl in list(itertools.combinations(l,2)):
    d1 = abs(int(cl[1])-int(cl[0]))
    if (d1>1):
        continue
    nl = list(l)
    nl.remove(cl[0])
    nl.remove(cl[1])
    for ml in list(itertools.product(ms,ms,ms)):
        r = defineOut(str(nl),ml)
        if (r):
            bp = defineHead(r)
            if (bp):
                for o in out:
                    for co in o:
                        nl.remove(co)
                d2 = abs(int(nl[1])-int(nl[0]))
                if (d2>1 or (d1==1 and d2==1)):
                    continue
                elif (d1==0 and d2==0):
                    print(bp + "(" + nl[0] + nl[1] + ")" + "[" + cl[0] + cl[1] + "]")
                    print(bp + "(" + cl[0] + cl[1] + ")" + "[" + nl[0] + nl[1] + "]")
                elif (d1==0):
                    print(bp + "(" + cl[0] + cl[1] + ")" + "[" + nl[0] + nl[1] + "]")
                elif (d2==0):
                    print(bp + "(" + nl[0] + nl[1] + ")" + "[" + cl[0] + cl[1] + "]")
```

```
$ python a.py 1112224588899
(111)(222)(888)(99)[45]
$ python a.py 1122335556799
(123)(123)(567)(99)[55]
(123)(123)(567)(55)[99]
(123)(123)(555)(99)[67]
$ python a.py 1112223335559
(123)(123)(123)(555)[9]
(111)(222)(333)(555)[9]
$ python a.py 1223344888999
(234)(234)(888)(999)[1]
(123)(234)(888)(999)[4]
(123)(888)(999)(44)[23]
$ python a.py 1112345678999
(111)(345)(678)(999)[2]
(111)(234)(678)(999)[5]
(111)(234)(567)(999)[8]
(123)(456)(789)(99)[11]
(123)(456)(789)(11)[99]
(123)(456)(999)(11)[78]
(345)(678)(999)(11)[12]
(111)(456)(789)(99)[23]
(123)(678)(999)(11)[45]
(111)(234)(789)(99)[56]
(111)(234)(567)(99)[89]
```

# Refs.
* [萬子](http://majandofu.com/character-tiles)
* [チンイツ](http://majandofu.com/mahjong-hand-full-flush)
* [槓子](http://majandofu.com/mahjong-glossary-kantsu)
* [面前](http://www.xn--y5q537nn1aq9s.jp/knowledge/%E9%9D%A2%E5%89%8D%E3%81%A8%E9%B3%B4%E3%81%8D.html)
* [九蓮宝燈](http://www2.odn.ne.jp/~cbm15900/html/y13.html)
* [find](http://www2.odn.ne.jp/~cbm15900/html/y13.html)
* [if not](https://www.sejuku.net/blog/20674)
* [print(...,end="")](https://stackoverflow.com/questions/493386/how-to-print-without-newline-or-space)
* [global](https://stackoverflow.com/questions/10588317/python-function-global-variables)
* [直積](https://qiita.com/junkls/items/10384950963056cc8e08)
* [simple iteration](https://www.geeksforgeeks.org/loops-and-loop-control-statements-continue-break-and-pass-in-python/)
* [sorted](https://stackoverflow.com/questions/8866652/determine-if-2-lists-have-the-same-elements-regardless-of-order)
* [sorted(...,reverse=True)](https://stackoverflow.com/questions/4183506/python-list-sort-in-descending-order)
* [combination](https://qiita.com/zaburo/items/9a8564edfb81e02599ce)