import re
import functools
def compose_cycles(input_string):
# Split the cycles by regex
cycles = re.findall("\(([\d,]+)\)", input_string)
# Break each cycle into a list of integers
cycles = [list(map(int, x.split(","))) for x in cycles]
# Make each cycle into a Sympy Permutation
cycles = [Permutation([x]) for x in cycles]
composition = functools.reduce(lambda x, y: y * x, cycles)
return str(composition)
compose_cycles('(1,6,5,3)(1,4,2,3)')
Sympy包很好地处理循环置换。您编写排列的方法称为“不相交循环表示法”。下面是一个使用您的周期的示例:
这为
new_perm
提供了输出(142)(365)
对于这些循环中的任何一个,您都可以像调用函数一样调用它们。例如,我们可以将
1
输入到new_perm
,并期望4
作为输出:编辑
辛置换可以用作将循环置换组合在一起的函数的构造块。原始问题要求输入和输出字符串。以下是一个示例(您可能需要根据字符串输入进行修改):
函数的最后一行调用
str
,它返回字符串表示(而不是原始排列)。我们的输出是'(1 4 2)(3 6 5)'
相关问题 更多 >
编程相关推荐