Discuss HELP.. I need to do this fast :$ in the Python Programming forum on Dev Shed. HELP.. I need to do this fast :$ Python Programming forum discussing coding techniques, tips and tricks, and Zope related information. Python was designed from the ground up to be a completely object-oriented programming language.
Posts: 44
Time spent in forums: 11 h 46 m 55 sec
Reputation Power: 1
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)
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 --
Posts: 44
Time spent in forums: 11 h 46 m 55 sec
Reputation Power: 1
Quote:
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)