epochs-demos commited on
Commit
4052fa4
1 Parent(s): 82d9279

Upload app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +34 -55
app.py CHANGED
@@ -1,13 +1,14 @@
1
 
 
2
  import streamlit as st
3
  from streamlit_ace import st_ace
4
  import pandas as pd
5
 
6
  button_style = st.markdown('''
7
  <style>
8
- div.stButton > button:first-child {
9
  background-color: #3A7EF1;
10
- }
11
  </style>''', unsafe_allow_html=True)
12
 
13
  left_co, cent_co,last_co = st.columns(3)
@@ -17,100 +18,76 @@ st.markdown("<h5 style='color: #707379; text-align: center;'>School of AI by 10
17
  st.markdown('')
18
 
19
 
20
- def check_solution(instructor_code, student_code, function_name, test_cases):
21
  try:
 
22
  globals_dict = {}
23
- exec(instructor_code, globals_dict)
24
- instructor_function = globals_dict.get(function_name)
25
 
26
- if not callable(instructor_function):
27
- raise ValueError(f"{instructor_function} is not a callable function.")
28
-
29
- # Execute student's code
30
- globals_dict = {}
31
- exec(student_code, globals_dict)
32
- student_function = globals_dict.get(function_name)
33
-
34
- if not callable(student_function):
35
- raise ValueError(f"{function_name} is not a callable function.")
36
 
37
  # Run the tests and populate the results table
38
  expected_result_list = []
39
  actual_result_list = []
40
  test_result = []
41
- for test_case in test_cases:
42
- expected_result = instructor_function(*test_case)
43
- actual_result = student_function(*test_case)
44
- expected_result_list.append(expected_result)
45
- actual_result_list.append(actual_result)
46
- test_result.append("Passed" if expected_result == actual_result else "Failed")
47
  result_df = pd.DataFrame({
48
- "Expected": expected_result_list,
49
- "Run": actual_result_list,
50
- "Result": test_result
51
  }, index=None)
52
  st.markdown(result_df.style.hide(axis="index").to_html(), unsafe_allow_html=True)
53
  st.markdown("<br>", unsafe_allow_html=True)
54
 
55
  # Check if all tests passed
56
- if all(expected == actual for expected, actual in zip(expected_result_list, actual_result_list)):
57
  st.success("All tests passed!")
58
  else:
59
  st.error("Some tests failed.")
60
  return True
61
 
62
  except SyntaxError as e:
63
- st.error(f"Error: {e}")
64
 
65
 
66
  def main():
67
-
68
  with st.form('app'):
69
  st.markdown("<h2 style='color: #707379;'>Coding Challenge - Code Practice</h2>", unsafe_allow_html=True)
70
-
71
  # Description of the challenge
72
  st.write("Create a function sum that meets the following criteria:")
73
- st.write('''Create a function that sums any two values''')
74
-
75
- func = 'sum'
76
- def execute_instructor_code(params):
77
- # Execute the instructor's code to get the result
78
- globals_dict = {}
79
- exec('''def sum(a,b):
80
- return a+b''', globals_dict)
81
- instructor_function = globals_dict.get(func)
82
-
83
- if not callable(instructor_function):
84
- raise ValueError(f"{func} is not a callable function.")
85
-
86
- return instructor_function(*params)
87
-
88
- # Generate sample examples dynamically by executing the instructor's code
89
- test_cases = [(1, 2), (100, 200), (0, 3)]
90
- sample_examples = "\n".join([f"{func}({', '.join(map(str, params))}) -> {execute_instructor_code(params)}" for params in test_cases])
91
  st.code(sample_examples, language="python")
92
  show_solution_button = st.form_submit_button("Show Solution")
93
  if show_solution_button:
94
  solution = '''def sum(a,b):
95
- return a+b'''
96
  if solution:
97
  st.code('''def sum(a,b):
98
- return a+b''', language="python")
99
  else:
100
  st.error('No Solution Provided!')
101
-
102
  # Code input area
103
  code = st_ace(value='''def sum(a,b):''', language="python", key="my_editor", theme='clouds', height=200)
104
  # Check solution button
105
  if st.form_submit_button("Check Solution"):
106
  if code:
107
  # Check the solution
108
- check_solution('''def sum(a,b):
109
- return a+b''',
110
- code,
111
- 'sum',
112
- [(1, 2), (100, 200), (0, 3)]
113
- )
114
  else:
115
  st.error("Please provide a solution.")
116
 
@@ -118,3 +95,5 @@ def main():
118
 
119
  if __name__ == "__main__":
120
  main()
 
 
 
1
 
2
+
3
  import streamlit as st
4
  from streamlit_ace import st_ace
5
  import pandas as pd
6
 
7
  button_style = st.markdown('''
8
  <style>
9
+ div.stButton > button:first-child {{
10
  background-color: #3A7EF1;
11
+ }}
12
  </style>''', unsafe_allow_html=True)
13
 
14
  left_co, cent_co,last_co = st.columns(3)
 
18
  st.markdown('')
19
 
20
 
21
+ def check_solution(code, func_params):
22
  try:
23
+ exec(code)
24
  globals_dict = {}
25
+ exec(code, globals_dict)
26
+ function_to_test = globals_dict.get(func_params.get('function_name'))
27
 
28
+ if not callable(function_to_test):
29
+ raise ValueError(f"{{func_params.get('function_name')}} is not a callable function.")
 
 
 
 
 
 
 
 
30
 
31
  # Run the tests and populate the results table
32
  expected_result_list = []
33
  actual_result_list = []
34
  test_result = []
35
+ for input_params, expected_output in func_params["test_cases"]:
36
+ expected_result_list.append(expected_output)
37
+ result = function_to_test(*input_params)
38
+ actual_result_list.append(result)
39
+ test_result.append("Passed" if expected_result_list == actual_result_list else "Failed")
 
40
  result_df = pd.DataFrame({
41
+ "Expected": expected_result_list,
42
+ "Run": actual_result_list,
43
+ "Result": test_result
44
  }, index=None)
45
  st.markdown(result_df.style.hide(axis="index").to_html(), unsafe_allow_html=True)
46
  st.markdown("<br>", unsafe_allow_html=True)
47
 
48
  # Check if all tests passed
49
+ if expected_result_list == actual_result_list:
50
  st.success("All tests passed!")
51
  else:
52
  st.error("Some tests failed.")
53
  return True
54
 
55
  except SyntaxError as e:
56
+ st.error(f"Error: {{e}}")
57
 
58
 
59
  def main():
60
+
61
  with st.form('app'):
62
  st.markdown("<h2 style='color: #707379;'>Coding Challenge - Code Practice</h2>", unsafe_allow_html=True)
63
+
64
  # Description of the challenge
65
  st.write("Create a function sum that meets the following criteria:")
66
+ st.write('''Sum of two values''')
67
+
68
+ # Display sample example for all test cases
69
+ sample_examples = "Sample"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
70
  st.code(sample_examples, language="python")
71
  show_solution_button = st.form_submit_button("Show Solution")
72
  if show_solution_button:
73
  solution = '''def sum(a,b):
74
+ return a+b '''
75
  if solution:
76
  st.code('''def sum(a,b):
77
+ return a+b ''', language="python")
78
  else:
79
  st.error('No Solution Provided!')
80
+
81
  # Code input area
82
  code = st_ace(value='''def sum(a,b):''', language="python", key="my_editor", theme='clouds', height=200)
83
  # Check solution button
84
  if st.form_submit_button("Check Solution"):
85
  if code:
86
  # Check the solution
87
+ check_solution(code, {
88
+ "function_name": "sum",
89
+ "test_cases": [((1, 2), 3), ((1, 0), 1), ((-1, -2), -3)]
90
+ })
 
 
91
  else:
92
  st.error("Please provide a solution.")
93
 
 
95
 
96
  if __name__ == "__main__":
97
  main()
98
+
99
+