import streamlit as st
from streamlit_ace import st_ace
import pandas as pd
button_style = st.markdown('''
''', 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("
School of AI by 1001epochs
", unsafe_allow_html=True)
st.markdown('')
def check_solution(code, func_params):
try:
exec(code)
globals_dict = {}
exec(code, globals_dict)
function_to_test = globals_dict.get(func_params.get('function_name'))
if not callable(function_to_test):
raise ValueError(f"{func_params.get('function_name')} is not a callable function.")
# Run the tests and populate the results table
expected_result_list = []
actual_result_list = []
test_result = []
for input_params, expected_output in func_params["test_cases"]:
expected_result_list.append(expected_output)
result = function_to_test(*input_params)
actual_result_list.append(result)
test_result.append("Passed" if expected_result_list == actual_result_list 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("
", unsafe_allow_html=True)
# Check if all tests passed
if 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("Coding Challenge - Code Practice
", unsafe_allow_html=True)
# Description of the challenge
st.write("Create a function twoSum that meets the following criteria:")
st.write('''1. Two Sum
Easy
Topics
Companies
Hint
Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.
You may assume that each input would have exactly one solution, and you may not use the same element twice.
You can return the answer in any order.
Example 1:
Input: nums = [2,7,11,15], target = 9
Output: [0,1]
Explanation: Because nums[0] + nums[1] == 9, we return [0, 1].
Example 2:
Input: nums = [3,2,4], target = 6
Output: [1,2]
Example 3:
Input: nums = [3,3], target = 6
Output: [0,1]
''')
# Display sample example for all test cases
sample_examples = "\n".join([f"twoSum({params}) -> {expected_output}" for params, expected_output in [([2, 7, 11, 15], 9), [0, 1]]])
st.code(sample_examples, language="python")
show_solution_button = st.form_submit_button("Show Solution")
if show_solution_button:
solution = '''def twoSum(self, nums: List[int], target: int) -> List[int]:
n = len(nums)
for i in range(n - 1):
for j in range(i + 1, n):
if nums[i] + nums[j] == target:
return [i, j]
return [] # No solution found'''
if solution:
st.code('''def twoSum(self, nums: List[int], target: int) -> List[int]:
n = len(nums)
for i in range(n - 1):
for j in range(i + 1, n):
if nums[i] + nums[j] == target:
return [i, j]
return [] # No solution found''', language="python")
else:
st.error('No Solution Provided!')
# Code input area
code = st_ace(value='''def twoSum(self, nums: List[int], target: int) -> List[int]:
''', 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(code, {
"function_name": "twoSum",
"test_cases": [([2, 7, 11, 15], 9), [0, 1]]
})
else:
st.error("Please provide a solution.")
st.markdown("Copyright© by 1001epochs. All rights reserved.")
if __name__ == "__main__":
main()