Background
This question is inspired by The Caesar Cipher. Given a modified ASCII value i
where i > 122 or i < 97
, the goal is to find the char represented by its value. For example, since 'z'
value is 122
, then 123
references 'a'
, as well as 149
and 97 +- 26n
.
Goal
The function calc_chr
should calculate shifted ASCII values while staying inside the scope of the abc
, i.e. [97, 122]
decimal.
Clear Example
let 'x'
be 120
let new_val
be 'x' + 5
-->125
calc_chr(new_val)
is equal to 'c'
The function returns 'c'
, as a result of shifting 'x'
5 times: x -> y, z, a, b, c
.
Workaround
The workaround isn't efficient.
while new_val > 122:
new_val -= 26
while new_val < 97:
new_val += 26
The desired solution, if exists, is a calculation (not a loop).
2021 Update
This question was written terribly in 2019. Since then, I have learned a thing or two... Therefore, I would appreciate if the community, i.e. you, could rejudge this post and consider giving it a 0
score, or even a positive
score if deserved :)
I will accept an answer in 24 hours. The current accepted solution was suggested by Ture
. Thanks in advance.
from Convert a shifted ASCII value to char inside the abc scope
No comments:
Post a Comment