-
Notifications
You must be signed in to change notification settings - Fork 1
/
bonus.js
158 lines (135 loc) · 4.76 KB
/
bonus.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
// CREATES ITERABLE ARRAY FROM INPUT STRING
const neatArray = (str)=>{
// separates parenthesis with commas
str = str.replace(/\(/gi, ",(,").replace(/\)/gi, ",)");
// establishes spaces and commas as separators
let separators = [' ', '\\,'];
// establishes a variable meaning 'space or comma'
let regExpSeparators = new RegExp(separators.join('|'));
// creates an array out of the string with the content between spaces or commas
let arr = str.split(regExpSeparators);
// filters out empty cells
arr = arr.filter(function(cell){return cell!==''});
return arr;
}
// CREATES A GENERATION-ENUMERATED ARRAY
const genEnumArr = (str)=>{
// takes a string formatted like the provided example and makes a workable, neat array out of it
let arr = neatArray(str);
let newArr = [];
let generation = 0;
// replaces all parenthesis with generation numbers and adds generation numbers before every work not surrounded with parentheses
for (var i=0; i<arr.length; i++){
if (arr[i]==='('){
generation++;
} else if (arr[i]===')'){
generation--;
} else {
newArr.push(generation);
newArr.push(arr[i]);
}
}
return newArr;
}
// ALPHABETICALLY SORTS GENERATION-ENUMERATED ARRAY
const alphaSort = (str)=>{
let arr = genEnumArr(str);
// SEPARATES GENERATIONS 0, 1, AND 2 AND REMOVES ENUMERATION (FUNCTION FOUND BELOW)
let genSep = genSeparator(arr);
let gen1 = genSep[0];
let gen2 = genSep[1];
let gen3 = genSep[2];
// SORTS GENERATIONS 0, 1, AND 2
gen1.sort();
gen2.sort();
gen3.sort();
// IDENTIFIES PARENT OF EACH GENERATION (FUNCTION FOUND BELOW)
let gen2Parent = parentIdentifier(arr, gen2, 2);
let gen3Parent = parentIdentifier(arr, gen3, 3);
// ADDS GENERATION ARRAYS TO gen1 AND ADDS ENUMERATION (FUNCTION FOUND BELOW)
const gen2ParentNum = gen1.indexOf(gen2Parent)
gen1 = addGeneration(gen1, gen2ParentNum, gen2, 2);
const gen3ParentNum = gen1.indexOf(gen3Parent)
gen1 = addGeneration(gen1, gen3ParentNum, gen3, 3);
return gen1;
}
const parentIdentifier = (arr, gen, num)=>{
// once the generation number increases, the word directly previous to the increased number is designated parent of the new generation
for (var i=0; i<arr.length; i++){
if (arr[i-1]===num){
return gen.parent = arr[i-2];
}
}
return gen.parent;
}
const genSeparator = (arr)=>{
let gen1 = [];
let gen2 = [];
let gen3 = [];
// takes different generations from a three-generation array and produces three different generation-specific arrays
for (var i=0; i<arr.length; i++){
if (typeof arr[i]==='number'){
continue;
}
if (arr[i-1]===1){
gen1.push(arr[i]);
}
if (arr[i-1]===2){
gen2.push(arr[i])
}
if (arr[i-1]===3){
gen3.push(arr[i])
}
}
return [gen1, gen2, gen3];
}
const addGeneration= (arr, genparent, gen, num)=>{
// inserts younger generations where they correspond to the correct sibling in older generations
for (var i=gen.length-1; i>=0; i--){
arr.splice(genparent+1, 0, num, gen[i]);
}
return arr;
}
// GENERATES OUTPUT STRING
function outputStr(str){
let arr = alphaSort(str);
let newStr = '';
// produces a string in the desired output format by putting as many dashes as the generation of a word in front of it and a line break after each word
for (var i=0; i<arr.length; i++){
if (typeof arr[i]==='string'){
newStr+=arr[i]+'\n';
}
if (typeof arr[i]==='number'){
newStr+='-'.repeat(arr[i]-1)+' ';
}
}
return newStr;
}
console.log(outputStr('(id,created,employee(id,firstname,employeeType(id), lastname),location)'));
/*
SIMPLIFYING THIS SOLUTION (in a way that can take in strings with more and deeper sublists):
- Use clearly-defined, simple functions that perform one task - obvious input/ output
- Consider using a json object data structure instead of generation-enumerated arrays and recursively call itself
ONE POSSIBLE APPROACH:
High Level
- Parse String to a recursive data structure object (such as JSON)
- Alphabetize the object
- Call Print on the object
Parse - takes a string and converts to JSON object (or some recursive data structure)
example input: "(a(b),c(d(e))"
example output: [{"a":[{"b":[]}]},{"c":[{"d":[{"e":[]}]});
Sort - takes a JSON Object and recursively sorts all the keys
example input: [{"reptile":[{"snake":[{"viper":[]},{"python":[]}],{"lizard":[]}]},{"mammal":[{"zebra":[]},{"monkey":[]}]}]
example output: [{"mammal":[{"monkey":[]},{"zebra":[]}]},"reptile":[{"lizard":[]},{"snake":[{"python":[]},{"viper":[]}]}]}]
Print - takes a JSON Object and recursively prints to string by depth
example input: [{"mammal":[{"monkey":[]},{"zebra":[]}]},"reptile":[{"snake":[{"python":[]},{"viper":[]}]}]}]
example output:
"mammal
- monkey
- zebra
reptile
- snake
-- python
-- viper
- lizard"
*/