What's the downside to my solution

The challenge reads:
" Write a function named max_key that takes a dictionary named my_dictionary as a parameter. The function should return the key associated with the largest value in the dictionary."

The solution is

def max_key(my_dictionary):
  largest_key = float("-inf")
  largest_value = float("-inf")
  for key, value in my_dictionary.items():
    if value > largest_value:
      largest_value = value
      largest_key = key
  return largest_key

I did not know that you could represent infinity with float(‘inf’) so this is how I solved it:

def max_key(my_dictionary):
  max_value = sorted(my_dictionary.values())[-1]
  return list(my_dictionary.keys())[list(my_dictionary.values()).index(max_value)]

Is there a downside to this solution? Is there a circumstance where it wouldn’t work the way it is supposed to? So far it has worked for every dictionary I have passed it. Thanks in advance for your help.

Hmm if you’re looking for a little criticism I’d say that second option is difficult to read at present (perhaps it could be spread out a little). There’s also a lot of wasted processing/memory on temporaries, each list you create for example (with sorted() or with list()) is going to take a certain amount of processing to create and a certain amount of memory to store.

For a small enough dataset that wouldn’t matter but for scaling it could be very important (the loop example takes up little to no memory, a couple of references to pre-existing values and iterates just the once, O(n)).

The other thing that springs to mind is that you might want to sort on something other than numeric types sometimes (depends on the dictionary contents) but they both might struggle a little with that.

This is exactly the type of feedback I was looking for! Can you point me toward more resources that explain this type of thought process. Code academy doesn’t really teach this sort of thing. At least I haven’t arrived there yet.

1 Like

Ah grand so. You’d probably find bits of what you’re looking for in the software or comp sci courses (there’s often some discussion with most “pass the technical interview” courses too). You’re looking for things covering algorithms, Big O / asymptotic notation and probably a bit of data structures too.

For example:
https://www.codecademy.com/paths/computer-science/tracks/cspath-cs-102/modules/asymptotic-notation/articles/cspath-why-asymptotic-notation

There’s plenty of resources outside cc for the same but just be aware it’s a very very broad topic. Well studied though so there are some excellent options for getting stuck in. Takes a while but it’s something you’ll likely never fully forget and therefore worth the time and effort.

This is great. Thank you so much for all of your help.

1 Like

As a reference text, it’s hard to go wrong with CLRS. Introduction to Algorithms - Wikipedia

And there’s a free MIT course on youtube that teaches algorithms at a pretty good level (syllabus and exercises are included in pdf’s!): Introduction to Algorithms | Electrical Engineering and Computer Science | MIT OpenCourseWare

1 Like

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.