-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproblem4.js
190 lines (157 loc) · 6.41 KB
/
problem4.js
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/*
**Problem**
You are given a list of numbers in a "short-hand" range where
only the significant part of the next number is written because
we know the numbers are always increasing (ex. "1, 3, 7, 2, 4, 1" represents [1, 3, 7, 12, 14, 21]).
Some people use different separators for their ranges
(ex. "1-3, 1-2", "1:3, 1:2", "1..3, 1..2" represent the same numbers [1, 2, 3, 11, 12]).
Range limits are always inclusive.
Your job is to return a list of complete numbers.
The possible separators are: ["-", ":", ".."]
- "1, 3, 7, 2, 4, 1" --> 1, 3, 7, 12, 14, 21
- "1-3, 1-2" --> 1, 2, 3, 11, 12
- "1:5:2" --> 1, 2, 3, 4, 5, 6, ... 12
- "104-2" --> 104, 105, ... 112
- "104-02" --> 104, 105, ... 202
- "545, 64:11" --> 545, 564, 565, .. 611
564 to 611
Understanding the Problem
- input:
- string
- output:
- an array of integers
- model of problem:
- when curren number is less than the previous number
- we need to increment the previous by number by 1
- UNTIL the last x number of digits matches the current number
- that will be our new number
- ranges represent a series of intgers increasing by 1
- can be represented with two integers separated by '-', ':', '..'
- when ever a range exists, we must treat the missing numbers as if they were actually there
- separate the two numbers
- if the second is less than the first then update second number to correct value
- add all the integers between the two values to the range
**Examples / Test Cases**
getRange("1, 3, 7, 2, 4, 1") === [1, 3, 7, 12, 14, 21]
getRange("1-3, 1-2") === [1, 2, 3, 11, 12]
getRange("1:5:2") === [1, 2, 3, 4, 5, 6, 7, 8 ,9, 10, 11, 12]
getRange("104-2"); // 104, 105, ... 112
getRange("104-02"); // 104, 105, ... 202
getRange("545, 64:11") // 545, 564, 565, .. 611
**Data Structures**
- an array to split and hold each element of the input string
- an array to add and hold our return range
**Algorithm**
CalculateNextInt
-takes the previous # or 0, and the current # string
- adds 1 to the previous number
- checks to see if the the last digits of the current # match the last digits of the previous number
- use length of current # string to get - index of previous number string to match
- if match return the new number as integer
- if not repeat
CreateRange
- function will need the previous value if it exists OR set previous value to 0
- split range string ito individidaul string numbers
- create a return array to hold new values and add previous value/0 as first element
- iterate over the array of string numbers
- pass the value to CalulateNextInt along with last value in array OR previousInt
- add the return value to the rangeArray
- this leaves us with an array on non-consecutive integers
- pass the rangeArray to fillRange function to fill out
FIllRange
- creates a return array
- for loop starting with index equal to the rangeMin and going until index <= rangeMax
- on each loop add the current index to the return array
- return the array
Get Range
- create an empty array to store final values
- split input string on ', ' to create array of strings
- iterate over those strings
- if a string contains ["-", ":", ".."]
- pass it to createRange function
- iterate over the return and add each value to the return string
- else if string converted to number is greater than the last value in the array (or 0 if array empty)
- add integer version of string to return array
- else
- pass string to calculateNextInt function
- add return value to return array
- return the return array
*/
const RANGE_DELIMITER = new RegExp(/-|:|\.\./);
function getRange(stringRange) {
let rangeArray = [];
let stringArray = stringRange.split(', ');
stringArray.forEach(str => {
let previousInt = rangeArray[rangeArray.length -1] || 0;
if (str.match(RANGE_DELIMITER)){
let subRange = createRange(str, previousInt);
rangeArray = rangeArray.concat(subRange);
} else if (parseInt(str, 10) < previousInt) {
rangeArray.push(calculateNextInt(str, previousInt));
} else {
rangeArray.push(parseInt(str, 10));
}
});
return rangeArray;
}
function calculateNextInt(numString, previousInt) {
let newInt = previousInt;
let match = false;
let sliceIndex = numString.length * -1;
while (!match) {
newInt += 1;
let newIntSlice = String(newInt).slice(sliceIndex);
if (newIntSlice === numString) {
match = true;
}
}
return newInt;
}
function createRange(rangeStr, previousInt = 0) {
let rangeArray = [];
let strArray = rangeStr.split(RANGE_DELIMITER); // ["-", ":", ".."]
strArray.forEach(strNum => {
if (rangeArray.length === 0)
rangeArray.push(calculateNextInt(strNum, previousInt));
else {
rangeArray.push(calculateNextInt(strNum, rangeArray[rangeArray.length -1]));
}
});
rangeArray = fillRange(rangeArray);
return rangeArray;
}
function fillRange(minMaxArray) {
let filledArray = [];
let min = Math.min(...minMaxArray);
let max = Math.max(...minMaxArray);
for (let index = min; index <= max; index += 1) {
filledArray.push(index);
}
return filledArray;
}
// Calulate Next Integer
// console.log(calculateNextInt('2', 7));
// console.log(calculateNextInt('1', 3));
// console.log(calculateNextInt('2', 104));
// console.log(calculateNextInt('02', 104));
// console.log(calculateNextInt('11', 564));
// Test Range Min Max Creation
// console.log(createRange("1-3")); // [ 1, 3 ]
// console.log(createRange("1:5:2")); // [ 1, 5, 12 ]
// console.log(createRange("104-2")); // [ 104, 112 ]
// console.log(createRange("104-02")); // [ 104, 202 ]
// console.log(createRange("64:11", 545)); // [ 564, 611 ]
// Test FilL Range
// console.log(fillRange([ 1, 3 ])); // 1...3
// console.log(fillRange([ 1, 5, 12 ]));
// console.log(fillRange([ 104, 112 ]));
// console.log(fillRange([ 104, 202 ]));
// console.log(fillRange([ 564, 611 ]));
// Test final implementation
console.log(getRange("1, 3, 7, 2, 4, 1")); // [1, 3, 7, 12, 14, 21]
console.log(getRange("1-3, 1-2")); // [1, 2, 3, 11, 12])
console.log(getRange("1:5:2")); //[1, 2, 3, 4, 5, 6, 7, 8 ,9, 10, 11, 12]);
console.log(getRange("1-5:2")); //[1, 2, 3, 4, 5, 6, 7, 8 ,9, 10, 11, 12]);
console.log(getRange("104-2")); // 104, 105, ... 112
console.log(getRange("104-02")); // 104, 105, ... 202
console.log(getRange("545, 64:11")); // 545, 564, 565, .. 611