Skip to content

Commit 4f414e5

Browse files
authored
Merge pull request #100 from guanquann/questionSeed
Seed question with test cases and code template + bug fix
2 parents 563a247 + 853ecff commit 4f414e5

34 files changed

+615
-41
lines changed

backend/code-execution-service/src/controllers/codeExecutionControllers.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,16 @@ export const executeCode = async (req: Request, res: Response) => {
7979
stdout = "";
8080
}
8181

82-
// Extract the last line as the result value
83-
// and the rest as stdout
84-
const lines = stdout.trim().split("\n");
85-
const resultValue = lines.pop() || "";
86-
stdout = lines.join("\n");
82+
let resultValue = "";
83+
if (restofResult.stderr) {
84+
resultValue = "";
85+
stdout = stdout.trim();
86+
} else {
87+
// Extract the last line as the result value and the rest as stdout only if there is no error
88+
const lines = stdout.trim().split("\n");
89+
resultValue = lines.pop() || "";
90+
stdout = lines.join("\n");
91+
}
8792

8893
return {
8994
...restofResult,
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Question Service Seed
2+
3+
Questions, test cases, code templates, and solutions are generated using ChatGPT.
4+
5+
Disclaimer: They may not be fully accurate.
6+

backend/question-service/src/scripts/seed.ts

Lines changed: 181 additions & 35 deletions
Large diffs are not rendered by default.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Please do not modify the main function
2+
def main():
3+
a = input().strip()
4+
b = input().strip()
5+
print(solution(a, b))
6+
7+
8+
# Write your code here
9+
def solution(a, b):
10+
# Make sure a is the longer string
11+
if len(b) > len(a):
12+
a, b = b, a
13+
14+
# Initialize result and carry
15+
result = []
16+
carry = 0
17+
b = b.zfill(len(a)) # Pad the shorter string with leading zeros
18+
19+
# Add binary numbers from the end to the start
20+
for i in range(len(a) - 1, -1, -1):
21+
sum = int(a[i]) + int(b[i]) + carry
22+
result.append(str(sum % 2)) # Append the current binary digit
23+
carry = sum // 2 # Calculate the carry
24+
25+
# If there’s a carry left after the final addition, add it
26+
if carry:
27+
result.append("1")
28+
29+
# Join and reverse result since we appended in reverse order
30+
return "".join(result[::-1])
31+
32+
33+
if __name__ == "__main__":
34+
main()
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
11
2+
1
3+
4+
1010
5+
1011
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
100
2+
3+
10101
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# Definition for a binary tree node.
2+
class TreeNode:
3+
def __init__(self, val=0, left=None, right=None):
4+
self.val = val
5+
self.left = left
6+
self.right = right
7+
8+
9+
# Please do not modify the main function
10+
def main():
11+
root = deserialize(input().strip())
12+
print(solution(root))
13+
14+
15+
# Write your code here
16+
def solution(root):
17+
result = []
18+
19+
def inorder(node):
20+
if not node:
21+
return
22+
inorder(node.left)
23+
result.append(node.val)
24+
inorder(node.right)
25+
26+
inorder(root)
27+
return result
28+
29+
30+
# Helper function to deserialize input in space-separated format
31+
def deserialize(data):
32+
if not data:
33+
return None
34+
35+
nodes = data.split()
36+
root = TreeNode(int(nodes[0]))
37+
queue = [root]
38+
i = 1
39+
40+
while queue and i < len(nodes):
41+
node = queue.pop(0)
42+
if nodes[i] != "null":
43+
node.left = TreeNode(int(nodes[i]))
44+
queue.append(node.left)
45+
i += 1
46+
if i < len(nodes) and nodes[i] != "null":
47+
node.right = TreeNode(int(nodes[i]))
48+
queue.append(node.right)
49+
i += 1
50+
51+
return root
52+
53+
54+
if __name__ == "__main__":
55+
main()
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
1 null 2 3
2+
3+
1 2 3 4 5 null 8 null null 6 7 9
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
1 3 2
2+
3+
4 2 6 5 7 1 3 9 8
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# Definition for a binary tree node.
2+
class TreeNode:
3+
def __init__(self, val=0, left=None, right=None):
4+
self.val = val
5+
self.left = left
6+
self.right = right
7+
8+
9+
# Please do not modify the main function
10+
def main():
11+
root = deserialize(input().strip())
12+
print(solution(root))
13+
14+
15+
# Write your code here
16+
def solution(root):
17+
def max_gain(node):
18+
nonlocal max_sum
19+
if not node:
20+
return 0
21+
22+
# Recursively calculate the maximum path sum of left and right subtrees
23+
left_gain = max(max_gain(node.left), 0) # Ignore negative paths
24+
right_gain = max(max_gain(node.right), 0) # Ignore negative paths
25+
26+
# Current node max path sum includes both left and right contributions
27+
current_path_sum = node.val + left_gain + right_gain
28+
29+
# Update the maximum path sum found so far
30+
max_sum = max(max_sum, current_path_sum)
31+
32+
# Return the max gain if we continue the same path with this node
33+
return node.val + max(left_gain, right_gain)
34+
35+
max_sum = float("-inf")
36+
max_gain(root)
37+
return max_sum
38+
39+
40+
# Helper function to deserialize input in space-separated format
41+
def deserialize(data):
42+
if not data:
43+
return None
44+
45+
nodes = data.split()
46+
root = TreeNode(int(nodes[0]))
47+
queue = [root]
48+
i = 1
49+
50+
while queue and i < len(nodes):
51+
node = queue.pop(0)
52+
if nodes[i] != "null":
53+
node.left = TreeNode(int(nodes[i]))
54+
queue.append(node.left)
55+
i += 1
56+
if i < len(nodes) and nodes[i] != "null":
57+
node.right = TreeNode(int(nodes[i]))
58+
queue.append(node.right)
59+
i += 1
60+
61+
return root
62+
63+
64+
if __name__ == "__main__":
65+
main()

0 commit comments

Comments
 (0)