Playing Cards
For this question, we ask you to design a card game using the traditional 52-card deck. We divide this question in to three parts, so you can complete them in order.
Part One
For the first part, you must design a Game
class representing the game, and
these following functions associated with the class.
add_card(suit, value)
: Creates a new card object with a suit from one of the following strings:Hearts
,Spades
,Clubs
,Diamonds
, and a value from one of the following strings:A
,2
~10
,J
,Q
,K
. This card is represented byi
, wherei
is an integer indicating how many cards have been created before.card_string(card)
: Returns the string representation of the card represented byi
. It follows the format<value> of <suit>
. For example, a card created byadd_card("Spades", "3")
should have a string representation of3 of Spades
.card_beats(card_a, card_b)
: Check if the card represented bycard_a
beats the one represented bycard_b
. A card beats another card if and only if it has a greater value. The value of the cards are ordered fromA
toK
.
You may implement these however you like. However, preferably this should be easily expandable to accommodate new requirements.
Try it yourself
Solution
There are numerous approach we can take to design this problem. The sample solution will provide an object-oriented approach, since it allows us to easily add new types of cards to accommodate new requirements.
Different languages have different tools, but the most basic concept in object oriented programming is inheritance, which is a class deriving from a superclass and inheriting its methods. In this situation, a playing card from the 52 is a card. The reason for this design is that we can easily add other types of cards if we want.
Below is an implementation:
1from enum import Enum, auto
2
3class Card:
4 @property
5 def card_value(self) -> int:
6 raise NotImplementedError()
7
8 def __lt__(self, other):
9 return self.card_value < other.card_value
10
11class Suit(Enum):
12 CLUBS = auto()
13 DIAMONDS = auto()
14 HEARTS = auto()
15 SPADES = auto()
16
17class PlayingCard(Card):
18 SUITS = {
19 "Clubs": Suit.CLUBS,
20 "Diamonds": Suit.DIAMONDS,
21 "Hearts": Suit.HEARTS,
22 "Spades": Suit.SPADES,
23 }
24 SUIT_NAMES = {e: n for n, e in SUITS.items()}
25 VALUES = {
26 "A": 1,
27 **{str(i): i for i in range(2, 11)},
28 "J": 11,
29 "Q": 12,
30 "K": 13,
31 }
32 VALUE_NAMES = {e: n for n, e in VALUES.items()}
33
34 def __init__(self, suit: str, value: str):
35 super().__init__()
36 self.__suit = self.SUITS[suit]
37 self.__value = self.VALUES[value]
38
39 @property
40 def card_value(self) -> int:
41 return self.__value
42
43 def __str__(self) -> str:
44 value = self.VALUE_NAMES[self.__value]
45 suit = self.SUIT_NAMES[self.__suit]
46 return f"{value} of {suit}"
47
48class Game:
49 def __init__(self):
50 self.__cards: list[Card] = []
51
52 def add_card(self, suit: str, value: str) -> None:
53 self.__cards.append(PlayingCard(suit, value))
54
55 def card_string(self, card: int) -> str:
56 return str(self.__cards[card])
57
58 def card_beats(self, card_a: Card, card_b: Card) -> bool:
59 return self.__cards[card_a] > self.__cards[card_b]
60
61if __name__ == '__main__':
62 game = Game()
63 suit, value = input().split()
64 game.add_card(suit, value)
65 print(game.card_string(0))
66 suit, value = input().split()
67 game.add_card(suit, value)
68 print(game.card_string(1))
69 print("true" if game.card_beats(0, 1) else "false")
70
1import java.util.ArrayList;
2import java.util.HashMap;
3import java.util.Map;
4import java.util.Scanner;
5import java.util.Map.Entry;
6import java.util.stream.Collectors;
7
8class Solution {
9 public static abstract class Card implements Comparable<Card> {
10 public abstract int getValue();
11
12 @Override
13 public int compareTo(Card o) {
14 return Integer.compare(getValue(), o.getValue());
15 }
16 }
17
18 public enum Suit {
19 SPADES, HEARTS, DIAMONDS, CLUBS
20 }
21
22 public static class PlayingCard extends Card {
23 private Suit suit;
24 private int value;
25
26 public static final Map<String, Suit> SUITS = Map.of(
27 "Spades", Suit.SPADES,
28 "Hearts", Suit.HEARTS,
29 "Diamonds", Suit.DIAMONDS,
30 "Clubs", Suit.CLUBS
31 );
32 // Inverts the above map to convert back to string.
33 public static final Map<Suit, String> SUIT_NAMES = SUITS.entrySet().stream().collect(Collectors.toMap(Map.Entry::getValue, Map.Entry::getKey));
34
35 // Map.of is limited to 10 entries, so we initialize a static map instead
36 public static final Map<String, Integer> VALUES = new HashMap<>();
37 static {
38 VALUES.put("A", 1);
39 for (int i = 2; i <= 10; i++) {
40 VALUES.put(String.valueOf(i), i);
41 }
42 VALUES.put("J", 11);
43 VALUES.put("Q", 12);
44 VALUES.put("K", 13);
45 }
46 // Inverts the above map to convert back to string.
47 public static final Map<Integer, String> VALUE_NAMES = VALUES.entrySet().stream().collect(Collectors.toMap(Map.Entry::getValue, Map.Entry::getKey));
48
49 public PlayingCard(String suit, String value) {
50 this.suit = SUITS.get(suit);
51 this.value = VALUES.get(value);
52 }
53
54 @Override
55 public int getValue() {
56 return value;
57 }
58
59 @Override
60 public String toString() {
61 return String.format("%s of %s", VALUE_NAMES.get(value), SUIT_NAMES.get(suit));
62 }
63 }
64
65 public static class Game {
66 private ArrayList<Card> cards;
67
68 public Game() {
69 cards = new ArrayList<>();
70 }
71
72 public void addCard(String suit, String value) {
73 cards.add(new PlayingCard(suit, value));
74 }
75
76 public String cardString(int card) {
77 return cards.get(card).toString();
78 }
79
80 public boolean cardBeats(int cardA, int cardB) {
81 return cards.get(cardA).compareTo(cards.get(cardB)) > 0;
82 }
83 }
84
85 public static void main(String[] args) {
86 Scanner scanner = new Scanner(System.in);
87 Game game = new Game();
88 String[] segs = scanner.nextLine().split(" ");
89 game.addCard(segs[0], segs[1]);
90 System.out.println(game.cardString(0));
91 segs = scanner.nextLine().split(" ");
92 game.addCard(segs[0], segs[1]);
93 System.out.println(game.cardString(1));
94 System.out.println(game.cardBeats(0, 1));
95 scanner.close();
96 }
97}
98
The Game
class stores a list of cards
which takes O(n)
space where n
is the number of cards added.
Each method (add_card
, card_string
, card_beats
) uses O(1)
time and O(1)
space.
Part Two
For this part, we ask you to implement the Jokers into the system.
In addition to the functionalities above, also implement the following functions:
add_joker(color)
: Creates a Joker card of withcolor
of eitherRed
orBlack
.- Joker beats everything else except other jokers. This card is represented by
i
, wherei
is an integer indicating how many cards have been created before, including both normal cards and jokers. - A joker's string representation is
Red Joker
orBlack Joker
, depending on the color.
- Joker beats everything else except other jokers. This card is represented by
You may copy the code from the previous question here.
Try it yourself
Solution
We add a Joker
class that inherits the base Card
. For the purpose of this question,
its value is 14
, which is greater than other cards. We do not need to write extra logic
for comparing Jokers with other cards, since that logic is already there under Card
.
Below is the updated implementation:
1from enum import Enum, auto
2
3class Card:
4 @property
5 def card_value(self) -> int:
6 raise NotImplementedError()
7
8 def __lt__(self, other):
9 return self.card_value < other.card_value
10
11class Suit(Enum):
12 CLUBS = auto()
13 DIAMONDS = auto()
14 HEARTS = auto()
15 SPADES = auto()
16
17class PlayingCard(Card):
18 SUITS = {
19 "Clubs": Suit.CLUBS,
20 "Diamonds": Suit.DIAMONDS,
21 "Hearts": Suit.HEARTS,
22 "Spades": Suit.SPADES,
23 }
24 SUIT_NAMES = {e: n for n, e in SUITS.items()}
25 VALUES = {
26 "A": 1,
27 **{str(i): i for i in range(2, 11)},
28 "J": 11,
29 "Q": 12,
30 "K": 13,
31 }
32 VALUE_NAMES = {e: n for n, e in VALUES.items()}
33
34 def __init__(self, suit: str, value: str):
35 super().__init__()
36 self.__suit = self.SUITS[suit]
37 self.__value = self.VALUES[value]
38
39 @property
40 def card_value(self) -> int:
41 return self.__value
42
43 def __str__(self) -> str:
44 value = self.VALUE_NAMES[self.__value]
45 suit = self.SUIT_NAMES[self.__suit]
46 return f'{value} of {suit}'
47
48class JokerColor(Enum):
49 RED = auto()
50 BLACK = auto()
51
52class Joker(Card):
53 COLORS = {
54 "Red": JokerColor.RED,
55 "Black": JokerColor.BLACK,
56 }
57
58 COLOR_NAMES = {e: n for n, e in COLORS.items()}
59
60 def __init__(self, color: str):
61 super().__init__()
62 self.__color = self.COLORS[color]
63
64 @property
65 def card_value(self):
66 return 14
67
68 def __str__(self) -> str:
69 return f"{self.COLOR_NAMES[self.__color]} Joker"
70
71class Game:
72 def __init__(self):
73 self.__cards: list[Card] = []
74
75 def add_card(self, suit: str, value: str) -> None:
76 self.__cards.append(PlayingCard(suit, value))
77
78 def card_string(self, card: int) -> str:
79 return str(self.__cards[card])
80
81 def card_beats(self, card_a: int, card_b: int) -> bool:
82 return self.__cards[card_a] > self.__cards[card_b]
83
84 def add_joker(self, color: str) -> None:
85 self.__cards.append(Joker(color))
86
87if __name__ == '__main__':
88 game = Game()
89 suit, value = input().split()
90 game.add_joker(value) if suit == "Joker" else game.add_card(suit, value)
91 print(game.card_string(0))
92 suit, value = input().split()
93 game.add_joker(value) if suit == "Joker" else game.add_card(suit, value)
94 print(game.card_string(1))
95 print("true" if game.card_beats(0, 1) else "false")
96
1import java.util.ArrayList;
2import java.util.HashMap;
3import java.util.Map;
4import java.util.Scanner;
5import java.util.Map.Entry;
6import java.util.stream.Collectors;
7
8class Solution {
9 public static abstract class Card implements Comparable<Card> {
10 public abstract int getValue();
11
12 @Override
13 public int compareTo(Card o) {
14 return Integer.compare(getValue(), o.getValue());
15 }
16 }
17
18 public enum Suit {
19 SPADES, HEARTS, DIAMONDS, CLUBS
20 }
21
22 public static class PlayingCard extends Card {
23 private Suit suit;
24 private int value;
25
26 public static final Map<String, Suit> SUITS = Map.of(
27 "Spades", Suit.SPADES,
28 "Hearts", Suit.HEARTS,
29 "Diamonds", Suit.DIAMONDS,
30 "Clubs", Suit.CLUBS
31 );
32 // Inverts the above map to convert back to string.
33 public static final Map<Suit, String> SUIT_NAMES = SUITS.entrySet().stream().collect(Collectors.toMap(Map.Entry::getValue, Map.Entry::getKey));
34
35 // Map.of is limited to 10 entries, so we initialize a static map instead
36 public static final Map<String, Integer> VALUES = new HashMap<>();
37 static {
38 VALUES.put("A", 1);
39 for (int i = 2; i <= 10; i++) {
40 VALUES.put(String.valueOf(i), i);
41 }
42 VALUES.put("J", 11);
43 VALUES.put("Q", 12);
44 VALUES.put("K", 13);
45 }
46 // Inverts the above map to convert back to string.
47 public static final Map<Integer, String> VALUE_NAMES = VALUES.entrySet().stream().collect(Collectors.toMap(Map.Entry::getValue, Map.Entry::getKey));
48
49 public PlayingCard(String suit, String value) {
50 this.suit = SUITS.get(suit);
51 this.value = VALUES.get(value);
52 }
53
54 @Override
55 public int getValue() {
56 return value;
57 }
58
59 @Override
60 public String toString() {
61 return String.format("%s of %s", VALUE_NAMES.get(value), SUIT_NAMES.get(suit));
62 }
63 }
64
65 public enum JokerColor {
66 RED, BLACK
67 }
68
69 public static class Joker extends Card {
70 private JokerColor color;
71
72 public static final Map<String, JokerColor> COLORS = Map.of(
73 "Red", JokerColor.RED,
74 "Black", JokerColor.BLACK
75 );
76 // Inverts the above map to convert back to string.
77 public static final Map<JokerColor, String> COLOR_NAMES = COLORS.entrySet().stream().collect(Collectors.toMap(Map.Entry::getValue, Map.Entry::getKey));
78
79 public Joker(String color) {
80 this.color = COLORS.get(color);
81 }
82
83 @Override
84 public int getValue() {
85 return 14;
86 }
87
88 @Override
89 public String toString() {
90 return String.format("%s Joker", COLOR_NAMES.get(color));
91 }
92 }
93
94 public static class Game {
95 private ArrayList<Card> cards;
96
97 public Game() {
98 cards = new ArrayList<>();
99 }
100
101 public void addCard(String suit, String value) {
102 cards.add(new PlayingCard(suit, value));
103 }
104
105 public String cardString(int card) {
106 return cards.get(card).toString();
107 }
108
109 public boolean cardBeats(int cardA, int cardB) {
110 return cards.get(cardA).compareTo(cards.get(cardB)) > 0;
111 }
112
113 public void addJoker(String color) {
114 cards.add(new Joker(color));
115 }
116 }
117
118 public static void main(String[] args) {
119 Scanner scanner = new Scanner(System.in);
120 Game game = new Game();
121 String[] segs = scanner.nextLine().split(" ");
122 if (segs[0].equals("Joker"))
123 game.addJoker(segs[1]);
124 else
125 game.addCard(segs[0], segs[1]);
126 System.out.println(game.cardString(0));
127 segs = scanner.nextLine().split(" ");
128 if (segs[0].equals("Joker"))
129 game.addJoker(segs[1]);
130 else
131 game.addCard(segs[0], segs[1]);
132 System.out.println(game.cardString(1));
133 System.out.println(game.cardBeats(0, 1));
134 scanner.close();
135 }
136}
137
The method add_joker
uses O(1)
time and O(1)
space.
Part Three
This game also involve a concept of a Hand
and comparing the size of the two hands.
For this part, add these following functions to the Game
class:
add_hand(card_indices)
: Create a newHand
with cards represented by the list of integer representation of cardscard_indices
. The hand can be represented byi
, wherei
is the number of hands added before.hand_string(hand)
: Return the string representation of the hand represented byhand
. It is a list of string representation of cards by their insertion order, separated by", "
. For example, ifhand
has a 9 of Clubs, K of Hearts, and a Black Joker, the string representation is"9 of Clubs, K of Hearts, Black Joker"
.beats_hand(hand_a, hand_b)
: Check if the hand represented byhand_a
beats the hand represented byhand_b
according to the following rules:- Starting from the largest card in each hand, compare them. If a card beats another, that hand beats the other hand. Otherwise, compare the next largest card.
- Repeat this process until one hand beats the other, or one hand runs out of cards. If a hand runs out of cards, neither hand beat each other.
Try it yourself
Solution
For this part, we implement the Hand
class by having it contain a list of cards. When we
compare two hands, because we defined a comparison function between two cards, we can
sort them using the default sorting algorithm.
Below is the implementation:
1from enum import Enum, auto
2from typing import List
3
4class Card:
5 @property
6 def card_value(self) -> int:
7 raise NotImplementedError()
8
9 def __lt__(self, other):
10 return self.card_value < other.card_value
11
12class Suit(Enum):
13 CLUBS = auto()
14 DIAMONDS = auto()
15 HEARTS = auto()
16 SPADES = auto()
17
18class PlayingCard(Card):
19 SUITS = {
20 "Clubs": Suit.CLUBS,
21 "Diamonds": Suit.DIAMONDS,
22 "Hearts": Suit.HEARTS,
23 "Spades": Suit.SPADES,
24 }
25 SUIT_NAMES = {e: n for n, e in SUITS.items()}
26 VALUES = {
27 "A": 1,
28 **{str(i): i for i in range(2, 11)},
29 "J": 11,
30 "Q": 12,
31 "K": 13,
32 }
33 VALUE_NAMES = {e: n for n, e in VALUES.items()}
34
35 def __init__(self, suit: str, value: str):
36 super().__init__()
37 self.__suit = self.SUITS[suit]
38 self.__value = self.VALUES[value]
39
40 @property
41 def card_value(self) -> int:
42 return self.__value
43
44 def __str__(self) -> str:
45 value = self.VALUE_NAMES[self.__value]
46 suit = self.SUIT_NAMES[self.__suit]
47 return f'{value} of {suit}'
48
49class JokerColor(Enum):
50 RED = auto()
51 BLACK = auto()
52
53class Joker(Card):
54 COLORS = {
55 "Red": JokerColor.RED,
56 "Black": JokerColor.BLACK,
57 }
58
59 COLOR_NAMES = {e: n for n, e in COLORS.items()}
60
61 def __init__(self, color: str):
62 super().__init__()
63 self.__color = self.COLORS[color]
64
65 @property
66 def card_value(self):
67 return 14
68
69 def __str__(self) -> str:
70 return f"{self.COLOR_NAMES[self.__color]} Joker"
71
72class Hand:
73 def __init__(self, cards):
74 super().__init__()
75 self.cards = [*cards]
76
77 def __str__(self) -> str:
78 return ", ".join(str(card) for card in self.cards)
79
80 def __lt__(self, other):
81 for card_a, card_b in zip(reversed(sorted(self.cards)), reversed(sorted(other.cards))):
82 if card_a < card_b:
83 return True
84 elif card_b < card_a:
85 return False
86 return False
87
88class Game:
89 def __init__(self):
90 self.__cards: list[Card] = []
91 self.__hands: list[Hand] = []
92
93 def add_card(self, suit: str, value: str) -> None:
94 self.__cards.append(PlayingCard(suit, value))
95
96 def card_string(self, card: int) -> str:
97 return str(self.__cards[card])
98
99 def card_beats(self, card_a: int, card_b: int) -> bool:
100 return self.__cards[card_a] > self.__cards[card_b]
101
102 def add_joker(self, color: str) -> None:
103 self.__cards.append(Joker(color))
104
105 def add_hand(self, card_indices: List[int]) -> None:
106 self.__hands.append(Hand([self.__cards[i] for i in card_indices]))
107
108 def hand_string(self, hand: int) -> str:
109 return str(self.__hands[hand])
110
111 def hand_beats(self, hand_a: int, hand_b: int) -> bool:
112 return self.__hands[hand_a] > self.__hands[hand_b]
113
114if __name__ == '__main__':
115 game = Game()
116 hand_a_list = []
117 n_1 = int(input())
118 for i in range(n_1):
119 suit, value = input().split()
120 game.add_joker(value) if suit == "Joker" else game.add_card(suit, value)
121 hand_a_list.append(i)
122 game.add_hand(hand_a_list)
123 print(game.hand_string(0))
124 hand_b_list = []
125 n_2 = int(input())
126 for i in range(n_1, n_1 + n_2):
127 suit, value = input().split()
128 game.add_joker(value) if suit == "Joker" else game.add_card(suit, value)
129 hand_b_list.append(i)
130 game.add_hand(hand_b_list)
131 print(game.hand_string(1))
132 print("true" if game.hand_beats(0, 1) else "false")
133
1import java.util.ArrayList;
2import java.util.Collections;
3import java.util.HashMap;
4import java.util.List;
5import java.util.Map;
6import java.util.Scanner;
7import java.util.Map.Entry;
8import java.util.stream.Collectors;
9
10class Solution {
11 public static abstract class Card implements Comparable<Card> {
12 public abstract int getValue();
13
14 @Override
15 public int compareTo(Card o) {
16 return Integer.compare(getValue(), o.getValue());
17 }
18 }
19
20 public enum Suit {
21 SPADES, HEARTS, DIAMONDS, CLUBS
22 }
23
24 public static class PlayingCard extends Card {
25 private Suit suit;
26 private int value;
27
28 public static final Map<String, Suit> SUITS = Map.of(
29 "Spades", Suit.SPADES,
30 "Hearts", Suit.HEARTS,
31 "Diamonds", Suit.DIAMONDS,
32 "Clubs", Suit.CLUBS
33 );
34 // Inverts the above map to convert back to string.
35 public static final Map<Suit, String> SUIT_NAMES = SUITS.entrySet().stream().collect(Collectors.toMap(Map.Entry::getValue, Map.Entry::getKey));
36
37 // Map.of is limited to 10 entries, so we initialize a static map instead
38 public static final Map<String, Integer> VALUES = new HashMap<>();
39 static {
40 VALUES.put("A", 1);
41 for (int i = 2; i <= 10; i++) {
42 VALUES.put(String.valueOf(i), i);
43 }
44 VALUES.put("J", 11);
45 VALUES.put("Q", 12);
46 VALUES.put("K", 13);
47 }
48 // Inverts the above map to convert back to string.
49 public static final Map<Integer, String> VALUE_NAMES = VALUES.entrySet().stream().collect(Collectors.toMap(Map.Entry::getValue, Map.Entry::getKey));
50
51 public PlayingCard(String suit, String value) {
52 this.suit = SUITS.get(suit);
53 this.value = VALUES.get(value);
54 }
55
56 @Override
57 public int getValue() {
58 return value;
59 }
60
61 @Override
62 public String toString() {
63 return String.format("%s of %s", VALUE_NAMES.get(value), SUIT_NAMES.get(suit));
64 }
65 }
66
67 public enum JokerColor {
68 RED, BLACK
69 }
70
71 public static class Joker extends Card {
72 private JokerColor color;
73
74 public static final Map<String, JokerColor> COLORS = Map.of(
75 "Red", JokerColor.RED,
76 "Black", JokerColor.BLACK
77 );
78 // Inverts the above map to convert back to string.
79 public static final Map<JokerColor, String> COLOR_NAMES = COLORS.entrySet().stream().collect(Collectors.toMap(Map.Entry::getValue, Map.Entry::getKey));
80
81 public Joker(String color) {
82 this.color = COLORS.get(color);
83 }
84
85 @Override
86 public int getValue() {
87 return 14;
88 }
89
90 @Override
91 public String toString() {
92 return String.format("%s Joker", COLOR_NAMES.get(color));
93 }
94 }
95
96 public static class Hand implements Comparable<Hand> {
97 public ArrayList<Card> cards;
98
99 public Hand(List<Card> cards) {
100 this.cards = new ArrayList<>(cards);
101 }
102
103 @Override
104 public String toString() {
105 return cards.stream().map(String::valueOf).collect(Collectors.joining(", "));
106 }
107
108 @Override
109 public int compareTo(Hand o) {
110 ArrayList<Card> selfCards = new ArrayList<>(cards);
111 ArrayList<Card> otherCards = new ArrayList<>(o.cards);
112 Collections.sort(selfCards);
113 Collections.sort(otherCards);
114 for (int i = selfCards.size() - 1, j = otherCards.size() - 1; i >= 0 && j >= 0; i--, j--) {
115 int res = selfCards.get(i).compareTo(otherCards.get(j));
116 if (res != 0) {
117 return res;
118 }
119 }
120 return 0;
121 }
122 }
123
124 public static class Game {
125 private ArrayList<Card> cards;
126 private ArrayList<Hand> hands;
127
128 public Game() {
129 cards = new ArrayList<>();
130 hands = new ArrayList<>();
131 }
132
133 public void addCard(String suit, String value) {
134 cards.add(new PlayingCard(suit, value));
135 }
136
137 public String cardString(int card) {
138 return cards.get(card).toString();
139 }
140
141 public boolean cardBeats(int cardA, int cardB) {
142 return cards.get(cardA).compareTo(cards.get(cardB)) > 0;
143 }
144
145 public void addJoker(String color) {
146 cards.add(new Joker(color));
147 }
148
149 public void addHand(List<Integer> cardIndices) {
150 ArrayList<Card> cardObjects = new ArrayList<>();
151 for (int i : cardIndices) {
152 cardObjects.add(cards.get(i));
153 }
154 hands.add(new Hand(cardObjects));
155 }
156
157 public String handString(int hand) {
158 return hands.get(hand).toString();
159 }
160
161 public boolean handBeats(int handA, int handB) {
162 return hands.get(handA).compareTo(hands.get(handB)) > 0;
163 }
164 }
165
166 public static void main(String[] args) {
167 Scanner scanner = new Scanner(System.in);
168 Game game = new Game();
169 ArrayList<Integer> handAList = new ArrayList<>();
170 int listALength = Integer.parseInt(scanner.nextLine());
171 for (int i = 0; i < listALength; i++) {
172 String[] segs = scanner.nextLine().split(" ");
173 if (segs[0].equals("Joker"))
174 game.addJoker(segs[1]);
175 else
176 game.addCard(segs[0], segs[1]);
177 handAList.add(i);
178 }
179 game.addHand(handAList);
180 System.out.println(game.handString(0));
181 ArrayList<Integer> handBList = new ArrayList<>();
182 int listBLength = Integer.parseInt(scanner.nextLine());
183 for (int i = listALength; i < listALength + listBLength; i++) {
184 String[] segs = scanner.nextLine().split(" ");
185 if (segs[0].equals("Joker"))
186 game.addJoker(segs[1]);
187 else
188 game.addCard(segs[0], segs[1]);
189 handBList.add(i);
190 }
191 game.addHand(handBList);
192 System.out.println(game.handString(1));
193 System.out.println(game.handBeats(0, 1));
194 scanner.close();
195 }
196}
197
add_hand
is to initialize a Hand
instance, it uses O(n)
time complexity and stores the list of cards using O(n)
space, where n
is the number of cards given to the hand.
both hand_string
and hand_beats
takes O(1)
space.
hand_string
takes O(1)
time and hand_beats
takes O(min(n,m))
time where n
and m
are the size of the two hands respectively.
Still not clear? Submit the part you don't understand to our editors.