Last active
December 25, 2017 15:34
-
-
Save carl-parrish/b2feeb9f8e5fed3ac29363ec1aff930b to your computer and use it in GitHub Desktop.
Revisions
-
carl-parrish revised this gist
Dec 25, 2017 . No changes.There are no files selected for viewing
-
carl-parrish revised this gist
Dec 8, 2017 . 1 changed file with 8 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -61,9 +61,16 @@ function reverseWords(arr) { ## Third Solution ```js function reverseWords(arr, ans=[], start=0) { //If this is the first time called reverse the entire array if (start === 0) arr.reverse(); //Find the next space that delimits a word let end = (arr.includes(' ', start)) ? arr.indexOf(' ', start): arr.length; //Reverse the word and add it to the ans array ans = [...ans, ...arr.slice(start, end).reverse(), " "]; //If we haven't reached the end of the array repeat with new starting point return (end++ < arr.length)? reverseWords(arr, ans, end++): ans.slice(0,-1); } ``` -
carl-parrish renamed this gist
Dec 8, 2017 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
carl-parrish revised this gist
Dec 8, 2017 . 1 changed file with 5 additions and 5 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -69,11 +69,11 @@ function reverseWords(arr, ans=[], start=0) { ``` ## Results | |Time: | Tests Passed: | Failed: | O() | |-----------|---------|---------------|---------|------| |Solution 1 | 547 ms | 6 | 0 | O(n) | |Solution 2 | 458 ms | 6 | 0 | O(n) | |Solution 3 | 441 ms | 6 | 0 | O(n) | #### Test Case #1 :white_check_mark: ``` -
carl-parrish revised this gist
Dec 8, 2017 . 1 changed file with 5 additions and 4 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -69,10 +69,11 @@ function reverseWords(arr, ans=[], start=0) { ``` ## Results |Time: | Tests Passed: | Failed: | O() | -----------|---------|---------------|---------|------| Solution 1 | 547 ms | 6 | 0 | O(n) | Solution 2 | 458 ms | 6 | 0 | O(n) | Solution 3 | 441 ms | 6 | 0 | O(n) | #### Test Case #1 :white_check_mark: ``` -
carl-parrish revised this gist
Dec 8, 2017 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -31,7 +31,7 @@ output: [ 'p', 'r', 'a', 'c', 't', 'i', 'c', 'e', ' ', function reverseWords(arr) { const str = arr.join('').replace(',',''); //Convert arry to string to split const input = str.split(' '); // Split into words const ans = input.reduceRight((a,b)=> a.concat((b+" ").split('')), []); //reverse words, add space back return ans.slice(0,-1); //trim last space. } ``` -
carl-parrish revised this gist
Dec 5, 2017 . 1 changed file with 0 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -72,7 +72,6 @@ function reverseWords(arr, ans=[], start=0) { Time: 547 ms | Passed: 6 | Failed: 0 | Solution 1 | -------------|-----------|-----------|------------| Time: 458 ms | Passed: 6 | Failed: 0 | Solution 2 | Time: 441 ms | Passed: 6 | Failed: 0 | Solution 3 | #### Test Case #1 :white_check_mark: -
carl-parrish revised this gist
Dec 5, 2017 . 1 changed file with 11 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -58,12 +58,22 @@ function reverseWords(arr) { },[]).slice(0,-1); } ``` ## Third Solution ```js function reverseWords(arr, ans=[], start=0) { if (start ==0) arr.reverse(); //first time let end = (arr.includes(' ', start)) ? arr.indexOf(' ', start): arr.length; ans = [...ans, ...arr.slice(start, end).reverse(), " "]; return (end++ < arr.length)? reverseWords(arr, ans, end++): ans.slice(0,-1); } ``` ## Results Time: 547 ms | Passed: 6 | Failed: 0 | Solution 1 | -------------|-----------|-----------|------------| Time: 458 ms | Passed: 6 | Failed: 0 | Solution 2 | -------------|-----------|-----------|------------| Time: 441 ms | Passed: 6 | Failed: 0 | Solution 3 | #### Test Case #1 :white_check_mark: ``` -
carl-parrish revised this gist
Dec 5, 2017 . 1 changed file with 1 addition and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -55,8 +55,7 @@ function reverseWords(arr) { // Reverse words, break into chars & trim last space return wordsArray.reduceRight((a,b)=> { return [...a, ...b, " "]; },[]).slice(0,-1); } ``` -
carl-parrish revised this gist
Dec 5, 2017 . 1 changed file with 16 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -44,6 +44,22 @@ function reverseWords(arr) { } ``` ## Second Solution with different spacing ```js function reverseWords(arr) { // Combine characters then split on spaces into words const wordsArray = arr.join('') .replace(',','') .split(' '); // Reverse words, break into chars & trim last space return wordsArray.reduceRight((a,b)=> { return [...a, ...b, " "]; } ,[]).slice(0,-1); } ``` ## Results Time: 547 ms | Passed: 6 | Failed: 0 | Solution 1 | -
carl-parrish revised this gist
Dec 5, 2017 . 1 changed file with 4 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -45,9 +45,11 @@ function reverseWords(arr) { ``` ## Results Time: 547 ms | Passed: 6 | Failed: 0 | Solution 1 | -------------|-----------|-----------|------------| Time: 458 ms | Passed: 6 | Failed: 0 | Solution 2 | #### Test Case #1 :white_check_mark: ``` Input: [" "," "] -
carl-parrish revised this gist
Dec 5, 2017 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -46,8 +46,8 @@ function reverseWords(arr) { ## Results Time: 547ms | Passed: 6 |Failed: 0 | Solution 1 | ------------|-----------|----------|------------| #### Test Case #1 :white_check_mark: ``` Input: [" "," "] -
carl-parrish revised this gist
Dec 4, 2017 . 1 changed file with 1 addition and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -23,8 +23,7 @@ output: [ 'p', 'r', 'a', 'c', 't', 'i', 'c', 'e', ' ', - ***[time limit]*** 5000ms - ***[input]*** array.character `arr` - 0 ≤ arr.length ≤ 100 - ***[output]*** array.character ## First Solution -
carl-parrish revised this gist
Dec 4, 2017 . 1 changed file with 9 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -18,6 +18,15 @@ output: [ 'p', 'r', 'a', 'c', 't', 'i', 'c', 'e', ' ', 'p', 'e', 'r', 'f', 'e', 'c', 't' ] ``` ## Constraints: - ***[time limit]*** 5000ms - ***[input]*** array.character `arr` -- 0 ≤ arr.length ≤ 100 - ***[output]*** array.character ## First Solution ```js function reverseWords(arr) { -
carl-parrish revised this gist
Dec 4, 2017 . 1 changed file with 15 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -41,31 +41,39 @@ function reverseWords(arr) { Time: 547ms | Passed: 6 |Failed: 0 ------------|-----------|----------- #### Test Case #1 :white_check_mark: ``` Input: [" "," "] Expected: [" "," "] Actual: [ ' ', ' ' ] ``` #### Test Case #2 :white_check_mark: ``` Input: ["a"," "," ","b"] Expected: ["b"," "," ","a"] Actual: [ 'b', ' ', ' ', 'a' ] ``` #### Test Case #3 :white_check_mark: ``` Input: ["h","e","l","l","o"] Expected: ["h","e","l","l","o"] Actual: [ 'h', 'e', 'l', 'l', 'o' ] ``` #### Test Case #4 :white_check_mark: ``` Input: ["p","e","r","f","e","c","t"," ","m","a","k","e","s"," ","p","r","a","c","t","i","c","e"] Expected: ["p","r","a","c","t","i","c","e"," ","m","a","k","e","s"," ","p","e","r","f","e","c","t"] Actual:[ 'p', 'r', 'a', @@ -88,10 +96,14 @@ Actual:[ 'p', 'e', 'c', 't' ] ``` #### Test Case #5 :white_check_mark: ``` Input: ["y","o","u"," ","w","i","t","h"," ","b","e"," ","f","o","r","c","e"," ","t","h","e"," ","m","a","y"] Expected: ["m","a","y"," ","t","h","e"," ","f","o","r","c","e"," ","b","e"," ","w","i","t","h"," ","y","o","u"] Actual:[ 'm', 'a', 'y', @@ -117,8 +129,10 @@ Actual:[ 'm', 'y', 'o', 'u' ] ``` #### Test Case #6 :white_check_mark: ``` Input: ["g","r","e","a","t","e","s","t"," ","n","a","m","e"," ","f","i","r","s","t"," ","e","v","e","r"," ","n","a","m","e"," ","l","a","s","t"] Expected: ["l","a","s","t"," ","n","a","m","e"," ","e","v","e","r"," ","f","i","r","s","t"," ","n","a","m","e"," ","g","r","e","a","t","e","s","t"] @@ -157,3 +171,4 @@ Actual: [ 'l', 'e', 's', 't' ] ``` -
carl-parrish revised this gist
Dec 4, 2017 . 1 changed file with 106 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -40,16 +40,120 @@ function reverseWords(arr) { Time: 547ms | Passed: 6 |Failed: 0 ------------|-----------|----------- #### Test Case #1 :white_check_mark: Input: [" "," "] Expected: [" "," "] Actual: [ ' ', ' ' ] #### Test Case #2 :white_check_mark: Input: ["a"," "," ","b"] Expected: ["b"," "," ","a"] Actual: [ 'b', ' ', ' ', 'a' ] #### Test Case #3 :white_check_mark: Input: ["h","e","l","l","o"] Expected: ["h","e","l","l","o"] Actual: [ 'h', 'e', 'l', 'l', 'o' ] #### Test Case #4 :white_check_mark: Input: ["p","e","r","f","e","c","t"," ","m","a","k","e","s"," ","p","r","a","c","t","i","c","e"] Expected: ["p","r","a","c","t","i","c","e"," ","m","a","k","e","s"," ","p","e","r","f","e","c","t"] Actual:[ 'p', 'r', 'a', 'c', 't', 'i', 'c', 'e', ' ', 'm', 'a', 'k', 'e', 's', ' ', 'p', 'e', 'r', 'f', 'e', 'c', 't' ] #### Test Case #5 :white_check_mark: Input: ["y","o","u"," ","w","i","t","h"," ","b","e"," ","f","o","r","c","e"," ","t","h","e"," ","m","a","y"] Expected: ["m","a","y"," ","t","h","e"," ","f","o","r","c","e"," ","b","e"," ","w","i","t","h"," ","y","o","u"] Actual:[ 'm', 'a', 'y', ' ', 't', 'h', 'e', ' ', 'f', 'o', 'r', 'c', 'e', ' ', 'b', 'e', ' ', 'w', 'i', 't', 'h', ' ', 'y', 'o', 'u' ] #### Test Case #6 :white_check_mark: Input: ["g","r","e","a","t","e","s","t"," ","n","a","m","e"," ","f","i","r","s","t"," ","e","v","e","r"," ","n","a","m","e"," ","l","a","s","t"] Expected: ["l","a","s","t"," ","n","a","m","e"," ","e","v","e","r"," ","f","i","r","s","t"," ","n","a","m","e"," ","g","r","e","a","t","e","s","t"] Actual: [ 'l', 'a', 's', 't', ' ', 'n', 'a', 'm', 'e', ' ', 'e', 'v', 'e', 'r', ' ', 'f', 'i', 'r', 's', 't', ' ', 'n', 'a', 'm', 'e', ' ', 'g', 'r', 'e', 'a', 't', 'e', 's', 't' ] -
carl-parrish revised this gist
Dec 4, 2017 . 1 changed file with 7 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -46,3 +46,10 @@ Input: [" "," "] Expected: [" "," "] Actual: [ ' ', ' ' ] ### Test Case #2 :white_check_mark: Input: ["a"," "," ","b"] Expected: ["b"," "," ","a"] Actual: [ 'b', ' ', ' ', 'a' ] -
carl-parrish revised this gist
Dec 4, 2017 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -40,7 +40,7 @@ function reverseWords(arr) { Time: 547ms | Passed: 6 |Failed: 0 ------------|-----------|----------- ### Test Case #1 :white_check_mark: Input: [" "," "] Expected: [" "," "] -
carl-parrish revised this gist
Dec 4, 2017 . 1 changed file with 2 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -42,5 +42,7 @@ Time: 547ms | Passed: 6 |Failed: 0 ------------|-----------|----------- ### Test Case #1 Input: [" "," "] Expected: [" "," "] Actual: [ ' ', ' ' ] -
carl-parrish revised this gist
Dec 4, 2017 . 1 changed file with 3 additions and 9 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -41,12 +41,6 @@ function reverseWords(arr) { Time: 547ms | Passed: 6 |Failed: 0 ------------|-----------|----------- ### Test Case #1 Input: [" "," "] Expected: [" "," "] Actual: [ ' ', ' ' ] -
carl-parrish revised this gist
Dec 4, 2017 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -32,7 +32,7 @@ function reverseWords(arr) { ```js function reverseWords(arr) { const wordsArray = arr.join('').replace(',','').split(' '); //Split array into words return wordsArray.reduceRight((a,b)=> [...a, ...b, " "],[]).slice(0,-1); // Reverse words, break into chars & trim last space } ``` -
carl-parrish revised this gist
Dec 4, 2017 . 1 changed file with 2 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,11 +1,12 @@ # Sentence Reverse You are given an array of characters arr that consists of sequences of characters separated by space characters. Each space-delimited sequence of characters defines a word. Implement a function reverseWords that reverses the order of the words in the array in the most efficient manner. Explain your solution and analyze its time and space complexities. ## Example: ``` input: arr = [ 'p', 'e', 'r', 'f', 'e', 'c', 't', ' ', -
carl-parrish revised this gist
Dec 4, 2017 . 1 changed file with 2 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -36,10 +36,10 @@ function reverseWords(arr) { ``` ## Results Time: 547ms | Passed: 6 |Failed: 0 ------------|-----------|----------- ### Test Case #1 Input: [" "," "] @@ -49,4 +49,3 @@ Expected: Actual: [ ' ', ' ' ] -
carl-parrish revised this gist
Dec 4, 2017 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -38,7 +38,7 @@ function reverseWords(arr) { ## Results ``` Time: 547ms | Passed: 6 |Failed: 0 ------------|-----------|----------- Test Case #1 Input: -
carl-parrish revised this gist
Dec 4, 2017 . 1 changed file with 2 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -37,9 +37,8 @@ function reverseWords(arr) { ## Results ``` Time: 547ms | Passed: 6 |Failed: 0 ---------------------------------- Test Case #1 Input: -
carl-parrish revised this gist
Dec 4, 2017 . 1 changed file with 20 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -30,7 +30,24 @@ function reverseWords(arr) { ## Second Solution (does this help or hurt readability?) ```js function reverseWords(arr) { const wordsArray = arr.join('').replace(',','').split(' '); //Split array into words return wordsArray.reduceRight((a,b)=> [...a, ...b, " "],[]).slice(0,-1); // Reverse words trimming last space and spliting back to chars } ``` ## Results ``` Time: 547ms Passed: 6 Failed: 0 Test Case #1 Input: [" "," "] Expected: [" "," "] Actual: [ ' ', ' ' ] ``` -
carl-parrish revised this gist
Dec 4, 2017 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -30,7 +30,7 @@ function reverseWords(arr) { ## Second Solution (does this help or hurt readability?) ```js function reverseWords(arr) { const input = arr.join('').replace(',','').split(' '); //Split array into words return input.reduceRight((a,b)=> [...a, ...b, " "],[]).slice(0,-1); // Reverse words trimming last space } ``` -
carl-parrish revised this gist
Dec 4, 2017 . 1 changed file with 8 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -25,4 +25,12 @@ function reverseWords(arr) { const ans = input.reduceRight((a,b)=> a.concat((b+" ").split('')), []); //revers words add space back return ans.slice(0,-1); //trim last space. } ``` ## Second Solution (does this help or hurt readability?) ```js function reverseWords(arr) { const input = arr.join('').replace(',','').split(' '); //Convert arry to string to split return input.reduceRight((a,b)=> [...a, ...b, " "],[]).slice(0,-1); // Reverse words trimming last space } ``` -
carl-parrish revised this gist
Dec 4, 2017 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -5,7 +5,7 @@ Implement a function reverseWords that reverses the order of the words in the ar Explain your solution and analyze its time and space complexities. # Example: ``` input: arr = [ 'p', 'e', 'r', 'f', 'e', 'c', 't', ' ', @@ -17,7 +17,7 @@ output: [ 'p', 'r', 'a', 'c', 't', 'i', 'c', 'e', ' ', 'p', 'e', 'r', 'f', 'e', 'c', 't' ] ``` ## First Solution ```js function reverseWords(arr) { const str = arr.join('').replace(',',''); //Convert arry to string to split -
carl-parrish renamed this gist
Dec 4, 2017 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes.
NewerOlder