Spaces:
Sleeping
Sleeping
epochs-demos
commited on
Commit
•
7a80f4b
1
Parent(s):
4c3e064
Upload app.py with huggingface_hub
Browse files
app.py
CHANGED
@@ -1,133 +1,96 @@
|
|
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)
|
14 |
-
with cent_co:
|
15 |
-
st.image("https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTzYyVPVc8EX7_N2QOh9D86t54SS7ucBDcOU_Tn52LwBi8BQTwcdeCVKcyjSKBGExjB4g&usqp=CAU")
|
16 |
-
st.markdown("<h5 style='color: #707379; text-align: center;'>School of AI by 1001epochs</h5>", unsafe_allow_html=True)
|
17 |
-
st.markdown('')
|
18 |
-
|
19 |
-
|
20 |
-
def check_solution(code, func_params):
|
21 |
-
try:
|
22 |
-
exec(code)
|
23 |
-
globals_dict = {}
|
24 |
-
exec(code, globals_dict)
|
25 |
-
function_to_test = globals_dict.get(func_params.get('function_name'))
|
26 |
-
|
27 |
-
if not callable(function_to_test):
|
28 |
-
raise ValueError(f"{func_params.get('function_name')} is not a callable function.")
|
29 |
-
|
30 |
-
# Run the tests and populate the results table
|
31 |
-
expected_result_list = []
|
32 |
-
actual_result_list = []
|
33 |
-
test_result = []
|
34 |
-
for input_params, expected_output in func_params["test_cases"]:
|
35 |
-
expected_result_list.append(expected_output)
|
36 |
-
result = function_to_test(*input_params)
|
37 |
-
actual_result_list.append(result)
|
38 |
-
test_result.append("Passed" if expected_result_list == actual_result_list else "Failed")
|
39 |
-
result_df = pd.DataFrame({
|
40 |
-
"Expected": expected_result_list,
|
41 |
-
"Run": actual_result_list,
|
42 |
-
"Result": test_result
|
43 |
-
}, index=None)
|
44 |
-
st.markdown(result_df.style.hide(axis="index").to_html(), unsafe_allow_html=True)
|
45 |
-
st.markdown("<br>", unsafe_allow_html=True)
|
46 |
-
|
47 |
-
# Check if all tests passed
|
48 |
-
if expected_result_list == actual_result_list:
|
49 |
-
st.success("All tests passed!")
|
50 |
-
else:
|
51 |
-
st.error("Some tests failed.")
|
52 |
-
return True
|
53 |
-
|
54 |
-
except SyntaxError as e:
|
55 |
-
st.error(f"Error: {e}")
|
56 |
-
|
57 |
-
|
58 |
-
def main():
|
59 |
-
|
60 |
-
with st.form('app'):
|
61 |
-
st.markdown("<h2 style='color: #707379;'>Coding Challenge - Code Practice</h2>", unsafe_allow_html=True)
|
62 |
-
|
63 |
-
# Description of the challenge
|
64 |
-
st.write("Create a function
|
65 |
-
st.write('''
|
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 |
-
if show_solution_button:
|
98 |
-
solution = '''def twoSum(self, nums: List[int], target: int) -> List[int]:
|
99 |
-
n = len(nums)
|
100 |
-
for i in range(n - 1):
|
101 |
-
for j in range(i + 1, n):
|
102 |
-
if nums[i] + nums[j] == target:
|
103 |
-
return [i, j]
|
104 |
-
return [] # No solution found'''
|
105 |
-
if solution:
|
106 |
-
st.code('''def twoSum(self, nums: List[int], target: int) -> List[int]:
|
107 |
-
n = len(nums)
|
108 |
-
for i in range(n - 1):
|
109 |
-
for j in range(i + 1, n):
|
110 |
-
if nums[i] + nums[j] == target:
|
111 |
-
return [i, j]
|
112 |
-
return [] # No solution found''', language="python")
|
113 |
-
else:
|
114 |
-
st.error('No Solution Provided!')
|
115 |
-
|
116 |
-
# Code input area
|
117 |
-
code = st_ace(value='''def twoSum(self, nums: List[int], target: int) -> List[int]:
|
118 |
-
''', language="python", key="my_editor", theme='clouds', height=200)
|
119 |
-
# Check solution button
|
120 |
-
if st.form_submit_button("Check Solution"):
|
121 |
-
if code:
|
122 |
-
# Check the solution
|
123 |
-
check_solution(code, {
|
124 |
-
"function_name": "twoSum",
|
125 |
-
"test_cases": [([2, 7, 11, 15], 9), [0, 1]]
|
126 |
-
})
|
127 |
-
else:
|
128 |
-
st.error("Please provide a solution.")
|
129 |
-
|
130 |
-
st.markdown("Copyright� by 1001epochs. All rights reserved.")
|
131 |
-
|
132 |
-
if __name__ == "__main__":
|
133 |
-
main()
|
|
|
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)
|
14 |
+
with cent_co:
|
15 |
+
st.image("https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTzYyVPVc8EX7_N2QOh9D86t54SS7ucBDcOU_Tn52LwBi8BQTwcdeCVKcyjSKBGExjB4g&usqp=CAU")
|
16 |
+
st.markdown("<h5 style='color: #707379; text-align: center;'>School of AI by 1001epochs</h5>", unsafe_allow_html=True)
|
17 |
+
st.markdown('')
|
18 |
+
|
19 |
+
|
20 |
+
def check_solution(code, func_params):
|
21 |
+
try:
|
22 |
+
exec(code)
|
23 |
+
globals_dict = {}
|
24 |
+
exec(code, globals_dict)
|
25 |
+
function_to_test = globals_dict.get(func_params.get('function_name'))
|
26 |
+
|
27 |
+
if not callable(function_to_test):
|
28 |
+
raise ValueError(f"{func_params.get('function_name')} is not a callable function.")
|
29 |
+
|
30 |
+
# Run the tests and populate the results table
|
31 |
+
expected_result_list = []
|
32 |
+
actual_result_list = []
|
33 |
+
test_result = []
|
34 |
+
for input_params, expected_output in func_params["test_cases"]:
|
35 |
+
expected_result_list.append(expected_output)
|
36 |
+
result = function_to_test(*input_params)
|
37 |
+
actual_result_list.append(result)
|
38 |
+
test_result.append("Passed" if expected_result_list == actual_result_list else "Failed")
|
39 |
+
result_df = pd.DataFrame({
|
40 |
+
"Expected": expected_result_list,
|
41 |
+
"Run": actual_result_list,
|
42 |
+
"Result": test_result
|
43 |
+
}, index=None)
|
44 |
+
st.markdown(result_df.style.hide(axis="index").to_html(), unsafe_allow_html=True)
|
45 |
+
st.markdown("<br>", unsafe_allow_html=True)
|
46 |
+
|
47 |
+
# Check if all tests passed
|
48 |
+
if expected_result_list == actual_result_list:
|
49 |
+
st.success("All tests passed!")
|
50 |
+
else:
|
51 |
+
st.error("Some tests failed.")
|
52 |
+
return True
|
53 |
+
|
54 |
+
except SyntaxError as e:
|
55 |
+
st.error(f"Error: {e}")
|
56 |
+
|
57 |
+
|
58 |
+
def main():
|
59 |
+
|
60 |
+
with st.form('app'):
|
61 |
+
st.markdown("<h2 style='color: #707379;'>Coding Challenge - Code Practice</h2>", unsafe_allow_html=True)
|
62 |
+
|
63 |
+
# Description of the challenge
|
64 |
+
st.write("Create a function sum that meets the following criteria:")
|
65 |
+
st.write('''Sum of two values''')
|
66 |
+
|
67 |
+
# Display sample example for all test cases
|
68 |
+
sample_examples = "\n".join([f"sum({params}) -> {expected_output}" for params, expected_output in [(1, 2), (2, 3), (3, 4)]])
|
69 |
+
st.code(sample_examples, language="python")
|
70 |
+
show_solution_button = st.form_submit_button("Show Solution")
|
71 |
+
if show_solution_button:
|
72 |
+
solution = '''def sum(a,b):
|
73 |
+
return a+b'''
|
74 |
+
if solution:
|
75 |
+
st.code('''def sum(a,b):
|
76 |
+
return a+b''', language="python")
|
77 |
+
else:
|
78 |
+
st.error('No Solution Provided!')
|
79 |
+
|
80 |
+
# Code input area
|
81 |
+
code = st_ace(value='''def sum(a,b):''', language="python", key="my_editor", theme='clouds', height=200)
|
82 |
+
# Check solution button
|
83 |
+
if st.form_submit_button("Check Solution"):
|
84 |
+
if code:
|
85 |
+
# Check the solution
|
86 |
+
check_solution(code, {
|
87 |
+
"function_name": "sum",
|
88 |
+
"test_cases": [(1, 2), (2, 3), (3, 4)]
|
89 |
+
})
|
90 |
+
else:
|
91 |
+
st.error("Please provide a solution.")
|
92 |
+
|
93 |
+
st.markdown("Copyright© by 1001epochs. All rights reserved.")
|
94 |
+
|
95 |
+
if __name__ == "__main__":
|
96 |
+
main()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|