Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,28 +4,28 @@
|
|
| 4 |
# Author: Your Name
|
| 5 |
# Course: CISC-121 (Intro to Computing Science)
|
| 6 |
# Description:
|
| 7 |
-
#
|
| 8 |
-
#
|
| 9 |
-
# showing the list separately from each search step.
|
| 10 |
# ============================================================
|
| 11 |
|
| 12 |
import random
|
| 13 |
import gradio as gr
|
| 14 |
|
| 15 |
# ------------------------------------------------------------
|
| 16 |
-
# Binary Search
|
| 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
|
|
@@ -33,6 +33,7 @@ def binary_search_visual(user_number):
|
|
| 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")
|
|
@@ -41,46 +42,44 @@ def binary_search_visual(user_number):
|
|
| 41 |
steps.append(f"{arr[mid]} is larger β searching left half")
|
| 42 |
high = mid - 1
|
| 43 |
|
| 44 |
-
if not
|
| 45 |
steps.append("β Not found. Try again!")
|
| 46 |
|
| 47 |
-
#
|
| 48 |
-
|
| 49 |
-
|
| 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 |
-
|
|
|
|
| 55 |
|
| 56 |
|
| 57 |
# ------------------------------------------------------------
|
| 58 |
-
# Gradio Interface
|
| 59 |
# ------------------------------------------------------------
|
| 60 |
with gr.Blocks(title="Binary Search Game") as demo:
|
| 61 |
gr.Markdown(
|
| 62 |
"""
|
| 63 |
# π― Binary Search Game (Step-by-Step)
|
| 64 |
-
|
| 65 |
-
Enter a number and
|
| 66 |
"""
|
| 67 |
)
|
| 68 |
|
| 69 |
-
# Input
|
| 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:
|
| 74 |
list_box = gr.Textbox(label="Generated Sorted List", lines=2, interactive=False)
|
| 75 |
|
| 76 |
-
#
|
| 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 |
-
#
|
| 81 |
run_button.click(
|
| 82 |
fn=binary_search_visual,
|
| 83 |
-
inputs=user_number,
|
| 84 |
outputs=[list_box, *step_boxes]
|
| 85 |
)
|
| 86 |
|
|
@@ -88,13 +87,13 @@ with gr.Blocks(title="Binary Search Game") as demo:
|
|
| 88 |
"""
|
| 89 |
---
|
| 90 |
**How it works:**
|
| 91 |
-
1. Binary Search divides the list
|
| 92 |
-
2.
|
| 93 |
-
3.
|
| 94 |
-
4. Repeats until
|
| 95 |
"""
|
| 96 |
)
|
| 97 |
|
| 98 |
-
# Run app
|
| 99 |
if __name__ == "__main__":
|
| 100 |
demo.launch()
|
|
|
|
| 4 |
# Author: Your Name
|
| 5 |
# Course: CISC-121 (Intro to Computing Science)
|
| 6 |
# Description:
|
| 7 |
+
# Generates a random sorted list and performs binary search
|
| 8 |
+
# step-by-step, showing each comparison in separate boxes.
|
|
|
|
| 9 |
# ============================================================
|
| 10 |
|
| 11 |
import random
|
| 12 |
import gradio as gr
|
| 13 |
|
| 14 |
# ------------------------------------------------------------
|
| 15 |
+
# Binary Search Function
|
| 16 |
# ------------------------------------------------------------
|
| 17 |
def binary_search_visual(user_number):
|
| 18 |
try:
|
| 19 |
target = int(user_number)
|
| 20 |
except ValueError:
|
| 21 |
+
return "β οΈ Please enter a valid integer.", *["" for _ in range(8)]
|
| 22 |
|
| 23 |
# Generate a random sorted list of 10 unique numbers between 1β50
|
| 24 |
arr = sorted(random.sample(range(1, 50), 10))
|
| 25 |
|
| 26 |
steps = []
|
| 27 |
low, high = 0, len(arr) - 1
|
| 28 |
+
found = False
|
| 29 |
|
| 30 |
while low <= high:
|
| 31 |
mid = (low + high) // 2
|
|
|
|
| 33 |
|
| 34 |
if arr[mid] == target:
|
| 35 |
steps.append(f"β
Found {target} at index {mid}")
|
| 36 |
+
found = True
|
| 37 |
break
|
| 38 |
elif arr[mid] < target:
|
| 39 |
steps.append(f"{arr[mid]} is smaller β searching right half")
|
|
|
|
| 42 |
steps.append(f"{arr[mid]} is larger β searching left half")
|
| 43 |
high = mid - 1
|
| 44 |
|
| 45 |
+
if not found:
|
| 46 |
steps.append("β Not found. Try again!")
|
| 47 |
|
| 48 |
+
# Pad steps so each step textbox is filled consistently
|
| 49 |
+
while len(steps) < 8:
|
| 50 |
+
steps.append("")
|
|
|
|
|
|
|
|
|
|
| 51 |
|
| 52 |
+
# Return: one string for list, then 8 step strings
|
| 53 |
+
return f"Random Sorted List:\n{arr}", *steps[:8]
|
| 54 |
|
| 55 |
|
| 56 |
# ------------------------------------------------------------
|
| 57 |
+
# Gradio Interface
|
| 58 |
# ------------------------------------------------------------
|
| 59 |
with gr.Blocks(title="Binary Search Game") as demo:
|
| 60 |
gr.Markdown(
|
| 61 |
"""
|
| 62 |
# π― Binary Search Game (Step-by-Step)
|
| 63 |
+
Each time you click **Run**, a new sorted list of 10 random numbers (1β50) will be generated.
|
| 64 |
+
Enter a number and see how **Binary Search** checks values step-by-step!
|
| 65 |
"""
|
| 66 |
)
|
| 67 |
|
| 68 |
+
# Input
|
| 69 |
user_number = gr.Number(label="Enter a number to search (1β50)", value=1, precision=0)
|
| 70 |
run_button = gr.Button("Run Binary Search π")
|
| 71 |
|
| 72 |
+
# Output: list display
|
| 73 |
list_box = gr.Textbox(label="Generated Sorted List", lines=2, interactive=False)
|
| 74 |
|
| 75 |
+
# Step outputs
|
| 76 |
gr.Markdown("### πΉ Search Steps:")
|
| 77 |
step_boxes = [gr.Textbox(label=f"Step {i+1}", lines=1, interactive=False) for i in range(8)]
|
| 78 |
|
| 79 |
+
# Link logic
|
| 80 |
run_button.click(
|
| 81 |
fn=binary_search_visual,
|
| 82 |
+
inputs=[user_number],
|
| 83 |
outputs=[list_box, *step_boxes]
|
| 84 |
)
|
| 85 |
|
|
|
|
| 87 |
"""
|
| 88 |
---
|
| 89 |
**How it works:**
|
| 90 |
+
1. Binary Search divides the sorted list into halves.
|
| 91 |
+
2. It compares your number with the middle value.
|
| 92 |
+
3. If smaller β searches left half. If larger β searches right half.
|
| 93 |
+
4. Repeats until the number is found or range is empty.
|
| 94 |
"""
|
| 95 |
)
|
| 96 |
|
| 97 |
+
# Run the app locally or deploy to Hugging Face
|
| 98 |
if __name__ == "__main__":
|
| 99 |
demo.launch()
|