Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,22 +5,28 @@
|
|
| 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
|
| 9 |
# ============================================================
|
| 10 |
|
| 11 |
import random
|
| 12 |
import gradio as gr
|
| 13 |
|
|
|
|
| 14 |
# ------------------------------------------------------------
|
| 15 |
-
# Binary Search
|
| 16 |
# ------------------------------------------------------------
|
| 17 |
def binary_search_visual(user_number):
|
| 18 |
try:
|
| 19 |
target = int(user_number)
|
| 20 |
except ValueError:
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
arr = sorted(random.sample(range(1, 50), 10))
|
| 25 |
|
| 26 |
steps = []
|
|
@@ -43,14 +49,19 @@ def binary_search_visual(user_number):
|
|
| 43 |
high = mid - 1
|
| 44 |
|
| 45 |
if not found:
|
| 46 |
-
steps.append("β
|
| 47 |
|
| 48 |
-
# Pad
|
| 49 |
while len(steps) < 8:
|
| 50 |
steps.append("")
|
| 51 |
|
| 52 |
-
#
|
| 53 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
|
| 55 |
|
| 56 |
# ------------------------------------------------------------
|
|
@@ -59,9 +70,9 @@ def binary_search_visual(user_number):
|
|
| 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
|
| 65 |
"""
|
| 66 |
)
|
| 67 |
|
|
@@ -69,31 +80,34 @@ with gr.Blocks(title="Binary Search Game") as demo:
|
|
| 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
|
| 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 |
-
#
|
|
|
|
|
|
|
|
|
|
| 80 |
run_button.click(
|
| 81 |
fn=binary_search_visual,
|
| 82 |
inputs=[user_number],
|
| 83 |
-
outputs=[list_box, *step_boxes]
|
| 84 |
)
|
| 85 |
|
| 86 |
gr.Markdown(
|
| 87 |
"""
|
| 88 |
---
|
| 89 |
-
**How
|
| 90 |
-
1.
|
| 91 |
-
2.
|
| 92 |
-
3. If smaller β
|
| 93 |
-
4.
|
| 94 |
"""
|
| 95 |
)
|
| 96 |
|
| 97 |
-
# Run
|
| 98 |
if __name__ == "__main__":
|
| 99 |
demo.launch()
|
|
|
|
| 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 and final result.
|
| 9 |
# ============================================================
|
| 10 |
|
| 11 |
import random
|
| 12 |
import gradio as gr
|
| 13 |
|
| 14 |
+
|
| 15 |
# ------------------------------------------------------------
|
| 16 |
+
# Binary Search Logic (returns list, steps, and final result)
|
| 17 |
# ------------------------------------------------------------
|
| 18 |
def binary_search_visual(user_number):
|
| 19 |
try:
|
| 20 |
target = int(user_number)
|
| 21 |
except ValueError:
|
| 22 |
+
# Return blanks for all boxes if invalid input
|
| 23 |
+
return (
|
| 24 |
+
"β οΈ Please enter a valid integer.",
|
| 25 |
+
*["" for _ in range(8)],
|
| 26 |
+
"β οΈ Invalid input.",
|
| 27 |
+
)
|
| 28 |
+
|
| 29 |
+
# Generate a random sorted list of 10 unique numbers (1β50)
|
| 30 |
arr = sorted(random.sample(range(1, 50), 10))
|
| 31 |
|
| 32 |
steps = []
|
|
|
|
| 49 |
high = mid - 1
|
| 50 |
|
| 51 |
if not found:
|
| 52 |
+
steps.append("β Number not found in the list.")
|
| 53 |
|
| 54 |
+
# Pad to fill consistent 8 steps
|
| 55 |
while len(steps) < 8:
|
| 56 |
steps.append("")
|
| 57 |
|
| 58 |
+
# Create a clear summary message for the last box
|
| 59 |
+
final_message = (
|
| 60 |
+
f"β
The number {target} was found!" if found else f"β The number {target} was not found."
|
| 61 |
+
)
|
| 62 |
+
|
| 63 |
+
# Return list display, all 8 steps, and final message
|
| 64 |
+
return f"Random Sorted List:\n{arr}", *steps[:8], final_message
|
| 65 |
|
| 66 |
|
| 67 |
# ------------------------------------------------------------
|
|
|
|
| 70 |
with gr.Blocks(title="Binary Search Game") as demo:
|
| 71 |
gr.Markdown(
|
| 72 |
"""
|
| 73 |
+
# π― Binary Search Game (Step-by-Step)
|
| 74 |
Each time you click **Run**, a new sorted list of 10 random numbers (1β50) will be generated.
|
| 75 |
+
Enter a number and watch **Binary Search** in action, step by step!
|
| 76 |
"""
|
| 77 |
)
|
| 78 |
|
|
|
|
| 80 |
user_number = gr.Number(label="Enter a number to search (1β50)", value=1, precision=0)
|
| 81 |
run_button = gr.Button("Run Binary Search π")
|
| 82 |
|
| 83 |
+
# Output: show the generated list
|
| 84 |
list_box = gr.Textbox(label="Generated Sorted List", lines=2, interactive=False)
|
| 85 |
|
| 86 |
+
# Step-by-step outputs
|
| 87 |
gr.Markdown("### πΉ Search Steps:")
|
| 88 |
step_boxes = [gr.Textbox(label=f"Step {i+1}", lines=1, interactive=False) for i in range(8)]
|
| 89 |
|
| 90 |
+
# Final result box
|
| 91 |
+
result_box = gr.Textbox(label="Final Result", lines=1, interactive=False)
|
| 92 |
+
|
| 93 |
+
# Button event linking
|
| 94 |
run_button.click(
|
| 95 |
fn=binary_search_visual,
|
| 96 |
inputs=[user_number],
|
| 97 |
+
outputs=[list_box, *step_boxes, result_box],
|
| 98 |
)
|
| 99 |
|
| 100 |
gr.Markdown(
|
| 101 |
"""
|
| 102 |
---
|
| 103 |
+
**How Binary Search Works:**
|
| 104 |
+
1. The list must be sorted.
|
| 105 |
+
2. Compare your target with the middle element.
|
| 106 |
+
3. If smaller β search the left half. If larger β search the right.
|
| 107 |
+
4. Repeat until the number is found or the range is empty.
|
| 108 |
"""
|
| 109 |
)
|
| 110 |
|
| 111 |
+
# Run app locally or deploy to Hugging Face
|
| 112 |
if __name__ == "__main__":
|
| 113 |
demo.launch()
|