Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8881241
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T20:18:18+00:00 2026-06-14T20:18:18+00:00

I was trying to write the code for an exercise, and while it works

  • 0

I was trying to write the code for an exercise, and while it works as it should, it looks terrifying and I was wondering if anyone could help me remove anything that’s not necessary, and potentially just combine some of the functions?

Here’s an example of the use of the function:

choices([['YES', 'NO', 'YES', 'YES'], ['NO', 'NO', 'YES', 'NO'], ['YES', 'YES', 'YES', 'YES']])

Each list within the list has four yes/no choices (INDICES below lists four options as well, e.g. green, red, blue, yellow; but it doesn’t have to be four). The number of lists within the list is how many people cast their votes.

i = 0
num = 0
total_num = 0
num_choices = len(INDICES)
choices_index = 0
choices_votes = []
choices_votes_num = []
index = 0
total_choices = []
winning_choice = ''
winning_index = 0
while i < len(parameter):
    while num < num_choices:
        for item in parameter:
            choices_votes.append(item[num])
        num += 1
    i += 1
while total_num < len(choices_votes):
    if choices_votes[total_num] == 'YES':
        choices_votes_num.append(1)
        total_num += 1
    elif choices_votes[total_num] == 'NO':
        choices_votes_num.append(0)
        total_num += 1
while choices_index < len(choices_votes_num):
    count = int(len(choices_votes_num) / num_choices)
    total = 0
    total = sum(choices_votes_num[choices_index:(choices_index + count)])
    total_choices.append(total)
    choices_index = choices_index + count
for score in total_choices:
    winning_index = max(total_choices)
    winning_choice = INDEX_TO_NAME[total_choices.index(winning_index)]
return winning_choice, total_choices

INDEX_TO_NAME is just a dictionary set up to connect indices to choices (the colours).

Basically the code is supposed to count each yes as 1 point and each no as zero points, add up the total points for each available choice, and then return the totals and the winner.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T20:18:19+00:00Added an answer on June 14, 2026 at 8:18 pm

    Lets start with:

    c = [['YES', 'NO', 'YES', 'YES'],
         ['NO', 'NO', 'YES', 'NO'],
         ['YES', 'YES', 'YES', 'YES']]
    
    INDICES = ['red', 'green', 'blue', 'yellow']
    

    Since you were using INDICES to source your poll anyways, we can assume that the answers will always line up with the number of INDICES.

    We can make use of zip to reorganize this data:

    zip(*c)
    #[('YES', 'NO', 'YES'),
    # ('NO', 'NO', 'YES'),
    # ('YES', 'YES', 'YES'),
    # ('YES', 'NO', 'YES')]
    

    This expands the answers to arguments and recombines them to be grouped by the actual indices. So the first index is ‘red’, second is ‘green’, etc

    Now we can zip again to combine them with the indices:

    results = zip(INDICES, zip(*c))
    #[('red', ('YES', 'NO', 'YES')),
    # ('green', ('NO', 'NO', 'YES')),
    # ('blue', ('YES', 'YES', 'YES')),
    # ('yellow', ('YES', 'NO', 'YES'))]
    

    And we can loop over results, simply counting the occurrences of ‘YES’:

    totals = [(ind, answers.count('YES')) for ind,answers in results]
    #[('red', 2), ('green', 1), ('blue', 3), ('yellow', 2)]
    

    So here we have the totals. We could pass this to a call to max to have it tell us which one was the winner:

    max(totals, key=lambda x: x[1])
    #('blue', 3)
    

    max will by default look at the first index, so we can pass it a key function instructing it to pull the index 1 instead. It shows us blue was the winner.

    To be more efficient, we can actually pass a generator to max:

    totals = ((ind, answers.count('YES')) for ind,answers in results)
    #<generator object <genexpr> at 0x102581fa0>
    max(totals, key=lambda x: x[1])
    #('blue', 3)
    

    The final single statement could be written like this:

    max(((ind, answers.count('YES')) for (ind,answers) in zip(INDICES, zip(*c))), 
        key=lambda x: x[1])
    

    I know this answer introduces some list comprehension and lambda (and generator), but you asked for a way to clean it up and this is an example of the tools that are available to you. Hopefully this gives you the help you needed!

    Note: This answer doesn’t take into account a tie. But I am sure you can work from here to integrate these examples into your actual code

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I m trying write code that after reset set up rrpmax as 3000. It
I'm trying to write code in my controller that when run, will create a
I am trying to write code for getting my cords on the mobile. I
I'm trying to write code that will load an image from a resource, and
I've been trying to write code that loads a .png file, attaches hotspot information,
Alright, here I am again trying to write code from scratch and I can't
Trying to write a code at the moment that basically tests to see if
I'm trying to write a code to actually sort my array in an ascending
I'm trying to write a code that adds a class to a div for
I am trying to write some code that will generate accurate .proto files from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.