GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — FreeCodeCampPythonProjects/ProbabilityCalculator.py at main · biwasxyz/FreeCodeCampPythonProjects

This repository was archived by the owner on Mar 13, 2026. It is now read-only.

Expand file tree

/

ProbabilityCalculator.py

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
36 lines (33 loc) · 2.28 KB
/

ProbabilityCalculator.py

Copy path

File metadata and controls

36 lines (33 loc) · 2.28 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
import copy # Importing the copy module to create deep copies of objects
import random # Importing the random module for random selection
class Hat:
def __init__(self, **kwargs):
self.contents = [] # Initialize an empty list to store the contents of the hat
# Loop through the keyword arguments (color: count) and add balls to the contents list
for color, count in kwargs.items():
self.contents.extend([color] * count)
def draw(self, num_balls):
drawn_balls = [] # Initialize an empty list to store the drawn balls
if num_balls >= len(self.contents):
return self.contents # If the number of balls to draw exceeds available quantity, return all balls
# Randomly draw balls from the hat without replacement
for _ in range(num_balls):
ball = random.choice(self.contents) # Randomly select a ball from the contents list
self.contents.remove(ball) # Remove the selected ball from the contents list
drawn_balls.append(ball) # Add the selected ball to the drawn balls list
return drawn_balls # Return the list of drawn balls
def experiment(hat, expected_balls, num_balls_drawn, num_experiments):
successful_experiments = 0 # Initialize a counter for successful experiments
for _ in range(num_experiments):
hat_copy = copy.deepcopy(hat) # Create a deep copy of the original hat
drawn_balls = hat_copy.draw(num_balls_drawn) # Draw a specified number of balls from the hat
drawn_dict = {color: drawn_balls.count(color) for color in set(drawn_balls)} # Count the occurrence of each color in the drawn balls
success = True # Initialize a variable to track if the experiment is successful
# Check if the drawn balls contain the expected number of each color
for color, count in expected_balls.items():
if color not in drawn_dict or drawn_dict[color] < count:
success = False # If any expected color is not found or the count is less than expected, the experiment fails
break
if success:
successful_experiments += 1 # Increment the counter for successful experiments
return successful_experiments / num_experiments # Return the probability of successful experiments
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.