PyD*_*Der 1 python permutation
我正在尝试在此txt文件中找到另一个数字的排列,然后将它们添加到列表中:
167
168
148
143
289
194
683
491
Run Code Online (Sandbox Code Playgroud)
在此文件中,排列为491和194.
这是我的代码:
numberList = []
file = open("file.txt", "r")
for line in file:
numberList.append(line)
Run Code Online (Sandbox Code Playgroud)
现在将它们添加到numberList,如何从此列表中删除它们(491和194).
好吧,让我们做一些群论:
假设您可以将您的数字x分解为数字X [i](删除\n它真的是微不足道的,当然其他人会介绍这个).
然后我们知道十进制系统如何工作,
![$$ x =\sum_ {i = 0} ^ {\ log_ {10} x} x [i] 10 ^ i $$](https://i.stack.imgur.com/9ut2V.png)
我们需要找到的是一个函数y = f(x),它将相同数字的排列的x'映射到相同的y,但x'不是不同y的排列.
我们可以使用这样的事实:不同数字的素数因子分解是不同的,并且简单地找到不是数字(并且大于数字*数字的长度)的素数的指数的总和.如果我们假设少于9位,这会变得更容易,所以我们会这样做.
对于这个例子,让我们坚持到17(这进一步限制了我们的数字位数,但是很好).
![$$ y = f(x)=\sum_ {i = 0} ^ {6} 17 ^ {x [i]} $$](https://i.stack.imgur.com/Q1Yhi.png)
所以,现在你将这个函数用作python中的比较键set并完成.
所以,非常天真(并且非常未经测试):
class permutation(object):
def __init__(self, x):
"""
we'll assume x is an integer
"""
digits = str(x)
self.original = x
self._hash = sum([17**int(digit) for digit in digits])
def __hash__(self):
return self._hash
def __eq__(self,other):
return self._hash == hash(other)
def __ne__(self,other):
return self._hash != hash(other)
permutation_free = set()
file = open("file.txt", "r")
for line in file:
x = int(line)
permutation_free.add(permutation(x))
print [perm.original for perm in permutation_free]
Run Code Online (Sandbox Code Playgroud)
编辑:甚至测试过它.