-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathstring.rb
51 lines (42 loc) · 1.56 KB
/
string.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
def num_to_s(num, base)
# handle the case where num == 0.
return "0" if num == 0
# first find the least-significant digit, the part that goes in the
# "ones" column. The leftover amount is now a multiple of `base`. We
# can recursively call our method to represent the high order bits.
low_digit = num % base
leftover = num - low_digit
if leftover > 0
num_to_s(leftover / base, base) + low_digit.to_s
else
# nothing leftover, done.
low_digit.to_s
end
# e.g.,
# num_to_s(144, 10) => {:low_digit => 4, :leftover => 140}
# num_to_s(14, 10) => {:low_digit => 4, :leftover => 10}
# num_to_s(1, 10) => {:low_digit => 1, :leftover => 0}
end
def caesar(string, shift)
# The computer represents letters as a sequence of ASCII code
# numbers. ASCII letters are consecutive. So:
# "a" => 97, "b" => 98, "c" => 99, ..., "y" => 121, "z" => 122
#
# We can iterate through a string's ASCII codes letter by letter
# using `each_byte`. We can also use `ord` to get a single letter's
# code. `Fixnum#chr` does the opposite translation from ASCII code
# to string letter.
# only works on downcased words
string = string.downcase
encoded_string = ""
string.each_byte do |ascii|
# letter_pos is the position of the letter in the alphabet. "a" is
# the zeroth letter.
letter_pos = ascii - "a".ord
# we use modulo to avoid overshifting; we need to wrap!
shifted_letter_pos = (letter_pos + shift) % 26
# convert back to string format
encoded_string += ("a".ord + shifted_letter_pos).chr
end
encoded_string
end