Finding pairs out of five - java

I am having trouble finding pairs in a string of size five. So therefore, there can only be two pairs. For every pair that I have found, I should increase the score by 2 points.
Here is what I have so far, but it is incorrect.
String temp = "4 5 4 3 3";
String tempLine = temp.replaceAll(" ", "");
String[] hand = temp.split(" ");
for(int i = 0; i < hand.length; i++)
{
if(hand[i].equals(tempLine.substring(0, 1)) && i !=0 )
score += 1;
if(hand[i].equals(tempLine.substring(1, 2)) && i != 1 )
score += 1;
if(hand[i].equals(tempLine.substring(2, 3)) && i!= 2 )
score += 1;
if(hand[i].equals(tempLine.substring(3, 4)) && i!= 3)
score += 1;
if(hand[i].equals(tempLine.substring(4)) && i != 4)
score += 1;
}
EDIT: I am trying to find pairs in the hand that have similar value, for example 4 would be one pair found in this hand

Sort the hand first and then loop though looking for hand[i] == hand[i-1]. Note that you might have to be slightly clever as to not count sets of 3s or 4s twice but this should get you started.

Create an actual Hand class and don't use Strings to represent your cards. A Card's rank and suit are suitable candidates for an enum:
class Card {
enum Suite { ... };
enum Rank { ... };
private final Suite s;
private final Rank r;
// ...
}
class Hand {
private Card[] cards;
// ...
}
And sort the Card[] in your Hand class which makes it easier to evaluate .
Checkout Oracle's enum tutorial which has an example with cards: http://download.oracle.com/javase/1.5.0/docs/guide/language/enums.html

I think this fits what you're trying to do.
char[] hand = {'a','b','c','b','c'};
/* sort the hand to ensure pairs are next to each other */
for(int x=0;x<hand.length - 1;x++){
for(int y=(x+1);y<hand.length;y++){
if((int)hand[x] > (int)hand[y]){
char temp = hand[y];
hand[y] = hand[x];
hand[x] = temp;
}
}
}
int score = 0;
for(int x=0;x<hand.length - 1;x++){
if(hand[x] == hand[x + 1]){
score++;
/*if you want to make sure you only get pairs
add an "x++;" that way it'll skip over the
letter you just tested*/
}
}

1) Why aren't you comparing elements of the hand array to other elements? You already did the work to interpret the string and create the array data; use it.
2) There are more possible pairs of elements than that. Put down five similar objects in a row in front of you at the computer and try it out for yourself. You should be able to identify ten pairs. You should also be able to identify a pattern to how to find/label the pairs.

This example below is one possibility of how to count pairs. The question is how it should behave (increase the score) in case there are three equal values in the string or array or whatever. Shall this be counted as three pairs or not at all...?
123 is in fact three combinations 12 && 13 && 23
111 is in fact three combinations 11 && 11 && 11
package edu.harris.pairs;
public class FindPairs {
public static void main(String[] args) {
String s = "hello";
int score = 0;
for (int i = 0; i < s.length() - 1; i++) {
for (int j = i + 1; j < s.length(); j++) {
if (s.charAt(i) == s.charAt(j)) {
score++;
}
}
}
System.out.println(score);
}
}
Hope this helps a bit.

Related

credit card numbers need to be added without the dash

