因此,我正在尝试编写代码来执行以下操作:
r""" (list of str, poetry pattern, pronunciation dictionary) -> list of str
Precondition: len(poem_lines) == len(pattern[0])
Return a list of lines from poem_lines that do not have the right number of
syllables for the poetry pattern according to the pronunciation dictionary.
If all lines have the right number of syllables, return the empty list.
>>> poem_lines = ['The first line leads off,', 'With a gap before the next.', 'Then the poem ends.']
>>> pattern = ([5, 5, 4], ['*', '*', '*'])
>>> word_to_phonemes = {'NEXT': ['N', 'EH1', 'K', 'S', 'T'],
... 'GAP': ['G', 'AE1', 'P'],
... 'BEFORE': ['B', 'IH0', 'F', 'AO1', 'R'],
... 'LEADS': ['L', 'IY1', 'D', 'Z'],
... 'WITH': ['W', 'IH1', 'DH'],
... 'LINE': ['L', 'AY1', 'N'],
... 'THEN': ['DH', 'EH1', 'N'],
... 'THE': ['DH', 'AH0'],
... 'A': ['AH0'],
... 'FIRST': ['F', 'ER1', 'S', 'T'],
... 'ENDS': ['EH1', 'N', 'D', 'Z'],
... 'POEM': ['P', 'OW1', 'AH0', 'M'],
... 'OFF': ['AO1', 'F']}
>>> check_syllables(poem_lines, pattern, word_to_phonemes)
['With a gap before the next.', 'Then the poem ends.']
>>> poem_lines = ['The first line leads off,']
>>> check_syllables(poem_lines, ([0], ['*']), word_to_phonemes)
[]
"""
我不明白它到底是怎么工作的,因为列表中的每个字符串的音节数都比给定的要多。例:第一个音节有7个音节,但模式是5个,第二个音节有8个,但5个音节又给出了,依此类推。在
我不清楚从什么开始,我的代码应该做什么,到目前为止我有这样的:
^{pr2}$
问题是如何分解的:
(1)所需功能的签名:
(2)关于投入结构的保证:
^{pr2}$(3)returm值说明:
(4)示例输入-第一个参数[list of str]:
(5)样本输入-第二个参数[诗歌模式]:
(6)示例输入-第三个参数[发音字典]:
(7)第一个样本输出-返回值[list of str]:
(8)第二个样本输出-返回值[list of str]:
第一个示例输出(7)显示输入行(5)的预期返回值,该输入行与模式(6)不匹配。也就是说,三个输入行具有模式
[5, 7, 5]
,它与最后两个元素中的模式[5, 5, 4]
不匹配,因此相应的行由函数返回。在第二个示例输入(8)显示当输入行(5)执行匹配模式(6)时的输出结果,即返回一个空列表。在
PS:
看起来(8)中有一个错误。示例代码应该是:
相关问题 更多 >
编程相关推荐