-
Notifications
You must be signed in to change notification settings - Fork 0
/
TextFile2.txt
237 lines (207 loc) · 7.09 KB
/
TextFile2.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
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/*
Our local radio station is running a show where the songs are ordered in a very specific way. The last word of the title of one song must match the first word of the title of the next song - for example, "Silent Running" could be followed by "Running to Stand Still". No song may be played more than once.
Given a list of songs and a starting song, find the longest chain of songs that begins with that song, and the last word of each song title matches the first word of the next one. Write a function that returns the longest such chain. If multiple equivalent chains exist, return any of them.
Example:
songs1 = [
"Down By the River",
"River of Dreams",
"Take me to the River",
"Dreams",
"Blues Hand Me Down",
"Forever Young",
"American Dreams",
"All My Love",
"Cantaloop",
"Take it All",
"Love is Forever",
"Young American",
"Every Breath You Take",
]
song1_1 = "Every Breath You Take"
chaining(songs1, song1_1) => ['Every Breath You Take', 'Take it All', 'All My Love', 'Love is Forever', 'Forever Young', 'Young American', 'American Dreams', 'Dreams']
Additional Input:
song1_2 = "Dreams"
song1_3 = "Blues Hand Me Down"
song1_4 = "Cantaloop"
songs2 = [
"Bye Bye Love",
"Nothing at All",
"Money for Nothing",
"Love Me Do",
"Do You Feel Like We Do",
"Bye Bye Bye",
"Do You Believe in Magic",
"Bye Bye Baby",
"Baby Ride Easy",
"Easy Money",
"All Right Now",
]
song2_1 = "Bye Bye Bye"
song2_2 = "Bye Bye Love"
songs3 = [
"Love Me Do",
"Do You Believe In Magic",
"Magic You Do",
"Magic Man",
"Man In The Mirror"
]
song3_1 = "Love Me Do"
All Test Cases:
chaining(songs1, song1_1) => ['Every Breath You Take', 'Take it All', 'All My Love', 'Love is Forever', 'Forever Young', 'Young American', 'American Dreams', 'Dreams']
chaining(songs1, song1_2) => ['Dreams']
chaining(songs1, song1_3) => ['Blues Hand Me Down', 'Down By the River', 'River of Dreams', 'Dreams']
chaining(songs1, song1_4) => ['Cantaloop']
chaining(songs2, song2_1) => ['Bye Bye Bye', 'Bye Bye Baby', 'Baby Ride Easy', 'Easy Money', 'Money for Nothing', 'Nothing at All', 'All Right Now']
chaining(songs2, song2_2) => ['Bye Bye Love', 'Love Me Do', 'Do You Feel Like We Do', 'Do You Believe in Magic']
chaining(songs3, song3_1) => ['Love Me Do', 'Do You Believe in Magic', 'Magic Man', 'Man In The Mirror']
Complexity Variable:
n = number of songs in the input
*/
const songs1 = [
'Down By the River',
'River of Dreams',
'Take me to the River',
'Dreams',
'Blues Hand Me Down',
'Forever Young',
'Forever and Ever',
'American Dreams',
'All My Love',
'Cantaloop',
'Take it All',
'Love is Forever',
'Young American',
'Every Breath You Take'
];
const song1_1 = 'Every Breath You Take';
const song1_2 = 'Dreams';
const song1_3 = 'Blues Hand Me Down';
const song1_4 = 'Cantaloop';
const songs2 = [
'Bye Bye Love',
'Nothing at All',
'Money for Nothing',
'Love Me Do',
'Do You Feel Like We Do',
'Bye Bye Bye',
'Do You Believe in Magic',
'Bye Bye Baby',
'Baby Ride Easy',
'Easy Money',
'All Right Now'
];
const song2_1 = 'Bye Bye Bye';
const song2_2 = 'Bye Bye Love';
const songs3 = [
'Love Me Do',
'Do You Believe In Magic',
'Magic You Do',
'Magic Man',
'Man In The Mirror'
];
const song3_1 = 'Love Me Do';
function getSongFirstAndLastNode (songTitle) {
let words = songTitle.split(' ');
return { first : words[0], last : words[words.length-1], song:songTitle, connections:[], visited:false };
}
function deepCopy (o){
return JSON.parse(JSON.stringify(o));
}
function toString(ob) {
return JSON.stringify(ob, null, 2);
}
function getSongWordsNodes (songTitles) {
let songsFirstAndLast = [];
songTitles.forEach ((currentElement)=>{
let songFirstAndLast = getSongFirstAndLastNode (currentElement);
songsFirstAndLast.push(songFirstAndLast);
});
return(songsFirstAndLast);
}
// function walkTree(nodes, index, output, prefix) {
// if(nodes[index].visited) {return false;}
// nodes[index].visited = true;
// let nodeCopy = deepCopy(nodes);
// let wentDeeper = false;
// prefix = prefix + "\n" +nodes[index].song;
// console.log(prefix);
// nodes[index].connections.forEach((nextIndex)=>{
// if(nodes[nextIndex].visited) {return false;}
// walkTree(nodeCopy, nextIndex, output, prefix);
// wentDeeper = true;
// });
// // console.log(nodes[index].song);
// if(!wentDeeper){
// console.log("*****");
// output.push(prefix);
// }
// return output;
// }
function walkTree(nodes, index, output, accumulator) {
if(nodes[index].visited) {return false;}
nodes[index].visited = true;
let nodeCopy = deepCopy(nodes);
let wentDeeper = false;
let accumulator2 = [...accumulator];
accumulator2.push(nodes[index].song);
// console.log(toString(accumulator2));
nodes[index].connections.forEach((nextIndex)=>{
if(nodes[nextIndex].visited) {return false;}
walkTree(nodeCopy, nextIndex, output, accumulator2);
wentDeeper = true;
});
// console.log(nodes[index].song);
if(!wentDeeper){
// console.log("*****");
output.push(accumulator2);
}
return output;
}
function getLongestPlaylist(songs, startingSong){
console.log("songs "+ toString(songs));
console.log("start "+ toString(startingSong));
let songNodes = getSongWordsNodes (songs);
let start = -1;
getSongFirstAndLastNode (startingSong);
(node) => node.song == startingSong;
start = songNodes.findIndex( (node) => node.song === startingSong);
if( start == -1){
// not found
}
// console.log("start "+ start);
songNodes.forEach((node, i)=>{
songNodes.forEach((second, j)=>{
if(i == j){return;}
if(node.last == second.first){
node.connections.push(j);
}
})
});
// console.log(toString(songNodes));
let playlists = walkTree(songNodes, start, [], []);
console.log(toString(playlists));
let longestPlaylistIndex = 0;
let longestPlaylist = -1;
playlists.forEach((songs, index)=>{
if(longestPlaylist < songs.length){
longestPlaylistIndex = index;
longestPlaylist = songs.length;
}
});
console.log(`the longest playlist is number ${longestPlaylistIndex+1} at ${longestPlaylist} songs long:`);
console.log("--------------");
console.log(playlists[longestPlaylistIndex]);
console.log("--------------\n");
}
console.log("songlists for songs1, starting with "+song1_1+"\n");
getLongestPlaylist(songs1, song1_1);
console.log("songlists for songs1, starting with "+song1_2+"\n");
getLongestPlaylist(songs1, song1_2);
console.log("songlists for songs1, starting with "+song1_3+"\n");
getLongestPlaylist(songs1, song1_3);
console.log("songlists for songs2, starting with "+song2_1+"\n");
getLongestPlaylist(songs2, song2_1);
console.log("songlists for songs2, starting with "+song2_2+"\n");
getLongestPlaylist(songs2, song2_2);
console.log("songlists for songs3, starting with "+song3_1+"\n");
getLongestPlaylist(songs3, song3_1);