So one of the conditions for the credit card number to be valid is that "the sum of first 4 digits must be 1 less than the sum of the last 4 digits" I believe the problem could be it's counting the dashes as a digit but not sure. the rule 4 is that the sum of all digits must be divisible by 4, which seems to work, but rule 5 doesn't.
int sum = ccNumber.chars().filter(Character::isDigit).map(Character::getNumericValue).sum();
if(sum%4!=0){
valid = false;
errorCode = 4;
return;
}
// set values and for loop for fifth rule.
String digits = ccNumber.replaceAll("[ˆ0-9]","");
int firstfourdigits = 0;
int lastfourdigits = 0;
for(int i=0; i<4; i++)
firstfourdigits = firstfourdigits + Character.getNumericValue(ccNumber.charAt(i));
for (int i=0, m = ccNumber.length()-1; i<4; i++, m--)
lastfourdigits = lastfourdigits + Character.getNumericValue(ccNumber.charAt(m));
// mutator for fifth rule
if(lastfourdigits!= firstfourdigits -1){
valid = false;
errorCode = 5;
return;
}
sorry I'm lost and new to coding.
Edit since you altered your question. Original anwser to the original question is at the bottom part
Checking if first part and last part have a difference of one
The code you currently have is close, but there are some mistakes here and there.
Filtering out only digits: The code you use to filter out all characters that are not numeric should work, but in your following code you are no longer using this filtered value in your loop.
firstfourdigits + Character.getNumericValue(ccNumber.charAt(i));
This should use the variable with only your numeric values => digits
firstfourdigits = firstfourdigits + Character.getNumericValue(digits.charAt(i));
Difference in first group vs last group: The -1 should be replaced by +1 here. When you are experiencing problems with this type of checks, it's always adviced to try and calculate it on a piece of paper. Lets assume the sum of the first 4 digits is 8 and the sum of the last 4 digits is 9. As per the requirement, this is a valid number, and should result to false in your check if(lastfourdigits!= firstfourdigits -1)
Let's fill it in: 9 != 8-1 => 9 != 7 so this returns false, and marks it as invalid. If we base it on the requirement, you could write the sum of the first 4 digits should be one less then the last 4 digits as: firstfourdigits = lastfourdigits - 1. This is mathmatically the same as lastfourdigits = firstfourdigits + 1. However, in our check we want to know if this check is not correct, so we should change the statement to: if(lastfourdigits != firstfourdigits + 1)
These 2 changes should give you the results you asked for. Combining these changes, we come to the following code example
String digits = ccNumber.replaceAll("[ˆ0-9]", "");
int firstfourdigits = 0;
int lastfourdigits = 0;
for (int i = 0; i < 4; i++)
firstfourdigits = firstfourdigits + Character.getNumericValue(digits.charAt(i));
for (int i = 0, m = ccNumber.length() - 1; i < 4; i++, m--)
lastfourdigits = lastfourdigits + Character.getNumericValue(digits.charAt(m));
if(lastfourdigits!= firstfourdigits + 1){
valid = false;
errorCode = 5;
return;
}
Other recommendations
The above example should work for what you asked, and is based on your code. However there are some optimalisations possible to the code to make everything more readable
Use brackets on your for loop: To make it clearer what is inside the for loop, and what isn't, I would advise you to make use of curly brackets. Though they are not required, they will make it very clear what is and isn't in the for loop and will prevent hard to spot issues when you add something extra in the for loop
Use the short notation for addition: Instead of writing firstfourdigits = firstfourdigits + Character.getNumericValue(digits.charAt(i));, You could use a shorter notation of +=. This will take the value on the left side of your equals, and will calculate the sum of that value on the right side. firstfourdigits += Character.getNumericValue(digits.charAt(i));
The code looks like this then:
String digits = ccNumber.replaceAll("[ˆ0-9]", "");
int firstfourdigits = 0;
int lastfourdigits = 0;
for (int i = 0; i < 4; i++){
firstfourdigits += Character.getNumericValue(digits.charAt(i));
}
for (int i = 0, m = ccNumber.length() - 1; i < 4; i++, m--) {
lastfourdigits += Character.getNumericValue(digits.charAt(m));
}
if(lastfourdigits!= firstfourdigits + 1){
valid = false;
errorCode = 5;
return;
}
Anwser to original question to calculate the sum of all digits
You could make use of Character.isDigit(char). To simplify the for loop, you can even make use of a stream to get the sum
int sum = ccNumber.chars().filter(Character::isDigit).map(Character::getNumericValue).sum();
if (sum % 4 != 0) {
valid = false;
errorCode = 4;
return;
}
.chars(): This will create a stream of all the characters in the provided string so that we can loop over them one by one
.filter(Character::isDigit): This will filter out every character that is not a digit
.map(Character::getNumericValue): This will map the stream from Characters to their numeric values so that we can use those further
sum() will calculate the sum of the numeric values that we currently have in the Stream
The difference is always a positive value e.g. the difference between 4 and 5 or between 5 and 4 is the same i.e. 1. In other words, you need to compare the absolute value of the subtraction with 1.
Therefore, replace
if(lastfourdigits!= firstfourdigits -1)
with
if(Math.abs(lastfourdigits - firstfourdigits) != 1)
Another mistake in your code is that you have used ccNumber, instead of digits in your loops.
Some recommendations to make your code easier to understand:
Replace for (int i=0, m = digits.length()-1; i<4; i++, m--) with for (int m = digits.length() - 1; m >= digits.length() - 4; m--). Note that I've already replaced ccNumber, with digits in these statements.
Replace ccNumber.replaceAll("[^0-9]","") with ccNumber.replaceAll("\\D", "").
Replace firstfourdigits = firstfourdigits + Character.getNumericValue(digits.charAt(i)) with firstfourdigits += Character.getNumericValue(digits.charAt(i)). Note that I've already replaced ccNumber, with digits in these statements.
Always enclose the body of if and loop statements within { } even if there is just one statement inside the body.
Demo:
public class Main {
public static void main(String[] args) {
System.out.println(isValidOnDiffCriteria("1234-5678-9101-1213"));
System.out.println(isValidOnDiffCriteria("1234-5678-9101-1235"));
System.out.println(isValidOnDiffCriteria("1235-5678-9101-1234"));
}
static boolean isValidOnDiffCriteria(String ccNumber) {
String digits = ccNumber.replaceAll("\\D", "");
int firstfourdigits = 0;
int lastfourdigits = 0;
for (int i = 0; i < 4; i++) {
firstfourdigits += Character.getNumericValue(digits.charAt(i));
}
for (int m = digits.length() - 1; m >= digits.length() - 4; m--) {
lastfourdigits += Character.getNumericValue(digits.charAt(m));
}
if (Math.abs(lastfourdigits - firstfourdigits) != 1) {
return false;
}
return true;
}
}
Output:
false
true
true
Try the code above. Should be what you asked. You don't need a try catch.
static boolean isCardValid(String creditCard) {
// group digits in a string array
String[] cards = creditCard.split("-");
int sumAll = 0;
// for every group of digits we convert it to char[]
for (String card : cards) {
sumAll += sum(card.toCharArray());
}
int firstGroupOfDigits = sum(cards[0].toCharArray()) ;
int lastGroupOfDigits = sum(cards[cards.length-1].toCharArray());
if( firstGroupOfDigits == lastGroupOfDigits -1){
if (sumAll % 4 == 0) {
return true;
}
}
return false;
}
// sum the group of digits separated by "-"
static int sum(char[] chr) {
int sum = 0;
for (char c : chr) {
sum += Character.getNumericValue(c);
}
return sum;
}
Well, your program is not that bad and as far as I can tell there is only one problem and that is the you simply reversed the required test on the first and last groups. I would advise you to ensure the valid is initialized to true as the default. Then if none of the error codes are set, it will return true.
Presently you have the following:
if (lastfourdigits != firstfourdigits - 1) {
valid = false;
errorCode = 5;
}
But what you need is this
if (lastfourdigits != firstfourdigits + 1) {
valid = false;
errorCode = 5;
}
Your also have the following, unnecessary code.
String digits = ccNumber.replaceAll("[ˆ0-9]","");
The reason being is that you are simply using ccNumber starting at the beginning for the first four characters and starting at the end for the last four. In this way you are not encountering dashes so you don't need to get just the digits.
Another recommendation is that as soon as you find an error you set the error code and return immediately. What's the use in continuing to process a card that has already been found to be flawed?
Other considerations and an alterative approach
It may not be a part of the assignment but I would also consider the following:
What if you have more or less than 16 digits?
What if you have more than three dashes giving more than four groups of numbers.
Checking the above would require additional logic and would complicate your effort. But it is something to consider. What follows demonstrates one way to check on those particular format issues and report them. This uses basic techniques and avoids streams so as not to repeat unnecessary operations.
This example throws selective errors based on problems found. Those may be changed or eliminated altogether as explained later. Credit card validation is a task where the most straightforward solution is best and should require low overhead.
First, declare a special exception to catch credit card errors.
class BadCreditCardException extends Exception {
public BadCreditCardException(String message) {
super(message);
}
}
Now declare some test data.
String[] testData = {
"1234-4566-9292-0210",
"1500-4009-2400-1600",
"1500-4009-2400-160000",
"1234-45669292-0210",
"1#34-45-66-9292-0210",
"1234-45B6-9292-0210",
"1234-4566-9292-2234",
"1234-4566-9292-021022",
"1234-4566-9292-0210",
"4567-4566-92!2-6835",
"1234-4566-9292-0210",
"1234-45+6-9292-0210",
"1234-4566-92x2-0210",
"1234-4566-9292-0210",
};
Test the credit cards and report errors. Note that only first encountered errors are reported. There may be multiple errors in the number.
String fmt = "%-23s - %s%n";
for(String card : testData) {
try {
validate(card);
System.out.printf(fmt,card, "Valid");
} catch (BadCreditCardException bce) {
System.out.printf(fmt,card, bce.getMessage());
}
}
The above prints.
1234-4566-9292-0210 - Invalid credit card checksum
1500-4009-2400-1600 - Valid
1500-4009-2400-160000 - Non group of 4 digits
1234-45669292-0210 - Insufficient or too may dashes
1#34-45-66-9292-0210 - Insufficient or too may dashes
1234-45B6-9292-0210 - Non digit found.
1234-4566-9292-2234 - Valid
1234-4566-9292-021022 - Non group of 4 digits
1234-4566-9292-0210 - Invalid credit card checksum
4567-4566-92!2-6835 - Non digit found.
1234-4566-9292-0210 - Invalid credit card checksum
1234-45+6-9292-0210 - Non digit found.
1234-4566-92x2-0210 - Non digit found.
1234-4566-9292-0210 - Invalid credit card checksum
The Explanation
The validate method. The method works as follows.
split the card into groups using the dash (-) as a delimiter.
If there are not four groups, throw an exception.
Otherwise, sum each of the groups as follows each of these is checked during the summation process.
first check that the group is of size four, if not throw an exception.
as the group characters are iterated, if a non-digit is encountered, throw an exception.
otherwise, continue computing the sum for the current group as follows:
If the character is a digit, subtract 0 to convert it to an int
and add to the current sums array element.
when completed, add that group sum to the totalSum of all digits.
if the totalSum is divisible by four and the first group is one less than the last group, it is a valid card. Otherwise, throw an exception.
Alternative error handling modification
If the exceptions are not wanted, but just a pass or fail indication, then make the following changes.
change the void return type to boolean
if an exception was throw, simply return false
if all tests pass, then the last statement should return true
public static void validate(String cardNumber) throws BadCreditCardException {
int [] groupSums = new int[4];
int totalSum = 0;
String [] groups = cardNumber.split("-");
if (groups.length != 4) {
throw new BadCreditCardException("Insufficient or too may dashes");
}
for (int i = 0; i < groupSums.length; i++) {
if (groups[i].length() != 4) {
throw new BadCreditCardException("Non group of 4 digits");
}
for(int digit : groups[i].toCharArray()) {
if (!Character.isDigit(digit)) {
throw new BadCreditCardException("Non digit found.");
}
groupSums[i]+= digit -'0';
}
totalSum += groupSums[i];
}
if (groupSums[0]+1 != groupSums[3] || totalSum % 4 != 0) {
throw new BadCreditCardException("Invalid credit card checksum");
}
}
A separate class for Credit card and its parts
Add a Part class that manages a portion of the credit card
Add a CreditCard class that manages these portions
Valid each portion
In addition to validating each potion individually, validate additional check
Depending on the number of times, the valid & sumDigits method will be called, validation/sum can be added in respective methods or in constructor.
import java.util.Arrays;
public class CreditCard {
private final String input;
private final Part[] parts;
private final boolean valid;
CreditCard(String card) {
this.input = card;
if (card == null || card.length() != 19) {
valid = false;
parts = null;
} else {
parts = Arrays.stream(card.split("-")).map(Part::new).toArray(Part[]::new);
final int totalSum = Arrays.stream(parts).mapToInt(Part::sumDigits).sum();
valid = totalSum % 4 == 0 && parts.length == 4
&& parts[0].sumOfDigits + 1 == parts[3].sumOfDigits
&& Arrays.stream(parts).allMatch(Part::isValid);
}
}
static class Part {
final int num;
final boolean valid;
final int sumOfDigits;
Part(String part) {
int localNum = 0;
try {
localNum = Integer.parseInt(part);
} catch (Throwable ignored) {
}
this.num = localNum;
valid = part.length() == 4 && part.equals(String.format("%04d", num));
if (valid) {
sumOfDigits = part.chars().map(Character::getNumericValue).sum();
} else {
sumOfDigits = -1;
}
}
boolean isValid() {
return valid;
}
int sumDigits() {
return sumOfDigits;
}
}
public static void main(String[] args) {
String[] creditCards = {
"1000-0000-0001-0002",
"0000-0000-0000-0000",
"10000-0000-0001-0002",
"10000000-0001-0002",
"1a00-0000-0001-0002",
"1234-4826-6535-1235",
};
Arrays.stream(creditCards).map(CreditCard::new)
.forEach(c -> System.out.println(c.input + " is " + c.valid));
}
}
Everything is fine except the second for loop and your if condition.
Replace your code with the following changes and it should work fine:
int firstfourdigits = 0, lastfourdigits = 0;
for(int i=0; i<4; i++)
firstfourdigits = firstfourdigits + Character.getNumericValue(ccNumber.charAt(i));
for (int m = ccNumber.length()-1; m>ccNumber.length()-5; m--)
lastfourdigits = lastfourdigits + Character.getNumericValue(ccNumber.charAt(m));
if(firstfourdigits != lastfourdigits - 1){
valid = false;
errorCode = 5;
return;
}
You do not need to extract digits at all.
public boolean ccnCheck(String ccn){
String iccn = ccn.replaceAll("-","");
int length = iccn.length();
int fsum = 0;
int lsum = 0;
int allsum = 0;
for( int i = 0; i < length; i++){
int val = Character.getNumericValue(iccn.charAt(m))
if( i < 4)
fsum += val;
if( i >= length-4)
lsum += val;
allsum += val;
}
if( (allsum % 4) != 0)
return false;
if( fsum != lsum-1 )
return false;
return true;
}
In your rule five check, you're using ccNumber instead of your digits string.
For example, shouldn't
Character.getNumericValue(ccNumber.charAt(i));
be this instead:
Character.getNumericValue(digits.charAt(i));

