Answer to Question #162910 in Python for Ruturaj

Question #162910

Q1. Write a program to input n elements of a list and display  the following pattern:                         

Eg. If the elements entered by the user is :  1  2   3   4   5

The pattern printed should be:

1 0 0 0 0

1 2 0 0 0

1 2 3 0 0

1 2 3 4 0

1 2 3 4 5

Q2. Write a program to input a list of n elements and print all the prime numbers(if any) in the list. If the list does not contain any prime number,display ”No Prime Number in the List"

Q3. Write a program to input a list of N elements and rearrange it so that all odd elements appears after all even elements of the list.

Eg. If the entered list is:  1  2  3  4  5  6

Then the output list should be:

2  4  6  1  3




1
Expert's answer
2021-02-11T04:22:52-0500

First (Q1):

numbers = input().split()
len_numbers = len(numbers)

result = ''
for number in numbers:
  result += number
  print(result.ljust(len_numbers, '0'))

Second (Q2):

def isPrime(n):
  if n % 2 == 0:
    return n == 2
     
  d = 3
  while d * d <= n and n % d != 0:
    d += 2
  return d * d > n
   
numbers = list(map(int, input().split()))

prime_numbers = []
for each in numbers:
  if isPrime(each):
    prime_numbers.append(each)

print(prime_numbers)

Third (Q3):

numbers = list(map(int, input().split()))

even_numbers = []
odd_numbers = []
for each in numbers:
  if each % 2 == 0:
    even_numbers.append(each)
  else:
    odd_numbers.append(each)
   
print(sorted(even_numbers) + sorted(odd_numbers))

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