Advertisement
SimeonTs

SUPyF Dictionaries - 02. Count Real Numbers

Jun 23rd, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.38 KB | None | 0 0
  1. """
  2. Dictionaries and Functional Programming
  3. Проверка: https://judge.softuni.bg/Contests/Practice/Index/945#1
  4.  
  5. SUPyF Dictionaries - 02. Count Real Numbers
  6.  
  7. Problem:
  8. Read a list of real numbers and print them in ascending order along with their number of occurrences.
  9.  
  10. Examples:
  11. Input:                  Output:
  12. 8 2.5 2.5 8 2.5         2.5 -> 3 times
  13.                        8 -> 2 times
  14. ---------------------------------------
  15. Input:                  Output:
  16. 1.5 5 1.5 3             1.5 -> 2 times
  17.                        3 -> 1 times
  18.                        5 -> 1 times
  19. ----------------------------------------
  20. Input:                  Output:
  21. -2 0.33 0.33 2          -2 -> 1 times
  22.                        0.33 -> 2 times
  23.                        2 -> 1 times
  24.  
  25. Hints:
  26. - Use dictionary (key=nums, value=count) named counts.
  27. - Pass through each input number num and increase counts[num] (when num exists in the dictionary)
  28. or assign counts[num] = 1 (when num does not exist in the dictionary).
  29. - Pass through all numbers num in the dictionary (counts.keys())
  30. and print the number num and its count of occurrences counts[num].
  31. """
  32. nums = [float(item) for item in input().split(" ")]
  33.  
  34. nums.sort()
  35.  
  36. counts = {}
  37.  
  38. for num in nums:
  39.     if num not in counts:
  40.         counts[num] = nums.count(num)
  41.  
  42. for key, value in counts.items():
  43.     print(f"{key} -> {value} times")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement