Home > database >  what's the errors for my "check if value strings equal dictionary exercise"?
what's the errors for my "check if value strings equal dictionary exercise"?

Time:11-15

Here's the question of my exercise:

Write a function called is_1_to_1 that accepts a dictionary whose keys and values are strings as its parameter and returns True if no two keys map to the same value.

and I got this:

def is_1_to_1(dic):
for value_1 in dic.values():
    for value_2 in dic.values():
        check_list = []
        if value_2 == value_1 :
            check_list.append(value_2)
        else:
            pass

if check_list != dic:
    return (f"Dictionary: {dict.items(dic)} does not have unique entries.")

else:
    return (f"DIctionary: {dict.items(dic)} does have unique entries.")

dic_1 = {"Marty": "206-9024", "Hawking": "123-4567",
"Smith": "949-0504", "Newton": "123-4567"}

dic_2 = {"Marty": "206-9024", "Hawking": "555-1234",
"Smith": "949-0504", "Newton": "123-4567"}

print(is_1_to_1(dic_1))

print(is_1_to_1(dic_2))

it return:

Dictionary: dict_items([('Marty', '206-9024'), ('Hawking', '123-4567'), ('Smith', '949-0504'), ('Newton', '123-4567')]) does not have unique entries.
Dictionary: dict_items([('Marty', '206-9024'), ('Hawking', '555-1234'), ('Smith', '949-0504'), ('Newton', '123-4567')]) does not have unique entries.

CodePudding user response:

You have few mistakes.

  1. comparing check_list != dic is useless because it will always gives True. You should rather check if check_list is empty if not check_list:

  2. you create check_list inside for-loops so it replace it with new empty list and you may get empty list if last elements are different.

  3. you compare the same values - like dict["Marry"] == dict["Marry"] - so you always get value in check_list. You should first check key1 != key2

  4. in question you have ... and return True but you always return strings.


Because not check_list gives True or False so it can be reduced to return (not check_list)`

def is_1_to_1(dic):
    check_list = []
    
    for key1, val1 in dic.items():
        for key2, val2 in dic.items():
            if key1 != key2 and val1 == val2 :
                check_list.append(val2)

    #if not check_list:  # check if empty
    #    return True
    #else:
    #    return False

    # shorter
    return (not check_list)

# --- main ---

dic_1 = {
    "Marty": "206-9024",
    "Hawking": "123-4567",
    "Smith": "949-0504",
    "Newton": "123-4567",
}

dic_2 = {
    "Marty": "206-9024",
    "Hawking": "555-1234",
    "Smith": "949-0504",
    "Newton": "123-4567",
}

print(is_1_to_1(dic_1))
print(is_1_to_1(dic_2))

You could also reduce it to

def is_1_to_1(dic):
    for key1, val1 in dic.items():
        for key2, val2 in dic.items():
            if key1 != key2 and val1 == val2 :
                return False  # exit on first matching element

    # after checking all elements
    return True

CodePudding user response:

There are usually many different ways to solve a problem. A quick solution for this could be as simple as:

def is_1_to_1(dic):
    values = dic.values()
    return len(values) == len(set(values))

A Python set only consists of unique values. So if the lengths differ, it means the original set of dict values includes duplicates.

As a side note, your code cannot succeed because of the comparison:

if check_list != dic:

As you have coded it, check_list is an array, and dic is a dict. So they will never truly match, and this comparison will always be a True. Plus, with the logic you use, cycling through the list of dict values twice, you will end up with check_list simply being the same as dic.values().

  • Related