Шаг 1. Считывание csv-файлов и сохранение их в виде объектов Python

In [12]:
d = {}
d['b'] = 123
d
Out[12]:
{'b': 123}
In [25]:
s = "state,Clinton,Trump,Johnson"
s.split(",")[1:]
Out[25]:
['Clinton', 'Trump', 'Johnson']
In [33]:
def read_electors(filename):
    """
    input: filename - name of file with electors to read
    output: dictionary: state => electors
                        state: str
                        electors: int
            example: {'Alabama': 3,
                      'Florida': 32, ...}
    """
    with open(filename, encoding='utf-8') as f:
        electors = {}
        f.readline()
        for line in f:
            state, elect = line.split(",")
            electors[state] = int(elect)
    return electors


def read_results(filename):
    """
    input: filename - name of file with results
    output: (participants, table)
             participants: ['Clinton', 'Trump', ...]
             table: [["Florida", 0.216, 0.618, ...], ...]
             
    """
    with open(filename, encoding='utf-8') as f:
        table = []
        participants = f.readline().rstrip().split(",")[1:]
        for line in f:
            row = line.split(",")
            new_row = [row[0]]
            for res in row[1:]:
                new_row.append(float(res))
            table.append(new_row)
    return participants, table

print(read_results("results.csv.txt"))
(['Clinton', 'Trump', 'Johnson', 'Stein', 'Castle', 'McMullin'], [['Florida', 0.21643392098220207, 0.6184467954467638, 0.04457343708226084, 0.028206170212040343, 0.06040515766063959, 0.03193451861609321], ['Connecticut', 0.33520975022273736, 0.3766576029922333, 0.07870358442413657, 0.04334859443230534, 0.08235315208120927, 0.0837273158473782], ['Georgia', 0.4786199981189612, 0.3494725992118029, 0.050897989103549694, 0.02234779867428596, 0.047794714437646595, 0.05086690045375351], ['Texas', 0.410667528332076, 0.4813180907230389, 0.021310166265861324, 0.03752816094103558, 0.02542006784111675, 0.023755985896871366], ['Vermont', 0.5353890026996816, 0.28225024426095585, 0.05655536561322371, 0.049578262892858875, 0.03644173785366371, 0.039785386679616154], ['New Mexico', 0.4158397091526902, 0.47664383953304273, 0.02670927485607486, 0.03942785914334315, 0.02361710353619899, 0.01776221377864994], ['Illinois', 0.4743584708792889, 0.3574384953328693, 0.05135747721701323, 0.04894248765356084, 0.026575014717688243, 0.04132805419957962], ['Kentucky', 0.4716464226473731, 0.401216301411775, 0.03277304851222111, 0.04360626629684036, 0.025530409821447296, 0.025227551310343013], ['Iowa', 0.236086504366802, 0.554566177140065, 0.05570465392857892, 0.05548367885530835, 0.044951252574761055, 0.053207733134484736], ['Alaska', 0.2663768204276882, 0.5587807977525588, 0.027314939534628758, 0.04561850423467764, 0.04745269022035842, 0.054456247830088296], ['New York', 0.5747666330410445, 0.2605845144736537, 0.03764385692660972, 0.05069472340693114, 0.03889025831428587, 0.037420013837475216], ['Massachusetts', 0.4106972100822512, 0.3821628157511584, 0.05465900876843583, 0.0523988736115916, 0.04777256230434037, 0.052309529482222464], ['Arkansas', 0.3141697353938596, 0.5076086907371066, 0.02716533267183349, 0.03146410378826324, 0.05277774165463266, 0.06681439575430434], ['Missouri', 0.3760603447730306, 0.4950945582642935, 0.040535655139352186, 0.031929557633160674, 0.032592656081644876, 0.023787228108518292], ['Kansas', 0.5215971254164922, 0.27400921349868185, 0.02877234283454981, 0.06302101531960412, 0.06105525580471374, 0.05154504712595834], ['Idaho', 0.44639635107501596, 0.40237852997923496, 0.04787747221779149, 0.03952014873606892, 0.024004464569822758, 0.039823033422065945], ['Wisconsin', 0.46510999687522686, 0.384973991111762, 0.04806441509311391, 0.04594364441866852, 0.01890427954062005, 0.037003672960608684], ['Mississippi', 0.48467918615339056, 0.39964533617354486, 0.03128039713527716, 0.04000910488267859, 0.025286838327784816, 0.019099137327324073], ['Washington', 0.4531011842654208, 0.29500481606601575, 0.06924304534081381, 0.07257452906756942, 0.046492235250432216, 0.06358419000974803], ['Oklahoma', 0.4150163724855623, 0.4600279679632641, 0.03571747195828865, 0.04723033558068806, 0.020983186144048906, 0.021024665868147968], ['California', 0.36125535086896626, 0.47450432264148135, 0.02658527517996591, 0.048928310870575774, 0.049233178923536854, 0.03949356151547395], ['South Carolina', 0.28401193764846705, 0.5637114056804956, 0.02534987895268883, 0.0333125669319926, 0.057032652371005306, 0.036581558415350594], ['Hawaii', 0.3402046940471157, 0.49555760024195467, 0.04784192064201783, 0.04655349438004107, 0.02207486426625546, 0.047767426422615424], ['Maryland', 0.33066758595706525, 0.45476316230983205, 0.060220243904618845, 0.06247406243477058, 0.06184053651554146, 0.030034408878172058], ['Arizona', 0.40296226985639383, 0.46175463358164903, 0.03207998678275915, 0.028231968870549882, 0.04046592005353107, 0.034505220855117036], ['Montana', 0.30102941907587616, 0.46771032491910836, 0.06585070757324749, 0.06486502568348113, 0.05965389136162584, 0.04089063138666098], ['Ohio', 0.40856539969778494, 0.44237530809121683, 0.036773913776128625, 0.04416737941981635, 0.03725620498472721, 0.030861794030326056], ['Oregon', 0.5070366846595816, 0.3261984619429086, 0.033468542272569075, 0.03124846387776888, 0.06005732296188944, 0.041990524285282396], ['Rhode Island', 0.4807960978612476, 0.37504157137941085, 0.032021033469355, 0.03581275123369219, 0.03569669187306346, 0.040631854183230905], ['South Dakota', 0.44275286207350417, 0.4056746772768758, 0.033402749691904564, 0.029011882079030223, 0.04169229471462517, 0.04746553416406005], ['Alabama', 0.5020801815736082, 0.33193866582989917, 0.02557433132651497, 0.053110827318455414, 0.03549068087923332, 0.05180531307228879], ['North Dakota', 0.35179783355616523, 0.5184986031693871, 0.022922738945052056, 0.03881872080784947, 0.041380638975106236, 0.02658146454643997], ['Virginia', 0.39889126373143063, 0.4437177910223862, 0.029539108836097637, 0.0467410369939161, 0.03969853870843969, 0.0414122607077298], ['New Jersey', 0.5211120955205312, 0.3109932116171313, 0.05180453189205294, 0.05411602768723856, 0.03499095379000749, 0.02698317949303861], ['Wyoming', 0.3677867705893511, 0.47063917749918477, 0.025698091421034713, 0.06460571934776348, 0.024209323448937805, 0.04706091769372804], ['Maine', 0.41959859001171645, 0.4683746153957153, 0.02379641207141553, 0.026384480831578152, 0.029826719244724954, 0.03201918244484951], ['D.C.', 0.43530063784974476, 0.3772769786731301, 0.039179785131110895, 0.03863361685362548, 0.049569334571904745, 0.06003964692048405], ['Tennessee', 0.5184675726800922, 0.3123635041244324, 0.04153535562522038, 0.039084314363749116, 0.04828337079914617, 0.04026588240735968], ['Pennsylvania', 0.36859127145774384, 0.462394686292574, 0.03148790698451336, 0.06859667928235291, 0.03542871657065476, 0.03350073941216121], ['Nebraska', 0.3893833026524249, 0.4418986275803871, 0.05090526479813376, 0.04341309886921488, 0.0369248520833409, 0.03747485401649828], ['Delaware', 0.4097322303462565, 0.46083647915148007, 0.029507157066160913, 0.021225121814630626, 0.0326772761053767, 0.04602173551609497], ['Michigan', 0.4597008730534449, 0.3577384520109954, 0.05046205043505046, 0.03479026339934864, 0.05648265110461152, 0.04082570999654914], ['New Hampshire', 0.36438885210807354, 0.47369390868072203, 0.03871291419234115, 0.0363339662868175, 0.04753817211796958, 0.03933218661407603], ['Indiana', 0.5081926996900423, 0.2631729528731829, 0.05957106556770206, 0.06237024030002978, 0.06283263206739685, 0.043860409501645915], ['North Carolina', 0.4259239621240531, 0.4227388515846315, 0.03138313051534267, 0.0304053225676363, 0.054822752447020714, 0.034725980761315674], ['Colorado', 0.4707250256269726, 0.36502263978388705, 0.053772656630501445, 0.04595820162493052, 0.020433765737012496, 0.04408771059669583], ['West Virginia', 0.3837547229875771, 0.4621270596658467, 0.027528655937108605, 0.0456439483132747, 0.027392691860608196, 0.05355292123558456], ['Utah', 0.44901535624953004, 0.406635768145451, 0.031201239629890937, 0.048795831999543994, 0.018636701386637925, 0.04571510258894608], ['Minnesota', 0.3341196099700935, 0.4996433811157294, 0.06374996542592322, 0.036953054067302725, 0.04149467327189461, 0.024039316149056612], ['Louisiana', 0.4146524946847373, 0.42551146272538903, 0.03031485593699451, 0.05887132690185096, 0.026953483321768158, 0.04369637642926001], ['Nevada', 0.4427106095052385, 0.3313130105385348, 0.062424311173213114, 0.05926361661522881, 0.05276867381355388, 0.05151977835423095]])

Шаг 2. Найти победителя в каждом штате

In [8]:
def get_winners(participants, table):
    """
    input: participants and table, as returned by read_results
    output: dictionary: state => winner
                        state: str
                        winner: str
            Example: {'Florida': 'Trump',
                      'California': 'Clinton', ...}
    """

Шаг 3. Посчитать количество голосов выборщиков у каждого кандидата

In [9]:
def get_elector_votes(winners, electors):
    """
    Input: winners: as returned by get_winners
           electors: as returned by read_electors
    Output: dictionary: participant => elector_votes
                        participant: str
                        elector_votes: int
            Example: {'Trump': 1234, 'Clinton': 231, ...}
    """

Шаг 4. Найти кандидата с максимальных количеством голосов выборщиков

In [10]:
def get_final_winner(elector_votes):
    """
    Input: elector_votes, as returned by get_elector_votes
    Output: winner: str
    """
In [ ]: