Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Creative category for Texas Referee by Slepice1
CARDS_VAL = ["23456789TJQKA","scdh"]
def texas_referee(cards_str):
cards_l = cards_str.split(",")
ranks = [find_straight(sort_cards(cards_l,[1,0])),
find_pairs(sort_cards(cards_l,[0,1]),[4,1]),
find_pairs(sort_cards(cards_l,[0,1]),[3,2]),
find_similiar_cards(sort_cards(cards_l,[1,0]),1,5),
find_straight(sort_cards(cards_l,[0,1])),
find_pairs(sort_cards(cards_l,[0,1]),[3,1,1]),
find_pairs(sort_cards(cards_l,[0,1]),[2,2,1]),
find_pairs(sort_cards(cards_l,[0,1]),[2,1,1,1]),
find_pairs(sort_cards(cards_l,[0,1]),[1,1,1,1,1])]
for rank in ranks:
if rank:
return ",".join(sort_cards(rank,[0,1]))
def find_pairs(cards,pairs):
to_return = []
for pair in pairs:
if pair > 1:
cards_pair = find_similiar_cards(cards,0,pair)
if not cards_pair:
return False
to_return += cards_pair
else:
to_return.append(find_highest(cards))
cards = list(filter(lambda x: x not in to_return, cards))
return to_return
def find_highest(cards):
return sort_cards(cards, [0,1])[0]
def find_similiar_cards(cards, prop, lenght):
to_return = [cards[0]]
num = CARDS_VAL[prop].index(cards[0][prop])
for i in range(1,len(cards)):
if CARDS_VAL[prop].index(cards[i][prop]) == num:
to_return.append(cards[i])
else:
to_return = [cards[i]]
num = CARDS_VAL[prop].index(cards[i][prop])
if len(to_return) == lenght:
return to_return
return False
def find_straight(cards):
to_return = [cards[0]]
num = CARDS_VAL[0].index(cards[0][0])
for i in range(1,len(cards)):
if CARDS_VAL[0].index(cards[i][0])+1 == num:
to_return.append(cards[i])
num -= 1
elif CARDS_VAL[0].index(cards[i][0]) == num:
continue
else:
to_return = [cards[i]]
num = CARDS_VAL[0].index(cards[i][0])
if len(to_return) == 5:
return to_return
return False
def sort_cards(cards, by):
changed = True
while changed:
changed = False
for i in range(len(cards)-1):
if CARDS_VAL[by[0]].index(cards[i][by[0]]) > CARDS_VAL[by[0]].index(cards[i+1][by[0]]):
cards[i], cards[i+1] = cards[i+1], cards[i]
changed = True
if CARDS_VAL[by[0]].index(cards[i][by[0]]) == CARDS_VAL[by[0]].index(cards[i+1][by[0]]):
if CARDS_VAL[by[1]].index(cards[i][by[1]]) > CARDS_VAL[by[1]].index(cards[i+1][by[1]]):
cards[i], cards[i+1] = cards[i+1], cards[i]
changed = True
return list(reversed(cards))
April 9, 2015