How to list combinations in java with methods that stack upon each other.
To print the combinations, you need to print when amount == 0.
To do that, you need to accumulate what has been done to get to that value, i.e. which coins has been applied to the amount value.
One way would be to build a string, appending a space and the coin value as part of the recursive call. The resultant string will start with a space, so it has to be skipped when printing.
public static int combo(int amount, int currentCoin, String combo) {
// Added: ^^^^^^^^^^^^^^
if (amount == 0) {
System.out.println(combo.substring(1)); // <<<<< Added
return 1;
}
if (amount < 0) {
return 0;
}
int nCombos = 0;
for (int coin = currentCoin; coin < coins.length; coin++) {
nCombos += combo(amount - coins[coin], coin, combo + " " + coins[coin]);
// Added: ^^^^^^^^^^^^^^^^^^^^^^^^^^^
}
return nCombos;
}
Test
System.out.println("combo = " + combo(20, 0, ""));
// Added: ^^^^
Output
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 5
1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2
1 1 1 1 1 1 1 1 1 1 1 1 1 2 5
1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2
1 1 1 1 1 1 1 1 1 1 1 2 2 5
1 1 1 1 1 1 1 1 1 1 2 2 2 2 2
1 1 1 1 1 1 1 1 1 1 5 5
1 1 1 1 1 1 1 1 1 2 2 2 5
1 1 1 1 1 1 1 1 2 2 2 2 2 2
1 1 1 1 1 1 1 1 2 5 5
1 1 1 1 1 1 1 2 2 2 2 5
1 1 1 1 1 1 2 2 2 2 2 2 2
1 1 1 1 1 1 2 2 5 5
1 1 1 1 1 2 2 2 2 2 5
1 1 1 1 1 5 5 5
1 1 1 1 2 2 2 2 2 2 2 2
1 1 1 1 2 2 2 5 5
1 1 1 2 2 2 2 2 2 5
1 1 1 2 5 5 5
1 1 2 2 2 2 2 2 2 2 2
1 1 2 2 2 2 5 5
1 2 2 2 2 2 2 2 5
1 2 2 5 5 5
2 2 2 2 2 2 2 2 2 2
2 2 2 2 2 5 5
5 5 5 5
combo = 29
Related
I have an issue solving a problem. I have to add ONLY words into a treeset (and output the size of treeset) using bufferedReader but the problem is I cannot pass the compilator speed test limit. The text contains only letters and whitespaces (it can be an empty line). I have to find out a new solution but seems not this :
BufferedReader read = new BufferedReader(new InputStreamReader(System.in));
Set<String> text = new TreeSet<String>();
String words[], line;
while ((line = read.readLine()) != null) {
words = line.split("\\s+");
for (int i = 0; i < words.length && words[0].length() > 0; i++) {
text.add(words[i]);
}
}
System.out.println(text.size());
Is there any other "split" method to use so the compiler use less "time-thinking"?
In line
words = line.split("\\s+");
you split by regex, which is much slower, than splitting by one char (5 times on my machine).
Java split String performances
If the words are exactly separated by only one space, then the solution is simple
words = line.split(" ");
just replace with this line and your code will run faster.
If words can be separated by several spaces, then add such a line after the loop
text.remove("");
and still replace your regex split with 1 char split.
public class Test {
public static void main(String[] args) throws IOException {
// string contains 1, 2 and two spaces between 1 and 2. text size should be 2
String txt = "1 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1\n" +
"1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1\n" +
"1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1\n" +
"1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1\n" +
"1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1\n" +
"1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1";
InputStream inpstr = new ByteArrayInputStream(txt.getBytes());
BufferedReader read = new BufferedReader(new InputStreamReader(inpstr));
Set<String> text = new TreeSet<>();
String[] words;
String line;
long startTime = System.nanoTime();
while ((line = read.readLine()) != null) {
//words = line.split("\\s+"); -- runs 5 times slower
words = line.split(" ");
for (int i = 0; i < words.length; i++) {
text.add(words[i]);
}
}
text.remove(""); // add only if words can be separated with multiple spaces
long endTime = System.nanoTime();
System.out.println((endTime - startTime) + " " + text.size());
}
}
Also you can replace your for loop with
text.addAll(Arrays.asList(words));
based on the assumption you provided, I would simply add everything to the set and at the end delete unwanted values from it. This hopefully reduces the time to check for the condition (which is not much really)
BufferedReader read = new BufferedReader(new InputStreamReader(System.in));
Set<String> text = new TreeSet<String>();
String words[], line;
while ((line = read.readLine()) != null) {
words = line.split("\\s+");
for(String value: words) {
text.add(value);
}
}
text.remove(" ");
text.remove("");
text.remove(null);
System.out.println(text.size());
You can of course stream your BufferedReader into your TreeSet:
Collection<String> c = read.lines().flatMap(line -> Stream.of(line.split("\\s+")).filter(word -> word.length() > 0)).collect(Collectors.toCollection(TreeSet::new));
I need help with my code which that my input ratings is
-1 2 3 5 -1 5 3 3 1 4 2 2 5 -1 1 3 3 5 4 3
-1 1 1 4 1 3 3 1 2 3 4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 -1 3 2 1 -1 -1 -1 1 3 1 3 3 1 -1 -1 -1 3
4 3 3 -1 4 2 -1 4 -1 -1 2 4 -1 3 4 2 -1 -1 -1 4
-1 5 1 -1 4 1 -1 3 2 2 -1 4 1 3 3 1 -1 -1 -1 3
-1 4 2 1 5 -1 -1 2 1 1 -1 5 -1 5 4 1 2 2 -1 1
2 5 2 -1 3 -1 -1 1 -1 2 -1 4 2 4 3 -1 2 1 -1 -1
2 5 1 1 4 -1 2 1 -1 -1 2 4 -1 3 4 2 -1 -1 -1 4
which I am taking the first role of the rate by vertically which my average[] has 20 values.
I am confused about where is wrong with my code such that if number = -1, then it set to 0 and then doesn't count.
This is a code to count the average rating of a book, my average[] value comes out different then my calculation on a calculator.
public static double [] recommandratings(double[][]ratings) {
double [] average = new double[20];
int [] count = new int[20];
double[] sum = new double[30];
Arrays.fill(count,0);
Arrays.fill(sum, 0);
for(int i= 0;i<20;i++) {
for(int j=0; j<30; j++) {
if(ratings[j][i] == -1) {
ratings[j][i] = 0;
sum[i] += ratings[j][i];
count[i]--;
} else {
sum[i] += ratings[j][i];
count[i]++;
}
}
}
for(int a =0; a<20;a++) {
average[a] = sum[a]/count[a];
}
return average;
}
I think the way of iteration of the 2D array is wrong in your case. I made the following changes to make it simple and dynamic. I hope the following code will give you desired result.
public static double [] recommandratings (double[][]ratings) {
int rowSize = ratings.length;
int colSize = ratings[0].length;
double [] average = new double[colSize];
int[] count = new int[colSize];
for (int row= 0;row < rowSize;row++) {
for (int col=0; col < colSize; col++) {
if (ratings[row][col] != -1) {
average[col] += ratings[row][col];
count[col]++;
}
}
}
for (int a =0; a < colSize;a++) {
int sum = (int) average[a];
average[a] = average[a] / count[a];
System.out.println("count[" + a + "]=" + count[a] + "; sum[" + a + "]=" + sum + "; average[" + a + "]=" + average[a]);
}
return average;
}
P.S. I removed the sum[] and reused the average[] to optimize memory.
Update
This update is based on the comments where you state that the count[a] is always returning 30, where it should be less than 30. I performed a test with both your exact dataset and a smaller dataset. I found my solution is returning less than 30 which is expected. Therefore, I think you made some mistake in integrating my solution. So, I have made a gist with complete source code and input/output as well which will help you integrate it correctly.
Since you don't want to count number with value -1, there is no use of count[i]-- because you are increasing count only when there is a number not equal to -1.
I have two individuals I need to perform crossover on that are of different lengths.
The individuals may be like this, but could be much longer:
0 1 2 2 1 2 0 [0] 1 2 1 2 0 1 2 [0] 1 2 1 2 0 2 1 [1]
1 2 1 1 0 2 0 [0] 1 2 1 2 0 0 1 [1]
However, I need to keep their original length after crossover. I also need to ensure that every 8th bit (in square brackets) cannot be a 2. The length of each individual will always be multiples of 8.
How can I perform crossover on these individuals without changing the length and structure of either individual?
I haven't been able to find a solution to this so any help would be greatly appreciated.
I assume you're talking about a single-point crossover. You could do something like this:
Select a random number between 1 and the length of the shorter individual. This will be your crossover point.
Cut both individuals at this point.
Swap them, as per regular crossover.
Example:
0 1 2 2 1 2 0 [0] 1 2 1 2 0 1 2 [0] 1 2 1 2 0 2 1 [1]
1 2 1 1 0 2 0 [0] 1 2 1 2 0 0 1 [1]
The shorter individual has length 16, so we generate a random number between 1 and 16 --> e.g., 9
Crossover point:
|
0 1 2 2 1 2 0 [0] 1|2 1 2 0 1 2 [0] 1 2 1 2 0 2 1 [1]
1 2 1 1 0 2 0 [0] 1|2 1 2 0 0 1 [1]
|
Swap sub-sections after the point:
|
0 1 2 2 1 2 0 [0] 1|2 1 2 0 0 1 [1]
1 2 1 1 0 2 0 [0] 1|2 1 2 0 1 2 [0] 1 2 1 2 0 2 1 [1]
|
This preserves the length of both individuals, and preserves the no-2s-in-brackets rule.
I've tried this type of solution:
it works (but different cross-over location:
the code is in python
from random import choices, randint, randrange, random
a = [1,2,4,5,8,11,3,4,7,2,4,6]
b = [3,4,5,6,9,1,3,6,7]
length_a =len(a)
length_b = len(b)
crossover_prob = 0.8
p = randint(1, length_a - 1)
q = randint(1, length_b -1)
while p < crossover_prob:
c = a[0:p] + b[q:]
while q < crossover_prob:
d = b[0:q] + a[p:]
print (c)
print (d)
result:
[1, 3, 4, 5, 6, 9, 1, 3, 6, 7]
[2, 4, 5, 8, 11, 3, 4, 7, 2, 4, 6]
I am writing a program that has to decode QR Codes. The codes are internally represented as a 2D Boolean List. The codes will usually be read as text files containing 1s and 0s, where 1 means a dark module (true) in the matrix, and 0 a light module (false).
I have to implement a method, which will take an InputStream and then has to return a QR Code back. I'll have to read .txt which contains 1s, 0s and comment lines beginning with '#'. I need to make the method ignore those comment lines, but I am not sure how to do it.
Here are some relevant parts of the code:
First the QR Code constructor (All of the methods used in the constructor work and do their job):
public class QRCode {
private List<List<Boolean>> data; //A 2D Boolean matrix, true means black, false means white.
public QRCode(List<List<Boolean>> data)
{
if (data == null)
throw new NullPointerException();
if (QRCode.isQuadratic(data) == false)
throw new InvalidQRCodeException("Matrix must be quadratic!");
if (QRCode.versionCheck(data) < 1 || QRCode.versionCheck(data) > 40)
throw new InvalidQRCodeException("Invalid Dimensions (Version).");
if (QRCode.correctlyAlligned(data) != true)
throw new InvalidQRCodeException("Improper Allignment!");
if (QRCode.correctTimers(data) != true)
throw new InvalidQRCodeException("Incorrect Timing Pattern!");
if (QRCode.correctFormatting(data) != true)
throw new InvalidQRCodeException("Incorrect Formatting!");
this.data = data;
}
}
This is the method I'm referring to. What I wrote sofar at least. Also, if the .txt file contains anything other than 1s, 0s and comment, it should throw an exception.
PS: I've never used InputStreams before, I tried googling this but all answers I found were for specific types of Streams, and they use a .readLine() method, which my IS here is not letting me use.
public static QRCode fromFile(InputStream is) throws IOException
{
int i;
List<List<Boolean>> data = new ArrayList<>(); //a 2D Boolean Matrix
int y = -1, x = -1;
if (is == null)
throw new NullPointerException();
while((i = is.read())!=-1) //Reading begin
{
if (i == (byte) '\n') //If a line in .txt file ends.
{
y++;
data.add(new ArrayList<Boolean>());
x = 0;
}
if ((char) i == '1') //|| (char) i == '0')
{
data.get(y).add(true);
x++;
}
if ((char) i == '0') //||
{
data.get(y).add(false);
x++;
}
}
return new QRCode(data);
}
An example of text files that I'd be handling:
# name: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
1 0 1 1 1 0 1 0 1 0 0 1 0 1 0 1 1 1 1 1 1 1 0 1 1
1 0 1 1 1 0 1 0 1 0 0 0 1 1 1 0 0 1 1 0 0 0 1 1 1
1 0 1 1 1 0 1 0 0 0 1 1 0 1 0 0 1 1 0 1 1 0 0 1 1
1 0 0 0 0 0 1 0 1 0 1 1 1 1 0 0 0 0 1 1 0 1 1 1 1
1 1 1 1 1 1 1 0 1 0 1 1 1 1 0 0 1 0 1 0 0 1 0 0 1
First, I suggest you use a Reader to read text. Then you can use a BufferedReader with its readLine() method to work with lines of text, rather than a stream of bytes.
Regardless, given your current code: once your read a \n, set a boolean flag to indicate that you've just saw a new line character. Then, as you begin reading the next line, switch on that flag such that if it's true, and you see a # as the next character, you should read until you see another \n. If it the flag is not true, then read the rest of the line as you are doing.
You may want to consider whitespace when finding the #, depending how lax you want this to be.
Why not use Files.lines instead of InputStream?
Files.lines(Paths.get("path_to_File.txt")).filter(s -> !s.contains("#"));// let's
//read all lines form the file and skip the lines with `#` symbol
Than we can convert each line to a list of Boolean:
s -> Stream.of(s.split(" ")).map("1"::equals).collect(Collectors.toList())// split
string by ` ` (space) symbol and convert to Boolean "1" - true, "0" - false
Now let's put everything together:
List<List<Boolean>> qr = Files.lines(Paths.get("data/fromFile.txt"))
.filter(s -> !s.contains("#")).map(
s -> Stream.of(s.split(" ")).map("1"::equals)
.collect(Collectors.toList())
).collect(Collectors.toList());
For the file:
# name: small
# type: bool matrix
# rows: 1
# columns: 3
1 1 1
1 0 1
Output will be
[true, true, true]
[true, false, true]
If you decide to use Reader then you may use regex to eleminate the lines which includes any character except '0' and '1'. You can find detail about regex usage below link.
How to check if a string contains only digits in Java
You can modify regex expression for only 1 and 0 like String regex = "[0-1]+";
After you can use below sample code to get each character.
String s1="hello";
char[] ch=s1.toCharArray();
I am working in an Authomata and need three numbers that suumed equal n
For example, if n = 2 the numbers I need are:
200
020
002
110
101
011
It doesn´t matter if the combination are repeated.
If n = 3 I need:
300
030
003
210
201
021
120
012
102
111
So I read thar this was similar to the partition in the theory of numbers but I can get the particular case where only 3 numbers give me the target value (n). (The code is from a example I get here)
package automata2;
import java.util.ArrayList;
/**
*
* #author jaime
*/
public class Automata2 {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
int N = 14;
partition(N);
}
public static void partition(int n) {
partition(n, n, "");
}
public static void partition(int n, int max, String prefix) {
if (n == 0) {
System.out.println(prefix);
return;
}
for (int i = Math.min(max, n); i >= 1; i--) {
partition(n-i, i, prefix + " " + i);
}
}
}
But I only need al the combinations with three digits, not all since 14 until 1 1 1 1 1 1 1 1 1 1 1 1 1 1
The algorithm is like this to generate the numbers. This code will generate all nos.
public class main1 {
public static void main(String args[]) {
int N = 5, n1, n2, n3;
for (n1 = 0; n1 <= N; n1++) {
for (n2 = 0; n2 <= N; n2++) {
for (n3 = 0; n3 <= N; n3++) {
if ( (n1+n2+n3)==N ) {
System.out.println(n1 + " " + n2 + " " + n3);
}
}
}
}
}
}
what you can do is instead of System.out.println(n1+" "+n2+" "+n3), you can store the numbers in an array list then arrange the list.
One solution is to check number of numbers is three and then print it
public static void main(String[] args) {
int N = 14;
partition(N);
}
public static void partition(int n) {
partition(n, n, "");
}
public static void partition(int n, int max, String prefix) {
// added condition here
if (n == 0 && prefix.trim().split(" ").length == 3) {
System.out.println(prefix);
return;
}
for (int i = Math.min(max, n); i >= 1; i--) {
partition(n-i, i, prefix + " " + i);
}
}
Output :
12 1 1
11 2 1
10 3 1
10 2 2
9 4 1
9 3 2
8 5 1
8 4 2
8 3 3
7 6 1
7 5 2
7 4 3
6 6 2
6 5 3
6 4 4
5 5 4
Using nested loops - only for 3 number solution
int N = 14;
for (int i = 0; i <= N; i++) {
for (int j = 0; j <= N; j++) {
for (int k = 0; k <= N; k++) {
if (i + j + k == N) {
System.out.println(i + " " + j + " " + k);
}
}
}
}
Output :
0 0 14
0 1 13
0 2 12
0 3 11
0 4 10
0 5 9
0 6 8
0 7 7
0 8 6
0 9 5
0 10 4
0 11 3
0 12 2
0 13 1
0 14 0
1 0 13
1 1 12
1 2 11
1 3 10
1 4 9
1 5 8
1 6 7
1 7 6
1 8 5
1 9 4
1 10 3
1 11 2
1 12 1
1 13 0
2 0 12
2 1 11
2 2 10
2 3 9
2 4 8
2 5 7
2 6 6
2 7 5
2 8 4
2 9 3
2 10 2
2 11 1
2 12 0
3 0 11
3 1 10
3 2 9
3 3 8
3 4 7
3 5 6
3 6 5
3 7 4
3 8 3
3 9 2
3 10 1
3 11 0
4 0 10
4 1 9
4 2 8
4 3 7
4 4 6
4 5 5
4 6 4
4 7 3
4 8 2
4 9 1
4 10 0
5 0 9
5 1 8
5 2 7
5 3 6
5 4 5
5 5 4
5 6 3
5 7 2
5 8 1
5 9 0
6 0 8
6 1 7
6 2 6
6 3 5
6 4 4
6 5 3
6 6 2
6 7 1
6 8 0
7 0 7
7 1 6
7 2 5
7 3 4
7 4 3
7 5 2
7 6 1
7 7 0
8 0 6
8 1 5
8 2 4
8 3 3
8 4 2
8 5 1
8 6 0
9 0 5
9 1 4
9 2 3
9 3 2
9 4 1
9 5 0
10 0 4
10 1 3
10 2 2
10 3 1
10 4 0
11 0 3
11 1 2
11 2 1
11 3 0
12 0 2
12 1 1
12 2 0
13 0 1
13 1 0
14 0 0
One by one checking with skipping unnecessary partitions.
public static void main(String[] args) {
int n =14;
ArrayList<String> temp = partition(n);
for (int z =0; z<temp.size(); z++){
System.out.println(temp.get(z));
}
}
public static ArrayList<String> partition(int n){
ArrayList<String> out = new ArrayList<String>();
int a=0, b=0, c=0;
for (c=n; c>=0; c--){
for(int j =a; j<=n;j++){
if((c+j)>n){
j=n+1;
continue;
}
for (int i=b;i<=n; i++){
int p=c+j+i;
if (p>n) i=n+1;
if(p==n){
String s = Integer.toString(c)+" "+ Integer.toString(j)+" "+ Integer.toString(i);
out.add(s);
}
}
}
}
return out;
}
Out put:
14 0 0
13 0 1
13 1 0
12 0 2
12 1 1
12 2 0
11 0 3
11 1 2
11 2 1
11 3 0
10 0 4
10 1 3
10 2 2
10 3 1
10 4 0
9 0 5
9 1 4
9 2 3
9 3 2
9 4 1
9 5 0
8 0 6
8 1 5
8 2 4
8 3 3
8 4 2
8 5 1
8 6 0
7 0 7
7 1 6
7 2 5
7 3 4
7 4 3
7 5 2
7 6 1
7 7 0
6 0 8
6 1 7
6 2 6
6 3 5
6 4 4
6 5 3
6 6 2
6 7 1
6 8 0
5 0 9
5 1 8
5 2 7
5 3 6
5 4 5
5 5 4
5 6 3
5 7 2
5 8 1
5 9 0
4 0 10
4 1 9
4 2 8
4 3 7
4 4 6
4 5 5
4 6 4
4 7 3
4 8 2
4 9 1
4 10 0
3 0 11
3 1 10
3 2 9
3 3 8
3 4 7
3 5 6
3 6 5
3 7 4
3 8 3
3 9 2
3 10 1
3 11 0
2 0 12
2 1 11
2 2 10
2 3 9
2 4 8
2 5 7
2 6 6
2 7 5
2 8 4
2 9 3
2 10 2
2 11 1
2 12 0
1 0 13
1 1 12
1 2 11
1 3 10
1 4 9
1 5 8
1 6 7
1 7 6
1 8 5
1 9 4
1 10 3
1 11 2
1 12 1
1 13 0
0 0 14
0 1 13
0 2 12
0 3 11
0 4 10
0 5 9
0 6 8
0 7 7
0 8 6
0 9 5
0 10 4
0 11 3
0 12 2
0 13 1
0 14 0
import java.util.Scanner;
public class ExampleMinNumber {
public static void main(String args[])
{
System.out.println("enter number which comibnation of sum you want");
Scanner input=new Scanner(System.in);
int num=input.nextInt();
for(int i=0;i<=3;i++)
{
int sum=0;
for(int j=0;j<=3;j++)
{
for(int k=0;k<=3;k++)
{
sum=i+j+k;
if(sum==num)
{
System.out.println(+i+""+j+""+k);
}
}
}
}
input.close();
}
}