Question

Using Python, Lists: Write a program that randomly generates 1000 numbers between 0 and 9 and...

Using Python,

Lists: Write a program that randomly generates 1000 numbers between 0 and 9 and counts the count of each number. The output should have this form:

3 occurs 5 times

5 occurs 1 time

Notice that if there is more than 1. The word times is used and if there is only 1, the word times is used.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
import random

d = {}

for i in range(1000):
    num = random.randint(0, 9)
    if num not in d:
        d[num] = 0
    d[num] += 1

for num in sorted(d.keys()):
    count = d[num]
    if count == 1:
        print("{} occurs {} time".format(num, count))
    elif count > 1:
        print("{} occurs {} times".format(num, count))
Add a comment
Know the answer?
Add Answer to:
Using Python, Lists: Write a program that randomly generates 1000 numbers between 0 and 9 and...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • please write in python Write a program that asks for beginning and ending number. The program...

    please write in python Write a program that asks for beginning and ending number. The program generates a set of 20 random numbers between the numbers and displays a count of each number generated. Sample output Enter starting and ending number separated by comma: 5, 25 5 occurs 2 times 7 occurs 1 time 8 occurs 1 time 10 occurs 2 times 12 occurs 1 time 13 occurs 2 time 15 occurs 5 times 16 occurs 1 time 20 occurs...

  • (Count occurrence of numbers) Write a program that reads some integers between 1 and 100 and...

    (Count occurrence of numbers) Write a program that reads some integers between 1 and 100 and counts the occurrences of each. Here is a sample run of the program: Enter integers between 1 and 100: 2 occurs 2 times 3 occurs 1 time 4 occurs 1 time 5 occurs 2 times 6 occurs 1 time 23 occurs 1 time 43 occurs 1 time 2 5 6 5 4 3 23 43 2 Note that if a number occurs more than...

  • *IN PYTHON* (Count single digits) Write a program that generates 1,000 random integers between 0 and...

    *IN PYTHON* (Count single digits) Write a program that generates 1,000 random integers between 0 and 9 and displays the count for each number. (Hint: Use a list of ten integers, say counts, to store the counts for the number of 0s, 1s, ..., 9s.) Rubric 5 pts: Proper use of list to count occurrences of numbers 5 pts: Proper calculations of counts 5 pts: Proper output Note: No software dev plan or UML required

  • PYTHON 3 PROGRAM 20.2 (Comparing performance) Write a test program that randomly generates 500000 numbers and...

    PYTHON 3 PROGRAM 20.2 (Comparing performance) Write a test program that randomly generates 500000 numbers and inserts them into a BinaryTree, reshuffles the 500000 numbers and performs search, and reshuffles the numbers again before deleting them from the tree. Write another test program that does the same thing for an AVLTree. Compare the execution times of these two programs.

  • C++ language Write a program that 1. generates a random integer between 100 and 1000, call...

    C++ language Write a program that 1. generates a random integer between 100 and 1000, call the number N 2. reads a file name from the keyboard 3. opens a file for output using the file name from step 2 4. generates N random numbers between 1 and 100 and writes them to the file from step 3 5. closes the file 6. opens the file from steps 3, and 4 for input 7. reads the numbers from the file...

  • PYTHON: Using Lists and Arrays! 3. Design a program that generates a seven-digit lottery number. The...

    PYTHON: Using Lists and Arrays! 3. Design a program that generates a seven-digit lottery number. The program should generate seven random numbers, each in the range of 0 through 9, and assign each number to a list element. (Random numbers were discussed in Chapter 5.) Then write another loop that displays the contents of the list.

  • Write a program that prompts the user to enter the number of milliseconds and converts the milliseconds to a string hours:minutes:seconds

    Problem 1.Write a program that prompts the user to enter the number of milliseconds and converts the milliseconds to a string hours:minutes:seconds. The program should use the convertMillismethod with the following header:public static String convertMillis(long millis)For example, convertMillis(5500) returns the string 0:0:5, convertMillis(100000) returns the string 0:1:40, andconvertMillis(555550000) returns the string154:19:10.Problem 2. (Count occurrence of numbers)Write a program that reads integers between 1 and 100 and counts the occurrence of each (you should store the numbers in an array). Output...

  • 4. Write a program that generates one hundred random integers between 0 and 9 and displays...

    4. Write a program that generates one hundred random integers between 0 and 9 and displays the count for each number. (Hint: Use rand()%10 to generate a random integer between 0 and 9. Use an array of ten integers, say counts, to store the counts for the number of 0’s, 1’s,…..,9’s.) this program kinda works but i cant figure out why its making more than 100 random numbers ls) [*] test2.cpp test3.cpp #include<stdio.h> #include<stdlib.h> int main() 4 { int a[14],is...

  • PYTHON PROGRAMMING: DO NOT USE INPUT FUNCTION, use sys.argv Write a program that generates two random...

    PYTHON PROGRAMMING: DO NOT USE INPUT FUNCTION, use sys.argv Write a program that generates two random integer numbers between 1 and 100. Then, print out the numbers between the two randomly generated ones using a while loop. The output is shown below. Output: a) start:4, end:14 4 5 6 7 8 9 10 11 12 13 14 b) start:15, end:20 15 16 17 18 19 20

  • HW Help? I cannot seem to figure this one out.. --Write a program that reads the...

    HW Help? I cannot seem to figure this one out.. --Write a program that reads the integers between 1 and 100 and counts the occurrences of each. assume the input ends with 0. Example: 2 5 6 5 4 3 23 43 2 0 ENTER 2 occurs 2 times 3 occurs 1 time 4 occurs 1 time 5 occurs 2 times 6 occurs 1 time 23 occurs 1 time 43 occurs 1 time Note that if a number occurs more...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT