Skip to content

Commit c0fa969

Browse files
committed
resolve #81, #82
1 parent 5ed7519 commit c0fa969

File tree

5 files changed

+190
-41
lines changed

5 files changed

+190
-41
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@ note.md
22
__pycache__/
33
venv/
44
stats/
5+
.vscode
6+
sync.sh

daily.py

Lines changed: 155 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1780,43 +1780,38 @@ def test_78():
17801780
"""
17811781

17821782

1783-
def canBecomeNonincreasing(nums):
1783+
def canBecomeNondecreasing(nums):
17841784
count = 0
17851785
valley_idx = 0
1786-
pre = nums[0]
17871786
for i in range(1, len(nums)):
1788-
if nums[i] < pre:
1787+
if nums[i] < nums[i - 1]:
17891788
count += 1
17901789
valley_idx = i
1791-
pre = nums[i]
1792-
if count == 0:
1793-
return True
1794-
if count > 1:
1795-
return False
1796-
# count = 1
1797-
if (
1798-
valley_idx == 1
1790+
if count > 1:
1791+
return False
1792+
# count = 0 or 1
1793+
return (
1794+
count == 0
1795+
or valley_idx == 1
17991796
or valley_idx == len(nums) - 1
18001797
or nums[valley_idx - 1] <= nums[valley_idx + 1]
1801-
):
1802-
return True
1803-
else:
1804-
return False
1798+
)
18051799

18061800

18071801
def test_79():
18081802
nums = [10, 5, 7]
1809-
assert canBecomeNonincreasing(nums)
1803+
assert canBecomeNondecreasing(nums)
18101804
nums = [10, 5, 1]
1811-
assert not canBecomeNonincreasing(nums)
1805+
assert not canBecomeNondecreasing(nums)
18121806
nums = [1, 5, 2, 7]
1813-
assert canBecomeNonincreasing(nums)
1807+
assert canBecomeNondecreasing(nums)
18141808
nums = [1, 5, 2, 3]
1815-
assert not canBecomeNonincreasing(nums)
1809+
assert not canBecomeNondecreasing(nums)
18161810
nums = [1, 2, 3, -10]
1817-
assert canBecomeNonincreasing(nums)
1811+
assert canBecomeNondecreasing(nums)
18181812

18191813

1814+
test_79()
18201815
"""
18211816
question 80
18221817
Given the root of a binary tree, return a deepest node. For example, in the
@@ -1850,3 +1845,143 @@ def question80():
18501845

18511846
def test_80():
18521847
pass
1848+
1849+
1850+
"""
1851+
question 81
1852+
Given a mapping of digits to letters (as in a phone number), and a digit string,
1853+
return all possible letters the number could represent. You can assume each
1854+
valid number in the mapping is a single digit.
1855+
1856+
For example if {“2”: [“a”, “b”, “c”], 3: [“d”, “e”, “f”], …} then “23” should
1857+
return [“ad”, “ae”, “af”, “bd”, “be”, “bf”, “cd”, “ce”, “cf"].
1858+
1859+
-------------------
1860+
One digit can be mapped to different letters. We need to exhaust all the combinations
1861+
of the possible letters of each digit.
1862+
1863+
For a two-digit number, we can use two nested loops to exhaust all the combinations of
1864+
the two lists.
1865+
For a n-digit number, we need more general way to handle it.
1866+
There are two approaches: iteration and recursion
1867+
1868+
Iteration
1869+
Create a queue and each element in the queue is a list of string.
1870+
Initially for every digit of the given number, get its mapped letters from the map and
1871+
store it to the queue.
1872+
1873+
Then we can iterate the queue until only one element left.
1874+
At each step remove two elements from the queue and merge them into one,
1875+
with all combinations of the two list. Then add the merged list to the queue.
1876+
Return the last element.
1877+
1878+
Recursion
1879+
Break down the problem into smaller problems.
1880+
The problem is that for a given number we need to return a list of string reprented by letter.
1881+
For a n-digit number, split it to two parts: first digit and n-1 digits.
1882+
First sesolve the two smaller problels seperately and recursively.
1883+
Then merge the returned two lists into one and return.
1884+
The base case is when there is only one digit in the number, return its mapped letters directly.
1885+
1886+
Let's do this in recursive way.
1887+
"""
1888+
1889+
1890+
def convertToLetters(map, num):
1891+
def dfs(s):
1892+
if len(s) == 1:
1893+
return map[s]
1894+
rtn = []
1895+
l1 = dfs(s[0])
1896+
l2 = dfs(s[1:])
1897+
for e1 in l1:
1898+
for e2 in l2:
1899+
rtn.append(e1 + e2)
1900+
return rtn
1901+
1902+
return dfs(num)
1903+
1904+
1905+
def test_81():
1906+
map = {"2": ["a", "b", "c"], "3": ["d", "e", "f"], "4": ["h", "i"]}
1907+
num = "23"
1908+
assert convertToLetters(map, num) == [
1909+
"ad",
1910+
"ae",
1911+
"af",
1912+
"bd",
1913+
"be",
1914+
"bf",
1915+
"cd",
1916+
"ce",
1917+
"cf",
1918+
]
1919+
num = "342"
1920+
assert convertToLetters(map, num) == [
1921+
"dha",
1922+
"dhb",
1923+
"dhc",
1924+
"dia",
1925+
"dib",
1926+
"dic",
1927+
"eha",
1928+
"ehb",
1929+
"ehc",
1930+
"eia",
1931+
"eib",
1932+
"eic",
1933+
"fha",
1934+
"fhb",
1935+
"fhc",
1936+
"fia",
1937+
"fib",
1938+
"fic",
1939+
]
1940+
1941+
1942+
"""
1943+
question 82
1944+
Using a read7() method that returns 7 characters from a file, implement readN(n)
1945+
which reads n characters.
1946+
1947+
For example, given a file with the content “Hello world”, three read7() returns
1948+
“Hello w”, “orld” and then “”.
1949+
1950+
-------------------
1951+
1952+
"""
1953+
input = "abcdefghijklmnopqrstuvw"
1954+
idx = 0
1955+
1956+
1957+
def read7():
1958+
global idx
1959+
diff = min(7, len(input) - idx)
1960+
idx += diff
1961+
return input[idx - diff : idx]
1962+
1963+
1964+
def readN(n):
1965+
li = []
1966+
while n > 0:
1967+
s = read7()
1968+
if s == "":
1969+
break
1970+
elif n < len(s):
1971+
li.append(s[:n])
1972+
else:
1973+
li.append(s)
1974+
n -= 7
1975+
return "".join(li)
1976+
1977+
1978+
def test_82():
1979+
global idx
1980+
assert len(readN(20)) == 20
1981+
idx = 0
1982+
assert len(readN(14)) == 14
1983+
idx = 0
1984+
assert len(readN(50)) == len(input)
1985+
1986+
1987+
test_82()

format.py

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
# Read problem description from a file
2-
# the first line of the file is the quesiton number.
3-
# Generate a output file with the description included in a block comment
2+
# the first line of the file is the question number.
3+
# Generate an output file with the description included in a block comment
44
# with each line less than 80.
55
# and create question and test functions for it.
6+
#
7+
# read from question.txt, write to daily.py
68
def format_question(text: str, num: int, limit: int) -> str:
7-
output = '"""\nquestion ' + str(num) + "\n"
9+
output = '\n"""\nquestion ' + str(num) + "\n"
810
cur_line = ""
911
for line in text.splitlines():
1012
for token in line.split(" "):
@@ -34,8 +36,8 @@ def format_question(text: str, num: int, limit: int) -> str:
3436

3537

3638
def read_and_format(input_file, output_file):
37-
with open(input_file) as file_object:
38-
lines = file_object.readlines()
39+
with open(input_file, "r") as input_obj:
40+
lines = input_obj.readlines()
3941
text = ""
4042
for i, line in enumerate(lines):
4143
if i == 0:
@@ -44,8 +46,19 @@ def read_and_format(input_file, output_file):
4446
text += line
4547

4648
output = format_question(text, num, 80)
47-
with open(output_file, "w") as file_object:
48-
file_object.write(output)
49+
test_method = "test_" + str(num) + "()"
50+
with open(output_file, "a+") as output_obj:
51+
output_obj.seek(0)
52+
lines = output_obj.readlines()
53+
for line in lines:
54+
if test_method in line:
55+
print(
56+
"The question is already included in ",
57+
output_file + ".",
58+
"Do Nothing.",
59+
)
60+
return
61+
output_obj.write(output)
4962

5063

51-
read_and_format("question.txt", "output.txt")
64+
read_and_format("question.txt", "daily.py")

output.txt

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
"""
2-
question 80
3-
Given the root of a binary tree, return a deepest node. For example, in the
4-
following tree, return d.
2+
question 82
3+
Using a read7() method that returns 7 characters from a file, implement readN(n)
4+
which reads n characters.
5+
6+
For example, given a file with the content “Hello world”, three read7() returns
7+
“Hello w”, “orld” and then “”.
58

69
-------------------
710

811

912
"""
10-
def question80(): pass
11-
def test_80(): pass
12-
test_80()
13+
def question82(): pass
14+
def test_82(): pass
15+
test_82()

question.txt

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
80
2-
Given the root of a binary tree, return a deepest node. For example, in the following tree, return d.
1+
82
2+
Using a read7() method that returns 7 characters from a file, implement readN(n) which reads n characters.
33

4-
a
5-
/ \
6-
b c
7-
/
8-
d
4+
For example, given a file with the content “Hello world”, three read7() returns “Hello w”, “orld” and then “”.

0 commit comments

Comments
 (0)