DeadMarshall / codecademy

codecademy courses
0 stars 0 forks source link

Wrong implementation #5

Closed sen1 closed 6 years ago

sen1 commented 6 years ago

https://github.com/DeadMarshall/codecademy/blob/6f67a46034f5b5584c9f0e26fd028b57f73e80b6/bstinsonMurder%2BMystery.py#L228-L244

mutual_appearances and appearances needed to be sum. So len() is the wrong operator here. Also you didnt count those which exists in table 2 but not in table 1. Here is how I would do it:

def frequency_comparison(table1, table2):
    appearances = 0
    mutual_appearances = 0
    for key in table1:
        if key in table2:
            value1 = table1.get(key)
            value2 = table2.get(key)
            if value2:
                if value1 > value2:
                    mutual_appearances += value2
                    appearances += value1
                else:
                    mutual_appearances += value1
                    appearances += value2
            else:
                appearances += value1
        else:
            appearances = table1.get(key)

    keys_not_in_table1 = [key for key in table2 if key not in table1]
    for key in keys_not_in_table1:
        appearances += table2.get(key)

    return mutual_appearances / appearances
DeadMarshall commented 6 years ago

fixed thanks a lot