问题描述
我需要制作一个程序,使用尝试来翻译/插入/删除莫尔斯电码,但是我在搜索(公共汽车)中得到了一部分,当我只输入莫尔斯电码的一部分时,它返回以该代码开头的字母点或破折号,我不想要。对于'A',例如用户需要输入'.-'而不仅仅是'.'。我也不知道如何删除用户想要的元素。帮帮我
ps:有些词是葡萄牙语
我的代码:
class TrieNode:
"""A node in the trie structure"""
def __init__(self,char):
# the character stored in this node
self.char = char
# whether this can be the end of a word
self.is_end = False
# a counter indicating how many times a word is inserted
# (if this node's is_end is True)
self.counter = 0
# a dictionary of child nodes
# keys are characters,values are nodes - keys are point and trace,values are caracteres
self.children = {}
class Trie(object):
"""The trie object"""
def __init__(self):
"""
The trie has at least the root node.
The root node does not store any character
"""
self.root = TrieNode("")
def insert(self,word):
"""Insert a word into the trie"""
node = self.root
cont = 0
# Loop through each character in the word
# Check if there is no child containing the character,create a new child for the current node
for char in word:
if char in node.children:
node = node.children[char]
else:
# If a character is not found,# create a new node in the trie
new_node = TrieNode(char)
node.children[char] = new_node
node = new_node
cont +=1
if cont == 0:
return 'Codigo ja utilizado'
# Mark the end of a word
node.is_end = True
return 'Caractere inserido com sucesso'
# Increment the counter to indicate that we see this word once more
def dfs(self,node,prefix):
"""Depth-first traversal of the trie
Args:
- node: the node to start with
- prefix: the current prefix,for tracing a
word while traversing the trie
"""
if node.is_end:
self.output.append(node.char)
for child in node.children.values():
self.dfs(child,prefix + node.char)
def query(self,x):
"""Given an input (a prefix),retrieve all words stored in
the trie with that prefix,sort the words by the number of
times they have been inserted
"""
# Use a variable within the class to keep all possible outputs
# As there can be more than one word with such prefix
self.output = []
node = self.root
# Check if the prefix is in the trie
for char in x:
if char in node.children:
node = node.children[char]
else:
return False
# Traverse the trie to get all candidates
self.dfs(node,x[:-1])
# Sort the results in reverse order and return
return sorted(self.output)
def remove(self,node):
print('idk')
tree = Trie()
invalido = False
while True:
escolha = input()
escolha_separar = escolha.split()
if escolha_separar[0] == 'INSERIR:':
print(tree.insert(escolha_separar[2] + escolha_separar[1]))
if escolha_separar[0] == "BUSCAR:":
try:
resultado = tree.query(escolha_separar[1])
print(resultado[-1])
except:
print('Codigo Invalido')
if escolha_separar[0] == "REMOVER:":
tree.query(escolha_separar[1])
if escolha_separar[0] == 'DECODIFICAR:':
r = ''
tamanho = len(escolha_separar) - 1
for codigo in range(tamanho):
resultado1 = tree.query(escolha_separar[codigo+1])
if resultado1 == False:
invalido = True
break
else:
r += resultado1[-1]
if not invalido:
print(r)
else:
print(f'Codigo Invalido: {escolha_separar[codigo+1]}')
if escolha_separar[0] == 'FIM':
break
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)