diff --git a/Lectures/06 Level I/05 Monty Hall Problem Simulation/solutions/solution-1/README.md b/Lectures/06 Level I/05 Monty Hall Problem Simulation/solutions/solution-1/README.md index 7f073c6..796839f 100644 --- a/Lectures/06 Level I/05 Monty Hall Problem Simulation/solutions/solution-1/README.md +++ b/Lectures/06 Level I/05 Monty Hall Problem Simulation/solutions/solution-1/README.md @@ -14,6 +14,7 @@ This implementation solves the famous Monty Hall problem, presenting an interact ├─ monty_hall.py └─ app.py ``` + - `README.md`: This descriptive file - `requirements.txt`: Contains all the required modules and libraries needed to run the project - `src/monty_hall.py`: Contains the Python program to simulate the Monty Hall game @@ -23,6 +24,7 @@ This implementation solves the famous Monty Hall problem, presenting an interact - Python 3.7 or higher - Streamlit +- pandas To install necessary packages, run `pip install -r requirements.txt`. @@ -32,7 +34,7 @@ You can play the Monty Hall game simulation by adding the `src` directory to the `python3 src/monty_hall.py` -To start the Streamlit dashboard, run: +To start the Streamlit dashboard, run: `streamlit run src/app.py` diff --git a/Lectures/06 Level I/05 Monty Hall Problem Simulation/solutions/solution-1/src/app.py b/Lectures/06 Level I/05 Monty Hall Problem Simulation/solutions/solution-1/src/app.py index 29e4acd..3cfdd0c 100644 --- a/Lectures/06 Level I/05 Monty Hall Problem Simulation/solutions/solution-1/src/app.py +++ b/Lectures/06 Level I/05 Monty Hall Problem Simulation/solutions/solution-1/src/app.py @@ -1,4 +1,5 @@ import streamlit as st +import pandas as pd import time from monty_hall import simulate_games @@ -14,11 +15,13 @@ col1, col2 = st.columns(2) col1.subheader("Win Percentage Without Switching") -chart1 = col1.line_chart(x=None, y=None, width=0, height=400) -chart1.add_rows([1.0]) # Add a row to ensure the chart y-axis starts at 1 +chart1 = col1.empty() +no_switch_history = [] + col2.subheader("Win Percentage With Switching") -chart2 = col2.line_chart(x=None, y=None, width=0, height=400) -chart2.add_rows([1.0]) # Add a row to ensure the chart y-axis starts at 1 +chart2 = col2.empty() +switch_history = [] + for i in range(num_games): # Simulate one game at a time @@ -28,9 +31,23 @@ wins_no_switch += num_wins_without_switching wins_switch += num_wins_with_switching - # # Display the current percentages after each game - chart1.add_rows([wins_no_switch / (i + 1)]) - chart2.add_rows([wins_switch / (i + 1)]) + no_switch_history.append(wins_no_switch / (i+1)) + switch_history.append(wins_switch / (i+1)) + + data1 = pd.DataFrame({ + "Trial": range(1, len(no_switch_history) + 1), + "Switch": no_switch_history + }) + + data2 = pd.DataFrame({ + "Trial": range(1, len(switch_history) + 1), + "Switch": switch_history + }) + + data1 = data1.set_index("Trial") + chart1.line_chart(data1) + data2 = data2.set_index("Trial") + chart2.line_chart(data2) # Add a delay to create a slow loop effect time.sleep(0.01)