Question

1.4 Get All Candidates (2 points] Complete and test the function get_all_candidates: • Input: a list . This list can be a lis

0 0
Add a comment Improve this question Transcribed image text
Answer #1
Thanks for the questions, I was not able to understand Question 1.5 as there are some back references with the question which are not included in the post. Need to go through it. 

I have coded the rest of the functions all below with screenshots. Please share the complete details for question 1.5 and will help you out.

thanks.

==========================================================================

# Question 1.4

def get_all_candidates(input):

    candidates=[]
    for item in input:
        if isinstance(item,str):
            candidates.append(item)
        elif isinstance(item,list):
            candidates.extend(item)
        elif isinstance(item,dict):
            candidates.extend(list(item.keys()))

    unique_candidates=set(candidates)
    return list(unique_candidates)

input=[{'GREEN':3,'NDP':5},{'NDP':2,'LIBERAL':4},['CPC','NDP'],'BLOC']

candidates=get_all_candidates(input)
print(candidates)

==============================================================

def get all_candidates (input) 1 candidates= [] for item in input: if isinstance (item, str) 5 candidates.append (item) elif

# Question 1.6

import random
def get_winner(voters):
    winners = []
    winner_votes = 0
    for voter, votes in voters.items():
        if winners is [] or winner_votes <= votes:
            winners.append(voter)
            winner_votes = votes
    return random.choice(winners)


voters = {'NDP': 2, 'GREEN': 1, 'LIBERAL': 0, 'BLOC': 0}

winner = get_winner(voters)
print(winner)
import random def get winner (voters) 2 winners = 3 winner_votes - 0 for voter, votes in voters.items () 5 if winners is [or

# Question 1.7

import random
def last_place(voters):
    last_voters = []
    last_votes = 0
    for voter, votes in voters.items():
        if last_voters is [] or last_votes >= votes:
            last_voters.append(voter)
            last_votes = votes
    return random.choice(last_voters)


voters = {'NDP': 2, 'GREEN': 1, 'LIBERAL': 0, 'BLOC': 0}

last_vote = last_place(voters)
print(last_vote)

import random 1 def last place (voters) 2 last_voters last votes = 0 for voter, votes in voters.items ) 4 5 if last_voters is

Add a comment
Know the answer?
Add Answer to:
1.4 Get All Candidates (2 points] Complete and test the function get_all_candidates: • Input: a list...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • This is a python task. 2.3 Count Rated Ballots (5 points Complete and test the function...

    This is a python task. 2.3 Count Rated Ballots (5 points Complete and test the function count_rated: • Input: a list of rated ballots • Here, on cach ballot, the voter gave cach candidate a score out of 5. Sum up the scores for cach candidate. • Return: a dictionary of how many points cach candidate got Example: >>> count_rated('LIBERAL': 5, 'NDP':2}, {'NDP':4, GREEN':5}]) {'LIBERAL': 5, 'NDP': 6, 'GREEN': 5} Electing a winner this way is called Score voting or...

  • Need some help with this question! Each dictionary is essentially one votes so first example total...

    Need some help with this question! Each dictionary is essentially one votes so first example total votes is 3. Please add comments to explain. Language is python Votes Needed to Win [5 points] 3.1 Many voting systems use the Droop Quota to determine if a candidate has enough votes to win: total votes RoundDown 1 winners 1 So far in this assignment we have been dealing with voting systems where there is only one winner in each riding. (This will...

  • (10 points) Define a function rem_duplicate: ''a list -> ''a list which takes in input a list and removes all the duplicates. Test your code with sample input and report result.

    (10 points) Define a function rem_duplicate: ''a list -> ''a list which takes in input a list and removes all the duplicates. Test your code with sample input and report result.

  • Questions 1. How to create a comment in python? 2. The way to obtain user input...

    Questions 1. How to create a comment in python? 2. The way to obtain user input from command line 3. List standard mathematical operators in python and explain them 4. List comparison operators in python 5. Explain while loop. Give example 6. Explain for loop. Give example 7. How to create infinite loop? And how to stop it? 8. Explain a built-in function ‘range’ for ‘for’ loop 9. Explain break statement 10. Explain continue statement 11. Explain pass statement 12....

  • Question 1a - Increasing Numbers in List - First Occurence(3 points) Write a function numIncreasing1(L) that...

    Question 1a - Increasing Numbers in List - First Occurence(3 points) Write a function numIncreasing1(L) that takes as input a list of numbers and returns a list of the first sequence within that is increasing order, and has a length of at least 2. If no increasing sequential numbers are found, (ie. the input list is in descending order) then naturally a list of just the first value is returned. Increasing sequence means for a number to be valid it...

  • Create a function named build_letter_distribution that has the same signature as build_dictionary. This function returns a...

    Create a function named build_letter_distribution that has the same signature as build_dictionary. This function returns a dictionary; however, each key will be a letter and the value will be the count that represents how many times that letter was found. Essentially you will have a letter distribution over a sentence (which is a list of words). The letters should be case insensitive. See the lesson on "Working with Lists" on how to get the individual characters from a string Normalize...

  • I need to complete the code by implementing the min function and the alpha betta pruning...

    I need to complete the code by implementing the min function and the alpha betta pruning in order to complete the tic tac toe game using pything. code: # -*- coding: utf-8 -*- """ Created on: @author: """ import random from collections import namedtuple GameState = namedtuple('GameState', 'to_move, utility, board, moves') infinity = float('inf') game_result = { 1:"Player 1 Wins", -1:"Player 2 Wins", 0:"It is a Tie" } class Game: """To create a game, subclass this class and implement actions,...

  • Complete function long_list_printer.print_list(). When it's finished, it should be able to print this list, a =...

    Complete function long_list_printer.print_list(). When it's finished, it should be able to print this list, a = [ [93, 80, 99, 72, 86, 84, 85, 41, 69, 31], [15, 37, 58, 59, 98, 40, 63, 84, 87, 15], [48, 50, 43, 68, 69, 43, 46, 83, 11, 50], [52, 49, 87, 77, 39, 21, 84, 13, 27, 82], [64, 49, 12, 42, 24, 54, 43, 69, 62, 44], [54, 90, 67, 43, 72, 17, 22, 83, 28, 68], [18, 12, 10,...

  • I am practicing python by doing some projects, and I am stuck on this function. I'd...

    I am practicing python by doing some projects, and I am stuck on this function. I'd like to know how to do it without using statements like break, input, print, or continue. The first parameter represents a "person to friends" dictionary, the second parameter represents a "person to clubs" dictionary, and the third parameter represents a person's name in the same format as the dictionary keys). Using the recommendation system described below, the function should return the club recommendations for...

  • Hi, So I have a finished class for the most part aside of the toFile method...

    Hi, So I have a finished class for the most part aside of the toFile method that takes a file absolute path +file name and writes to that file. I'd like to write everything that is in my run method also the toFile method. (They are the last two methods in the class). When I write to the file this is what I get. Instead of the desired That I get to my counsel. I am having trouble writing my...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT