-
Notifications
You must be signed in to change notification settings - Fork 0
/
6-kyu-replace-with-alpha-position.rb
51 lines (40 loc) · 1.5 KB
/
6-kyu-replace-with-alpha-position.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
=begin
Given a string, replace every letter with its position in the alphabet.
If anything in the text isn't a letter, ignore it and don't return it.
"a" = 1, "b" = 2, etc.
Example
alphabet_position("The sunset sets at twelve o' clock.")
Should return "20 8 5 19 21 14 19 5 20 19 5 20 19 1 20 20 23 5 12 22 5 15 3 12 15 3 11"
Problem: Parse the String, and replace alpha characters with its position in alphabet
Rules: Ignore non-alpha characters
Input: String
Output: String of numbers/position in alphabet
Algo:
- initialize ALPHA constant as ('a'..'z').to_a
- initialize result as empty array
- Use String#chars to convert string to array
- each array element is a character
- Iterate through the array, Use Array#each
- If character.match?(/A-Za-z/) (If it's an alpha character)
- result << ALPHA.index(char) + 1
- Convert Array back to String, Use array.join(' ') on result
- return result
=end
ALPHA = ('a'..'z').to_a
def alphabet_position(str)
result = []
str.chars.each do |char|
result << ALPHA.index(char.downcase) + 1 if char.match?(/[A-Za-z]/)
end
result.join(' ')
end
p alphabet_position("The sunset sets at twelve o' clock.") == "20 8 5 19 21 14 19 5 20 19 5 20 19 1 20 20 23 5 12 22 5 15 3 12 15 3 11"
def alphabet_position(str)
result = []
str.chars.each do |char|
result << ('a'..'z').to_a.index(char.downcase) + 1 if char.match?(/[A-Za-z]/)
end
result.join(' ')
end
# Be very careful with the regex, char.match?(/[A-Za-z]/)
# I had forgotten to put [] and instead had (/A-Za-z/)