Turtle Fast
Turtle Fast

Reputation: 43

How do you compare two sets of list according to the an order?

I am trying to solve a problem where i am given two pairs of cards and i am supposed to compare which one is higher. I tried spreading out the list, but i have no idea on how to compare. So the purpose is to find if the first pair is higher than the others; if the first pair is higher than it returns true, if its the second pair that is higher than returns false. This is the base code so far:

RANK_ORDER = '34567890JQKA2'
SUIT_ORDER = 'DCHS'
def is_higher_pair(pair1, pair2): 
  dev1 = RANK_ORDER.find(pair1[0][0]) 
  dev2 = SUIT_ORDER.find(pair1[0][1])
  dev3 = RANK_ORDER.find(pair1[1][0]) 
  dev4  = SUIT_ORDER.find(pair1[0][1])
  your_answer = False
  if dev1 > dev3:
    your_answer = True 
  if dev1 == dev3:
    if dev2 > dev4:
      your_answer = True
  return your_answer //compares the card in the first pair but i dont know how to compare two pairs.
if __name__ == '__main__':
  print(is_higher_pair(['AH', 'AD'], ['8D', '8S']))
  print(is_higher_pair(['JS', 'JD'], ['2D', '2S']))
  print(is_higher_pair(['6D', '6S'], ['6H', '6C']))
  print(is_higher_pair(['KH', 'KS'], ['KD', 'KC']))
  print(is_higher_pair(['0H', '0D'], ['0S', '0C']))

my output should consider the rank and suit so for the given operation above this should be the results:

True
False
True
True
False

i tried comparing two cards but than lost on what i was doing.

Upvotes: 1

Views: 78

Answers (3)

Turtle Fast
Turtle Fast

Reputation: 43

I was able to figure out the solution, posting here:

RANK_ORDER = '34567890JQKA2'
SUIT_ORDER = 'DCHS'
def is_higher_pair(pair1, pair2):
  Rank1 = RANK_ORDER.find(pair1[0][0]) 
  Rank2 = RANK_ORDER.find(pair2[0][0]) 
  your_answer = False
  if Rank1 > Rank2:
    your_answer = True
  if Rank1 == Rank2:
    SuitPair1C1 = SUIT_ORDER.find(pair1[0][1])
    SuitPair1C2 = SUIT_ORDER.find(pair1[1][1])
    if SuitPair1C1 == 3 or SuitPair1C2 == 3:
      your_answer = True
  return your_answer


if __name__ == '__main__':
  print(is_higher_pair(['AH', 'AD'], ['8D', '8S']))
  print(is_higher_pair(['JS', 'JD'], ['2D', '2S']))

Upvotes: 1

steviestickman
steviestickman

Reputation: 1251

RANK_ORDER = '34567890JQKA2'
SUIT_ORDER = 'DCHS'

def rank(card):
    return RANK_ORDER.find(card[0])


def suit(card):
    return SUIT_ORDER.find(card[1])


def is_higher_pair(pair1, pair2):
    if rank(pair1[0]) != rank(pair2[0]):
        # return True if pair1 has the highest rank
        return rank(pair1[0]) > rank(pair2[0])
    # return True if pair1 has the highest suit
    return suit(max(pair1, key=suit)) > suit(max(pair2, key=suit))


if __name__ == "__main__":
    print(is_higher_pair(["AH", "AD"], ["8D", "8S"]))
    print(is_higher_pair(["JS", "JD"], ["2D", "2S"]))
    print(is_higher_pair(["6D", "6S"], ["6H", "6C"]))
    print(is_higher_pair(["KH", "KS"], ["KD", "KC"]))
    print(is_higher_pair(["0H", "0D"], ["0S", "0C"]))

i think this is the function op wants, it minimizes comparisons. it does assume that a pair is a valid pair and that cards are unique

Upvotes: 0

Silveris
Silveris

Reputation: 1186

If I understood well how to compare pairs, this code produces the right output:

RANK_ORDER = '34567890JQKA2'
SUIT_ORDER = 'DCHS'

class Pair:
  def __init__(self, card1, card2):
    # Get rank value of the pair
    v1 = RANK_ORDER.find(card1[0])
    v2 = RANK_ORDER.find(card1[0])
    self.rank_value = v1 + v2

    # Get highest suit value of the pair
    v1 = SUIT_ORDER.find(card1[1])
    v2 = SUIT_ORDER.find(card2[1])
    self.suit_value = max([v1, v2])

def is_higher_pair(pair1, pair2):
  p1 = Pair(pair1[0], pair1[1])
  p2 = Pair(pair2[0], pair2[1])

  if p1.rank_value > p2.rank_value:
    return True
  elif p1.rank_value == p2.rank_value and \
       p1.suit_value > p2.suit_value:
    return True
  return False

if __name__ == '__main__':
  print(is_higher_pair(['AH', 'AD'], ['8D', '8S']))
  print(is_higher_pair(['JS', 'JD'], ['2D', '2S']))
  print(is_higher_pair(['6D', '6S'], ['6H', '6C']))
  print(is_higher_pair(['KH', 'KS'], ['KD', 'KC']))
  print(is_higher_pair(['0H', '0D'], ['0S', '0C']))

EDIT: A shorter version to achieve this (without a Pair class) would be:

def get_rank_value(pair):
  return RANK_ORDER.find(pair[0][0]) + RANK_ORDER.find(pair[1][0])

def get_suit_value(pair):
  return max([SUIT_ORDER.find(pair[0][1]), SUIT_ORDER.find(pair[1][1])])

def is_higher_pair(pair1, pair2):
  if get_rank_value(pair1) > get_rank_value(pair2):
    return True
  elif get_rank_value(pair1) == get_rank_value(pair2) and \
       get_suit_value(pair1) > get_suit_value(pair2):
    return True
  return False

if __name__ == '__main__':
  print(is_higher_pair(['AH', 'AD'], ['8D', '8S']))
  print(is_higher_pair(['JS', 'JD'], ['2D', '2S']))
  print(is_higher_pair(['6D', '6S'], ['6H', '6C']))
  print(is_higher_pair(['KH', 'KS'], ['KD', 'KC']))
  print(is_higher_pair(['0H', '0D'], ['0S', '0C']))

Upvotes: 0

Related Questions