How to write complex Kotlin for loop - kotlin
It's easy to write simple loops like for (i in 0..10), but how to write more complex loops like:
for (byte i = 1 << 7; i != 0; i >>= 1)
or
for (byte i = 0x01; i != 0; i <<= 1)
Thanks for your help.
This is not what a for loop is for in Kotlin. You can use a normal while statement instead.
var i: Int = 1 shl 7
while (i != 0) {
// . . .
i = i shr 1
}
In case you don't like having a var and are ok with using a Sequence, you could also use something like generateSequence instead, e.g.:
generateSequence(1 shl 7) {
it shr 1
}
.takeWhile { it != 0 }
.forEach { ... }
// or: generateSequence(1 shl 7) { (it shr 1).takeIf { it != 0 } }.forEach { ... }
Otherwise Michaels answer about using while is perfectly fine.
Related
Variable interator in for cycle
This is my example for (i in array.indices) { if (array[i] == 10) { i -= 2//have error here } } How can i make 'i' variable mutable?
You can't. Use while loop instead: var i = 0 while (i < array.size) { if (array[i] == 10) { i -= 2 } ... i++ }
Use of uninitialized value of type Any in numeric context raku
I've come across a programming question at reddit (Take a look at the link for the question) This was one the solutions in Python: s="112213" k=2 result=0 for i in range(len(s)): num_seen = 0 window = {} for ind in range(i, len(s)): if not s[ind] in window: num_seen += 1 window[s[ind]] = 1 else: window[s[ind]] += 1 if window[s[ind]] == k: num_seen -= 1 if num_seen == 0: result +=1 elif window[s[ind]] > k: break print(result) I've tried to port this solution into Raku and here is my code: my #s=<1 1 2 2 1 3>; my $k=2; my $res=0; for ^#s { my $seen = 0; my %window; for #s[$_..*] { if $^a == %window.keys.none { $seen++; %window{$^a} = 1;} else { %window{$^a} += 1;} if %window{$^a} == $k { $seen--; if $seen == 0 { $res++;} } elsif %window{$^a} > $k { last;}}} say $res; It gives this error: Use of an uninitialized value of type Any in a numeric context in a block at ... line 13 How to fix it?
I don't feel that's a MRE. There are too many issues with it for me to get in to. What I did instead is start from the original Python and translated that. I'll add some comments: my \s="112213" .comb; # .comb to simulate Python string[n] indexing. my \k=2; my $result=0; # result is mutated so give it a sigil for ^s -> \i { # don't use $^foo vars with for loops my $num_seen = 0; my \window = {} for i..s-1 -> \ind { if s[ind] == window.keys.none { # usefully indent code! $num_seen += 1; window{s[ind]} = 1 } else { window{s[ind]} += 1 } if window{s[ind]} == k { $num_seen -= 1; if $num_seen == 0 { $result +=1 } } elsif window{s[ind]} > k { last } } } print($result) displays 4. I'm not saying that's a good solution in Raku. It's just a relatively mechanical translation. Hopefully it's helpful.
As usual, the answer by #raiph is correct. I just want to do the minimal changes to your program that get it right. In this case, it's simply adding indices to both loops to make stuff clearer. You were using the context variable $_ in the first, and $^a in the second (inner), and it was getting unnecesarily confusing. my #s=<1 1 2 2 1 3>; my $k=2; my $res=0; for ^#s -> $i { my $seen = 0; my %window; for #s[$i..*] -> $c { if $c == %window.keys.none { $seen++; %window{$c} = 1; } else { %window{$c} += 1; } if %window{$c} == $k { $seen--; if $seen == 0 { $res++; } } elsif %window{$c} > $k { last; } } } say $res; As you see , besides trying to indent everything a bit more properly, the only additional thing is to add -> $i and -> $c so that loops are indexed, and then use them where you were using implicit variables.
Kotlin decomposing numbers into powers of 2
Hi I am writing an app in kotlin and need to decompose a number into powers of 2. I have already done this in c#, PHP and swift but kotlin works differently somehow. having researched this I believe it is something to do with the numbers in my code going negative somewhere and that the solution lies in declaring one or more of the variable as "Long" to prevent this from happening but i have not been able to figure out how to do this. here is my code: var salads = StringBuilder() var value = 127 var j=0 while (j < 256) { var mask = 1 shl j if(value != 0 && mask != 0) { salads.append(mask) salads.append(",") } j += 1 } // salads = (salads.dropLast()) // removes the final "," println("Salads = $salads") This shoud output the following: 1,2,4,8,16,32,64 What I actually get is: 1,2,4,8,16,32,64,128,256,512,1024,2048,4096,8192,16384,32768,65536,131072,262144,524288,1048576,2097152,4194304,8388608,16777216,33554432,67108864,134217728,268435456,536870912,1073741824,-2147483648,1,2,4,8,16,32,64,128,256,512,1024,2048,4096,8192,16384,32768,65536,131072,262144,524288,1048576,2097152,4194304,8388608,16777216,33554432,67108864,134217728,268435456,536870912,1073741824,-2147483648,1,2,4,8,16,32,64,128,256,512,1024,2048,4096,8192,16384,32768,65536,131072,262144,524288,1048576,2097152,4194304,8388608,16777216,33554432,67108864,134217728,268435456,536870912,1073741824,-2147483648,1,2,4,8,16,32,64,128,256,512,1024,2048,4096,8192,16384,32768,65536,131072,262144,524288,1048576,2097152,4194304,8388608,16777216,33554432,67108864,134217728,268435456,536870912,1073741824,-2147483648,1,2,4,8,16,32,64,128,256,512,1024,2048,4096,8192,16384,32768,65536,131072,262144,524288,1048576,2097152,4194304,8388608,16777216,33554432,67108864,134217728,268435456,536870912,1073741824,-2147483648,1,2,4,8,16,32,64,128,256,512,1024,2048,4096,8192,16384,32768,65536,131072,262144,524288,1048576,2097152,4194304,8388608,16777216,33554432,67108864,134217728,268435456,536870912,1073741824,-2147483648,1,2,4,8,16,32,64,128,256,512,1024,2048,4096,8192,16384,32768,65536,131072,262144,524288,1048576,2097152,4194304,8388608,16777216,33554432,67108864,134217728,268435456,536870912,1073741824,-2147483648,1,2,4,8,16,32,64,128,256,512,1024,2048,4096,8192,16384,32768,65536,131072,262144,524288,1048576,2097152,4194304,8388608,16777216,33554432,67108864,134217728,268435456,536870912,1073741824,-2147483648, Any ideas?
This works for the one input that you specified, at the very least: fun powersOfTwo(value :Long): String { val result = ArrayList<String>() var i = 0 var lastMask = 0 while (lastMask < value) { val mask = 1 shl i if (value != 0.toLong() && mask < value) { result.add(mask.toString()) } lastMask = mask i += 1 } return result.joinToString(",") } Ran it in a unit test: #Test fun addition_isCorrect() { val result = powersOfTwo(127) assertEquals("1,2,4,8,16,32,64", result) } Test passed.
You can get a list of all powers of two that fit in Int and test each of them for whether the value contains it with the infix function and: val value = 126 val powersOfTwo = (0 until Int.SIZE_BITS).map { n -> 1 shl n } println(powersOfTwo.filter { p -> value and p != 0}.joinToString(",")) // prints: 2,4,8,16,32,64 See the entire code in Kotlin playground: https://pl.kotl.in/f4CZtmCyI
Hi I finally managed to get this working properly: fun decomposeByTwo(value :Int): String { val result = ArrayList<String>() var value = value var j = 0 while (j < 256) { var mask = 1 shl j if ((value and mask) != 0) { value -= mask result.add(mask.toString()) } j += 1 } return result.toString() } I hope this helps someone trying to get a handle on bitwise options!
Somehow you want to do the "bitwise AND" of "value" and "mask" to determine if the j-th bit of "value" is set. I think you just forgot that test in your kotlin implementation.
How to make multiconditional for loop in kotlin
In Java: for(int j = 0; j < 6 && j < ((int)abc[j] & 0xff); j++) { // ... } How we can make this loop in Kotlin?
I'd suggest to use a more functional approach like (0..5).takeWhile { it < (abc[it].toInt() and 0xff) // or `as Int` if array is not numeric }.forEach { // do something with `it` }
If you don't mind creating a new ArrayList instance, it can be done like this: (0..5).takeWhile { it < (abc[it] as Int and 0xff) } .forEach { // ... }
Note: the .takeWhile { ... }.forEach { ... } approach suggested in some answers is not equivalent to the Java for loop. The range is first processed with .takeWhile { ... } and only then the prefix it returned is iterated over. The problem is that the execution of the .forEach { ... } body won't affect the condition of .takeWhile { ... }, which has already finished execution by the time the body gets called on the first item. (see this runnable demo that shows how the behavior is different) To fix this, you can use Sequence<T>. In constrast with eager evaluation over Iterable<T>, it won't process the whole set of items with .takeWhile { ... } and will only check them one by one when .forEach { ... } is up to process a next item. See: the difference between Iterable<T> and Sequence<T>. To use the Sequence<T> and achieve the behavior that is equivalent to the Java loop, convert the range .toSequence(): (0..5).asSequence() .takeWhile { it < (abc[it].toInt() and 0xff) } .forEach { // Use `it` instead of `j` } Alternatively, just use the while loop: var j = 0 while (j < 6 && j < (abc[j] as Int and 0xff)) { // do something j++ }
This is how the kotlin version will look like. var j = 0 while (j < 6 && j < (abc[j] as Int and 0xff)) { // do something j++ } You can convert Java to Kotlin online here. Try Kotlin. Also if you are using IntelliJ, here is a link to help you convert from Java to Kotlin. IntelliJ Java to Kotlin.
I would move the j < ((int)abc[j] & 0xff) part into an if-test inside the loop. You could then do this: for (j in 0..5) { if (j < (abc[j].toInt() and 0xff)) { // Do stuff here } else break }
This is the output of the intellij plugin for conversion: var j = 0 while (j < 6 && j < abc[j] as Int and 0xff) { j++ // ... }
Appending a pointer to a Slice in Golang
I want to append a pointer to a slice.Is it possible..?In Partentnode.children is a slice I want to append it with X as pointer. https://play.golang.org/p/ghWtxWGOAU func Tree(Parentnode *Node) { if IsvisitedNode(Parentnode.currentvalue - 1) { m := MovesArray[Parentnode.currentvalue-1] for j := 0; j < 8; j++ { if m[j] != 0 { var X *Node X.parentnode = Parentnode X.currentvalue = m[j] if IsvisitedNode(m[j]) { Parentnode.children = append(Parentnode.children, *X) Tree(X) } } } } }
You have a off by one error. In main you set Y.currentvalue = 1. Then in Tree currentvalue walks to 64. X.currentvalue = m[j] fmt.Printf("cv: %v\n",X.currentvalue) //walks to 64 if IsvisitedNode(m[j]) { An in IsvisitedNode you test that index against visithistory that has 64 indexes, thus stops at index 63. -> index error var visithistory [64]bool func IsvisitedNode(position int) bool { if visithistory[position] == true { Things work if you set var visithistory [65]bool but I think you need to rethink you logic here somewhat.