Why are two strings or two arrays necessary to accomplish this?

This method is essentially taking an array that contains percentages, and based on whether or not the percentages are above or below 50%, a letter is appended onto a returned String to result in a final type.
I was told that I should "should create either two Strings or two arrays
that hold the characters, then loop through the dimensions and
test percentageB[i] with the 3 tests"
I don't understand why doing this is necessarily more valid or effective than the method I have already used. Could someone explain this to me?
public static String getPersonality(int[] percentageB) {
char [] types = {'E','I','S','N','T','F','J','P'};
String MBTItype = "";
for (int j = 0; j < types.length; j += 2) {
int i = j/2;
if (percentageB[i] < 50){
MBTItype += types[j];
} else if (percentageB[i] > 50) {
MBTItype += types[j + 1];
} else if (percentageB[i] == 50) {
MBTItype += 'X';
}
}
return MBTItype;
}
The reason you would have been told to use two arrays is that it makes the relationship between the letters clearer, at each position you are assigned the letter from one of the two lists. By combining them into a single array you are not making the mutually exclusive nature of the letters obvious to a reader of your code. Compare this implementation with your own
public static String getPersonality(int[] percentageB) {
char [] lowPercentTypes = {'E','S','T','J'};
char [] highPercentTypes = {'I','N','F','P'};
String MBTItype = "";
for (int i = 0; i < lowPercentTypes.length; i++) {
if (percentageB[i] < 50){
MBTItype += lowPercentTypes[i];
} else if (percentageB[i] > 50) {
MBTItype += highPercentTypes[i];
} else if (percentageB[i] == 50) {
MBTItype += 'X';
}
}
return MBTItype;
}
Reading this you do not need to go through the mental effort of knowing that each type has its compliment in the next item of the array, instead you are explicitly told that it is a high/low percentage type.
We are trying to reduce the cognitive load on the future reader of your code so you want to make things as straightforward as possible

