Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,92 +1,100 @@
|
|
| 1 |
# ============================================================
|
| 2 |
-
# π Binary Search Game
|
| 3 |
# ============================================================
|
| 4 |
# Author: Your Name
|
| 5 |
# Course: CISC-121 (Intro to Computing Science)
|
| 6 |
# Description:
|
| 7 |
-
# This app
|
| 8 |
-
#
|
| 9 |
-
#
|
| 10 |
# ============================================================
|
| 11 |
|
| 12 |
import random
|
| 13 |
import gradio as gr
|
| 14 |
|
| 15 |
# ------------------------------------------------------------
|
| 16 |
-
# Binary Search
|
| 17 |
# ------------------------------------------------------------
|
| 18 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
steps = []
|
| 20 |
low, high = 0, len(arr) - 1
|
| 21 |
|
| 22 |
-
steps.append(f"List: {arr}")
|
| 23 |
-
steps.append(f"Searching for: {target}\n")
|
| 24 |
-
|
| 25 |
while low <= high:
|
| 26 |
mid = (low + high) // 2
|
| 27 |
-
steps.append(f"
|
| 28 |
|
| 29 |
if arr[mid] == target:
|
| 30 |
-
steps.append(f"β
Found {target} at
|
| 31 |
-
|
| 32 |
-
|
| 33 |
elif arr[mid] < target:
|
| 34 |
-
steps.append(f"{arr[mid]} is
|
| 35 |
low = mid + 1
|
| 36 |
-
|
| 37 |
else:
|
| 38 |
-
steps.append(f"{arr[mid]} is
|
| 39 |
high = mid - 1
|
| 40 |
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
# ------------------------------------------------------------
|
| 46 |
-
# Function to handle user interaction
|
| 47 |
-
# ------------------------------------------------------------
|
| 48 |
-
def play_binary_search(user_number):
|
| 49 |
-
try:
|
| 50 |
-
user_number = int(user_number)
|
| 51 |
-
except ValueError:
|
| 52 |
-
return "β οΈ Please enter a valid number."
|
| 53 |
|
| 54 |
-
#
|
| 55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
-
|
| 58 |
-
return result
|
| 59 |
|
| 60 |
|
| 61 |
# ------------------------------------------------------------
|
| 62 |
-
# Gradio Interface
|
| 63 |
# ------------------------------------------------------------
|
| 64 |
with gr.Blocks(title="Binary Search Game") as demo:
|
| 65 |
gr.Markdown(
|
| 66 |
"""
|
| 67 |
-
# π― Binary Search Game
|
| 68 |
-
|
| 69 |
-
|
| 70 |
"""
|
| 71 |
)
|
| 72 |
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
output_box = gr.Textbox(label="Search Steps", lines=12, interactive=False)
|
| 76 |
-
|
| 77 |
run_button = gr.Button("Run Binary Search π")
|
| 78 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
|
| 80 |
gr.Markdown(
|
| 81 |
"""
|
| 82 |
---
|
| 83 |
-
**How
|
| 84 |
-
1.
|
| 85 |
-
2.
|
| 86 |
-
3.
|
| 87 |
-
4.
|
| 88 |
"""
|
| 89 |
)
|
| 90 |
|
|
|
|
| 91 |
if __name__ == "__main__":
|
| 92 |
demo.launch()
|
|
|
|
| 1 |
# ============================================================
|
| 2 |
+
# π Binary Search Game β Step-by-Step Interactive Gradio App
|
| 3 |
# ============================================================
|
| 4 |
# Author: Your Name
|
| 5 |
# Course: CISC-121 (Intro to Computing Science)
|
| 6 |
# Description:
|
| 7 |
+
# This app generates a random sorted list of numbers and lets
|
| 8 |
+
# the user guess a target value. Binary Search runs step-by-step,
|
| 9 |
+
# showing the list separately from each search step.
|
| 10 |
# ============================================================
|
| 11 |
|
| 12 |
import random
|
| 13 |
import gradio as gr
|
| 14 |
|
| 15 |
# ------------------------------------------------------------
|
| 16 |
+
# Binary Search (returns list + steps)
|
| 17 |
# ------------------------------------------------------------
|
| 18 |
+
def binary_search_visual(user_number):
|
| 19 |
+
try:
|
| 20 |
+
target = int(user_number)
|
| 21 |
+
except ValueError:
|
| 22 |
+
return "β οΈ Please enter a valid integer.", [], []
|
| 23 |
+
|
| 24 |
+
# Generate a random sorted list of 10 unique numbers between 1β50
|
| 25 |
+
arr = sorted(random.sample(range(1, 50), 10))
|
| 26 |
+
|
| 27 |
steps = []
|
| 28 |
low, high = 0, len(arr) - 1
|
| 29 |
|
|
|
|
|
|
|
|
|
|
| 30 |
while low <= high:
|
| 31 |
mid = (low + high) // 2
|
| 32 |
+
steps.append(f"Checking middle index {mid} β value {arr[mid]}")
|
| 33 |
|
| 34 |
if arr[mid] == target:
|
| 35 |
+
steps.append(f"β
Found {target} at index {mid}")
|
| 36 |
+
break
|
|
|
|
| 37 |
elif arr[mid] < target:
|
| 38 |
+
steps.append(f"{arr[mid]} is smaller β searching right half")
|
| 39 |
low = mid + 1
|
|
|
|
| 40 |
else:
|
| 41 |
+
steps.append(f"{arr[mid]} is larger β searching left half")
|
| 42 |
high = mid - 1
|
| 43 |
|
| 44 |
+
if not any("β
" in s for s in steps):
|
| 45 |
+
steps.append("β Not found. Try again!")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
+
# Return values for display
|
| 48 |
+
list_display = f"Random Sorted List:\n{arr}"
|
| 49 |
+
step_texts = [gr.Textbox.update(value=step) for step in steps]
|
| 50 |
+
# Fill remaining boxes with blanks to keep layout consistent
|
| 51 |
+
while len(step_texts) < 8:
|
| 52 |
+
step_texts.append(gr.Textbox.update(value=""))
|
| 53 |
|
| 54 |
+
return list_display, *step_texts
|
|
|
|
| 55 |
|
| 56 |
|
| 57 |
# ------------------------------------------------------------
|
| 58 |
+
# Gradio Interface Layout
|
| 59 |
# ------------------------------------------------------------
|
| 60 |
with gr.Blocks(title="Binary Search Game") as demo:
|
| 61 |
gr.Markdown(
|
| 62 |
"""
|
| 63 |
+
# π― Binary Search Game (Step-by-Step)
|
| 64 |
+
This app generates a **random sorted list (1β50)** each time.
|
| 65 |
+
Enter a number and watch how **Binary Search** checks values step-by-step.
|
| 66 |
"""
|
| 67 |
)
|
| 68 |
|
| 69 |
+
# Input section
|
| 70 |
+
user_number = gr.Number(label="Enter a number to search (1β50)", value=1, precision=0)
|
|
|
|
|
|
|
| 71 |
run_button = gr.Button("Run Binary Search π")
|
| 72 |
+
|
| 73 |
+
# Output: show list in one box
|
| 74 |
+
list_box = gr.Textbox(label="Generated Sorted List", lines=2, interactive=False)
|
| 75 |
+
|
| 76 |
+
# Create separate boxes for each step
|
| 77 |
+
gr.Markdown("### πΉ Search Steps:")
|
| 78 |
+
step_boxes = [gr.Textbox(label=f"Step {i+1}", lines=1, interactive=False) for i in range(8)]
|
| 79 |
+
|
| 80 |
+
# Click event
|
| 81 |
+
run_button.click(
|
| 82 |
+
fn=binary_search_visual,
|
| 83 |
+
inputs=user_number,
|
| 84 |
+
outputs=[list_box, *step_boxes]
|
| 85 |
+
)
|
| 86 |
|
| 87 |
gr.Markdown(
|
| 88 |
"""
|
| 89 |
---
|
| 90 |
+
**How it works:**
|
| 91 |
+
1. Binary Search divides the list in half each time.
|
| 92 |
+
2. Compares the middle element to your target.
|
| 93 |
+
3. Moves left or right based on the result.
|
| 94 |
+
4. Repeats until it finds your number or the range is empty.
|
| 95 |
"""
|
| 96 |
)
|
| 97 |
|
| 98 |
+
# Run app
|
| 99 |
if __name__ == "__main__":
|
| 100 |
demo.launch()
|