sbeve
02/12/2021, 8:25 PMfun 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()
}
ephemient
02/12/2021, 8:41 PMrotations !in 0..25
, that's something that %
can addresssbeve
02/12/2021, 8:42 PMsbeve
02/12/2021, 8:42 PMephemient
02/12/2021, 8:43 PMreturn when (val variableInput = input.toInt()) {
65..90 -> { ... }
97..112 -> { ... }
else -> input
}
but that isn't really an optimization, just stylesbeve
02/12/2021, 8:43 PMsbeve
02/12/2021, 8:43 PMfun 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()
}
sbeve
02/12/2021, 8:43 PMephemient
02/12/2021, 8:44 PM%
modulus does, or check that rotations is in boundssbeve
02/12/2021, 8:44 PMsbeve
02/12/2021, 8:44 PMephemient
02/12/2021, 8:44 PMsbeve
02/12/2021, 8:44 PMephemient
02/12/2021, 8:44 PMsbeve
02/12/2021, 8:45 PMephemient
02/12/2021, 8:45 PM'Á'.toInt() == 193
sbeve
02/12/2021, 8:45 PMsbeve
02/12/2021, 8:46 PMisUpperCase()
and not face any problems?ephemient
02/12/2021, 8:46 PMsbeve
02/12/2021, 8:46 PMsbeve
02/12/2021, 8:46 PMephemient
02/12/2021, 8:46 PMsbeve
02/12/2021, 8:48 PMephemient
02/12/2021, 8:48 PMsbeve
02/12/2021, 8:48 PMephemient
02/12/2021, 8:48 PMsbeve
02/12/2021, 8:48 PMsbeve
02/12/2021, 8:48 PMsbeve
02/12/2021, 8:49 PMsbeve
02/12/2021, 8:49 PMsbeve
02/12/2021, 8:49 PMephemient
02/12/2021, 8:50 PMsbeve
02/12/2021, 8:51 PMAnimesh Sahu
02/13/2021, 6:38 AMfun 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 😛sbeve
02/13/2021, 5:48 PM