uva online judge self describing sequence problem solution python | uva 10049

The self describing sequence problem  (uva 10049) is also very straight forward and easy programming challenge in competitive programming once understood well. Which can be stated as (from online judge) Solomon Golomb’s self–describing sequence ⟨f(1),f(2),f(3),...⟩ is the only nondecreasing sequence of positive integers with the property that it contains exactly f(k) occurrences of k for each k. A few moments thought reveals that the sequence must begin as follows: n 1 2 3 4 5 6 7 8 9 10 11   12 f(n) 1 2 2 3 3 4 4 4 5 5 5 6 In this problem you are expected to write a program that calculates the value of f(n) given the value of n. Sample Input 100  9999  123456  1000000000  0 Sample Output 21  356  1684  438744 Programming Explanation This is very simple problem states that for given range of n as input (as shown in above image), we have

uva online judge poker hands problem solution python | uva 10315

The Poker Hands  is a lengthy, tricky and one of the most interesting programming challenge in competitive programming, which can be stated as (from online judge)

A suit of 52 cards card contains each of following card with a value which is one of 2, 3, 4, 5, 6, 7, 8, 9, 10, jack, queen, king, ace (denoted 2, 3, 4, 5, 6, 7, 8, 9, T, J, Q, K, A ). They are ranked suck that 2 being lowest value and 'A' ace being highest valued card.

Poker hand game can be played as follows:
1) Each player has 2 cards and 3 card are placed common for all on table facing downwards.
2) At start by seeing their 2 cards players can choose weather to continue playing or not.
3) At each round, one of the card from table is faced upward and let players decide again to continue or quit.
4)Finally when all three cards are faced up, then players can compare their two card and common 3 cards (i.e 5 cards pair) by poker hands order given below.

Poker hands are ranked by the following partial order from lowest to highest

High Card:  Hands which do not fit any higher category are ranked by the value of their highest card. If the highest cards have the same value, the hands are ranked by the next highest, and so on.

Pair: 2 of the 5 cards in the hand have the same value. Hands which both contain a pair are ranked by the value of the cards forming the pair. If these values are the same, the hands are ranked by the values of the cards not forming the pair, in decreasing order.

Two Pairs: The hand contains 2 different pairs. Hands which both contain 2 pairs are ranked by the value of their highest pair. Hands with the same highest pair are ranked by the value of their other pair. If these values are the same the hands are ranked by the value of the remaining card.

Three of a Kind: Three of the cards in the hand have the same value. Hands which both contain three of a kind are ranked by the value of the 3 cards.

Straight: Hand contains 5 cards with consecutive values. Hands which both contain a straight are ranked by their highest card.

Flush: Hand contains 5 cards of the same suit. Hands which are both flushes are ranked using the rules for High Card.

Full House: 3 cards of the same value, with the remaining 2 cards forming a pair. Ranked by the value of the 3 cards.

Four of a kind: 4 cards with the same value. Ranked by the value of the 4 cards.

Straight flush: 5 cards of the same suit with consecutive values. Ranked by the highest card in the hand.

To know more about poker hands game visit Rob Akery's youtube video "How To Play Poker - Learn Poker Rules: Texas hold em rules

Sample Input

2H 3D 5S 9C KD 2C 3H 4S 8C AH
2H 4S 4C 2D 4H 2S 8S AS QS 3S
2H 3D 5S 9C KD 2C 3H 4S 8C KH
2H 3D 5S 9C KD 2D 3H 5C 9S KH

Sample Output


White wins.
Black wins.
Black wins.
Tie.

Explanation 

Given set of 10 cards (first 5 for player named Black and next 5 for player named White), we have to make different functions for each case i.e first if straight flush is not obtained then look for four of kind, then full house, and so on.

Code

import re

global black, white, Rank, flag, HighCard, INDEX

Ip = input().split()
HighCard = {} 
Rank = {}
Rank[1] = 0
Rank[2] = 0
black = Ip[:5]
white = Ip[5:]

def convert(Set):
    for i in range(0,5):
        temp = re.split('(\d+)',Set[i])
        try:
            temp[1]
        except:
            temp = ['',temp[0][0],temp[0][1]]
        #print(temp)
         
        if(temp[1]=='J'):
            temp[1]='11'
        elif(temp[1]=='Q'):
            temp[1]='12'
        elif(temp[1]=='K'):
            temp[1]='13'
        elif(temp[1]=='A'):
            temp[1]='14'
 
        Set[i] = [temp[1], temp[2]]
     
    return Set

 
def Straight(Set):
    for i in range(0,4):
        if(Set[i][0]<Set[i+1][0]):
            continue
        else:
            return 0
    else:
        return 1
     
def Flush(Set):
    for i in range(0,4):
        if(Set[i][1]==Set[i+1][1]):
            continue
        else:
            return 0
    else:
        return 1

def Straight_Flush(Set):
    if(Straight(Set) == 1 and Flush(Set) == 1):
        Rank[INDEX] = 1
        #print('black 1')
    else:
        Of_Kind(Set, 4)

def Of_Kind(Set, value):
    for i in range(0,5):
        count = 0
        for j in range(i,5):
            if(Set[i][0]==Set[j][0]):
                count+=1
        if(count==value):
            if(value==4):
                Rank[INDEX]=2
                #print('four of kind ')
            if(value==3):
                Rank[INDEX]=6
                #print('three of kind ')
            if(value==2):
                Rank[INDEX]=8
                #print('one pair')
            break
    else:
        if(value == 4):
            Full_House(Set)
        if(value == 3):
            Two_Pairs(Set)
        if(value == 2):
            High_card(Set)

def Full_House(Set):
    for i in range(0,5):
        acount = 0
        temp = []
        bcount = 1
        for j in range(i,5):
            if(Set[i][0]==Set[j][0]):
                acount+=1
            else:
                if(Set[j][0] in temp):
                    bcount+=1
                else:
                    temp.append(Set[j][0])
        if((acount==3 and bcount==2) or (acount==2 and bcount==3)):
            Rank[INDEX]=3
            #print('full house')
            break
    else:
        ans = Flush(Set)
        if(ans == 1):
            Rank[INDEX]=4
            #print('flush')
        else:
            ans = Straight(Set)
            if(ans == 1):
                Rank[INDEX]=5
                #print('straight')
            else:
                Of_Kind(Set, 3)

def Two_Pairs(Set):
    for i in range(0,5):
        acount = 0
        temp = []
        bcount = 1
        for j in range(i,5):
            if(Set[i][0]==Set[j][0]):
                acount+=1
            else:
                if(Set[j][0] in temp):
                    bcount+=1
                else:
                    temp.append(Set[j][0])
        if(acount==2 and bcount==2):
            Rank[INDEX]=7
            #print('Two pairs')
            break
    else:
        Of_Kind(Set, 2)

def High_card(Set):
    max = 0
    for j in range(0,4):
        for i in range(0, 4):
            if(Set[i][0]<Set[i+1][0]):
                max = Set[i+1][0]
    Rank[INDEX] = 9
    #print(max)
    HighCard[INDEX] = max

black = convert(black)
white = convert(white)
INDEX = 1
Straight_Flush(black)
INDEX = 2
Straight_Flush(white)

if(Rank[1] > Rank[2]):
    print('black wins')
elif(Rank[2] > Rank[1]):
    print('white wins')
elif(Rank[2]==Rank[1] and HighCard[1]==HighCard[2]):
    print('Tie')
else:
    if(HighCard[1]>HighCard[2]):
        print('Black wins')
    else:
        print('White wins')


Let's understand the program flow, First we separate the the given input into 5-5 such that every card has its card number( 2 to Ace) and card suit name (H, D, S, C for hearts, diamonds, spades, clubs).
Then for each pair of 5 cards (i.e first player 'Black') we calculate its rank, similarly for player 'White', then the player with highest rank is declared as winner.

Input list of players is passed first to function Straight_flush(), if output obtained then assign rank to respective player, else function Of_kind(set, 4) is called (i.e four_of_kind). Inside function Of_kind() for respective value, we check if number of cards of same suit is equal to value, if yes then assign rank, else execute else if block for respective value.

If above function failed to get four of kind then Full_House() function is called, if value is 3 (i.e three of kind) then Two_Pairs() function is called, if value is 2(i.e a pair) then High_card() function is called and so on.

Even after doing all stuff, same ranks are obtained then declare match as tie.

Hope this article found helpful to you, thank you.

Related keywords: poker hands, poker hands uva, poker hands problem, poker hands online judge, poker hands problem solution, uva 10315, uva 10315 solution python, poker hands source code, poker hands programming challenge.

Comments

Post a Comment

Popular posts from this blog

uva online judge vito's family problem solution python | uva 10041

uva online judge australian voting problem solution python | uva 10142

uva online judge common permutation problem solution python | uva 10252