Advertisement
Spocoman

03. Histogram

Dec 24th, 2021 (edited)
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.81 KB | None | 0 0
  1. n = int(input())
  2. p1 = 0
  3. p2 = 0
  4. p3 = 0
  5. p4 = 0
  6. p5 = 0
  7.  
  8. for i in range(n):
  9.     p = int(input())
  10.     if p < 200:
  11.         p1 += 1
  12.     elif 200 <= p < 400:
  13.         p2 += 1
  14.     elif 400 <= p < 600:
  15.         p3 += 1
  16.     elif 600 <= p < 800:
  17.         p4 += 1
  18.     else:
  19.         p5 += 1
  20.  
  21. print(f'{p1 / n * 100:.2f}%')
  22. print(f'{p2 / n * 100:.2f}%')
  23. print(f'{p3 / n * 100:.2f}%')
  24. print(f'{p4 / n * 100:.2f}%')
  25. print(f'{p5 / n * 100:.2f}%')
  26.  
  27.  
  28. Решение с list() и тернарен оператор:
  29.  
  30. parameters = {'p1': 0, 'p2': 0, 'p3': 0, 'p4': 0, 'p5': 0}
  31.  
  32. n = int(input())
  33.  
  34. for i in range(n):
  35.     p = int(input())
  36.     parameters['p5' if p >= 800 else 'p4' if p >= 600 else 'p3' if p >= 400 else 'p2' if p >= 200 else 'p1'] += 1
  37.  
  38. for p in parameters.values():
  39.     print(f'{p / n * 100:.2f}%')
  40.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement