def get_combinations(sequence,joker=10):
"""
Checks duplicates in sequence, returns tuple with char and duplicate count
"""
# if we got jokers in sequence
# we got to process them :D
if joker in sequence:
# prepare jokerless sequence
jokerless_sequence = []
# extract jokers from sequence
for char in sequence:
if char !=joker:
jokerless_sequence += [char]
# replace all jokers with first char of jokerless sequence
for i in range(0,len(sequence)):
if sequence[i] == joker:
# replacing joker with new character
sequence[i]=jokerless_sequence[0]
# count duplicates of first number
char = sequence[0]
count = 1
for i in range(1,len(sequence)):
if sequence[i] == char:
count +=1
else:
break
return (char,count)
Refactorings
No refactoring yet !
Ants
May 13, 2010, May 13, 2010 18:10, permalink
Is get_combinations() really a good name for this function if what you are really looking for is the counts of duplicates?
Why do you assume the first character is what you want duplicated? If my hand held 2,10,7,7,7 your routine would return (2,2), but wouldn't it be better if it returned (7,4) as a better poker hand?
rullon.myopenid.com
May 15, 2010, May 15, 2010 19:51, permalink
thanks for feedback!
>Is get_combinations() really a good name for this function if what you are really looking for is the counts of duplicates?
imho get_combination can be better name, i called it that because for me it's card combination :)
>Why do you assume the first character is what you want duplicated? If my hand held 2,10,7,7,7 your routine would return (2,2), but wouldn't it be better if it returned (7,4) as a better poker hand?
because in slot games score of each line(which contains cards sequence) counts only from left to right, and only first match plays. (i forgot to say it in task description)
Euribates
September 16, 2010, September 16, 2010 14:16, permalink
Maybe `get_max_score` would be a better name that `get_combinations`. The name of the thing is important IMHO :-)
What about this:
def get_combinations(sequence,joker=10):
value, count = [x for x in sequence if x <> joker][0], 0
for x in sequence:
if x == value or x == joker:
count += 1
continue
break;
return (value, count)
get max length of duplicate chars sequence started from first (left). 10 - like joker, allow putting any char instead.
Examples:
sequence is 1,10,1,2,5; 10 is substituting 1, and we got new sequence 1,1,1,2,5; returns (1,3) e.g. "1" char, 3 times
your refactors are welcome :)