ExplosionSearch / pages /55_Linear_Search_Skip.py
eaglelandsonce's picture
Rename pages/55_Linear_Search.py to pages/55_Linear_Search_Skip.py
7efaed0 verified
import streamlit as st
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.colors import ListedColormap
# Function to modify the generated number according to the rules
def modify_number(number):
modified_number = ''
for digit in number:
if digit not in '01': # Change any digit that is not 0 or 1 to 1
modified_number += '1'
else:
modified_number += digit
return modified_number
# Function to create the grid based on the boolean string
def create_grid(boolean_string):
if len(boolean_string) != 16:
st.error("The generated string is not 16 digits long.")
return None
# Convert string to a list of integers
grid = np.array([int(bit) for bit in boolean_string]).reshape(4, 4)
# Create a colormap for the grid
cmap = ListedColormap(['white', 'black'])
# Plot the grid
fig, ax = plt.subplots(figsize=(2, 2)) # Shrink the graph size
ax.matshow(grid, cmap=cmap)
# Add grid lines and remove ticks
ax.set_xticks(np.arange(-0.5, 4, 1), minor=True)
ax.set_yticks(np.arange(-0.5, 4, 1), minor=True)
ax.grid(which='minor', color='gray', linestyle='-', linewidth=2)
ax.tick_params(left=False, bottom=False, labelleft=False, labelbottom=False)
return fig
# Function to interpolate between start and end values
def interpolate_value(start_value, end_value, position, max_position):
difference = end_value - start_value
interpolated_value = start_value + (difference * (position / max_position))
return int(interpolated_value)
st.title("4x4 Boolean Grids with Interpolated Slider")
# Slider for selecting the position, starting at 0
max_slider_value = 10
position = st.slider("Select a position", 0, max_slider_value, 0)
# Define the start and distinct end values for each of the four numbers
start_value = 111111111111111
end_values = [0, 1111111100000000, 1111111111111111, 11111111]
# Interpolate each value based on the slider position
interpolated_values = [
interpolate_value(start_value, end_values[0], position, max_slider_value), # Number 1
interpolate_value(start_value, end_values[1], position, max_slider_value), # Number 2
interpolate_value(start_value, end_values[2], position, max_slider_value), # Number 3
interpolate_value(start_value, end_values[3], position, max_slider_value) # Number 4
]
# Display the interpolated numbers correctly
st.write(f"Interpolated Numbers:")
for i, value in enumerate(interpolated_values):
st.write(f"Number {i+1}: {value}")
# Convert the interpolated numbers to boolean strings
boolean_strings = [modify_number(str(value).zfill(16)) for value in interpolated_values]
# Create grids for each number and display them in a row
cols = st.columns(4)
for i, boolean_string in enumerate(boolean_strings):
with cols[i]:
st.write(f"Grid for Number {i+1}:")
st.write(modify_number(str(11).zfill(16)))
fig = create_grid(boolean_string)
if fig:
st.pyplot(fig)