This assignment was published long ago in ITmedia.
I finished the assignment about four hours.
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]