Skip to content

Commit ba3f029

Browse files
committed
resolve 85
1 parent ac6f54e commit ba3f029

File tree

6 files changed

+100
-22
lines changed

6 files changed

+100
-22
lines changed

src/083.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
"""
2+
question 83
3+
Invert a binary tree.
4+
5+
For example, given the following tree:
6+
7+
a
8+
/ \
9+
b c
10+
/ \\ /
11+
d e f
12+
13+
should become:
14+
15+
a
16+
/ \
17+
c b
18+
\\ / \
19+
f e d
20+
21+
-------------------
22+
23+
For every node its left and right child needs to be swapped.
24+
This needs to be done to every node in the tree.
25+
We can do this in recursive way (DFS) or interative way (BFS)
26+
"""
27+
28+
29+
def invertTree():
30+
pass
31+
32+
33+
def test_83():
34+
pass

src/084.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
"""
2+
question 84
3+
Given a matrix of 1s and 0s, return the number of "islands" in the matrix. A 1
4+
represents land and 0 represents water, so an island is a group of 1s that are
5+
neighboring whose perimeter is surrounded by water.
6+
7+
For example, this matrix has 4 islands.
8+
9+
1 0 0 0 0
10+
0 0 1 1 0
11+
0 1 1 0 0
12+
0 0 0 0 0
13+
1 1 0 0 1
14+
1 1 0 0 1
15+
16+
-------------------
17+
18+
19+
"""
20+
21+
22+
def number_of_island():
23+
pass
24+
25+
26+
def test_84():
27+
pass

src/085.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
"""
2+
question 85
3+
Given three 32-bit integers x, y, and b, return x if b is 1 and y if b is 0,
4+
using only mathematical or bit operations. You can assume b can only be 1 or 0.
5+
6+
-------------------
7+
8+
"""
9+
10+
11+
def x_or_y(x, y, b):
12+
return x * b + y * (1 - b)
13+
14+
15+
def test_85():
16+
assert x_or_y(10, 12, 1) == 10
17+
assert x_or_y(10, 12, 0) == 12
18+
19+
20+
test_85()

src/ds/classes.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ def print(self):
101101

102102

103103
class TreeNode:
104-
def __init__(self, val: int, left: object = None, right: object = None):
104+
def __init__(self, val, left: object = None, right: object = None):
105105
self.val = val
106106
self.left = left
107107
self.right = right

src/utils/format.py renamed to src/utils/formatter.py

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
#
22
# Utility to generate code template for a question.
33
#
4-
# Read problem description from a file.
4+
# Read from a file.
55
# The first line of the file is the question number.
6+
# Remaining lines are the problem's description.
7+
68
# Generate an output file with the description included in a block comment.
79
# with each line less than 80.
810
# and create question function and test function.
911
#
12+
import os
1013

11-
from_file = "src/question.txt"
12-
to_file = "src/daily.py"
14+
from_file = "src/utils/question.txt"
15+
to_dir = "src/"
1316

1417

1518
def format_question(text: str, num: int, limit: int) -> str:
@@ -46,30 +49,26 @@ def read_and_format(input_file, output_file):
4649
with open(input_file, "r") as input_obj:
4750
lines = input_obj.readlines()
4851
text = ""
52+
suffix = ""
4953
for i, line in enumerate(lines):
5054
if i == 0:
55+
suffix = line.strip()
56+
while len(suffix) < 3:
57+
suffix = "0" + suffix
5158
num = int(line.strip())
5259
else:
5360
text += line
54-
61+
output_file += suffix + ".py"
62+
if os.path.isfile(output_file):
63+
print(output_file, "already exists. Do nothing.")
64+
return
5565
output = format_question(text, num, 80)
56-
test_method = "test_" + str(num) + "()"
57-
with open(output_file, "a+") as output_obj:
58-
output_obj.seek(0)
59-
lines = output_obj.readlines()
60-
for line in lines:
61-
if test_method in line:
62-
print(
63-
"The question is already included in ",
64-
output_file + ".",
65-
"Do Nothing.",
66-
)
67-
return
66+
with open(output_file, "w") as output_obj:
6867
output_obj.write(output)
6968

7069

7170
def generate():
72-
read_and_format(from_file, to_file)
71+
read_and_format(from_file, to_dir)
7372

7473

7574
if __name__ == "__main__":

src/utils/question.txt

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,2 @@
1-
82
2-
Using a read7() method that returns 7 characters from a file, implement readN(n) which reads n characters.
3-
4-
For example, given a file with the content “Hello world”, three read7() returns “Hello w”, “orld” and then “”.
1+
85
2+
Given three 32-bit integers x, y, and b, return x if b is 1 and y if b is 0, using only mathematical or bit operations. You can assume b can only be 1 or 0.

0 commit comments

Comments
 (0)