It's possible to use for loop with double indices in Java, e.g:
for (int j = 0, k = myArray.length - 1; j < myArray.length; j++, k--)
To iterate myArray elements from the first element using j and from the last element using k simultaneously without using inner loop. How can we do this in Kotlin without using inner loop.
Kotlin doesn't provide a way to permit what you attempt to do.
Because I imagine your case is purely for exemple, I purpose you two solutions:
1. The good old way
The most optimized for the exemple you give.
for (i in a.indices) {
val j = a.size - 1 - i
println("($i, $j)")
}
(0, 3)(1, 2)(2, 1)(3, 0)
See code snippet on play.kotlinlang.org
2. The .zip() way
Can be usefull on some contexts, the zip method combine two lists of the same size on one list of Tuples which can be used after directly.
val indices = a.indices;
for (i in indices.zip(indices.reversed())) {
println(i)
}
(0, 3)(1, 2)(2, 1)(3, 0)
See code snippet on play.kotlinlang.org
You don't need an inner loop. Just create an additional variable inside for loop.
for(j in myArray.indices) {
val k = myArray.size - 1 - j
// Use j and k here
}
Or alternatively,
var k = myArray.size - 1
for(j in myArray.indices) {
// Use j and k here
k--
}
generateSequence() is powerful alternative for complex loops. Not so fast as pure for/while loop but very flexible and easy.
generateSequence(
0 to myArray.size - 1 // initial values
) {
it.first + 1 to it.second - 1 // new values after each iteration
}.takeWhile {
it.first < myArray.size // limit
}.forEach { (j, k) ->
println("$j $k")
}
And yes, it's good idea to not iterate second variable but calculate it from first (if applicable), as suggested in other answers.
Related
Since there is no traditional loop in Kotlin (as per this article), how would you write a for loop in Kotlin that loops n times, or specifically, zero times if n is zero?
The equivalent in Java would be
int n = 0;
for (int index = 0; index < n; index++) {
// do this n times...
}
You could use repeat for this instead of a for loop or a range...
val n = 5
repeat(n) {
// ...
}
If you need to use the counter, you can either refer to it with the default it or rename it:
repeat(n) { i ->
// Do something with i
}
Use ranges and progressions
val n = 5
for (a in 0 until n) {
//Do something
}
Is there a short/idiomatic way to iterate through every pair of elements in a collection?
Even better would be a method that iterates through all fixed-cardinality subsets of a collection.
The classic and ugly approach would be:
val s = setOf(1, 2, 3, 4)
for (i in s) {
for (j in s) {
if (i != j) {
println("$i $j")
}
}
}
For having bigger subsets, more loops are necessary, so this isn't scalable.
This is technically also O(n^2) but will have a little less than half as many iterations: (n^2 - n) / 2
val l = s.toList()
for (i in s.indices) {
for (j in i + 1 until s.size) {
println("${l[i]}, ${l[j]}")
println("${l[j]}, ${l[i]}")
}
}
I think you got already the most idiomatic way to solve it. If you want to do it more functional, this is what you can convert it to:
s.map { i -> s.map { i to it } }
.flatten()
.filter { (left, right) -> left != right }
.onEach { (i, j) -> println("$i $j") }
If you find an implementation of the power set operation, you can take all elements of the power set with length = 2.
I have a quick question how can I loop over an NSMutable array starting from a certain index.
For Example I have these double loops I want k to start from the same index as l.
for (Line *l in L)
{
for (Line *k in L)
{
............
}
}
To elaborate further, lets say L has 10 object so l start from 0-10 and k from 0 -10. What I want is if l is equal 1 k should start from 1-10 rather than 0 - 10 and when l is equal 2 k should start from 2- 10 rather than 0. Any help is Appreciated
Objective-C is an extension of C, lookup the C for loop and you'll have your answer. HTH
Addendum
I was going to let you benefit from the learning experience of looking up the C for yourself, however at the time of writing all other answers since added give the code but it is not complete, so here is what you need to produce the l and k values in the order you wish:
for(NSInteger lIndex = 0; lIndex < L.count; lIndex++)
{
Line *l = L[lIndex]; // index into your array to get the element
for(NSInteger kIndex = lIndex; kIndex < L.count; kIndex++)
{
Line *k = L[kIndex];
// process your l and k
}
}
As you can see the for has three sub-parts which are the initialisation, condition, and increment. The initialisation is performed first, then the condition to determine whether to execute the for body, and the increment is executed after the statements in the body and before the condition is tested to determine if another iteration should be performed. A for loop is roughly (there are some differences that are unimportant here) to the while loop:
initialisation;
while(condition)
{
body statements;
increment;
}
You simply need to modify for-statement.
NSInteger indexYouNeed;
NSInteger iterationCount;
for (int i = indexYouNeed; i < iterationCount; i++) {
/* Your code here */
}
You may find this link helpfulll.
You have to use an indexed (ordinary) for loop instead of fast enumeration (for-in):
int l;
for (l=startValue; l<=endValue; l++)
{
int i;
for (int i=l; i<=endValue; i++)
{
…
}
}
So i've tried interpreting this pseudocode a friend made and i wasn't exactly sure that my method returns the right result. Anyone who's able to help me out?
I've done some test cases where e.g. an array of [2,0,7] or [0,1,4] or [0, 8, 0] would return true, but not cases like: [1,7,7] or [2,6,0].
Array(list, d)
for j = 0 to d−1 do
for i = 0 to d−1 do
for k = 0 to d−1 do
if list[j] + list[ i] + list[k] = 0 then
return true
end if
end for
end for
end for
return false
And i've made this in java:
public class One{
public static boolean method1(ArrayList<String> A, int a){
for(int i = 0; i < a-1; i++){
for(int j = 0; j < a-1; j++){
for(int k = 0; k < a-1; k++){
if(Integer.parseInt(A.get(i)+A.get(j)+A.get(k)) == 0){
return true;
}
}
}
}
return false;
}
}
Thanks in advance
For a fix to your concrete problem, see my comment. A nicer way to write that code would be to actually use a list of Integer instead of String, because you will then want to convert the strings back to integers. So, your method looks better like this:
public static boolean method(List<Integer> A) {
for (Integer i : A)
for (Integer j : A)
for (Integer k : A)
if (i + j + k == 0)
return true;
return false;
}
See that you don't even need the size as parameter, since any List in Java embeds its own size.
Somehow offtopic
You're probably trying to solve the following problem: "Find if a list of integers contains 3 different ones that sum up to 0". The solution to this problem doesn't have to be O(n^3), like yours, it can be solved in O(n^2). See this post.
Ok, so here is what I believe the pseudo code is trying to do. It returns true if there is a zero in your list or if there are three numbers that add up to zero in your list. So it should return true for following test cases. (0,1,2,3,4,5), (1,2,3,4,-3). It will return false for (1,2,3,4,5). I just used d=5 as a random example. Your code is good for the most part - you just need to add the ith, jth and kth elements in the list to check if their sum equals zero for the true condition.
I want to make a cave explorer game in game maker 8.0.
I've made a block object and an generator But I'm stuck. Here is my code for the generator
var r;
r = random_range(0, 1);
repeat(room_width/16) {
repeat(room_height/16) {
if (r == 1) {
instance_create(x, y, obj_block)
}
y += 16;
}
x += 16;
}
now i always get a blank frame
You need to use irandom(1) so you get an integer. You also should put it inside the loop so it generates a new value each time.
In the second statement, you are generating a random real value and storing it in r. What you actually require is choosing one of the two values. I recommend that you use the function choose(...) for this. Here goes the corrected statement:
r = choose(0,1); //Choose either 0 or 1 and store it in r
Also, move the above statement to the inner loop. (Because you want to decide whether you want to place a block at the said (x,y) location at every spot, right?)
Also, I recommend that you substitute sprite_width and sprite_height instead of using the value 16 directly, so that any changes you make to the sprite will adjust the resulting layout of the blocks accordingly.
Here is the code with corrections:
var r;
repeat(room_width/sprite_width) {
repeat(room_height/sprite_height) {
r = choose(0, 1);
if (r == 1)
instance_create(x, y, obj_block);
y += sprite_height;
}
x += sprite_width;
}
That should work. I hope that helps!
Looks like you are only creating a instance if r==1. Shouldn't you create a instance every time?
Variable assignment r = random_range(0, 1); is outside the loop. Therefore performed only once before starting the loop.
random_range(0, 1) returns a random real number between 0 and 1 (not integer!). But you have if (r == 1) - the probability of getting 1 is a very small.
as example:
repeat(room_width/16) {
repeat(room_height/16) {
if (irandom(1)) {
instance_create(x, y, obj_block)
}
y += 16;
}
x += 16;
}
Here's a possible, maybe even better solution:
length = room_width/16;
height = room_height/16;
for(xx = 0; xx < length; xx+=1)
{
for(yy = 0; yy < height; yy+=1)
{
if choose(0, 1) = 1 {
instance_create(xx*16, yy*16, obj_block); }
}
}
if you want random caves, you should probably delete random sections of those blocks,
not just single ones.
For bonus points, you could use a seed value for the random cave generation. You can also have a pathway random generation that will have a guaranteed path to the finish with random openings and fake paths that generate randomly from that path. Then you can fill in the extra spaces with other random pieces.
But in regards to your code, you must redefine the random number each time you are placing a block, which is why all of them are the same. It should be called inside of the loops, and should be an integer instead of a decimal value.
Problem is on the first line, you need to put r = something in the for cycle