Using for loop to get the Hamming distance between 2 strings

In this task i need to get the Hamming distance (the Hamming distance between two strings of equal length is the number of positions at which the corresponding symbols are different - from Wikipedia) between the two strings sequence1 and sequence2.
First i made 2 new strings which is the 2 original strings but both with lowered case to make comparing easier. Then i resorted to using the for loop and if to compare the 2 strings. For any differences in characters in these 2 pair of string, the loop would add 1 to an int x = 0. The returns of the method will be the value of this x.
public static int getHammingDistance(String sequence1, String sequence2) {
int a = 0;
String sequenceX = sequence1.toLowerCase();
String sequenceY = sequence2.toLowerCase();
for (int x = 0; x < sequenceX.length(); x++) {
for (int y = 0; y < sequenceY.length(); y++) {
if (sequenceX.charAt(x) == sequenceY.charAt(y)) {
a += 0;
} else if (sequenceX.charAt(x) != sequenceY.charAt(y)) {
a += 1;
}
}
}
return a;
}
So does the code looks good and functional enough? Anything i could to fix or to optimize the code? Thanks in advance. I'm a huge noob so pardon me if i asked anything silly
From my point the following implementation would be ok:
public static int getHammingDistance(String sequence1, String sequence2) {
char[] s1 = sequence1.toCharArray();
char[] s2 = sequence2.toCharArray();
int shorter = Math.min(s1.length, s2.length);
int longest = Math.max(s1.length, s2.length);
int result = 0;
for (int i=0; i<shorter; i++) {
if (s1[i] != s2[i]) result++;
}
result += longest - shorter;
return result;
}
uses array, what avoids the invocation of two method (charAt) for each single char that needs to be compared;
avoid exception when one string is longer than the other.
your code is completely off.
as you said yourself, the distance is the number of places where the strings differ - so you should only have 1 loop, going over both strings at once. instead you have 2 nested loops that compare every index in string a to every index in string b.
also, writing an if condition that results in a+=0 is a waste of time.
try this instead:
for (int x = 0; x < sequenceX.length(); x++) { //both are of the same length
if (sequenceX.charAt(x) != sequenceY.charAt(x)) {
a += 1;
}
}
also, this is still a naive approach which will probbaly not work with complex unicode characters (where 2 characters can be logically equal yet not have the same character code)
public static int getHammingDistance(String sequenceX, String sequenceY) {
int a = 0;
// String sequenceX = sequence1.toLowerCase();
//String sequenceY = sequence2.toLowerCase();
if (sequenceX.length() != sequenceY.length()) {
return -1; //input strings should be of equal length
}
for (int i = 0; i < sequenceX.length(); i++) {
if (sequenceX.charAt(i) != sequenceY.charAt(i)) {
a++;
}
}
return a;
}
Your code is OK, however I'd suggest you the following improvements.
do not use charAt() of string. Get char array from string using toCharArray() before loop and then work with this array. This is more readable and more effective.
The structure
if (sequenceX.charAt(x) == sequenceY.charAt(y)) {
a += 0;
} else if (sequenceX.charAt(x) != sequenceY.charAt(y)) {
a += 1;
}
looks redundant. Fix it to:
if (sequenceX.charAt(x) == sequenceY.charAt(y)) {
a += 0;
} else {
a += 1;
}
Moreover taking into account that I recommended you to work with array change it to something like:
a += seqx[x] == seqY[x] ? 0 : 1
less code less bugs...
EDIT: as mentionded by #radai you do not need if/else structure at all: adding 0 to a is redundant.

