Skip to content

Instantly share code, notes, and snippets.

@ZechCodes
Created January 1, 2021 02:05
Show Gist options
  • Save ZechCodes/d0df4ea120b841fccf2137c061f2a856 to your computer and use it in GitHub Desktop.
Save ZechCodes/d0df4ea120b841fccf2137c061f2a856 to your computer and use it in GitHub Desktop.
Challenge 156 - Invert Keys & Values

Challenge 156 - Invert Keys & Values

Write a function that inverts the keys and values of a dictionary.

Examples

invert({ "z": "q", "w": "f" })
➞ { "q": "z", "f": "w" }

invert({ "a": 1, "b": 2, "c": 3 })
➞ { 1: "a", 2: "b", 3: "c" }

invert({ "zebra": "koala", "horse": "camel" })
➞ { "koala": "zebra", "camel": "horse" }
from __future__ import annotations
from typing import Union
import unittest
def invert(dictionary: dict[str, Union[str, int]]) -> dict[Union[str, int], str]:
return {} # Put your code here!!!
class Test(unittest.TestCase):
def test_1(self):
self.assertEqual(invert({"a": 1, "b": 2, "c": 3}), {1: "a", 2: "b", 3: "c"})
def test_2(self):
self.assertEqual(invert({"z": "q", "w": "f"}), {"q": "z", "f": "w"})
def test_3(self):
self.assertEqual(invert({"zebra": "koala", "horse": "camel"}), {"koala": "zebra", "camel": "horse"})
if __name__ == "__main__":
unittest.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment