Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """
- 3. Generate a list of X random integer numbers from 1 to 1000 (X must
- be entered from keyboard). Then, create a dictionary of next structure:
- {
- “even”: 563,
- “odd”: 437,
- “avg”: 586.43,
- “max”: 999,
- “min”: 2
- }
- Print the dictionary to the screen.
- """
- from random import randint
- import statistics as st
- import matplotlib.pyplot as plt
- number=input("Enter number: ")
- randnumbers=[]
- number=int(number)
- for i in range(number):
- randnumbers.append(randint(1,1000))
- #my stupid solution
- # even=0
- # odd=0
- # for item in randnumbers:
- # if item %2 == 0:
- # even +=1
- # else:
- # odd +=1
- #better one, inspired from internet
- evennumbers=[num for num in randnumbers if num % 2 == 0]
- even=len(evennumbers)
- odd=len(randnumbers)-even
- #print(min(randnumbers),max(randnumbers),st.mean(randnumbers), even, odd)
- dictionary={
- "even" : even,
- "odd" : odd,
- "avg" : st.mean(randnumbers),
- "max" : max(randnumbers),
- "min" : min(randnumbers)
- }
- print(dictionary)
- plt.hist(randnumbers, bins=100, label='all')
- plt.hist(evennumbers, bins=100, label='even')
- plt.axvline(st.mean(randnumbers), color='k', linestyle='dashed',linewidth='1')
- min_ylim, max_ylim = plt.ylim()
- plt.text(st.mean(randnumbers)*1.05, max_ylim*0.55,'Mean: {:.2f}'.format(st.mean(randnumbers)))
- plt.text(st.mean(randnumbers)*0.1, max_ylim*0.1,'Even: {:}'.format(even))
- plt.text(st.mean(randnumbers)*0.1, max_ylim*0.55,'Odd: {:}'.format(odd))
- plt.legend(loc='upper right')
- plt.xlabel("number")
- plt.ylabel("occurence")
- plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement