Skip to content
← Back to Community
Among Us Voting System: HALP AGAIN
Profile icon
Smart0ne

Hi repl.it

Let's say I have the following code:

votes = {"red":2,"yellow":1,"green":0,"cyan":2,"blue":1,"magenta":0}

It's a dictionary named "votes". Now I want a function that can return the key names of the values that have the maximum value. If this is unclear, the function should return

ejected = ["red","cyan"]

since "red" and "cyan" have the biggest values in the dictionary. Can someone help me implement this?

Thanks in advance!

Voters
Profile icon
fuzzyastrocat
Profile icon
DynamicSquid
Profile icon
heyitsmarcus
Profile icon
Smart0ne
Comments
hotnewtop
Profile icon
Smart0ne

Hello,

(using Python)

I have one more problem. Let's define a few more variables.

colours = ["red","yellow","green"]

I currently have code that turns the "colour" list (which has the people that are not dead) into the "vote_number" dictionary. So after the code is executed, we have

vote_number = {"red":0,"yellow":0,"green":0}

Now, I let the user choose who they vote for. I first display all the possible voting choices

Who do you vote for? 1 - red 2 - yellow 3 - green

And if they type in the number corresponding to a colour, I need to add one to the value in the dictionary. For example, if the user types in "3", I would have to execute

vote_number["green"] += 1

But ... I'm not sure how to do this. Currently, my code is very static, so I get errors like "Key Error".

How would I go implementing this?

Profile icon
heyitsmarcus

@Smart0ne Just make a quick number to string dictionary:

vote_number_lookup = { "1": "red", "2": "yellow", "3": "green" }

And then you can enter the input directly to get the color

# let's say they pick 1 color = vote_number_lookup[userInput] # increase red's votes by 1 vote_number[color] += 1 ``
Profile icon
heyitsmarcus

@Smart0ne if you found my example suited you, please mark my answer as the accepted answer so we can resolve this question. =]

Profile icon
Smart0ne

@heyitsmarcus Hm ... after trying your code, I get a "TypeError: 'dict' object not callable".

Also, my code is

print("\nWho do you vote for?") for i in range(len(colours)): print("{} - {}".format(i+1,colours[i])) while True: voting = input("---> ") if voting == "1" or voting == "2" or voting == "3" or voting == "4" or voting == "5" or voting == "6" : vote_number[vote_number_lookup(voting)] += 1 else: print("Please enter the NUMBER corresponding to the person you want to vote off.")
Profile icon
fuzzyastrocat

@Smart0ne vote_number[vote_number_lookup(voting)] should be vote_number[vote_number_lookup[voting]]

Profile icon
Smart0ne

@fuzzyastrocat Oops. I got myself confused. :P Thanks!

Profile icon
fuzzyastrocat

@Smart0ne No problem!

I'd also like to note that while @heyitsmarcus 's answer works it's a bit inefficient since it iterates through the dictionary twice. A more efficient version would be like this:

def findImposters(votes): ejected = [] max = 0 for color, voteCount in votes.items(): if voteCount == max: ejected.append(color) elif voteCount > max: ejected = [color] max = voteCount return ejected
Profile icon
fuzzyastrocat

@Smart0ne Actually, an even more efficient version would be as follows, since (as I understand it) Python slices don't copy values, they just make a reference:

def findImposters(votes): ejected = [] i = 0 max = 0 for color, voteCount in votes.items(): if voteCount == max: ejected.append(color) elif voteCount > max: ejected.append(color) max = voteCount i = len(ejected) - 1 return ejected[i:]
Profile icon
Smart0ne

@fuzzyastrocat YES IT WORKED! I'm not sure who to mark as answer though ...

Profile icon
fuzzyastrocat

@Smart0ne Lol I don't care, all I care is that it worked

Profile icon
Coder100

oh come on it's resolved
ok whatever happy coding @fuzzyastrocat

Profile icon
heyitsmarcus

As long as everything works that is great, but I originally looped through twice before I refactored.

votes = {"red":2,"yellow":1,"green":0,"cyan":2,"blue":1,"magenta":0} def findImpostors(votes): # this will created the ejected list ejected = [] # get the maximum value in the list of values of the dictionary maximum = max(votes.values()) # this will take care of any repeating numbers (i.e. the two with 2 votes) for color, voteCount in votes.items(): # if their vote count matches the max if voteCount == maximum: # they're getting ejected, fool! ejected.append(color) return ejected ejected = findImpostors(votes) print(ejected)
Profile icon
fuzzyastrocat

@heyitsmarcus That's looping through twice: max iterates through the entire list, then you iterate through with a for loop.

Profile icon
SUHASTADIPARTH1

You might want to do this:

votes = { "red": 3, "yellow": 1, "green": 0, "cyan": 3, "blue": 1, "magenta": 0 } def getKey(val): for k, v, in votes.items(): if val == v: return k new_list = [] for key, value in votes.items(): new_list.append(value) ejected = [] max_value = max(new_list) ejected.append(getKey(max_value)) print(ejected)

it will only give you the color with the largest votes
it will only return one colors, not two

Profile icon
Smart0ne

@SUHASTADIPARTH1 Thanks! :) My goal is to return all the ones with the highest value, but thanks for the code!

Profile icon
heyitsmarcus

@SUHASTADIPARTH1 The objective is to return both colors, not just one.

Profile icon
SUHASTADIPARTH1

@heyitsmarcus np. Mark my answer?

Profile icon
adsarebbbad

max(listName)

Profile icon
heyitsmarcus

@adsarebbbad votes is a dictionary so you have to do a little bit extra and grab the max from the values in the dict object:

maximum = max(votes.values())
Profile icon
RYANTADIPARTHI

USe the code below.

votes = {"red":11,"yellow":1,"green":0,"cyan":2,"blue":1,"magenta":0} maximum = max(votes.values()) print(maximum)

That should work

Profile icon
heyitsmarcus

@RYANTADIPARTHI max does not work in this instance. If you use max(dict, key = dict.get) you can get the first key it encounters that is the max. However, this doesn't work for multiple keys.

Just one way to get the max is to use the max function on the values:

maximum = max(votes.values())
Profile icon
heyitsmarcus

It looks like you're using Python, so I wrote up a script, rife with comments for you to check through how I did it.

Let me know if you have any questions!

https://repl.it/@heyitsmarcus/EjectTheImpostors

Profile icon
Smart0ne

@heyitsmarcus Thanks! I have one more problem, wait a sec.

Profile icon
heyitsmarcus

@Smart0ne no problem!