Answer to Question #314272 in Python for Dhea M. Maddela

Question #314272

Develop a python application that will randomly select n integers from 1 to 9 and will count the number of occurrence of the numbers without using the Counter from collections library.



Sample Output 1:

How many numbers?: 5

[4, 6, 8, 3, 3]

1-4

1-6

2-3


I need the code to have an output stated above.


1
Expert's answer
2022-03-19T04:13:55-0400
import random

n = int(input("How many numbers?: "))
arr=[]
count = [0] * 9
for i in range(n):
   arr.append(random.randint(1, 9))
   count[arr[i]-1] += 1
print(arr)
for i in range(8):
   if count[i] > 0:
      print(f"{count[i]} - {i+1}")

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS