@@ -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
18071801def 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"""
18211816question 80
18221817Given the root of a binary tree, return a deepest node. For example, in the
@@ -1850,3 +1845,143 @@ def question80():
18501845
18511846def 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 ()
0 commit comments