Computer Science, asked by s1282palak24416, 28 days ago

Write a program in Python which takes a list as input and replaces all prime number(s) by

the reverse of that prime number.

Example: List [45, 19, 15, 23, 88, 85]

After program execution [45, 91, 15, 32, 88, 85]​

Answers

Answered by allysia
0

Language:

Python

Program:

def prime(n):

   for i in range(2,n):

       if n%i==0:

           return False

           break      

   else:

       return True

a=[45, 19, 15, 23, 88, 85]

b=[]

for i in a:    

   if prime(i)==True:

       last=i%10

       quo= i//10

       b.append(last*10 + quo)

   else:

       b.append(i)

a=b

print(a)

Output:

Consider the attachment.

Explanation:

  • Define a prime function.
  • Use if-else statement to check if the number is prime using prime function.
  • If yes then exchange the terms and add it in a new list.
  • If no then add it in a new list as it is.
  • Assign the older list the new list's value.

Attachment:

Attachments:
Similar questions