@@ -28,50 +28,48 @@ function getSequences(root) {
2828function weave ( prefix = [ ] , arrays1 = [ [ ] ] , arrays2 = [ [ ] ] ) {
2929 let result = [ ] ;
3030
31- // TODO: validations of prefix as an array and array1/2 as a nested array
32-
33- // convert prefix to array if is not any
34- if ( prefix && ! Array . isArray ( prefix ) ) {
35- prefix = [ prefix ] ;
31+ // in case of two arguments let's asume it is for array 1 and 2 and that prefix is empty
32+ if ( arguments . length === 2 ) {
33+ [ arrays1 , arrays2 ] = [ prefix , arrays1 ] ;
34+ prefix = [ ] ;
3635 }
3736
37+ // make sure arrays 1 and 2 are an array of arrays and prefix is an array
38+ prefix = Array . isArray ( prefix ) ? prefix : [ prefix ] ;
39+ arrays1 = Array . isArray ( arrays1 [ 0 ] ) ? arrays1 : [ arrays1 ] ;
40+ arrays2 = Array . isArray ( arrays2 [ 0 ] ) ? arrays2 : [ arrays2 ] ;
41+
42+ console . log ( '*weave (' , prefix , arrays1 , arrays2 , ')' ) ;
43+
44+ // process weaving recursively
3845 arrays1 . forEach ( ( array1 ) => {
3946 arrays2 . forEach ( ( array2 ) => {
4047
4148 if ( ! array1 . length && ! array2 . length ) {
4249 result . push ( prefix ) ;
4350
4451 } else if ( ! array1 . length ) {
45- result = result . concat ( weave ( prefix . concat ( array2 ) , [ array1 ] ) ) ;
52+ result = result . concat ( weave ( prefix . concat ( array2 ) , [ array1 ] , [ ] ) ) ;
4653
4754 } else if ( ! array2 . length ) {
48- result = result . concat ( weave ( prefix . concat ( array1 ) , [ array2 ] ) ) ;
55+ result = result . concat ( weave ( prefix . concat ( array1 ) , [ ] , [ array2 ] ) ) ;
4956 } else {
5057 // weave the arrays
51- array2 . forEach ( function ( _ , i2 ) {
52- array1 . forEach ( function ( _ , i1 ) {
53- results . push ( prefix . concat ( array1 . slice ( i1 ) , array2 . slice ( 0 , i2 + 1 ) , array1 . slice ( ) , array2 . slice ( ) ) ) ;
54- } ) ;
58+ array1 . forEach ( function ( _ , i , array ) {
59+ if ( array . length > 1 && i >= Math . ceil ( array . length / 2 ) ) { return ; } // avoid repetition
60+ result = result . concat ( weave ( prefix . concat ( array1 . slice ( 0 , i + 1 ) ) , [ array1 . slice ( i + 1 ) ] , [ array2 ] ) ) ;
5561 } ) ;
5662
57- // array1.forEach((_, i, array) => {
58- // // if(array.slice(i+1).length == 1) { return; }
59- // result = result.concat(weave(prefix.concat(array1.slice(0, i + 1)), [array1.slice(i + 1)], [array2]));
60- // });
61- //
62- // array2.forEach((_, i, array) => {
63- // // if(array.slice(i+1).length == 1) { return; }
64- // result = result.concat(weave(prefix.concat(array2.slice(0, i + 1)), [array1], [array2.slice(i + 1)]));
65- // });
66-
67- // result = result.concat(weave(prefix.concat(array1), [array2]));
68- // result = result.concat(weave(prefix.concat(array2), [array1]));
63+ array2 . forEach ( function ( _ , i , array ) {
64+ if ( array . length > 1 && i >= Math . ceil ( array . length / 2 ) ) { return ; } // avoid repetition
65+ result = result . concat ( weave ( prefix . concat ( array2 . slice ( 0 , i + 1 ) ) , [ array1 ] , [ array2 . slice ( i + 1 ) ] ) ) ;
66+ } ) ;
6967 }
7068 } ) ;
7169 } ) ;
7270
73- console . log ( '\nweave (' , prefix , arrays1 , arrays2 , ')' ) ;
74- console . log ( '\t =>' , result ) ;
71+ console . log ( '\tweave (' , prefix , arrays1 , arrays2 , ')' ) ;
72+ console . log ( '\t\t =>' , result , '\n' ) ;
7573
7674 return result ;
7775}
0 commit comments