#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    44
    Rep Power
    2

    HELP.. I need to do this fast :$


    def get_winner (dict_winner):

    new_dict = {}
    for winner in dict_winner:
    first_letter = winner[0]
    value = dict_winner[winner]
    if first_letter in new_dict:
    new_dict[first_letter] += value
    else:
    new_dict[first_letter] = value
    return (new_dict)



    >>> get_winner({('C', 'A', 'B', 'D') :3, ('D', 'B', 'C', 'A') :2, ('C', 'D', 'A', 'B') :1, ('A', 'D', 'B', 'C'):2, ('A', 'D', 'C', 'B') :4, ('A', 'C', 'D', 'B') :2})

    Output =
    {'A': 8, 'D': 2, 'C': 4}

    But i want it to give me a final output of a tuple of str, NoneType..

    Also.. it is eliminating only the letter with the smallest value in first place only one time.. i want it to repeat this process until i get one winner in the end.. so in this case.. All the B's will be eliminated in the dict itself, not in the out put.
    for example:

    first time = [8, 0, 4, 2]
    second time =
    {('C', 'A', 'D') :3,
    ('D', 'C', 'A') :2,
    ('C', 'D', 'A') :1,
    ('A', 'D', 'C'):2,
    ('A', 'D', 'C') :4,
    ('A', 'C', 'D') :2})
    C = 4
    D = 2
    A = 8
    third time=
    {('C', 'A') :3,
    ('C', 'A') :2,
    ('C','A') :1,
    ('A', 'C'):2,
    ('A', 'C') :4,
    ('A', 'C') :2})
    C = 6
    A = 8

    8/ 14 = more than 50%, I know that should have been the case since the beginning because A already had the majority value. but i am assuming A has a value of 40% which is when elimination should begin.. So can you point out where i went wrong in coding this?... which makes A the winner!!
    And my output should be --

    ('A', None)
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,837
    Rep Power
    480

    Post your code correctly


    I tried your program, getting nowhere fast.
    Code:
    $ python
    Python 2.7.3 (default, Sep 26 2012, 21:51:14) 
    [GCC 4.7.2] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> def get_winner (dict_winner):
    new_dict = {} 
    
    ...   File "<stdin>", line 2
        new_dict = {} 
               ^
    IndentationError: expected an indented block
    >>>
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    44
    Rep Power
    2
    Originally Posted by b49P23TIvg
    I tried your program, getting nowhere fast.
    Code:
    $ python
    Python 2.7.3 (default, Sep 26 2012, 21:51:14) 
    [GCC 4.7.2] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> def get_winner (dict_winner):
    new_dict = {} 
    
    ...   File "<stdin>", line 2
        new_dict = {} 
               ^
    IndentationError: expected an indented block
    >>>
    I think there was an indentation problem:
    this should work:

    Code:
    def get_winner (dict_winner):
    
        new_dict = {} 
        for winner in dict_winner:
            first_letter = winner[0]
            value = dict_winner[winner]
            if first_letter in new_dict:
                new_dict[first_letter] += value
            else:
                new_dict[first_letter] = value
        return (new_dict)

    Comments on this post

    • b49P23TIvg agrees : Yes, there was indentation trouble.

IMN logo majestic logo threadwatch logo seochat tools logo