Pergunta de entrevista da empresa Zendesk

Write a simple cipher that will take a string and replace each letter in the range a-z with the corresponding character 13 steps along the alphabet.

Respostas da entrevista

Sigiloso

6 de out. de 2017

def cipher(string) string.chars.each_with_index do |char, index| string[index] = (char.ord + 13).chr end end

Sigiloso

7 de out. de 2017

Doesn't wrap around. I think what you're looking for is: def cipher(string) a = 'a'.ord string.chars.each_with_index do |char, index| string[index] = ((char.ord - a + 13) % (26) + a).chr end end

1