0

I'm very new to programming and I have been trying out an exercise. Basically I need to count the number of pairs of cards that I find in an unsorted String of 5 cards. The way I interpreted it was probably wrong, For example, one of the strings looks like this: 'sTh3c9hQ' with s being spades, T being ten, h being hearts ect... I'm not sure why my code isn't working.. Probably for a very logical reasons that I am blind to. Could someone offer me some suggestions? Thanks.

        int count = 0;
        for(int i = 0; i<hand.length()-2; i+=2){
            for(int j = 1; j<hand.length()-3; j+=2){
                if(hand.charAt(i) == hand.charAt(i+2) && hand.charAt(j) == 
                    hand.charAt(j+3)) {
                        count++;
                        }
                }
            }
        return count;

The desired output in the case of 'sTh3c9hQ' would be 0, as there are no pairs. The desired output in the case of 'sTh3c9sT' would be 1, as there is one pair. ect. If there were two pairs, count would be 2. If there were three, count would be 3, ect

3 Answers3

1

I personally think you should split the string into a list instead of keeping track of where you are in the string itself, it makes the for loop a lot easier to understand. something like that would look like:

public int getPairs(final String hand) {
    int count = 0;
    List<String> cards = getParts(hand, 2);
    for (int i = 0; i < cards.size() - 1; i++) {
        for (int j = i + 1; j < cards.size(); j++) {
            if (cards.get(i).charAt(1) == cards.get(j).charAt(1)) {
                count++;
            }
        }
    }
    return count;
}

private static List<String> getParts(final String string,
                                     final int partitionSize) {
    List<String> parts = new ArrayList<>();
    int len = string.length();
    for (int i = 0; i < len; i += partitionSize) {
        parts.add(string.substring(i, Math.min(len, i + partitionSize)));
    }
    return parts;
}
scigs
  • 529
  • 1
  • 5
  • 12
0

There are a couple of issues with your code that might be causing it to not work as expected:

The outer loop (i) iterates from 0 to hand.length() - 2 with a step of 2, which means it will skip the last character in the hand string. To include the last character, you should change the condition to i < hand.length() - 1.

The inner loop (j) iterates from 1 to hand.length() - 3 with a step of 2, which skips the second-to-last character in the hand string. To include that character, you should change the condition to j <= hand.length() - 3.

Inside the if condition, you are comparing characters at indices i+2 and j+3, which would skip characters and lead to incorrect comparisons. Instead, you should compare characters at indices i+1 and j+1 to check for pairs.

Here's the modified code with the necessary changes:

int count = 0;
for (int i = 0; i < hand.length() - 1; i += 2) {
    for (int j = 1; j <= hand.length() - 3; j += 2) {
        if (hand.charAt(i) == hand.charAt(j) && hand.charAt(i + 1) == hand.charAt(j + 1)) {
            count++;
        }
    }
}

return count;

With these changes, your code should be able to count the number of pairs correctly in the given hand string.

0

Here’s a one line solution:

return hand.replaceAll("[a-z]|(.)(?!.*\\1)", "").length();

This replaces all suits (lowercase letters) and all rank char that don’t have another copy later in the string with a blank (ie deletes them), leaving just the ranks that are paired. Calling length() on the result gives you the count of pairs.

See live demo of the regex matching all non-pair chars.

Bohemian
  • 412,405
  • 93
  • 575
  • 722