不要用太难的代码,希望可以带上标识什么意思,输出的结果应该是图片中的,5 × 5的表格如图中所示
问题描述:
编写一个函数get_diag-se(s, ncols)来返回一个字符串列表,该列表对应于运行' 东南 '的所有对角线,以及一个函数get_diag。ne(s, ncols)表示“东北”对角线。同样,使用图中
的5 × 5网格进行测试以及编写函数find_words(s, ncols, wordlist)来标识wordlist中的哪些单词实际出现在网格中。函数应该只返回网格中wordlist中的单词。通过在上面的5 × 5网格中查找单词["JOT", "DOT", "DIN", "GIN"]来测试你的功能。
答
你题目的解答代码如下:
s = [
["A","F","K","P","U"],
["B","G","L","Q","V"],
["C","H","M","R","W"],
["D","I","N","S","X"],
["E","J","O","T","Y"]
]
def get_diag_se(s, ncols):
li = []
for i in range(ncols*2-1):
st = ""
for j in range(ncols):
m = ncols-1-i+j
if 0<=m<ncols:
st += s[j][m]
li.append(st)
return li
def get_diag_ne(s, ncols):
li = []
for i in range(ncols*2-1):
st = ""
for j in range(ncols):
m = i-j
if 0<=m<ncols:
st += s[j][m]
li.append(st)
return li
def find_words(s, ncols, wordlist):
li = []
for w in wordlist:
for v in s:
st = "".join(v)
if w in st:
li.append(w)
return li
print(get_diag_se(s, 5))
print(get_diag_ne(s, 5))
print(find_words(s, 5, ["JOT", "DOT", "DIN", "GIN"]))
如有帮助,望采纳!谢谢!