groups2.html


001| ####################################################################################################
002| from itertools import permutations
003| ####################################################################################################
004| 
005| permstr = lambda p: f"({''.join([str(n) for n in p])})"
006| 
007| P2 = [[int(c) for c in p] for p in permutations('1234', 2)]
008| P3 = [[int(c) for c in p] for p in permutations('1234', 3)]
009| P4 = [[int(c) for c in p] for p in permutations('1234', 4)]
010| 
011| Base = [1, 2, 3, 4]
012| Perms = dict()
013| 
014| ##================================================================================================##
015| def list_permutations (ps):
016|     for p in ps:
017|         # Do the permutation...
018|         b = list(Base)
019|         p.append(p[0])
020|         for ix0,ix1 in zip(p,p[1:]):
021|             b[ix1-1] = Base[ix0-1]
022|         p.pop()
023|         # Add permutation to dictionary...
024|         k = tuple(b)
025|         if k in Perms:
026|             Perms[k].append(p)
027|         else:
028|             Perms[k] = [p]
029| 
030| ##================================================================================================##
031| def list_all_permutations ():
032|     # Build dictionary of permutations...
033|     list_permutations(P2)
034|     list_permutations(P3)
035|     list_permutations(P4)
036| 
037|     # List dictionary...
038|     for ix,p in enumerate(P4):
039|         k = tuple(p)
040|         if k in Perms:
041|             ps = [permstr(p) for p in Perms[k]]
042|             sp = ', '.join(ps)
043|             print(f'{ix+1:2d}: {list(k)}: {sp}')
044|         else:
045|             print(f'{ix+1:2d}: {list(k)}:')
046| 
047| 
048| ##================================================================================================##
049| def main ():
050|     list_all_permutations()
051| 
052| ####################################################################################################
053| if __name__ == '__main__':
054|     print()
055|     main()
056|     print()
057| ####################################################################################################
058| '''eof'''
059|