-2

The dict_key contains the correct spelling and its corresponding value contains the spelling of the candidate

The function should identify the degree of correctness as mentioned below:

CORRECT, if it is an exact match

ALMOST CORRECT, if no more than 2 letters are wrong

WRONG, if more than 2 letters are wrong or if length (correct spelling versus spelling given by contestant) mismatches.

and return a list containing the number of CORRECT answers, number of ALMOST CORRECT answers and number of WRONG

My program assumes that all the words are in uppercase and max word length is 10

Here is my code:

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)

Driver Code:

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

My Output: 0,2,0

Expected Output: 0,0,2

Vishal Borana
  • 17
  • 2
  • 6
  • `i_count` isn't reset inside the loop, so just keeps adding on, so once it reaches 3, it's always just going to add one and increment the incorrect – Andrew Dec 12 '18 at 14:03
  • Thanks, i have edited my code , but still it doesnt work for the following input: word_dict={'WHIZZY': 'MIZZLY', 'PRETTY': 'PRESEN'} – Vishal Borana Dec 12 '18 at 14:32

5 Answers5

1

So I came up with a much simpler solution. I hope I got your question right but it produces the desired output.

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)

Instead of going through every character I compare the Strings as lists. I got the idea fron the following post.

Nethale
  • 117
  • 4
  • Thanks for your answer, but unfortunately it doesn't work for inputs like {Rise:Rises} and other inputs with repeating chars. – Vishal Borana Dec 12 '18 at 14:31
0

This seems to work for your specified dictionaries, though there might be an edge case or two for which it doesn't work properly. If you have any cases this doesn't work for, then the problem is very likely to be with the if/elif/else block in the find_correct function and the way it's evaluating the length of the list.

I took my cue from the accepted answer, to convert the strings to lists, although instead of setting, I used the pop method to remove the required elements so that duplicates would be accounted for.

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))
Andrew
  • 1,072
  • 1
  • 7
  • 15
0
def find_correct(word_dict):
    correct,almost,incorrect=0,0,0
    for key,value in word_dict.items():
        count=0
        if(key==value):
            correct+=1
        elif(len(key)==len(value)):
            for i in range(0,len(key)):
                if(key[i]!=value[i]):
                    count+=1
            if(count<=2):
                almost+=1
            else:
                incorrect+=1
        else:
            incorrect+=1
    list=[correct,almost,incorrect]
    return list 

    word_dict={'WHIZZY': 'MIZZLY', 'PRETTY': 'PRESEN'}
    print(find_correct(word_dict))
0
    def find_correct(word_dict):
    correct=0
    almost_correct=0
    wrong=0
    for key,val in word_dict.items():
        key1=key;val1=val
        if(len(key)!=len(val)):
            wrong+=1
        elif(key==val):
            correct+=1
        else:
            var=0;count=0
            for i in range(len(key1)):
                for j in range(i+1):
                    var=j
                if(key1[i]!=val1[j]):
                    count+=1
            if(count<=2):
                almost_correct+=1
            else:
                wrong+=1
    li=[correct,almost_correct,wrong]
    return li
word_dict={"THEIR": "THEIR","BUSINESS":"BISINESS","WINDOWS":"WINDMILL","WERE":"WEAR","SAMPLE":"SAMPLE"}
print(find_correct(word_dict))
0
def find_correct(word_dict):

    correct_count=0
    almost_correct_count=0
    wrong_count=0
    list1=[]
    for k,v in word_dict.items():
        if len(k)<=10:
            if len(k)==len(v):
                if k==v:
                    correct_count+=1
                else:
                    x=[]
                    y=[]
                    for i in k:
                        x.append(i)
                    for i in v:
                        y.append(i)
                    count=0
                    for i in x:
                        if not(y[x.index(i)]==i):
                            count+=1
                    if count<=2:
                        almost_correct_count+=1
                    else:
                        wrong_count+=1
            else:
                wrong_count+=1 
        else:
                wrong_count+=1          
    list1.append(correct_count)
    list1.append(almost_correct_count)  
    list1.append(wrong_count)      
    return list1       
word_dict={'MOST': 'MICE', 'GET': 'GOT', 'COME': 'COME', 'THREE': 'TRICE'}
print(find_correct(word_dict))
סטנלי גרונן
  • 2,917
  • 23
  • 46
  • 68