|
| 1 | +=begin |
| 2 | +Codewars. 29/04/20. 'Bracket Duplicates'. 6kyu. Here we create a program that |
| 3 | +takes a string as an input and if there are consecutive duplicates of more than |
| 4 | +2 characters in the string, we return the string with all extra characters in |
| 5 | +square brackets. For example, "aaaabbcdefffffffg" should return |
| 6 | +"aa[aa]bbcdeff[fffff]g". Here is the solution I developed to solve the challenge. |
| 7 | +1) We define our method bracket_dups_ms, which takes a string as an argument. |
| 8 | +2) As requested in the kata, we return a custom error message if the input is |
| 9 | + not a string. |
| 10 | +3) If the input is a string, we convert it to an array of characters using the |
| 11 | + chars method. |
| 12 | +4) We call the slice_when method and create a new group every time there is a |
| 13 | + different character, thereby creating an array of arrays where duplicates |
| 14 | + are grouped together in sub-arrays. |
| 15 | +5) We map over the consecutively grouped array of arrays. If a group contains |
| 16 | + more than 2 characters, we use the insert method to place the front bracket |
| 17 | + in position 2 (after dups 0 and 1) and we use the insert method to place |
| 18 | + the end bracket at position -1 (after the rest of the duplicates). Now our |
| 19 | + duplicates have been bracketed in the format requested of us. |
| 20 | +6) We join the array of arrays back into a string. |
| 21 | +7) So that the line is not too long I've structured the method in a functional |
| 22 | + programming format. |
| 23 | +=end |
| 24 | + |
| 25 | +def bracket_dups_ms(str) |
| 26 | + return "Please enter a valid string" if !str.is_a?(String) |
| 27 | + str.chars.slice_when {|a,b| a != b}. |
| 28 | + map {|g| g.size > 2 ? g.insert(2, "[") && g.insert(-1, "]") : g }. |
| 29 | + join |
| 30 | +end |
| 31 | + |
| 32 | +=begin |
| 33 | +Here is a better soltuion, which uses a regex. |
| 34 | +1) We call the gsub method on the string. |
| 35 | +2) In our regex, we pattern match for capture group 1 (.), any character |
| 36 | + except a newline, this also could have been ([a-zA-Z]). |
| 37 | +3) Capture group 2 (\1) is 1 more occurrence of capture group 1, so so far |
| 38 | + we've matched for 2 consecutive characters. |
| 39 | +4) Capture group 3 (\1{1,}) is exactly 1 or more occurrence of capture group 1, |
| 40 | + so now we've matched for all consecutive characters. |
| 41 | +5) Our substitution argument puts capture group 1 (first character) next to |
| 42 | + capture group 2 (second same character) and then places square brackets |
| 43 | + around capture group 3 (the rest of the same characters). The substitution |
| 44 | + argument must be in single quotes '' not doulbe quotes "" in order to work. |
| 45 | +6) We use rescue to handle any exceptions which in this case will be an input |
| 46 | + that is not a string. |
| 47 | +=end |
| 48 | + |
| 49 | +def bracket_dups(str) |
| 50 | + str.gsub /(.)(\1)(\1{1,})/,'\1\2[\3]' rescue "Please enter a valid string" |
| 51 | +end |
0 commit comments