Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
Can you help me for my assignment. I don't know how to execute this kind of outputs?
1
2 3
4 5 6
7 8 9 10
and
1 2 3 4
5 6 7
8 9
10
and lastly
1 2 3 4 5 4 3 2 1
1 2 3 4 4 3 2 1
1 2 3 3 2 1
1 2 * 2 1
1 * * * 1
1 2 * 2 1
1 2 3 3 2 1
1 2 3 4 4 3 2 1
1 2 3 4 5 4 3 2 1
Thank you!
- 1st year programming student.
i think you could do something like
"var i = 1"
"var spaces = 1"
"var counter = 1"
"length = 1"
while(i < 11)
{
for(spaces <= counter)
{
print i
i++
spaces ++
}
print (pass line)
spaces = 0
counter ++
}
this way you will get to print the var "i" until it gets to 11
and with the counter and the spaces var you will pass a line at 1,2,3.... numbers respectivily.
but since its your homework and you didnt specified in which language do you want the code i will let that to you. also dont ask us to do your code in the future.
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I'm fairly new to using streams, and I wanted to try parsing a config file with them.
I have a file setup like so...
[Category 1]
1 2 3 4
5 6 7 8
[Category 2]
9 8 7 6
5 4 3 2
How would I go about getting all the lines from one of these categories using Streams? Is there a clean way to do it using Streams?
You could combine dropWhile and takeWhile methods introduced in Java 9. If you are searching for lets say [Category 2] drop all lines before it, and continue processing all lines until you find empty line (or until stream will end but that case is covered automatically).
Demo (for more info about split("\\R") see https://stackoverflow.com/a/31060125):
String data =
"[Category 1]\n" +
"1 2 3 4\n" +
"5 6 7 8\n" +
"\n" +
"[Category 2]\n" +
"9 8 7 6\n" +
"5 4 3 2\n" +
"\n" +
"[Category 3]\n" +
"1 3 7 6\n" +
"4 4 3 2\n";
Stream.of(data.split("\\R")) // form Java 8 regex engine supports \R as line separator
.dropWhile(line->!line.equals("[Category 2]"))
.takeWhile(Predicate.not(String::isEmpty))
.forEach(System.out::println);
Output:
[Category 2]
9 8 7 6
5 4 3 2
Write code that will print the following:
0 1 2 3 4 5 6 7 8 9(x10)
This is what i currently have:
for (int m=0;m<10;m++){
for (int j=1;j<=10;j++){
System.out.print(j);
}
}
You need to add carriage return
for (int m=0;m<10;m++)
for (int j=1;j<=10;j++)
System.out.print(j + " ");
System.out.println();
You can just add a space.
System.out.print(j+" ");
As an alternative, you can do as "one liner" by misusing the Java 8 streams API.
closed range of integers, join using space
pass into a stream generator, limit to 10 repeats, join with new line
output
Code
System.out.println(Stream.generate(() -> IntStream.rangeClosed(1, 10)
.mapToObj(Integer::toString).limit(10)
.collect(Collectors.joining(" ")))
.limit(10).collect(Collectors.joining("\n")));
Output
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I have created 2 ArrayLists of the same size (5) and i want to print out their contents side by side in columns.
For example:
list 1 = [1 , 2 , 3 , 4 , 5]
list 2 = [5 , 4 , 3 , 2 , 1]
I want to print it out to the console like this:
1 5
2 4
3 3
4 2
5 1
I have tried using a for loop within another for loop but I think I may be overthinking it.
Since, you need the lists to print side by side nested loops are not required.
for (int i = 0 ; i < list1.size(); i++) {
System.out.printf("%d\t%d\n", list1.get(i), list2.get(i));
}
Notice, the loop assumes that the two lists are of the same size. So, we need only one loop counter.
Output :
1 5
2 4
3 3
4 2
5 1
I am programming a 2d game, and I was wondering what the best data structure would be to store all of the hexagonal tiles in a map class? This is in java.
Just use a 2D array, but have the "rows" and "columns" at a 60 or 120 degree angle to each other.
My quick ASCII illustration:
0 1 2 3 4 5 6
1 2 3 4 5 6
1 2 3 4 5 6 7
2 3 4 5 6 7
2 3 4 5 6 7 8
3 4 5 6 7 8
3 4 5 6 7 8 9
(the numbers are row numbers, the column numbers are obvious...)
Effectively, you want to "stripe" the rows across the columns.
Just simple 2D array is good idea, you have all the info you need there :
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
I have the following list of lists List<List<Integer>>
1 2
3
4 5 6
7
8
9
10
I have to group randomly this list of lists as 3 subsets with 4 elements each (nevermind that in the last subset i'll have less than 4 elements), and in every subset i cant have elements from the same row ( i cant have 3 4 5 7 for example). What should be the basic algorithm????
What should be the basic algorithm????
Pluck four random elements from each of the first four1 randomly chosen non-empty lists in the list of lists. Remove the chosen items, and repeat for the next subset. Remove empty lists as you go.
Next, you need to note that not all of your inputs are solvable:
1
2
3
4
5 6 7 8 9
In this case, we choose randomly
1*
2*
3*
4
5 6 7 8 9*
-> 7 1 2 3 (chosen randomly)
[]
[]
[]
4*
5 6 8 9*
-> 5 4 . . <- uh, impossible to choose four items from different lists even though non-empty lists remain. You can detect this during iteration.
1: Or, choose randomly from all of the lists if less than four non-empty lists remain.
The easiest answer is to perform a random selection, check whether your "no two from the same" rule is violated. If it is, then throw away and try again.
Sort the lists by number of elements.
Pick 4 elements, one from each of the 4 lists with the most elements.
Resort.
Repeat from 2.
A heap could help here.
Why do we need to pick from the lists with the most elements?
Consider this case:
1 2
3 4
5 6
7
8
If we pick both 7 and 8, we'll be left with something like:
1 2
3
5
From here there's no possible way to pick 4 elements from different lists.
However, if we pick elements from the lists with the most elements, we'd be left with something like:
1
3
5
8
From here we can just pick one element from each list.