Question: I cant do contains part from typing import List def insert ( data , s: str ) - > None: if s = =

I cant do contains part
from typing import List
def insert(data, s: str)-> None:
if s =="":
return
if len(s)==1:
if s in data:
data[s][1]= True
else:
data[s]=[{}, True]
if s[0] in data:
insert(data[s[0]][0], s[1:])
else:
data[s[0]]=[{}, False]
insert(data[s[0]][0], s[1:])
def count_words(data)->int:
"""
Returns the number of words encoded in data. You may assume
data is a valid trie.
>>> data ={}
>>> insert(data, "test")
>>> insert(data, "testing")
>>> insert(data, "doc")
>>> insert(data, "docs")
>>> insert(data, "document")
>>> insert(data, "documenting")
>>> count_words(data)
6
"""
stack = list(data.values())
total_count =0
while stack:
node = stack.pop()
if node[1]:
total_count +=1
stack.extend(node[0].values())
return total_count
def contains(data, s: str)-> bool:
"""
Returns True if and only if s is encoded within data. You may
assume data is a valid trie.
>>> data ={}
>>> insert(data, "tree")
>>> insert(data, "trie")
>>> insert(data, "try")
>>> insert(data, "trying")
>>> contains(data, "try")
True
>>> contains(data, "trying")
True
>>> contains(data, "the")
False
"""

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!