24 lines
764 B
Mojo
24 lines
764 B
Mojo
# probe: shifting letters through an alphabet string with find()
|
|
# status: the expected output is Programming+'s interpreter's prediction; corpus.py verify confirms or refutes it on godbolt
|
|
# expect-stdout: khoor zruog
|
|
# expect-stdout: hello world
|
|
# expect-stdout: bcd
|
|
def shift(s: String, k: Int) -> String:
|
|
var alphabet = String("abcdefghijklmnopqrstuvwxyz")
|
|
var out = String("")
|
|
for i in range(s.count_codepoints()):
|
|
var c = String(s[codepoint=i])
|
|
var at = alphabet.find(c)
|
|
if at < 0:
|
|
out += c
|
|
else:
|
|
out += String(alphabet[codepoint=(at + k) % 26])
|
|
return out
|
|
|
|
|
|
def main():
|
|
var secret = shift("hello world", 3)
|
|
print(secret)
|
|
print(shift(secret, 23))
|
|
print(shift("xyz", 4))
|