Click here to Skip to main content
15,877,573 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
sort the unsorted elements and square the elements in it

input:

-9 -2 0 2 3

output:

0 4 4 9 81


instead I am getting :
81 4 0 4 9

What I have tried:

Python
l1 = list(map(int,input().split()))
l2 = sorted(l1)
for i in l2:
    n = i**2
    print(n,sep=" ")
Posted
Updated 22-Feb-22 7:12am
v2
Comments
Richard MacCutchan 22-Feb-22 11:37am    
What is the question?
CPallini 22-Feb-22 11:42am    
It looks a working solution.
shri harshan 22-Feb-22 12:55pm    
sorry forget to mention the error.

1 solution

Based on the requirements ("Sort the unsorted elements and square the elements in it"), the output you are getting is correct.
If you really wish to obtain a sorted list of squares, then you have first to generate a list of squares and then sort it.
Try
Python
lin = list(map(int,input().split())) # original list
lsq = ([x*x for x in lin]) # list of squares
lsq = sorted(lsq)  # sorted list of squares

for sq in lsq:
    print(sq, sep=" ")
 
Share this answer
 
v2
Comments
Maciej Los 23-Feb-22 10:11am    
5ed!
CPallini 23-Feb-22 10:30am    
Thank you, Maciej!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900