-
Notifications
You must be signed in to change notification settings - Fork 0
/
Simple_Histogram.py
59 lines (48 loc) · 2.37 KB
/
Simple_Histogram.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
'''
READ ME:
- random_numbers function
- get_frequencies function
- print_histogram function
- random module
DESCRIPTION:
- This module is a simple histogram generator. It generates a list of 100 random numbers between 1 and 10, calculates the frequency of each number, and prints a histogram where each bar represents the frequency of a number between the lowest and highest number in the random list.
- The random_numbers function generates the list of random numbers.
- The get_frequencies function calculates the frequency of each number in the list.
- The print_histogram function prints the histogram.
PARAMETERS:
- The random_numbers function does not take any parameters.
- The get_frequencies function takes a list of numbers as a parameter.
- The print_histogram function takes a list of numbers as a parameter.
LIMITATIONS:
- The program does not handle non-integer or out-of-range inputs.
- The program does not provide any way to customize the range of numbers or the number of numbers generated.
STRUCTURES:
- The random_numbers function uses a for-loop to generate the list of random numbers.
- The get_frequencies function uses a for-loop and a dictionary to calculate the frequencies.
- The print_histogram function uses a for-loop, the min and max functions, and the range function to print the histogram.
OUTPUTS:
- The random_numbers function returns a list of random numbers.
- The get_frequencies function returns a dictionary of number frequencies.
- The print_histogram function does not return anything; it prints the histogram directly.
'''
import random
def random_numbers():
numbers = []
for _ in range(100): # The underscore is used to indicate that the loop variable is not used in the loop body.
number = random.randint(1, 20)
numbers.append(number)
return numbers
def get_frequencies(numbers):
frequencies = {}
for number in numbers:
frequencies[number] = frequencies.get(number, 0) + 1
return frequencies
def print_histogram(numbers):
frequencies = get_frequencies(numbers)
min_number = min(numbers)
max_number = max(numbers)
for number in range(min_number, max_number + 1): # The range function generates a sequence of numbers from min_number to max_number.
frequency = frequencies.get(number, 0)
print(f"{number} {'#' * frequency}")
# Example usage
print_histogram(random_numbers())