Answer to Question #296011 in Python for Michael

Question #296011

write a python function Separate_List(A) which take list A as input and return five lists, one contains all prime numbers in A, second one contains composite numbers in A, the third list contains all numbers in A which are neither divisible by 2, fourth and fifth list contains number divisible by 3 and 5 respectively.

 

Example-1

Example-2

Input:

1

2

3

4

5

10

15

20

30

 

Output:

[2, 3, 5]

[4, 10, 15, 20, 30]

[2, 4, 10, 20, 30]

[3, 15, 30]

[5, 10, 15, 20, 30]

Input:

1

3

5

6

7

8

9

10

 

Output:

[3, 5, 7]

[6, 8, 9, 10]

[6, 8, 10]

[3, 9]

[5, 10]



1
Expert's answer
2022-02-10T08:42:43-0500
def is_prime(num):
	for i in range(2,num//2+1):
		if num%i == 0:
			return False
	return True


def Separate_List(A):
	prime = []
	conposite = []
	not_div_2 = []
	div_3 = []
	div_5 = []
	for num in A:
		if num > 1:
			if is_prime(num):
				prime.append(num)
			else:
				conposite.append(num)
		if num % 2 == 0:
			not_div_2.append(num)
		if num % 3 == 0:
			div_3.append(num)
		if num % 5 == 0:
			div_5.append(num)
	return prime, conposite, not_div_2, div_3, div_5




A = []
while True:
	n = input()
	if n:
		A.append(int(n))
	else:
		break
print(*Separate_List(A), sep="\n")

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS