Among Us Voting System: HALP AGAIN
Smart0ne (725)

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!

You are viewing a single comment. View All
Smart0ne (725)

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?

heyitsmarcus (342)

@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
``
heyitsmarcus (342)

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

Smart0ne (725)

@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.")
fuzzyastrocat (1513)

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

Smart0ne (725)

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

fuzzyastrocat (1513)

@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
fuzzyastrocat (1513)

@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:]
Smart0ne (725)

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

fuzzyastrocat (1513)

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

Coder100 (17035)

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

heyitsmarcus (342)

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)
fuzzyastrocat (1513)

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