-
Notifications
You must be signed in to change notification settings - Fork 1
/
Ruby - BracketDuplicates.txt
60 lines (40 loc) · 1.92 KB
/
Ruby - BracketDuplicates.txt
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
52
53
54
55
56
57
58
59
60
Description:
Create a program that will take in a string as input and, if there are duplicates of more than two alphabetical characters in the string, returns the string with all the extra characters in a bracket.
For example, the input "aaaabbcdefffffffg" should return "aa[aa]bbcdeff[fffff]g"
Please also ensure that the input is a string, and return "Please enter a valid string" if it is not.
- - - - - - - - My Answer - - - - - - - -
def string_parse string
newArr = []
answer = ""
if string.is_a? String
chars = string.split('')
chars.map { |char| if ((newArr[-1].is_a? Array) && (newArr[-1][0] == char))
newArr[-1] << char
elsif (newArr[-1] == char) && (newArr[-2] == char)
newArr << [char]
else
newArr << char
end
}
else
return "Please enter a valid string"
end
newArr.map { |el| (el.is_a? Array) ? (answer << "[" + el.join + "]") : answer << el }
answer
end
- First, I create an empty array and an empty string.
- Secondly I check to see if the submission is, in fact, a string, if not, return an error message.
- If it is a string, split it into individual chars
- If the last char is an array && the value stored in that array equal the current char, push it into the array.
- Else if the last two non-array elements both equal the current char, push char in as an array.
- Finally, if none of the above works, add the char to the end of the array.
- Of that created array, ‘.map’ it.
- If the element is just a char, push it into the string
- If the element is an array, ‘.join’ it into a string and add brackets.
- Finally, return the answer string
- - - - - - - - - alt - - - - - - - -
use of ‘.gsub’ and regEx is how to make this beautiful.
wow.
def string_parse string
string.gsub /(.)\1(\1+)/, '\1\1[\2]' rescue 'Please enter a valid string'
end