File tree Expand file tree Collapse file tree 3 files changed +62
-2
lines changed Expand file tree Collapse file tree 3 files changed +62
-2
lines changed Original file line number Diff line number Diff line change
1
+ import heapq
2
+
1
3
def heapify (ls ):
2
4
while True :
3
5
a = False
@@ -60,7 +62,7 @@ def heappop(ls):
60
62
61
63
def solution (input ):
62
64
ls = [8 ,7 ,6 ,5 ,4 ,3 ,1 ]
63
- heapify (ls )
64
- heappush ( ls , 2 )
65
+ heapq . heapify (ls )
66
+ print ( heapq . nlargest ( 2 , ls ) )
65
67
return str (ls )
66
68
Original file line number Diff line number Diff line change
1
+ class TrieNode :
2
+ def __init__ (self ) -> None :
3
+ self .children = [None for i in range (62 )]
4
+ self .end = False
5
+
6
+ def getindex (a ):
7
+ if "a" <= a <= "z" :
8
+ return ord (a )- ord ("a" )
9
+ elif "A" <= a <= "Z" :
10
+ return ord (a )- ord ("A" )+ 26
11
+ elif "0" <= a <= "9" :
12
+ return int (a ) + 52
13
+ raise KeyError
14
+
15
+ def getchr (i ):
16
+ if 0 <= i <= 25 :
17
+ return chr (i + ord ("a" ))
18
+ elif 26 <= i <= 51 :
19
+ return chr (i + ord ("A" )- 26 )
20
+ elif 52 <= i <= 61 :
21
+ return str (i - 52 )
22
+ raise IndexError
23
+
24
+ def insertTrie (root ,value ):
25
+ if value == "" :
26
+ root .end = True
27
+ else :
28
+ c = value [0 ]
29
+ i = getindex (c )
30
+ if root .children [i ] == None :
31
+ root .children [i ] = TrieNode ()
32
+ insertTrie (root .children [i ],value [1 :])
33
+
34
+ def printTrie (root ,prefix ):
35
+ if root .end :
36
+ print (prefix )
37
+ for i in range (62 ):
38
+ if root .children [i ] != None :
39
+ printTrie (root .children [i ],prefix + getchr (i ))
40
+
41
+
42
+ def searchTrie (root ,value ):
43
+ if value == "" :
44
+ return root .end
45
+ i = getindex (value [0 ])
46
+ if root .children [i ] == None :
47
+ return False
48
+ return searchTrie (root .children [i ], value [1 :])
49
+
50
+
51
+ def solution (input ):
52
+ root = TrieNode ()
53
+ insertTrie (root ,"aa" )
54
+ insertTrie (root ,"ab" )
55
+ insertTrie (root ,"acd" )
56
+ insertTrie (root ,"123Aa" )
57
+ printTrie (root ,"" )
58
+ print (searchTrie (root ,"aaz" ))
You can’t perform that action at this time.
0 commit comments