Spaces:
Sleeping
Sleeping
File size: 1,071 Bytes
98f847b |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
import streamlit as st
def binary_search(arr, target):
left, right = 0, len(arr) - 1
while left <= right:
mid = left + (right - left) // 2
if arr[mid] == target:
return mid
elif arr[mid] < target:
left = mid + 1
else:
right = mid - 1
return -1
def main():
st.title("Binary Search App")
# Input: string of numbers separated by commas
array_input = st.text_input("Enter sorted numbers (comma separated)", "1, 2, 3, 4, 5, 6, 7, 8, 9")
target_input = st.text_input("Enter the number to search for", "5")
try:
arr = list(map(int, array_input.split(',')))
target = int(target_input)
if st.button("Search"):
index = binary_search(arr, target)
if index != -1:
st.success(f"Found {target} at index {index}")
else:
st.error(f"{target} not found in the list")
except ValueError:
st.error("Invalid input! Please enter integers only.")
if __name__ == "__main__":
main() |