[英]Take user input and come up print if the combination of concatenations are prime numbers
I am trying to figure how to take a user input of numbers that are comma separated (eg: 1,7) and print out if their concatenated combinations are prime numbers (eg. 7, 17, 71).我试图弄清楚如何获取用户输入的逗号分隔的数字(例如:1,7)并打印出它们的连接组合是否是素数(例如 7、17、71)。
So far, the steps i take is:到目前为止,我采取的步骤是:
#convert user input into a list
values = input("Input some comma separated numbers : ")
num = values.split(",")
But i do not know what to do after to come up with all their combination in a form of a list like: num_combination = [1, 7, 17, 71].但是我不知道在以列表的形式提出所有组合之后该怎么做: num_combination = [1, 7, 17, 71]。
If i can convert them like the above, i think i can write a function like:如果我可以像上面那样转换它们,我想我可以写一个 function 像:
answer = []
for i in num_combination:
if i > 1 and i%i ==0:
answer.append(i)
else:
pass
Thank you very much for your help!非常感谢您的帮助!
So, I wasn't planning to answer this question, because I felt that the comments I had left would put you on the right track, but the question you have posted seems to have some interesting sub-problems, that would be interesting (and hopefully useful) to tackle.所以,我不打算回答这个问题,因为我觉得我留下的评论会让你走上正轨,但是你发布的问题似乎有一些有趣的子问题,那会很有趣(而且希望有用)来解决。
You currently have this code:您目前拥有以下代码:
#convert user input into a list
values = input("Input some comma separated numbers : ")
num = values.split(",")
Now, this code is part of what needs to be done.现在,这段代码是需要做的一部分。 There's actually one step missing in the code mentioned above, because right now,
num
is an array containing strings上面提到的代码实际上少了一步,因为现在
num
是一个包含字符串的数组
>>> num
["1", "7"]
To convert it into an array of numbers, you will also need to run the int
function on all elements in the array.要将其转换为数字数组,您还需要对数组中的所有元素运行
int
function。 There are multiple ways of doing this:有多种方法可以做到这一点:
new_num = list(map(int, num))
# OR
new_num = []
for v in num: new_num.append(int(v))
# OR
new_num = [int(v) for v in num]
Now, luckily for us, Python provides us with a utility that does at least part of our work for us.现在,对我们来说幸运的是,Python 为我们提供了一个实用程序,它至少可以为我们完成部分工作。
itertools.permutations
. itertools.permutations
。 This function takes two parameters: the array, and the number of elements to provide as permutations:此 function 采用两个参数:数组和作为排列提供的元素数量:
>>> itertools.permutations(['A','B','C'], 2)
[('A', 'B'), ('A', 'C'), ('B', 'A'), ('B', 'C'), ('C', 'A'), ('C', 'B')]
But for our case, we need all length permutations, so we'll have to do something a little more complex:但是对于我们的例子,我们需要所有长度的排列,所以我们必须做一些更复杂的事情:
num_permutations = []
for i in range(1,len(num)):
num_permutations += list(itertools.permutations(num, i))
There are other ways to get this list of permutations, without having a native function, but they're a little more detailed than necessary for this solution.还有其他方法可以获取此排列列表,而无需使用本机 function,但它们比此解决方案所需的要详细一些。 I'll cover them if I have time later.
如果以后有时间,我会介绍它们。
The definition of a prime number is a number that has prime factors of just 1
and itself.素数的定义是一个只有
1
和自身的素因数的数。 That means, to check if k
is a prime number, one needs to ensure that all the numbers in ( 2
, 3
, ..., k-2
, k-1
) do not divide k
.这意味着,要检查
k
是否为质数,需要确保 ( 2
, 3
, ..., k-2
, k-1
) 中的所有数字不整除k
。
The easiest way to do that is最简单的方法是
def is_prime(k):
prime = True
for i in range(2,k):
if k%i == 0:
prime = False
break
return prime
There are a lot of optimisations that can be done on this, but again, I won't go into them right now.可以对此进行很多优化,但是我现在不会再对它们进行 go 了。
There is one small edge-case that I have left out above.我在上面遗漏了一个小的边缘案例。 How do I convert a tuple of digits into an actual number?
如何将一组数字转换为实际数字? That's part of what the code below does:
这是下面代码的一部分:
def is_prime(k):
prime = True
for i in range(2, k):
if k%i == 0:
prime = False
break
return prime
values = input("Input some comma separated numbers : ")
num = map(int, values.split(","))
permutations = []
for i in range(1, len(num)):
for t in itertools.permutations(num, i):
str_num = int("".join(map(str, t)))
permutations.append(str_num)
answer = []
for i in permutations:
if is_prime(i):
answer.append(i)
print(answer)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.