Finding specific number in prime number array

I'm trying to find prime numbers with a specific condition in Java.
The challenge is to show all the prime numbers (under 100.000) which contain a '3' four times.
I already have a code which shows all the prime numbers under 100.000, but I can't seem to figure out how to count the ones that contain the number '3' four times.
I can however count all the prime numbers.
Can someone help me with this?
Here's the code I have, where am I going to put the numbers into strings?
package Proeftentamen;
import java.util.regex.*;
/**
*
* #author Stefan
*/
public class Vraag_6 {
/// priemgetallen waar 4x een 3 in voor komt???? wtf...
public static void main(String[] args) {
boolean[] lijst = new boolean[1000000]; // hoeveelheid getallen
vularray(lijst);
lijst = zeef(lijst);
drukaf(lijst);
}
public static void vularray(boolean[] lijst) {
for (int i = 2; i < lijst.length; i++) {
lijst[i] = true;
}
}
public static boolean[] zeef(boolean[] lijst) {
for (int i = 2; i < lijst.length / 2; i++) {
if (lijst[i]) {
for (int j = 2 * i; j < lijst.length; j += i) {
lijst[j] = false;
}
}
}
return lijst;
}
public static void drukaf(boolean[] lijst) {
int count = 0;
for (int i = 2; i < lijst.length; i++) {
if (lijst[i] == true) {
System.out.println(i + " " + lijst[i]);
count++;
}
}
System.out.println("Aantal priemgetallen: " + count);
}
}
This question really sounds like a homework, so you should write down what you have come up with and what you tried so far.
There are a lot of ways to count numbers. Just to give you a clue, you can use the reminder operation (in Java - %):
56 % 10 = 6
25 % 5 = 0
So, when you divide by 10 and use a reminder operation you can get the last digit of your number. Now use a loop and counter and you'll be fine.
Another option (very ugly, so don't really use it :) ) - to turn your number into a String and iterate (loop) over its characters.
Hope this helps and good luck!
This code generate 50 permutation of numbers that has four '3' in it's digits
so check each number that is prime or not
public void generateNumbers() {
StringBuilder s = new StringBuilder();
s.append("3333");
for (int i = 0; i < 5; i++) {
for (int j = 0; j <= 9; j++) {
if (j%3==0) continue;
s.insert(i,String.valueOf(j));
int number=Integer.parseInt(s.toString());
System.out.println(number);
s.delete(i,i+1);
}
}
}
Iterate across each prime number.
For each prime number, convert it to a string using the Integer.toString(int) static method.
With this string, iterate over every character (use a for loop and the non-static method String.charAt(int index)) and count the number of times that method returns '3'. (The character '3', not the String "3").
Unless you have some other purpose for an array of prime-number Strings, don't bother to store them anywhere outside the loop.
Please refer below code to validate all such prime numbers.
void getPrimes(int num ,int frequency,char digit) {
int count = 0;
String number=Integer.toString(num);
for (int i = 0; i < number.length(); i++) {
if (count < frequency) {
if (number.charAt(i) == digit)
count++;
}
if (count == frequency)
{
System.out.println(number);
return ;
}
}
}
Using the primes function from an exercise on the Sieve of Eratosthenes, as well as the digits and filter functions from the Standard Prelude, this Scheme expression finds the seven solutions:
(filter
(lambda (n)
(= (length
(filter
(lambda (d) (= d 3))
(digits n)))
4))
(primes 100000))
The outer filter runs over all the primes less than 100000 and applies the test of the outer lambda to each. The inner filter computes the digits of each prime number and keeps only the 3s, then the length function counts them and the equality predicate keeps only those that have 4 3s. You can run the program and see the solution at http://codepad.org/e98fow2u.
you only have at most five digits, four of which must be 3. So what can you say about the remaining digit?
It's not hard to just write out the resulting numbers by hand, and then test each one for primality. Since there are no more than 50 numbers to test, even the simplest trial division by odds will do.
But if you want to generate the numbers programmatically, just do it with 5 loops: add 10,000 to 03333 9 times; add 1,000 to 30333 9 times; add 100 to 33033 9 times; etc. In C++:
int results[50];
int n_res = 0;
int a[5] = {13333, 31333, 33133, 33313, 33331};
for( int i=0, d=10000; i<5; ++i, d/=10)
for( int j=1; j<9; ++j, a[i]+=d )
if( is_prime(a[i]) )
results[n_res++] = a[i];

Comparing poker hand in java

I am working on a poker game. So far I am stuck on comparing poker hands. I sort of have an idea on how to do it but I am not sure what is wrong with my code. Can somebody please point out what's wrong?
So let me give a brief description of what my code is doing.
My other part of the program will go through the cards in my hand and list the frequency of a card value in an array.
h = Hearts
c = Clubs
d = Diamonds
s = Spades
d4 = 4 of Diamond
So let's say in my hand I have
c3, h4, d4, s4, 2d
I will call the other part of my program to read the cards in my hand and return a tally array. The tally array for my hand above will be [0,0,1,1,3,0,0,0,0,0,0,0,0,0] So pretty much what this array is saying is that my hand has one 2, one 3 and three 4.
Now here is my method on trying to find Three of a Kind, it should return false for full house (which is three of a kind plus a pair)
public boolean hasThreeOfAKind() {
int [] temp;
temp = this.getCounts();
for (int i = 0; i< temp.length; i++){
if (temp [i] == 3 ){
for (int j = 0; j < temp.length ; j++)
if ( temp [j] != 2){
return true;
}
}
}return false;
So what I am trying to do above is first, I run through the array, and if if there is any 3. If there is a 3, I run through the array again and see if there is a 2. and if there is not a 2, then I know it is three of a kind. If there is a 2, then it is full house and I should return false. I think my logic is correct, but there is something wrong with my code.
And the second hand that I am having trouble is how to determine if my hand has EXACTLY one pair. It only return true if there is only one pair. Return false if there are two pairs etc.
public boolean hasOnePair() {
int [] temp;
temp = this.getCounts();
for (int i = 0; i< temp.length; i++){
if (temp [i] == 2 ){
return true;
}
}return false;
For this one, I am thinking of sort the value in the array either in descending or ascending order. If I chose the descending order, I first read the array and see if there is a 2, then I could scan the next one and see if the next value after the first 2 is also a 2. If there is another 2, then it would return false.
Can somebody please take a look at my code and point out what's wrong? Thank you very much.
Why are you working with such low level primitives? Is there a reason you're not using a full fledged Card class? Your code would be much simpler.
class Card
enum Value {
TWO,
...
ACE
};
enum Suit {
SPADES,
...
CLUBS
};
private Suit suit;
private Value value;
public Card(Suit suit, Value value) {
this.suit = suit;
this.value = value;
}
}
public class Hand {
private final List<Card> cards;
public Hand(Card first, Card second, Card third, Card fourth, Card fifth) {
// add to cards list.
// sort ascending by value
}
public boolean hasThreeOfAKind() {
for (int i = 0; i < 3; i++) {
Value firstValue = cards.get(i).getValue();
Value secondValue = cards.get(i+1).getValue();
Value thirdValue = cards.get(i+2).getValue();
if (firstValue == secondValue && secondValue == thirdValue) {
return true;
}
}
return false;
}
}
This doesn't directly answer your question, but in my opinion this sort of code is much more readable and maintainable and will be easier to debug than one involving only integers. Java isn't C and you don't really gain much by treating it like C.
In your hasThreeOfAKind() you have the following error:
for (int j = 0; j < temp.length ; j++)
if ( temp [j] != 2){
return true;
}
}
This will return true the first time it finds a non-2 j (which will be statisfied with the 3 you just checked above it -- thus returns true for a full house. You need instead:
boolean foundTwo = false;
for (int j = 0; j < temp.length ; j++)
if ( temp [j] == 2){
foundTwo = true;
}
}
if (!foundTwo) {
return false;
}
Similarly for the other: you need to check if you find another 2 that is different from the one you already found:
for (int i = 0; i< temp.length; i++) {
if (temp [i] == 2 ){
boolean foundAnother = false;
for (int j = 0; j< temp.length; j++) {
if (i != j && temp [j] == 2 ){
foundAnother = true;
}
}
if (!foundAnother) {
return true;
}
}
}
return false;
Another thing you could do is have filters for each recognized hand: a pair-filter, a three-filter, a full-house-filter, etc. and run all those filters through the hand. Don't worry if there is a better (higher value) match, just see which filters return true (found the pattern they were looking for) and select the highest point value among the ones that passed
In your threeOfAKind method, the second for loop will only get executed once unless the first number stored in your array is a 2, it should look more like this:
public boolean hasThreeOfAKind() {
int [] temp;
temp = this.getCounts();
for (int i = 0; i< temp.length; i++){
if (temp [i] == 3 ){
for (int j = 0; j < temp.length ; j++)
if ( temp [j] == 2){
return false;
}
}
}
return true;
}
in the above code, the first time it hits a pair it realizes that the hand is a full house instead of a three of a kind, and returns false.
and as for your other method hasOnePair() it should look more like:
public boolean hasOnePair() {
int [] temp;
temp = this.getCounts();
int count = 0;
for (int i = 0; i< temp.length; i++){
if (temp [i] == 2 ){
count++;
}
}
return count == 1;
}
Look for the number of each similar card and make arrays. SO cards{1,1,2,2,2,5,7} (lets ignore the suits for now) will map to (3,2,1,1) i.e a full house you can check the second array easily
I'm helping my son with a similar problem for his Java Course at college (the course I used to teach) and this is what I would suggest.
First sort your cards from lowest to highest 2 to Ace regardless of suit.
Then do comparing.
If card[0]==card[3] or card[1]==card[4], you have 4 of a kind, ignore next 2 lines.
If card[0]==card[2] or card[1]==card[3] or card[2]==card[4], you have 3 of a kind, ignore the next line.
If card[0]==card[1] or card[1]==card[2] or card[2]==card[3] or card[3]==card[4], you have a pair.

Categories