Right on! It may look clunky but it works with a minimum of code patterns, and does not use any built-ins. This model of code may seldom be used in production but it is infinitely important in developing our thinking. Well done.
Hi Everyone, I would like to share with you the way that I chose to solve it. (I saw a lot of methods to solve it until I finished my own, you rock guys )
def max_key(my_dictionary):
max = 0
for key in my_dictionary.keys():
if my_dictionary[key] > max:
max = my_dictionary[key]
output = key
return output
# Uncomment these function calls to test your function:
print(max_key({1:100, 2:1, 3:4, 4:10}))
#print 1
print(max_key({"a":100, "b":10, "c":1000}))
#"c"
def max_key(my_dictionary):
maxK, maxV = list(my_dictionary.items())[0]
for k in my_dictionary:
if my_dictionary[k] > maxV:
maxV = my_dictionary[k]
maxK = k
return maxK
# for time efficiency, check https://stackoverflow.com/a/14624923
def max_key2(my_dictionary):
return list(my_dictionary.keys())[list(my_dictionary.values()).index(max(my_dictionary.values()))]
def max_key3(my_dictionary):
for k in my_dictionary:
if my_dictionary[k] == max(my_dictionary.values()):
return k
def max_key4(my_dictionary):
return max(my_dictionary, key=my_dictionary.get)
How could we compare the efficiency of each approach against a large dataset?
I saw something called profile.run() mentioned in this post from Stack Overflow, but could not really figure out the docs with my current knowledge/vocab nor could I implement it on my terminalā¦
This is not my area but it never hurts to examine things and see what we spot. The first and last are both linear with a constant of 2 (for the two iterations) so O(2N). The second one looks to have a constant of 4 given there are four iterations, but still linear.
The third one would be the least efficient owing that it is quadratic (nested iteration). It could be cured by caching the max() before the loop. It never changes. Then it would be linear with a constant of 2.
Owing that we could very likely conclude that the source code for max() is highly optimized. The fourth one would be a best bet and itās doubtful we as learners or even seasoned programmers will ever top that for this language. I suspect the source code is written in some flavor or C.
The thing I like about the first example is it is closest to a naive implementation It still uses built-ins, though. list() and dict.items(). The fully naive form will use only linear iteration.
>>> def max_key(d):
k, v = (None, float("-inf"))
for h in d:
if d[h] > v:
k, v = (h, d[h])
return k
>>> max_key({"a":100, "b":10, "c":1000})
'c'
>>>
The only difference is that it uses negative infinity as the minimum instead of a value from the dictionary.
def max_key(my_dictionary):
i = -9999999
for value in my_dictionary.values():
if value > i:
i = value
for key, value in my_dictionary.items():
if value == i:
return key
Really cool to see some of these more concise answers! Iām impressed.
Also - if anyone knows a shorter hand way to get a really low integer than literally typing out -9999999, let me know!
If you check through some of the previous answers youāll find some neat ways of achieving your goal. Perhaps a small integer isnāt what you really need though, youāre just interested in finding the maximum form a set of values, why not start with one of them?
def max_key(my_dictionary):
biggest_val = max(my_dictionary.values())
for key in my_dictionary.keys():
if my_dictionary[key] == biggest_val:
return key
Are you referring to the use of max() ? I did become aware of this through reading some of the solutions, but chose not to implement it. I really want to see the best that can be done with just the methods that have been taught on the course up until this point
I was actually referring to obtaining a decent starting value to test against. A few points above cover the use of float("-inf") for a negative number but others propose starting with a value from the dictionary which I think might be the better choice.
Working with what youāve learnt isnāt a bad shout. Iād hazard a guess the original intention was to solve this with a loop to practice working with loops and dictionaries. Worth knowing max for the future but practising the topics covered should probably be the priority.
largest_value = float('-inf')
largest_key = ''
def max_key(my_dictionary):
for k,v in my_dictionary.items():
if v>largest_value:
largest_value = v
largest_key = k
return largest_key`Preformatted text`
For the above code, I got the following error:
File āscript.pyā, line 6, in max_key
if v>largest_value:
UnboundLocalError: local variable ālargest_valueā referenced before assignment
I have declared the variables prior to the function. So, being global variables, shouldnāt they be accessible within the function as well? or do I have to declare them locally, i.e. within the function? Why does the output say that the variables havenāt been declared?
Globals can be accessed for read, but not for write, hence the UnboundLocalError. Suggest move both of those variables to just inside the function, above the loop.
First off, globals need to be minimized, but those that we cannot shuffle away into a function should be read only for good reason. If one function can change a global then others can too. It can make collaboration a real challenge on a large code base.
Parameters are inputs, and as local variables they are reassignable, though we should have good reason to reassign them. Inputs are given with the expectation they will be useful in producing a return value.
For best practice keep your variables local and return something to the caller, rather than overwrite globals. Let the caller do that.
Segue
Thereās a Games of Chance challenge that warrants comparison. Everyone is so fixated on each game spelling out the outcome, rather than letting each function do what it is supposed to do, return either a positive value, or a negative one. Thatās it. Every function has the exact same return.
money is a global. Let all transactions with this variable also be global in scope. Why piss around with stuff inside individual functions when it has to happen for all of them, anyway? Sort out the scope for this challenge and the rest is childās play.
from random import choice
def coin_toss():
return choice(['Heads', 'Tails'])
def win(func, guess):
return guess.title() == func()