Okay so I rewrote the code, is this more optimized...
# announcements
s
Okay so I rewrote the code, is this more optimized? how can i make stuff better
Copy code
fun rotateLetter(input: Char, rotations: Int): Char {
    var variableInput = input.toInt()
    val isUpperCase = variableInput in 65..90
    val isLowerCase = variableInput in 97..112

    when {
        isUpperCase -> {
            if (variableInput + rotations <= 90) variableInput += rotations
            else variableInput = 64 + (variableInput + rotations - 90)
        }
        isLowerCase -> {
            if (variableInput + rotations <= 122) variableInput += rotations
            else variableInput = 96 + (variableInput + rotations - 122)
        }
        else -> return input
    }

    return variableInput.toChar()
}
e
may fail if
rotations !in 0..25
, that's something that
%
can address
s
true
idk how to fix that then
e
it is possible to bind it all at once,
Copy code
return when (val variableInput = input.toInt()) {
    65..90 -> { ... }
    97..112 -> { ... }
    else -> input
}
but that isn't really an optimization, just style
s
i updated the code a little
Copy code
fun rotateLetter(input: Char, rotations: Int): Char {
    var variableInput = input.toInt()

    when {
        input.isUpperCase() -> {
            if (variableInput + rotations <= 90) variableInput += rotations
            else variableInput = 64 + (variableInput + rotations - 90)
        }
        input.isLowerCase() -> {
            if (variableInput + rotations <= 122) variableInput += rotations
            else variableInput = 96 + (variableInput + rotations - 122)
        }
        else -> return input
    }

    return variableInput.toChar()
}
this is better
e
you should learn what
%
modulus does, or check that rotations is in bounds
s
i know what it does
but i can't put my finger on how to use it
e
no, that's worse: `isUpperCase()`/`isLowerCase()` will return true for non-ASCII characters
s
O
e
e.g. Á
s
that isn't gonna be a problem is it?
e
it is, because
'Á'.toInt() == 193
s
oh
anyway i could still use
isUpperCase()
and not face any problems?
e
not without more checking, no
s
not even with the help of %
?
e
no, because the other characters are not so nicely arranged
s
okay i will change that back to how it was. How could i use the modulus function to not face that issue you told me about earlier?
e
e.g. ÁB́Ć..Ź does not form a contiguous range
s
O
e
in fact, most of those do not even have a composed representation
s
i see
then i am gonna use the previous method of checking the case
I also need to figure out how to use the modulus function
i mean i know how it works
but i dk how to use it in this particular scenario
e
my code yesterday used it. try figuring it out (even if you ignore the bit twiddling)
s
okay i will try to understand it. the modulus function is kinda hard to work with imo like picturing whats happening becomes complicated
a
Copy code
fun rotateLetter(input: Char, rotations: Int): Char =
    when {
        input in 'A'..'Z' -> {
            if (input + rotations % 26 <= 'Z') input + rotations % 26
            else input + rotations % 26 - 25
        }
        input in 'a'..'z' -> {
            if (input + rotations % 26 <= 'z') input + rotations % 26
            else input + rotations % 26 - 25
        }
        else -> input
    }
How about shortened 😛
s
thank u