import math, cmath;
import operator as op;
import functools as ft;
import itertools as it;
def flatten(listOfLists):
return ft.reduce(op.concat,listOfLists);
def flatten1(listOfLists):
return it.chain.from_iterable(listOfLists);
def tReduce(function, iterable, initializer=None):
it = iter(iterable);
if initializer is None:
value = next(it);
else:
value = initializer;
for element in it:
value = function(value, element);
print(element,' # ',value,'\n');
return value;
def sortList(cList):
tupleList=[(round(z.real,6),round(z.imag,6))
for z in cList];
sortedList=sorted(tupleList,key=lambda t : t[0]);
sortedList=sorted(sortedList,key=lambda t : t[1]);
return [complex(re,im) for (re,im) in sortedList];
def cRoot(z,n):
rn=pow(z,1/n);
cn=2*math.pi/n;
return [rn*cmath.rect(1,cn*k)
for k in range(n)];
cRoot2=ft.partial(cRoot,n=2);
cRoot3=ft.partial(cRoot,n=3);
cRoot4=ft.partial(cRoot,n=4);
cRoot6=ft.partial(cRoot,n=6);
cRoot8=ft.partial(cRoot,n=8);
rows=list(map(int,list('123456789')));
cols=list(map(int,list('123456789')));
def getSudokuCells():
return (mBind(rows, lambda row:
mBind(cols, lambda col:
mUnit((row,col))
)));
def mBind(vList,f):
assert isinstance(vList,list);
return flatten(map(f,vList));
def mUnit(x):
return [x];
def foldWithBind(x,fList):
assert isinstance(fList,list);
return ft.reduce(mBind,fList,mUnit(x));
if __name__ == '__main__':
print('\n# Beginning...');
if False:
x=-1;
y=cRoot(x,2); print(sortList(y));
y=cRoot(x,4); print(sortList(y));
y=cRoot2(x); print(sortList(y));
y=cRoot3(x); print(sortList(y));
y=cRoot6(x); print(sortList(y));
x=-1;
y1=sortList(mBind(mBind(mUnit(x),cRoot2),cRoot3));
y2=sortList(cRoot6(x));
assert y1==y2;
if False:
print(' ',y1,'\n ',y2);
x=-1;
y1=mUnit(x);
y1=mBind(y1,cRoot2);
y1=mBind(y1,cRoot3);
y1=sortList(y1);
y2=sortList(cRoot6(x));
assert y1==y2;
if False:
print(' ',y1,'\n ',y2)
x=-1;
y1=mBind(mBind(mBind(mUnit(x),cRoot2),cRoot2),cRoot2);
y1=sortList(y1);
y2=sortList(cRoot8(x));
assert y1==y2;
if False:
print(' ',y1,'\n\n ',y2);
x=-1; n=3;
y1=mUnit(x);
for ix in range(3):
y1=mBind(y1,cRoot2);
y1=sortList(y1);
y2=sortList(cRoot8(x));
assert y1==y2;
if False:
print(' ',y1,'\n\n ',y2);
x=-1;
fList=[cRoot2,cRoot3];
y1=foldWithBind(x,fList);
y1=sortList(y1);
y2=sortList(cRoot6(x));
assert y1==y2;
if False:
print(' ',y1,'\n\n ',y2);
x=-1;
fList=[cRoot2,cRoot2,cRoot2];
y1=foldWithBind(x,fList);
y1=sortList(y1);
y2=sortList(cRoot8(x));
assert y1==y2;
if False:
print(' ',y1,'\n\n ',y2);
sudokuCells1=getSudokuCells();
Range9=range(1,10);
sudokuCells2=[(i,k) for i in Range9
for k in Range9];
assert sudokuCells1==sudokuCells2;
print('\n# Finished\n');