File size: 5,979 Bytes
36c6d97
29c212c
6378509
29c212c
7cefee2
 
 
 
0f09c68
7cefee2
 
 
 
 
 
09bb6e9
 
 
 
 
 
 
 
7cefee2
09bb6e9
7cefee2
 
 
258ff35
c22ed13
7cefee2
 
0f09c68
7cefee2
0f09c68
7cefee2
 
 
 
 
 
 
0f09c68
7cefee2
0f09c68
29c212c
7cefee2
 
 
 
 
 
 
 
 
 
 
 
 
 
0f09c68
7cefee2
0f09c68
29c212c
7cefee2
 
 
 
 
 
6378509
 
 
8359e4d
36c6d97
 
 
29c212c
36c6d97
1a22c9a
d519ff8
7cefee2
 
 
 
 
 
0f09c68
 
7cefee2
 
 
 
 
 
 
 
0f09c68
 
7cefee2
 
29c212c
7cefee2
 
 
 
 
0f09c68
 
7cefee2
 
 
 
 
 
 
 
 
0f09c68
 
7cefee2
 
 
6378509
 
 
 
7cefee2
6378509
 
 
 
 
 
 
 
7cefee2
6378509
7cefee2
36c6d97
 
 
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
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()