I've got a problem with finding all possible combinations of paths in java for an input like this:
Every number has list of sets of dependencies, for example number 2 depends on both 11 and 12 or there is a second possibility: both 10 and 12. Below are other examples of dependencies.
2=[[11, 12] OR [10, 12]]
1=[[9, 2, 8, 7] OR [9, 3, 8, 7] OR [2, 6, 8, 7] OR [3, 8, 7, 6]]
6=[[4] OR [5] OR [13, 10]]
22 = [[21, 13] OR [8]]
21= [23],
11 = []
12 = []
10 = []
13 = []
9 = []
8 = []
[...]
For every number we want an output which looks like this for 22:
22: [[21,13,23], [8]],
For 1:
1: [[9, 2, 11, 12, 8, 7],
[9, 2, 8, 7, 10, 12],
[9, 3, 8, 7],
[2, 6, 8, 7, 11, 12, 4],
[2, 6, 8, 7, 10, 12, 4],
[2, 6, 8, 7, 11, 12, 5],
[2, 6, 8, 7, 11, 12, 13, 10],
[2, 6, 8, 7, 10, 12, 5],
[2, 6, 8, 7, 10, 12, 13, 10],]
[3, 8, 7, 4]
[3, 8, 7, 5]
[3, 8, 7, 13, 10]]
And so on...
I tried to rewrite data to a tree and find all possible paths and second solution with recursion but I stuck.
I will be grateful for your help.
Related
I want to split a list of numbers into multiples of 2, 3, 5 and store the result in a Map. Sample output will be like :
2 -> 2, 6, 10, 18 etc.
3 -> 3, 6, 9 etc.
5 -> 5, 10 etc.
I tried different ways of grouping (by using Collectors.groupingBy()) etc. in the collect() below and got compile errors each time. How do I do this ?
public void groupingByDemo() {
//Split a list of numbers into multiples of 2, 3, 5. Store the result in a Map<Integer, Set>.
List<Integer> multipliers = Arrays.asList(2, 3, 5);
List<Integer> nums = Arrays.asList(1, 2, 4, 6, 7, 8, 9, 10, 11, 16, 17, 25, 27);
Map<Integer, Set> multiples = multipliers
.stream().collect(
//What to put here ? or should I use something else instead of collect() ?
);
}
Here is one way to do it.
List<Integer> multipliers = Arrays.asList(2, 3, 5);
List<Integer> nums = Arrays.asList(1, 2, 4, 6, 7, 8, 9, 10, 11, 16, 17, 25, 27);
Map<Integer, Set<Integer>> map = multipliers.stream()
.collect(Collectors.toMap(m -> m,
m-> nums.stream()
.filter(n -> n % m == 0)
.collect(Collectors.toSet())));
map.forEach((m, n)-> System.out.println(m + " -> " + n));
Here is the output.
2 -> [16, 2, 4, 6, 8, 10]
3 -> [6, 9, 27]
5 -> [25, 10]
Why am i getting NoSuchElementException?
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
class GFG {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
sc.nextLine();
for (int k = 0; k < t; k++) {
int n = sc.nextInt();
int xn = sc.nextInt();
int yn = sc.nextInt();
sc.nextLine();
ArrayList<Integer> l1 = new ArrayList<Integer>(2000);
ArrayList<Integer> l2 = new ArrayList<Integer>(2000);
int a[] = new int[2000];
int b[] = new int[2000];
for (int i = 0; i < n; i++) {
a[i] = sc.nextInt();
l1.add(a[i]);
}
sc.nextLine();
for (int i = 0; i < n; i++) {
b[i] = sc.nextInt();
l2.add(b[i]);
}
int tip = 0;
while ((xn > 0) && (yn > 0)&& (!l2.isEmpty())&& (!l1.isEmpty())) {
int pi = l1.indexOf(Collections.max(l1));
if (l1.get(pi) >= l2.get(pi)) {
tip += l1.get(pi);
xn--;
} else {
tip += l2.get(pi);
yn--;
}
l2.remove(pi);
l1.remove(pi);
}
if (yn > 0) {
while ((yn > 0) && (!l2.isEmpty())) {
tip += Collections.max(l2);
l2.remove(l2.indexOf(Collections.max(l2)));
yn--;
}
} else {
while ((xn > 0) && (!l1.isEmpty())) {
tip += Collections.max(l1);
l1.remove(l1.indexOf(Collections.max(l1)));
xn--;
}
}
System.out.println(tip);
}
}
}
Output:
Runtime Error: Runtime ErrorException in thread "main"
java.util.NoSuchElementException at
java.util.Scanner.throwFor(Scanner.java:862) at
java.util.Scanner.next(Scanner.java:1485) at
java.util.Scanner.nextInt(Scanner.java:2117) at
java.util.Scanner.nextInt(Scanner.java:2076) at
GFG.main(File.java:22)
I added some logs here:
int tip = 0;
while ((xn > 0) && (yn > 0)) {
System.out.println("l1:"+l1);
System.out.println("Collections.max(l1):"+Collections.max(l1));
int pi = l1.indexOf(Collections.max(l1));
Entring 1 15 9 9 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
I get a different error.
It prints out:
l1:[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
Collections.max(l1):15
l1:[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
Collections.max(l1):14
l1:[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
Collections.max(l1):13
l1:[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
Collections.max(l1):12
l1:[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
Collections.max(l1):11
l1:[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Collections.max(l1):10
l1:[1, 2, 3, 4, 5, 6, 7, 8, 9]
Collections.max(l1):9
l1:[1, 2, 3, 4, 5, 6, 7, 8]
Collections.max(l1):8
l1:[1, 2, 3, 4, 5, 6, 7]
Collections.max(l1):7
l1:[1, 2, 3, 4, 5, 6]
Collections.max(l1):6
l1:[1, 2, 3, 4, 5]
Collections.max(l1):5
l1:[1, 2, 3, 4]
Collections.max(l1):4
l1:[1, 2, 3]
Collections.max(l1):3
l1:[1, 2]
Collections.max(l1):2
l1:[1]
Collections.max(l1):1
l1:[]
Exception in thread "main" java.util.NoSuchElementException
at java.util.ArrayList$Itr.next(ArrayList.java:854)
at java.util.Collections.max(Collections.java:669)
at GFG.main(GFG.java:31)
The problem is that you are calling Collections.max on en empty list.
What will be the result of the following block of code?
ArrayList<Integer> myList = new ArrayList<Integer>();
for (int i = 0; i < myList.size(); i++) {
myList.remove(i);
}
The answer is provided as the loop will never run.
But for an input of [-1, 3, 28, 17, 9, 33],
I got [3, 17, 33] as output. What does it mean?
The condition is evaluated on each iteration.
At the beginning the list contains
[-1, 3, 28, 17, 9, 33]
and the loop begins:
i = 0; 0 < 6 is true, current list [-1, 3, 28, 17, 9, 33], element with index 0 (bolded) removed, remaining [3, 28, 17, 9, 33]
i = 1; 1 < 5 is true, current list [3, 28, 17, 9, 33], element with index 1 (bolded) removed, remaining [3, 17, 9, 33]
i = 2; 2 < 4 is true, current list [3, 17, 9, 33], element with index 2 (bolded) removed, remaining [3, 17, 33]
i = 3; 3 < 3 is false, loop ends, remains [3, 17, 33]
Wrong! The loop will run as long as the conditions are still satisfied. Every iteration will produce a new scenario and the conditions just bend around to suit the new values.
Using your example [-1, 3, 28, 17, 9, 33]:
First iteration: i is 0... 0 is less than 6.. element at index 0 (-1) is hence removed
Resultant arrraylist: [3, 28, 17, 9, 33]
Second iteration: i is 1... 1 is less than 5.. element at index 1 (28) is hence removed
Resultant arrraylist: [3, 17, 9, 33]
Third iteration: i is 2... 2 is less than 4.. element at index 2 (9) is hence removed
Resultant arrraylist: [3, 17, 33]
Fourth iteration: i is 3... 3 is not less than 3.. Iteration stops.
After the loop operation, your MyList arraylist will be reduced to:
[3, 17, 33]
I hope this helps, merry coding!
I'm trying to store a 2D array in a text file with BufferedWriter and I would also like to retrieve a 2D array from a text file and display in its original array format with BufferedReader. I have little experience with both methods.
The desired outcome to be saved in a txt file is:
1 5 7 8 2 3 9 6 4
4 8 3 7 6 9 2 1 5
6 2 9 5 1 4 7 3 8
5 3 1 9 4 2 6 8 7
2 7 4 3 8 6 5 9 1
8 9 6 1 7 5 3 4 2
9 4 8 2 3 7 1 5 6
3 6 2 4 5 1 8 7 9
7 1 5 6 9 8 4 2 3
BufferedWriter:
// Instantiate a Date object
static Date date = new Date();
static int[][] board = {{0, 0, 5, 9, 7, 1, 8, 4, 6},
{0, 7, 1, 2, 8, 6, 9, 3, 5},
{0, 9, 6, 4, 3, 5, 2, 7, 1},
{0, 6, 8, 7, 4, 9, 5, 2, 3},
{0, 4, 9, 5, 2, 3, 1, 6, 8},
{0, 5, 2, 1, 6, 8, 4, 9, 7},
{0, 2, 4, 8, 1, 7, 3, 5, 9},
{0, 1, 3, 6, 5, 2, 7, 8, 4},
{0, 8, 7, 3, 9, 4, 6, 1, 2}};
try {
File file = new File("/c:/sudoku" + date + ".txt");
if (!file.exists()) {
file.createNewFile();
}
FileWriter fw = new FileWriter(file.getAbsoluteFile());
try (BufferedWriter bw = new BufferedWriter(fw)) {
bw.write(board, 0, board.length());
}
System.out.println("Your Game was saved with success !");
} catch (IOException e) {
}
I suggest (as Bob did) to store it in csv format (comma separated values)
Date date = new Date();
int[][] board = {{0, 0, 5, 9, 7, 1, 8, 4, 6},
{0, 7, 1, 2, 8, 6, 9, 3, 5},
{0, 9, 6, 4, 3, 5, 2, 7, 1},
{0, 6, 8, 7, 4, 9, 5, 2, 3},
{0, 4, 9, 5, 2, 3, 1, 6, 8},
{0, 5, 2, 1, 6, 8, 4, 9, 7},
{0, 2, 4, 8, 1, 7, 3, 5, 9},
{0, 1, 3, 6, 5, 2, 7, 8, 4},
{0, 8, 7, 3, 9, 4, 6, 1, 2}};
StringBuilder builder = new StringBuilder();
for(int i = 0; i < board.length; i++)//for each row
{
for(int j = 0; j < board.length; j++)//for each column
{
builder.append(board[i][j]+"");//append to the output string
if(j < board.length - 1)//if this is not the last row element
builder.append(",");//then add comma (if you don't like commas you can use spaces)
}
builder.append("\n");//append new line at the end of the row
}
BufferedWriter writer = new BufferedWriter(new FileWriter("/c:/sudoku" + date + ".txt"));
writer.write(builder.toString());//save the string representation of the board
writer.close();
Hope it's all clear
EDIT:
Here is how to read back your board:
String savedGameFile = /*...*/;
int[][] board = new int[9][9];
BufferedReader reader = new BufferedReader(new FileReader(savedGameFile));
String line = "";
int row = 0;
while((line = reader.readLine()) != null)
{
String[] cols = line.split(","); //note that if you have used space as separator you have to split on " "
int col = 0;
for(String c : cols)
{
board[row][col] = Integer.parseInt(c);
col++;
}
row++;
}
reader.close();
I am trying to take the first and last number in each array and find the sum of the 2 and print it off in console. Some guidance? Not exactly sure how to do it thanks! This is what I have so far it just prints off the arrays.
public class Permutations {
public static void main(String args[]) {
List<Integer> permutation = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
for (int i = 0; i < 10; i++) {
Collections.shuffle(permutation);
}
System.out.println("List 1 " + permutation);
System.out.println("List 2 " + permutation);
System.out.println("List 3 " + permutation);
System.out.println("List 4 " + permutation);
System.out.println("List 5 " + permutation);
System.out.println("List 6 " + permutation);
System.out.println("List 7 " + permutation);
System.out.println("List 8 " + permutation);
System.out.println("List 9 " + permutation);
System.out.println("List 10 " + permutation);
}
}
//print off
List 1 [2, 3, 6, 10, 1, 4, 7, 9, 8, 5]
List 2 [2, 3, 6, 10, 1, 4, 7, 9, 8, 5]
List 3 [2, 3, 6, 10, 1, 4, 7, 9, 8, 5]
List 4 [2, 3, 6, 10, 1, 4, 7, 9, 8, 5]
List 5 [2, 3, 6, 10, 1, 4, 7, 9, 8, 5]
List 6 [2, 3, 6, 10, 1, 4, 7, 9, 8, 5]
List 7 [2, 3, 6, 10, 1, 4, 7, 9, 8, 5]
List 8 [2, 3, 6, 10, 1, 4, 7, 9, 8, 5]
List 9 [2, 3, 6, 10, 1, 4, 7, 9, 8, 5]
List 10 [2, 3, 6, 10, 1, 4, 7, 9, 8, 5]
Have you considered using List.get
Then you could simply do
for (int i = 0; i < 10; i++) {
Collections.shuffle(permutation);
System.out.println("List " + (i + 1) + " " + permutation.get(0) + " " +
permutation.get(9) + " sum is " + (permutation.get(0) + permutation.get(9)));
}
of course better to use length of Array rather then 9
Here's a different solution for summing certain elements for your interest:
IntStream.of(0, 9).map(permutation::get).sum();
The potential advantage of this is that it's easy to expand the items you want to sum or to filter or map them without changing the structure of the solution.