Skip to content

Commit caaf609

Browse files
committed
Add Trie part1
1 parent 3c8f6ea commit caaf609

File tree

3 files changed

+62
-2
lines changed

3 files changed

+62
-2
lines changed

learn/tree/day3/part1.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import heapq
2+
13
def heapify(ls):
24
while True:
35
a = False
@@ -60,7 +62,7 @@ def heappop(ls):
6062

6163
def solution(input):
6264
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))
6567
return str(ls)
6668

learn/tree/day4/part1.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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"))

learn/tree/day4/sample.txt

Whitespace-only changes.

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy