Spaces:
Build error
Build error
| import streamlit as st | |
| import sympy as sp | |
| import pandas as pd | |
| # Function to solve algebraic equations | |
| def solve_equation(equation): | |
| try: | |
| if "=" not in equation: | |
| return pd.DataFrame([["Invalid input. Please enter an equation with '=' (e.g., 2*x + 3 = 5)."]], columns=["Error"]) | |
| equation = equation.replace("^", "**") | |
| equation = equation.replace(" ", "") | |
| lhs, rhs = equation.split("=") | |
| lhs_expr = sp.sympify(lhs) | |
| rhs_expr = sp.sympify(rhs) | |
| solution = sp.solve(lhs_expr - rhs_expr) | |
| # Ensure the solution is in a format suitable for the table (convert to string) | |
| if isinstance(solution, list): # If the solution is a list of values | |
| solution = ', '.join(map(str, solution)) # Convert the list to a string | |
| else: | |
| solution = str(solution) # Convert any other solution (single value) to string | |
| return pd.DataFrame([["Equation", equation], ["Solution", solution]], columns=["Description", "Result"]) | |
| except Exception as e: | |
| return pd.DataFrame([["Error", str(e)]], columns=["Description", "Error"]) | |
| # Function to simplify expressions | |
| def simplify_expression(expression): | |
| try: | |
| expression = expression.replace("^", "**") | |
| expr = sp.sympify(expression) | |
| simplified_expr = sp.simplify(expr) | |
| return pd.DataFrame([["Simplified Expression", simplified_expr]], columns=["Expression", "Simplified"]) | |
| except Exception as e: | |
| return pd.DataFrame([["Error", str(e)]], columns=["Expression", "Error"]) | |
| # Function to factor expressions | |
| def factor_expression(expression): | |
| try: | |
| expression = expression.replace("^", "**") | |
| expr = sp.sympify(expression) | |
| factored_expr = sp.factor(expr) | |
| return pd.DataFrame([["Factored Expression", factored_expr]], columns=["Expression", "Factored"]) | |
| except Exception as e: | |
| return pd.DataFrame([["Error", str(e)]], columns=["Expression", "Error"]) | |
| # Function to solve basic arithmetic word problems | |
| def solve_word_problem(operation, num1, num2): | |
| try: | |
| if operation == "addition": | |
| result = num1 + num2 | |
| elif operation == "subtraction": | |
| result = num1 - num2 | |
| elif operation == "multiplication": | |
| result = num1 * num2 | |
| elif operation == "division": | |
| if num2 == 0: | |
| result = "Error! Division by zero." | |
| else: | |
| result = num1 / num2 | |
| return pd.DataFrame([["Operation", operation], ["Result", result]], columns=["Description", "Result"]) | |
| except Exception as e: | |
| return pd.DataFrame([["Error", str(e)]], columns=["Description", "Error"]) | |
| # Function to find the factors of a number | |
| def find_factors(number): | |
| factors = [] | |
| for i in range(1, number + 1): | |
| if number % i == 0: | |
| factors.append(i) | |
| # Creating a DataFrame to show factors in a table format | |
| df = pd.DataFrame(factors, columns=["Factors"]) | |
| return df | |
| # Streamlit Interface | |
| def main(): | |
| st.title("Math Problem-Solution App") | |
| # Tabs for different functionalities | |
| tab = st.radio("Select a problem type:", ["Solve Algebraic Equation", "Simplify Expression", "Factor Expression", | |
| "Solve Word Problem", "Find Factors of a Number"]) | |
| if tab == "Solve Algebraic Equation": | |
| st.header("Solve Algebraic Equation") | |
| equation_input = st.text_input("Enter an equation (e.g., 2*x + 3 = 5)", "") | |
| if st.button("Solve Equation"): | |
| if equation_input: | |
| solution_df = solve_equation(equation_input) | |
| st.table(solution_df) | |
| else: | |
| st.write("Please enter an equation!") | |
| elif tab == "Simplify Expression": | |
| st.header("Simplify Expression") | |
| expression_input = st.text_input("Enter an expression (e.g., 2*x + 3*x - 4)", "") | |
| if st.button("Simplify Expression"): | |
| if expression_input: | |
| simplified_expr_df = simplify_expression(expression_input) | |
| st.table(simplified_expr_df) | |
| else: | |
| st.write("Please enter an expression!") | |
| elif tab == "Factor Expression": | |
| st.header("Factor Expression") | |
| factor_input = st.text_input("Enter an expression to factor (e.g., x^2 - 5*x + 6)", "") | |
| if st.button("Factor Expression"): | |
| if factor_input: | |
| factored_expr_df = factor_expression(factor_input) | |
| st.table(factored_expr_df) | |
| else: | |
| st.write("Please enter an expression!") | |
| elif tab == "Solve Word Problem": | |
| st.header("Solve Word Problem") | |
| operation = st.selectbox("Choose an operation:", ["addition", "subtraction", "multiplication", "division"]) | |
| num1_input = st.number_input("Enter the first number", 0) | |
| num2_input = st.number_input("Enter the second number", 0) | |
| if st.button("Solve Word Problem"): | |
| result_df = solve_word_problem(operation, num1_input, num2_input) | |
| st.table(result_df) | |
| elif tab == "Find Factors of a Number": | |
| st.header("Find Factors of a Number") | |
| # Input: Let user enter multiple numbers as comma-separated values | |
| numbers_input = st.text_input("Enter numbers separated by commas (e.g., 10, 15, 20)", "") | |
| if st.button("Find Factors"): | |
| if numbers_input: | |
| # Parse the input and generate tables for each number | |
| numbers = [int(x.strip()) for x in numbers_input.split(',')] | |
| for num in numbers: | |
| factors_df = find_factors(num) | |
| st.write(f"Factors of {num}:") | |
| st.table(factors_df) # Display the factors as a table | |
| else: | |
| st.write("Please enter numbers!") | |
| # Run the app | |
| if __name__ == "__main__": | |
| main() | |