Skip to content

Xrowly/FreeCodeCamp_probability_calculator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Probability Calculator

Overview

This project is a Python-based probability calculator developed as part of the freeCodeCamp Scientific Computing with Python curriculum, specifically for the "Probability Calculator" project. It implements a Hat class to simulate drawing colored balls from a hat and an experiment function to estimate the probability of drawing specific combinations of balls. The project demonstrates key concepts such as object-oriented programming, random sampling, and Monte Carlo simulations, aligning with freeCodeCamp's focus on practical coding skills.

Features

  • Hat Class:
    • Initialize a hat with a specified number of colored balls (e.g., blue=5, red=4).
    • Draw a specified number of balls without replacement, removing them from the hat.
    • Handle cases where the draw request exceeds available balls.
  • Experiment Function:
    • Perform Monte Carlo simulations to estimate the probability of drawing a specified combination of balls.
    • Use deep copying to ensure each experiment starts with a fresh hat.
    • Calculate success based on meeting or exceeding expected ball counts.
  • Random Sampling: Utilizes Python's random.sample for unbiased draws without replacement.

Installation

  1. Ensure Python 3.x is installed on your system.
  2. Clone this repository:
    git clone https://github.com/Xrowly/probability-calculator.git 
  3. Navigate to the project directory:
    cd probability-calculator 
  4. The project requires the random and copy modules, which are part of Python's standard library. No external dependencies are needed.

Usage

  1. Run the main script to see example usage:
    python main.py 
  2. The main.py script demonstrates creating a Hat, performing draws, and calculating probabilities for specific outcomes.

Example Output

Running main.py might produce output like:

Hat contents: ['blue', 'blue', 'blue', 'blue', 'blue', 'red', 'red', 'red', 'red', 'green', 'green'] Example draw (4 balls): ['red', 'blue', 'green', 'blue'] Probability of drawing at least 1 red and 2 green (2000 experiments): 0.1235 

Note: The probability value will vary due to the random nature of the simulation.

Customizing Experiments

Modify main.py to create custom hats or experiments. Example:

from probability_calculator import Hat, experiment hat = Hat(blue=3, red=2, yellow=5) probability = experiment( hat=hat, expected_balls={"blue": 1, "yellow": 2}, num_balls_drawn=4, num_experiments=1000 ) print(f"Probability: {probability}")

Code Structure

  • probability_calculator.py: Defines the Hat class and experiment function, implementing the core logic for the probability calculations.
  • main.py: Demonstrates usage with sample hats, draws, and probability experiments, showcasing the functionality required by the freeCodeCamp project.

Limitations

  • The application is console-based and does not support persistent storage or graphical visualization.
  • Probability estimates depend on the number of experiments; fewer experiments may lead to less accurate results.
  • Draws are performed without replacement, as per the project requirements.

Contributing

Contributions are welcome! Please fork the repository and submit pull requests that align with the freeCodeCamp Scientific Computing with Python curriculum goals. Ensure changes maintain the project's educational focus.

License

This project is licensed under the MIT License. See the LICENSE file for details.

Acknowledgments

This project was completed as part of the freeCodeCamp Scientific Computing with Python curriculum, which emphasizes practical programming skills through hands-on projects. Visit freeCodeCamp.org for more details on the curriculum and other learning resources.

About

Python probability simulator from freeCodeCamp. Estimates ball draw probabilities using Monte Carlo methods and random sampling.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages