Skip to content

Latest commit

 

History

History
155 lines (120 loc) · 4.42 KB

palaxi0.asc

File metadata and controls

155 lines (120 loc) · 4.42 KB

Codeeval Card Game Simple Or Trump

This is about the comparison of two cards that belong to a normal deck of cards, which one has a higher value wins, but here comes the trick in this game, every round has a "trump" which is a random suite:

H

Hearts,

D

Diamonds,

C

Clubs,

S

Spades,

Every deck of cards has 4 cards of every suit from 2 to 10 and the J,Q,K and A that takes values of 11, 12, 13, 14 respectively.

Now first imagine you have 10D ten of diamonds and 4C four of clubs. If this was a common get the higher card game the 10D will win, but we are counting with a trump to tell us which card coul win. So first we will compare if the either one of cards has the trump suit and then compare the value with the other.

Example:

AD

Ace of diamonds

2H

Two of hearts

H

trump suit

So only 2H has the trump suit for this case so it automatically wins the round, in case both had the trump suit next thing to do is compare the value and the AD would have won.

Challenge: Simple or trump

We have two print the winner card according to the logic explained before.

Solution

Import the library to read a file and put it into a variable.

import sys

Here what we read is now in test_cases and every line in test_cases is stored as test.

with open(sys.argv[1], 'r') as test_cases:
    for test in test_cases:

        dicti = {
            '2':2, '3':3, '4':4, '5':5, '6':6,
            '7':7, '8':8, '9':9, '10':10,
            'J':11, 'Q':12, 'K':13, 'A':14
        }

The creation of a dictionary that tell us the value of every card number we could have.

        line = test.strip()
        card1 = line.split(" | ")[0].split(" ")[0]
        card2 = line.split(" | ")[0].split(" ")[1]

First create a variable line and make it equal to test without any over spaces at the begining or ending in every line. Then get both cards splitting line by a pipe so we will get the first part where the numbers are, this means the 0 position, then split that position again by empty space and for card1 get the 0 position then for the card2 get the 1 position.

        typecard = line.split(" | ")[1]
        cardlist = []
        cardlist2 = []

Here we create the typecard for storage the trump suit at the split by pipe of line in the first position. The create 2 lists that we will fill according to some conditions next.

        if card1[-1] == typecard and card2[-1] != typecard:
            cardlist.append(card1)

First comprobation is if the last item of the card1 ([-1] position) and trump suite typecard are equals, and the last item of card2 with the trump suite typecard are differents, if both conditions are true then we add card1 to cardlist.

        if card2[-1] == typecard and card1[-1] != typecard:
            cardlist.append(card2)

Now is if the last item of the card2 ([-1] position) and trump suite typecard are equals, and the last item of card1 with the trump suite typecard are differents, if both conditions are true then we add card2 to cardlist.

        if cardlist == []:
            if dicti[card1[0:-1]] == dicti[card2[0:-1]]:

Now if the cardlist is empty, means that neither of the two comprobations before were true. So now start to make the comprobation with the cards.

Inside that if condition make another if that compares the value of both cards with the help of the dictionary we made at first. Ei. 10A[0:-1] means the string but the last position → 10.

                cardlist2.append(card1)
                cardlist2.append(card2)

Knowing the value is the same means that every card should go to the cardlist2 because we know now that they have nothing to do with the trump

            else:
                if dicti[card1[0:-1]] > dicti[card2[0:-1]]:
                    cardlist2.append(card1)
                else:
                    cardlist2.append(card2)

If the values are not the same then we should just append the highest value.

            print" ".join(cardlist2)

Print with spaces the cardlist2 items when the cardlist empty comprobation is false.

        else:
            print" ".join(cardlist)

If the comprobation is true, means that cardlist already has our value so we print it.