Advertisement
SimeonTs

SUPyF Lists - 10. Square Numbers

Jun 15th, 2019
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.43 KB | None | 0 0
  1. """
  2. Lists
  3. Check your answer: https://judge.softuni.bg/Contests/Practice/Index/924#9
  4.  
  5. 10. Square Numbers
  6.  
  7. Problem:
  8. Read a list of integers and extract all square numbers from it and print them in descending order.
  9. A square number is an integer which is the square of any integer. For example, 1, 4, 9, 16 are square numbers.
  10. Examples
  11. Input                       Output
  12. 3 16 4 5 6 8 9              16 9 4
  13. 12 1 9 4 16 8 25 49 16      49 25 16 16 9 4 1
  14. """
  15.  
  16. """
  17. How I solved it:
  18.  
  19. First we will take all the added integers on one line divided by space and it will add them in a new list one by one:
  20. Then we need to create a second list which will contain all the numbers we want.
  21. Then using a For Loop we will check for each number in the first list if the number
  22. is bigger than 0(<0 can't be a perfect square (and it will crash :()) AND one wicked formula to check if the number
  23. is a square number. And then if both are True to add them in the new List.
  24. Then we need to organise the list in descending order using (.sort(reverse=True)).
  25. And Finally to print it, we will use a for loop. So they can be printed on one line with blank space in between.
  26. """
  27. nums = [int(item) for item in input().split(" ")]
  28. square_numbers = []
  29.  
  30. for num in nums:
  31.     if num > 0 and int(num**0.5)**2 == int(num):
  32.         square_numbers += [num]
  33.  
  34. square_numbers.sort(reverse=True)
  35.  
  36. for square_number in square_numbers:
  37.     print(square_number, end=" ")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement