假设我有一个元组列表,如下所示:
list_of_tuples = [('A', 'R'), ('B', 'R'), ('C', 'G'), ('D', 'G'), ('E', 'B'), ('D', 'B'), ('R', 'B'), ('F', 'R'), ('V', 'R'), ('A', 'G')]
每个元组中的第二个值总是R
、B
或G
。我想创建一个函数validate
,它检查是否可以使用每个元组第一个位置的字母构造某个单词,但前提是该元组的节位置的字母没有重复
例如,可以构造单词:
由于每个元组中的第二个值对应于不连续重复任何字母的RGB
,因此ACE
具有(A, R)
、(C, G)
和(E, B)
ACED
与(A, R), (C, G), (E, B), and ('D', 'B')
不可能,因为这将对应于RGBB
,其中有一个连续的B
请注意,有时同一个字母的第二个位置可能有不同的字母,例如:
('A', 'R') and ('A', 'G')
。如果选择第一个元组而不是第二个元组,则只能拼写ACE
,否则G
会重复
还要注意,像GBRBG
这样的组合是可能的,即使第二个位置字母“重复”它们不会连续重复
因此,我想要一个函数,可以通过以下方式验证单词:
def validate(submitted_word, list_of_tuples)
一种可能性是构造此集合可能的序列的每个可能组合,以及第二个序列中的字母将产生的相应序列,过滤掉有效单词,然后过滤掉具有连续字母重复的单词,但我担心,考虑到元组列表可能会变得如此之大,这将变得效率低下
这是一个图遍历问题。可用节点是(字母、颜色)的元组;您的边是给定单词中的字母过渡
对于每个输入,“simply”构造该单词的图形。有了A,你就有了
现在,您只需应用图形遍历函数(任何自尊心图形包都有一个)来解决拼写问题
有关独立的解决方案和测试,请参见下文:
控制台输出:
我们可以使用回溯,其中状态是每个字母使用的R、G、B的计数,以及在构造单词时选择的先前“RGB”
Python代码(未记忆):
相关问题 更多 >
编程相关推荐