Dictionary Code Golf

1

New to code golf and enjoying it. I have a problem that I cannot figure out and am curious if others can solve it.

Start with two dictionaries, y and z:

y = {'a': 1, 'c': 3, 'e': 5, 'g': 7}
z = {'b': 2, 'd': 4, 'f': 6, 'h': 8}

y and z both map length-1 strings to numbers, where each number is between 1 and 26 (inclusive) and represents an English letter: a = 1, b = 2, c = 3, d = 4 , ... , z = 26.

The goal is as follows:

  1. Double each number (i.e. value) in y, and replace the key with the letter corresponding to the new number.

  2. Do the same for z, but triple each number instead.

  3. Populate a dictionary x with the keys and values from both y and z in descending numerical order, for instance:

x = {'x': 24, 'n':18, 'm': 14, 'l':12, 'j': 10, 'f': 6, 'b':2}

Good luck!

Example:

Starting state:

y = {'a': 1, 'c': 3, 'e': 5, 'g': 7}
z = {'b': 2, 'd': 4, 'f': 6, 'h': 8}

After y operations:

y = {'b':2, 'f': 6, 'j': 10, 'm': 14}

After z operations:

z = {'f': 6, 'l': 12, 'n': 18, 'x': 24}

Julian Silvestri

Posted 2018-08-24T13:00:47.267

Reputation: 129

Question was closed 2018-08-29T00:41:31.010

Comments are not for extended discussion; this conversation has been moved to chat.

– Mego – 2018-08-28T00:51:33.917

Answers

10

Python, 174 155 113 86 70 68 66 bytes

It's actually my first time here and here's my attempt. Let me know if I am doing anything wrong.

lambda y,z:{chr(v*(3-v%2)+96):v*(3-v%2)for v in{**y,**z}.values()}

EDIT: After OP's edit and @Rod 's comment I noticed I have some mistakes. I also took out the sorting attempts because it simply can't work with Python dictionaries.

EDIT2: -16bytes thanks to @JonathanAllan

Try it online!

DimChtz

Posted 2018-08-24T13:00:47.267

Reputation: 916

Just so you know, at the moment this is a TIPS question, not a challenge. Please add an explanation to your answer. – mbomb007 – 2018-08-24T19:47:59.080