File size: 4,419 Bytes
7a80f4b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ce2b44b
7a80f4b
 
ce2b44b
 
7a80f4b
ce2b44b
 
4c14f94
 
 
 
 
 
 
 
7a80f4b
 
 
 
 
5173311
 
 
 
 
 
7a80f4b
5173311
 
 
7a80f4b
 
 
 
 
5173311
7a80f4b
 
 
 
 
 
 
 
 
 
a9c6ce7
7a80f4b
 
a9c6ce7
7a80f4b
925ca30
a9c6ce7
 
925ca30
d26a2e4
 
 
925ca30
33e5a93
e117b14
d26a2e4
 
e117b14
d26a2e4
 
 
 
dd9c5ec
3081366
7a80f4b
 
925ca30
7a80f4b
 
925ca30
7a80f4b
 
 
a9c6ce7
7a80f4b
925ca30
7a80f4b
 
 
 
925ca30
c95bfec
542edbc
925ca30
c95bfec
542edbc
7a80f4b
 
 
 
 
 
 
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

import streamlit as st
from streamlit_ace import st_ace
import pandas as pd

button_style = st.markdown('''
<style>
div.stButton > button:first-child {
    background-color: #3A7EF1;
}
</style>''', unsafe_allow_html=True)

left_co, cent_co,last_co = st.columns(3)
with cent_co:
    st.image("https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTzYyVPVc8EX7_N2QOh9D86t54SS7ucBDcOU_Tn52LwBi8BQTwcdeCVKcyjSKBGExjB4g&usqp=CAU")
st.markdown("<h5 style='color: #707379;  text-align: center;'>School of AI by 1001epochs</h5>", unsafe_allow_html=True)
st.markdown('')


def check_solution(instructor_code, student_code, function_name, test_cases):
    try:
        globals_dict = {}
        exec(instructor_code, globals_dict)
        instructor_function = globals_dict.get(function_name)

        if not callable(instructor_function):
            raise ValueError(f"{instructor_function} is not a callable function.")
        
        # Execute student's code
        globals_dict = {}
        exec(student_code, globals_dict)
        student_function = globals_dict.get(function_name)

        if not callable(student_function):
            raise ValueError(f"{function_name} is not a callable function.")

        # Run the tests and populate the results table
        expected_result_list = []
        actual_result_list = []
        test_result = []
        for test_case in test_cases:
            expected_result = instructor_function(*test_case)
            actual_result = student_function(*test_case)
            expected_result_list.append(expected_result)
            actual_result_list.append(actual_result)
            test_result.append("Passed" if expected_result == actual_result else "Failed")
        result_df = pd.DataFrame({
            "Expected": expected_result_list,
            "Run": actual_result_list,
            "Result": test_result
        }, index=None)
        st.markdown(result_df.style.hide(axis="index").to_html(), unsafe_allow_html=True)
        st.markdown("<br>", unsafe_allow_html=True)

        # Check if all tests passed
        if all(expected == actual for expected, actual in zip(expected_result_list, actual_result_list)):
            st.success("All tests passed!")
        else:
            st.error("Some tests failed.")
        return True

    except SyntaxError as e:
        st.error(f"Error: {e}")


def main():

    with st.form('app'):
        st.markdown("<h2 style='color: #707379;'>Coding Challenge - Code Practice</h2>", unsafe_allow_html=True)

        # Description of the challenge
        st.write("Create a function sum that meets the following criteria:")
        st.write('''sass''')

        func = 'sum'
        def execute_instructor_code(params):
            # Execute the instructor's code to get the result
            globals_dict = {}
            exec('''def sum(a,b):
    return a+b''', globals_dict)
            instructor_function = globals_dict.get(func)

            if not callable(instructor_function):
                raise ValueError(f"{func} is not a callable function.")

            return instructor_function(*params)

        # Generate sample examples dynamically by executing the instructor's code
        test_cases = [(1, 2), (2, 3), (3, 4)]  
        sample_examples = "\n".join([f"{func}({', '.join(map(str, params))}) -> {execute_instructor_code(params)}" for params in test_cases])
        show_solution_button = st.form_submit_button("Show Solution")
        if show_solution_button:
            solution = '''def sum(a,b):
    return a+b'''
            if solution:
                st.code('''def sum(a,b):
    return a+b''', language="python")
            else:
                st.error('No Solution Provided!')

        # Code input area
        code = st_ace(value='''def sum(a,b):''', language="python", key="my_editor", theme='clouds', height=200)
        # Check solution button
        if st.form_submit_button("Check Solution"):
            if code:
                # Check the solution
                check_solution('''def sum(a,b):
    return a+b''',
                                code,
                                '''sum''',
                                '''[(1, 2), (2, 3), (3, 4)]'''
                                )
            else:
                st.error("Please provide a solution.")

    st.markdown("Copyright© by 1001epochs. All rights reserved.")

if __name__ == "__main__":
    main()