Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Switch Keys to Values by dig
def switch_dict(data: dict[str, str]) -> dict[str, str]:
my_dic={}
for valorss in data.values():
conjunt=set()
for clauss in data.keys():
if data[clauss]==valorss:
conjunt.add(clauss)
my_dic[valorss]=conjunt
return my_dic
print("Example:")
print(switch_dict({"rouses": "red", "car": "red", "sky": "blue"}))
# These "asserts" are used for self-checking
assert switch_dict({"rouses": "red", "car": "red", "sky": "blue"}) == {
"red": {"car", "rouses"},
"blue": {"sky"},
}
assert switch_dict({"1": "one", "2": "two", "3": "one", "4": "two"}) == {
"one": {"3", "1"},
"two": {"4", "2"},
}
assert switch_dict({"a": "b", "b": "c", "c": "a"}) == {
"b": {"a"},
"c": {"b"},
"a": {"c"},
}
print("The mission is done! Click 'Check Solution' to earn rewards!")
Jan. 13, 2023