我正在尝试根据一些规则将字典键与其值匹配,而不使用其他库

问题描述 投票:-2回答:2

dict_key包含正确的拼写,其对应的值包含候选拼写

该功能应确定正确程度,如下所述:

正确,如果它是完全匹配

ALMOST CORRECT,如果不超过2个字母错误

错误,如果超过2个字母错误或长度(正确的拼写与参赛者给出的拼写错误)不匹配。

并返回一个列表,其中包含CORRECT答案的数量,ALMOST CORRECT答案的数量和WRONG的数量

我的程序假设所有单词都是大写的,最大单词长度是10

这是我的代码:

def find_correct(word_dict):
#start writing your code here
    correct_count=0
    almost_correct_count=0
    incorrect_count=0

    for k,v in word_dict.items():
        if len(k)<=10:
            if len(k)==len(v):
                if k==v:
                    correct_count+=1
                else:
                    for i in k:
                        i_count=0
                        #print(i)
                        for j in v:
                            #print(j)
                            if not i==j:
                               i_count+=1 
                               break
                    if i_count<=2:
                        almost_correct_count+=i_count
                    else:
                        incorrect_count+=i_count

            else:
                incorrect_count+=1
        else:
            incorrect_count+=1
    print(correct_count,almost_correct_count,incorrect_count)

驱动代码:

word_dict={"WhIZZY":"MIZZLY","PRETTY":"PRESEN"}
print(find_correct(word_dict))

我的输出:0,2,0

预期产出:0,0,2

python dictionary
2个回答
0
投票

这似乎适用于您指定的字典,但可能存在一个或两个边缘情况,它无法正常工作。如果你有任何情况这不起作用,那么问题很可能是if/elif/else函数中的find_correct块以及它评估列表长度的方式。

我从接受的答案中获取了我的提示,将字符串转换为列表,虽然不是使用set,但我使用pop方法删除了所需的元素,以便重复使用。

WORD_DICT = {"THEIR":"THEIR",
             "BUSINESS":"BISINESS",
             "WINDOWS":"WINDMILL",
             "WERE":"WEAR",
             "SAMPLE":"SAMPLE"}

second_dict = {'WHIZZY': 'MIZZLY', 'PRETTY': 'PRESEN'}

def find_correct(k, v):

    k, v = list(k), list(v)
    for k_letter in k:
        if k_letter in v:
            idx = v.index(k_letter)
            v.pop(idx)
    if len(v) == 0:
        return "correct"
    elif len(v) == 1:
        return "almost correct"
    else:
        return "incorrect"

def top_level_func(word_dict):

    d = {"correct":0, "almost correct":0, "incorrect":0}
    for k, v in word_dict.items():
        response = find_correct(k, v)
        d[response] += 1

    return d

results = top_level_func(second_dict)
for item in results.items():
    print("{} = {} instances".format(*item))

1
投票

所以我提出了一个更简单的解决方案。我希望我的问题正确,但它会产生所需的输出。

WORD_DICT = {"THEIR":"THEIR",
             "BUSINESS":"BISINESS",
             "WINDOWS":"WINDMILL",
             "WERE":"WEAR",
             "SAMPLE":"SAMPLE"}

def find_correct(word_dict):

    correct, almost_correct, incorrect = 0, 0, 0

    for key, value in WORD_DICT.items():

        diff_list = set(list(key)).symmetric_difference(set(list(value)))  
        diff = len(diff_list)

        if diff == 0:
            correct += 1
        elif diff <= 2:
            almost_correct += 1
        elif diff > 2:
            incorrect += 1


    print(correct, almost_correct, incorrect)


find_correct(WORD_DICT)

我没有通过每个字符,而是将字符串作为列表进行比较。我从下面的post得到了这个想法。

© www.soinside.com 2019 - 2024. All rights reserved.