Answer to Question #296119 in Python for Shreyas

Question #296119

Narendra is given a list containing non-repetitive numbers. You need to write a python function se-L(X) which takes input list X and returns five lists , one contains all prime numbers in A , 2nd one contain composite numbers in A , the third list contains all in A which are divisible by 2 , 4th and 5th list contain numbers divisible by 3 and 5 respectively

For example if X = [1,2,3,4,5,12,25,30,37] then 1st list = [2,3,5,37] , 2nd list=[4,12,25,30]

3rd list=[2,4,12,30] , 4th list=[3,12,30] and 5th list=[5,25,30] . Specify input in fixed form.


1
Expert's answer
2022-02-10T09:49:11-0500
def prime(N):
	for d in range(2,N):
		if N % d == 0:
			return False
	if N <= 1:
		return False
	return True


def composite(N):
	for d in range(2,N):
		if N % d == 0:
			return True
	return False


def se_L(X):
	l1,l2,l3,l4,l5 = [],[],[],[],[]
	for el in X:
		if prime(el):
			l1.append(el)
		if composite(el):
			l2.append(el)
		if el%2 == 0:
			l3.append(el)
		if el%3 == 0:
			l4.append(el)
		if el%5 == 0:
			l5.append(el)
	return l1,l2,l3,l4,l5


X = [1,2,3,4,5,12,25,30,37]
print(*se_L(X))

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