File size: 909 Bytes
1018204
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import streamlit as st

def execute_code(code_input):
    try:
        exec_globals = {}
        exec(f"def user_solution():\n{code_input}", exec_globals)
        user_solution = exec_globals.get('user_solution', None)

        if st.session_state.sample_test_case:
            test_args = eval(st.session_state.sample_test_case)
            if not isinstance(test_args, tuple):
                test_args = (test_args,)
            returned_output = user_solution(*test_args)
        else:
            returned_output = user_solution()

        st.sidebar.markdown(f"**Your Output:** {returned_output}")
        if str(returned_output) == st.session_state.expected_output:
            st.sidebar.success("Your output matches the expected output!")
        else:
            st.sidebar.error("Your output does not match the expected output.")
    except Exception as e:
        st.sidebar.error(f"Error: {e}")