Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# ============================================================
|
| 2 |
+
# 🧠 Binary Search Interactive App
|
| 3 |
+
# ============================================================
|
| 4 |
+
# Course: CISC-121 (Intro to Computing Science)
|
| 5 |
+
# Project: Searching & Sorting Algorithms
|
| 6 |
+
# Author: Your Name
|
| 7 |
+
# Description:
|
| 8 |
+
# This Gradio app demonstrates how Binary Search works
|
| 9 |
+
# step-by-step in an interactive "guess the number" style game.
|
| 10 |
+
# ============================================================
|
| 11 |
+
|
| 12 |
+
import gradio as gr
|
| 13 |
+
import random
|
| 14 |
+
|
| 15 |
+
# --------------------------
|
| 16 |
+
# Binary Search Logic
|
| 17 |
+
# --------------------------
|
| 18 |
+
def binary_search_game(target, low=1, high=100):
|
| 19 |
+
steps = []
|
| 20 |
+
while low <= high:
|
| 21 |
+
mid = (low + high) // 2
|
| 22 |
+
steps.append(f"Checking middle value: {mid}")
|
| 23 |
+
if mid == target:
|
| 24 |
+
steps.append(f"✅ Found the number {target}!")
|
| 25 |
+
return "\n".join(steps)
|
| 26 |
+
elif mid < target:
|
| 27 |
+
steps.append(f"{mid} is too low. Searching higher...")
|
| 28 |
+
low = mid + 1
|
| 29 |
+
else:
|
| 30 |
+
steps.append(f"{mid} is too high. Searching lower...")
|
| 31 |
+
high = mid - 1
|
| 32 |
+
steps.append("❌ Number not found (out of range).")
|
| 33 |
+
return "\n".join(steps)
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
# --------------------------
|
| 37 |
+
# Wrapper for Gradio
|
| 38 |
+
# --------------------------
|
| 39 |
+
def play_binary_search(user_number):
|
| 40 |
+
try:
|
| 41 |
+
user_number = int(user_number)
|
| 42 |
+
if not (1 <= user_number <= 100):
|
| 43 |
+
return "⚠️ Please choose a number between 1 and 100."
|
| 44 |
+
except ValueError:
|
| 45 |
+
return "⚠️ Please enter a valid number."
|
| 46 |
+
|
| 47 |
+
result = binary_search_game(user_number)
|
| 48 |
+
return result
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
# --------------------------
|
| 52 |
+
# Gradio Interface
|
| 53 |
+
# --------------------------
|
| 54 |
+
with gr.Blocks(title="Binary Search Simulator") as demo:
|
| 55 |
+
gr.Markdown(
|
| 56 |
+
"""
|
| 57 |
+
# 🔍 Binary Search Game
|
| 58 |
+
Try to see how **Binary Search** finds your chosen number step by step!
|
| 59 |
+
Pick any number between 1 and 100 and watch how the algorithm narrows down the range.
|
| 60 |
+
"""
|
| 61 |
+
)
|
| 62 |
+
|
| 63 |
+
with gr.Row():
|
| 64 |
+
user_number = gr.Textbox(label="Enter a number (1–100)")
|
| 65 |
+
output_box = gr.Textbox(label="Search Progress", lines=10)
|
| 66 |
+
|
| 67 |
+
run_button = gr.Button("Run Binary Search 🔎")
|
| 68 |
+
run_button.click(play_binary_search, inputs=user_number, outputs=output_box)
|
| 69 |
+
|
| 70 |
+
gr.Markdown(
|
| 71 |
+
"""
|
| 72 |
+
---
|
| 73 |
+
**How it works:**
|
| 74 |
+
1. Binary Search always checks the middle of the range.
|
| 75 |
+
2. If the guess is too high or too low, it halves the range again.
|
| 76 |
+
3. It continues until it finds your number!
|
| 77 |
+
"""
|
| 78 |
+
)
|
| 79 |
+
|
| 80 |
+
# Run app locally or on Hugging Face
|
| 81 |
+
if __name__ == "__main__":
|
| 82 |
+
demo.launch()
|