from sandbox import run_python_sandbox class TestSandboxAdvancedFeatures: def test_leetcode_string_input_two_sum(self): """LeetCode-style assignment strings should bind to method parameters.""" user_code = """ class Solution: def twoSum(self, nums: list[int], target: int) -> list[int]: lookup = {} for i, num in enumerate(nums): diff = target - num if diff in lookup: return [lookup[diff], i] lookup[num] = i return [] """ test_cases = [ {"input": "nums = [2,7,11,15], target = 9", "expected": "[0,1]"}, {"input": "nums = [3,2,4], target = 6", "expected": "[1,2]"}, ] result = run_python_sandbox(user_code, test_cases) assert result["success"] is True results = result["results"] assert len(results) == 2 assert results[0]["status"] == "passed" assert results[0]["passed"] is True assert results[0]["output"] == [0, 1] assert results[1]["status"] == "passed" assert results[1]["passed"] is True assert results[1]["output"] == [1, 2] def test_leetcode_boolean_string_comparison(self): """String outputs 'true' and 'false' should match Python booleans True and False.""" user_code = """ class Solution: def containsDuplicate(self, nums: list[int]) -> bool: seen = set() for n in nums: if n in seen: return True seen.add(n) return False """ test_cases = [ {"input": "nums = [1,2,3,1]", "expected": "true"}, {"input": "nums = [1,2,3,4]", "expected": "false"}, ] result = run_python_sandbox(user_code, test_cases) assert result["success"] is True results = result["results"] assert len(results) == 2 assert results[0]["status"] == "passed" assert results[0]["passed"] is True assert results[0]["output"] is True assert results[1]["status"] == "passed" assert results[1]["passed"] is True assert results[1]["output"] is False def test_linked_list_conversion(self): """Standard LeetCode ListNode conversion for inputs and outputs.""" user_code = """ class Solution: def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]: prev = None curr = head while curr: nxt = curr.next curr.next = prev prev = curr curr = nxt return prev """ test_cases = [ {"input": "head = [1,2,3,4,5]", "expected": "[5,4,3,2,1]"}, {"input": "head = [1,2]", "expected": "[2,1]"}, ] result = run_python_sandbox(user_code, test_cases) assert result["success"] is True results = result["results"] assert len(results) == 2 assert results[0]["status"] == "passed" assert results[0]["passed"] is True assert results[0]["output"] == [5, 4, 3, 2, 1] assert results[1]["status"] == "passed" assert results[1]["passed"] is True assert results[1]["output"] == [2, 1] def test_binary_tree_conversion(self): """Standard LeetCode TreeNode conversion for inputs and outputs.""" user_code = """ class Solution: def invertTree(self, root: Optional[TreeNode]) -> Optional[TreeNode]: if not root: return None root.left, root.right = self.invertTree(root.right), self.invertTree(root.left) return root """ test_cases = [ {"input": "root = [4,2,7,1,3,6,9]", "expected": "[4,7,2,9,6,3,1]"}, {"input": "root = [2,1,3]", "expected": "[2,3,1]"}, ] result = run_python_sandbox(user_code, test_cases) assert result["success"] is True results = result["results"] assert len(results) == 2 assert results[0]["status"] == "passed" assert results[0]["passed"] is True assert results[0]["output"] == [4, 7, 2, 9, 6, 3, 1] assert results[1]["status"] == "passed" assert results[1]["passed"] is True assert results[1]["output"] == [2, 3, 1] def test_in_place_mutation(self): """Functions mutating inputs in-place and returning None should be verified.""" user_code = """ class Solution: def rotate(self, matrix: list[list[int]]) -> None: matrix.reverse() for i in range(len(matrix)): for j in range(i): matrix[i][j], matrix[j][i] = matrix[j][i], matrix[i][j] """ test_cases = [ { "input": "matrix = [[1,2,3],[4,5,6],[7,8,9]]", "expected": "[[7,4,1],[8,5,2],[9,6,3]]", } ] result = run_python_sandbox(user_code, test_cases) assert result["success"] is True results = result["results"] assert len(results) == 1 assert results[0]["status"] == "passed" assert results[0]["passed"] is True assert results[0]["output"] == [[7, 4, 1], [8, 5, 2], [9, 6, 3]] def test_order_independent_group_anagrams(self): """Unordered lists of lists should compare correctly regardless of permutations.""" user_code = """ from collections import defaultdict class Solution: def groupAnagrams(self, strs: list[str]) -> list[list[str]]: ans = defaultdict(list) for s in strs: ans[tuple(sorted(s))].append(s) return list(ans.values()) """ test_cases = [ { "input": 'strs = ["eat","tea","tan","ate","nat","bat"]', "expected": '[["bat"],["nat","tan"],["ate","eat","tea"]]', } ] result = run_python_sandbox(user_code, test_cases) assert result["success"] is True results = result["results"] assert len(results) == 1 assert results[0]["status"] == "passed" assert results[0]["passed"] is True def test_design_pattern_min_stack(self): """Design problem execution with commands and arguments.""" user_code = """ class MinStack: def __init__(self): self.stack = [] self.min_stack = [] def push(self, val: int) -> None: self.stack.append(val) val = min(val, self.min_stack[-1] if self.min_stack else val) self.min_stack.append(val) def pop(self) -> None: self.stack.pop() self.min_stack.pop() def top(self) -> int: return self.stack[-1] def getMin(self) -> int: return self.min_stack[-1] """ test_cases = [ { "input": 'commands = ["MinStack","push","push","push","getMin","pop","top","getMin"], args = [[],[-2],[0],[-3],[],[],[],[]]', "expected": "[null,null,null,null,-3,null,0,-2]", } ] result = run_python_sandbox(user_code, test_cases) assert result["success"] is True results = result["results"] assert len(results) == 1 assert results[0]["status"] == "passed" assert results[0]["passed"] is True assert results[0]["output"] == [None, None, None, None, -3, None, 0, -2]