我尝试了哈希表:赎金注意问题,但在某些测试中获得了矛盾的结果



我在功能中应用了if语句,但返回了冲突的结果。

功能是将单词与杂志匹配。在if语句之后,它同时返回"是"one_answers"否"。谁能看我做错了什么?

测试输入是:

magazine=['give', 'me', 'one', 'grand', 'today', 'night']
note=['give', 'one', 'grand', 'today']
import math
import os
import random
import re
import sys
from collections import Counter
# Complete the checkMagazine function below.
def checkMagazine(magazine, note):
    m=Counter(magazine)
    d=Counter(note)
    match=False
    newdict = {k: m[k] for k in d}
    if newdict.items()>=d.items():
        match=True
    if match:
        print("Yes")
    print("No")

没有错误,它只是不匹配答案。答案应该是"是",但是我的功能同时返回"是"one_answers"否"。

您应该将print('No')放在条件下。

def checkMagazine(magazine, note):
    m=Counter(magazine)
    d=Counter(note)
    match=False
    newdict = {k: m[k] for k in d}
    if newdict.items()>=d.items():
        match=True
    if match:
        print("Yes")
    if match==False:
        print("No")

相关内容

最新更新