79333787

Date: 2025-01-06 17:09:44
Score: 1.5
Natty:
Report link

I try to answer the above as follows:

In order to invert a dictionary, we need to do the following:

  1. Identify the set of (the original) dictionary values that shall become keys in the inverse dictionary.
  2. Create groups of keys from the original dictionary partitioned by the values from the previous step. Later we would generate a list of elements (original keys) from each group in order to make it a value in the inverse dictionary.
  3. Generate the new (inverse) dictionary by merging information from the two previous steps.

On condition the LINQ expression in the question does these 3 steps, the LINQ rewrite is correct and fully replaces the "ugly" 20 lines' method I had started with.

There remains one more question:

Is the LINQ compaction worth doing in this case? It might darken the purpose of the method rather than illuminate it.

Reasons:
  • Long answer (-0.5):
  • No code block (0.5):
  • Contains question mark (0.5):
  • Self-answer (0.5):
  • Low reputation (0.5):
Posted by: Pavel Foltyn