Invert Keys and Values

Published by Helen Yu in

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

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" }

Notes

Keep the ordering in the hash the same.

Watch a quick demo on how Edabit works.