How to make multiconditional for loop in kotlin - 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++
// ...
}
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++ }
How to remake the program so that words are passed in function arguments in the KOTLIN programming language?
Need to create a function that implements the attached algorithm, to which all words are passed in the function arguments. For example: f ("dfd" dd "ddd"); My code: fun main() { var s = readLine(); var w = Array(128){0} //To mark characters from a word 1 var g = Array(128){0}//When we encounter a space, we add units from the first array to the corresponding elements of the second, zeroing them in the first. if(s!=null) { for(c in s) { if(c.toInt() > 127 || c.toInt()<0) { println("Input error, try again"); return; } //Checking for space. if(c.toInt() != 32) w[c.toInt()] = 1; else for(k in 0..127) { if(w[k] == 1) { g[k] += 1; w[k] = 0; } } } //For the last word, if there was no space after it. for(k in 0..127) { if(w[k] == 1) { g[k] += 1; w[k] = 0; } } } //Displaying matched characters to the screen for(k in 0..127) { if(g[k]>1) { println(k.toChar()); } } } This program searches for characters that match at least two words in a string Example input: hello world output: lo
There's already utilities for these in Kotlin, I highly recommend you to read the docs before asking these type of questions. The groupingBy should do what you want: readLine()?.let { input -> input.groupingBy { it }.eachCount() .forEach { if (it.value > 1 && it.key != ' ') println(it.key) } }
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 write complex Kotlin for loop
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.
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.