Advertisement
nanokatka

collections3

Feb 17th, 2020
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.56 KB | None | 0 0
  1. """
  2. 3. Generate a list of X random integer numbers from 1 to 1000 (X must
  3. be entered from keyboard). Then, create a dictionary of next structure:
  4.  
  5. {
  6. “even”: 563,
  7. “odd”: 437,
  8. “avg”: 586.43,
  9. “max”: 999,
  10. “min”: 2
  11. }
  12.  
  13. Print the dictionary to the screen.
  14.  
  15. """
  16. from random import randint
  17. import statistics as st
  18. import matplotlib.pyplot as plt
  19.  
  20. number=input("Enter number: ")
  21. randnumbers=[]
  22. number=int(number)
  23. for i in range(number):
  24. randnumbers.append(randint(1,1000))
  25.  
  26. #my stupid solution
  27. # even=0
  28. # odd=0
  29. # for item in randnumbers:
  30. # if item %2 == 0:
  31. # even +=1
  32. # else:
  33. # odd +=1
  34.  
  35. #better one, inspired from internet
  36. evennumbers=[num for num in randnumbers if num % 2 == 0]
  37. even=len(evennumbers)
  38. odd=len(randnumbers)-even
  39.  
  40. #print(min(randnumbers),max(randnumbers),st.mean(randnumbers), even, odd)
  41. dictionary={
  42. "even" : even,
  43. "odd" : odd,
  44. "avg" : st.mean(randnumbers),
  45. "max" : max(randnumbers),
  46. "min" : min(randnumbers)
  47. }
  48. print(dictionary)
  49.  
  50. plt.hist(randnumbers, bins=100, label='all')
  51. plt.hist(evennumbers, bins=100, label='even')
  52. plt.axvline(st.mean(randnumbers), color='k', linestyle='dashed',linewidth='1')
  53. min_ylim, max_ylim = plt.ylim()
  54. plt.text(st.mean(randnumbers)*1.05, max_ylim*0.55,'Mean: {:.2f}'.format(st.mean(randnumbers)))
  55. plt.text(st.mean(randnumbers)*0.1, max_ylim*0.1,'Even: {:}'.format(even))
  56. plt.text(st.mean(randnumbers)*0.1, max_ylim*0.55,'Odd: {:}'.format(odd))
  57. plt.legend(loc='upper right')
  58. plt.xlabel("number")
  59. plt.ylabel("occurence")
  60. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement