Swapping Variables in Kotlin [duplicate] - kotlin

This question already has answers here:
how to swap numbers in kotlin using function?
(2 answers)
Val cannot be reassigned a compile time error for a local variable in fun in Kotlin
(3 answers)
Closed 5 months ago.
I am a beginner, learning the basics of Kotlin. We were asked to swap the values of two variables (see the photo below). My question is, why is simply swapping the values of the variable not the expected solution as written in the image? It had to use a third variable.
Example:
Var x = 1
Var y = 2
println(x) // prints 1
println(y) // prints 2
x = 2
y = 1
println(x)
println(y)
Also, in the photo, was the Val tmp reassigned?

This will swap two integer:
x=x+y
y=x-y
x=x-y

Related

Multiple assignment in Kotlin [duplicate]

This question already has an answer here:
Reassigning Variables via Destructuring
(1 answer)
Closed 1 year ago.
I can declare and initialize a and b like this
var (a, b) = Pair(1, 2)
But I can't reassign them like this
(a, b) = Pair(3, 4)
How come this isn't possible? Am I missing something?
that because (a, b) is 2 separate variables, that you initialize by componnetN() function.
you can use a and b as different variables. If you vant have pair you have to write it like this var a = Pair(2, 1) or can use var a = 1 to 2
read here for more ingormation
https://kotlinlang.org/docs/destructuring-declarations.html
That's because in Kotlin (a, b, c, ...) is not a groupping operator but a destructuring declaration and hence it only works for declaring variables, not for reassigning.

Define a random variable from response match in karate [duplicate]

This question already has an answer here:
Karate - Select a random element from json
(1 answer)
Closed 2 years ago.
How can I define a variable as a random match?
item[]
0{}
1{
_id:item
}
2{}
3{}
I want to define a match not with item[1]._id but with item[random]._id.
Could you please help me with that?
Just a warning that trying to add such dynamic behavior to your tests makes them less maintainable in the long term.
Here is an example that works:
* def size = response.items.length
* def index = Math.floor(Math.random() * size)
* def item = response.items[index]

I heard that Haskell variables are immutable but i am able to reassign and update variable values [duplicate]

This question already has an answer here:
Haskell: What is immutable data?
(1 answer)
Closed 4 years ago.
I heard that Haskell variables are immutable but i am able to reassign and update variable values
First, note that GHCi syntax is not quite the same as Haskell source-file syntax. In particular, x = 3 actually used to be illegal as such:
GHCi, version 7.10.2: http://www.haskell.org/ghc/ :? for help
Prelude> x = 3
<interactive>:2:3: parse error on input ‘=’
Newer versions have made this possible by simply rewriting any such expression to let x = 3, which has always been ok:
GHCi, version 7.10.2: http://www.haskell.org/ghc/ :? for help
Prelude> let x = 3
Prelude> x
3
By contrast, in a Haskell source file, let x = 3 has never been legal by itself. This only works in a particular environment, namely a monadic do block.
main :: IO ()
main = do
let x = 3
print x
3
And the GHCi prompt by design actually works like the lines in a do block, so let's in the following discuss that. Note that I can also write
main = do
let x = 1
let x = 3
print x
3
And that's basically what's also going on in your GHCi session. However, as the others have remarked, this is not mutation but shadowing. To understand how this works, note that the above is essentially a shorthand way of writing
main =
let x = 1
in let x = 3
in print x
So, you have two nested scopes. When you look up a variable in some expression, Haskell always picks the “nearest one”, i.e. in the inner scope:
main =
let x = 1
┌──
in│let x = 3
│in print x
└─
The outer x isn't touched at all, it's basically unrelated to anything going on in the inner scope. The compiler will actually warn you about this, if asked if there's anything fishy in your file:
$ ghc -Wall wtmpf-file16485.hs
[1 of 1] Compiling Main ( wtmpf-file16485.hs, wtmpf-file16485.o )
wtmpf-file16485.hs:3:8: warning: [-Wunused-local-binds]
Defined but not used: ‘x’
|
3 | let x = 1
| ^
wtmpf-file16485.hs:3:12: warning: [-Wtype-defaults]
• Defaulting the following constraint to type ‘Integer’
Num p0 arising from the literal ‘3’
• In the expression: 3
In an equation for ‘x’: x = 3
In the expression:
do let x = 1
let x = 3
print x
|
3 | let x = 1
| ^
wtmpf-file16485.hs:4:8: warning: [-Wname-shadowing]
This binding for ‘x’ shadows the existing binding
bound at wtmpf-file16485.hs:3:8
|
4 | let x = 3
| ^
There: the second definition simply introduces a new, more local variable which also happens to be called x, but is unrelated to the outer variable. I.e. we might as well rename them:
main = do
let xOuter = 1
let xInner = 3
print xInner
A consequence of all this is that a variable that's “mutated” in this way has no influence on other functions which use the original variable. Example:
GHCi, version 8.2.1: http://www.haskell.org/ghc/ :? for help
Loaded GHCi configuration from /home/sagemuej/.ghci
Loaded GHCi configuration from /home/sagemuej/.ghc/ghci.conf
Prelude> let x = 1
Prelude> let info = putStrLn ("x is ="++show x++" right now")
Prelude> x = 3
Prelude> info
x is =1 right now
Another consequence is that “updates” which try to use the old value behave in a funny way:
Prelude> let s = "World"
Prelude> s = "Hello"++s
Prelude> s
"HelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHell^C
Here, the new binding does not just prepend "Hello" to the old s="World". Instead, it prepends "Hello" to its own result value, which is in turn defined by "Hello" prepended to... and so on, recursively.
You're shadowing, not mutating.

Read primitive datatype values in Kotlin [duplicate]

This question already has answers here:
Reading console input in Kotlin
(8 answers)
Closed 5 years ago.
Note: Please do not downvote question as I tried searching but did not find anything. The question has already been marked duplicate.
How to read primitive datatype values in Kotlin?
We can use Scanner object of java but I want to implement using readLine function of kotlin.
How do I scan numbers e.g num1 and num2 and perform some operation like sum ?
How do it convert following code to koltin without using scanner?
val sc = Scanner(System.in)
val num1 = sc.nextInt()
val num2 = sc.nextInt()
val sum = sum(num1, num2)
Just do something like:
fun main(vararg args: String) {
val (a, b) = readLine()!!.split(' ')
println(a.toInt() + b.toInt())
}

Checking to see if a number is divisible [duplicate]

This question already has an answer here:
Modulo operator in Objective C
(1 answer)
Closed 8 years ago.
I want to check if a number is divisible by 6 and if not, I need to increase it until it becomes divisible.
Use the modulus operator. This operator returns the remainder of the division operation. Check if this is 0 to check if it divides perfectly.
if (i % 6 == 0) {
// ...
}