Skip to content

Instantly share code, notes, and snippets.

@carl-parrish
Last active December 25, 2017 15:34
Show Gist options
  • Save carl-parrish/b2feeb9f8e5fed3ac29363ec1aff930b to your computer and use it in GitHub Desktop.
Save carl-parrish/b2feeb9f8e5fed3ac29363ec1aff930b to your computer and use it in GitHub Desktop.

Revisions

  1. carl-parrish revised this gist Dec 25, 2017. No changes.
  2. carl-parrish revised this gist Dec 8, 2017. 1 changed file with 8 additions and 1 deletion.
    9 changes: 8 additions & 1 deletion sentenceReverse.md
    Original 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 (start ==0) arr.reverse(); //first time
    //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);
    }
    ```
  3. carl-parrish renamed this gist Dec 8, 2017. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  4. carl-parrish revised this gist Dec 8, 2017. 1 changed file with 5 additions and 5 deletions.
    10 changes: 5 additions & 5 deletions gistfile1.md
    Original 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) |
    | |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:
    ```
  5. carl-parrish revised this gist Dec 8, 2017. 1 changed file with 5 additions and 4 deletions.
    9 changes: 5 additions & 4 deletions gistfile1.md
    Original file line number Diff line number Diff line change
    @@ -69,10 +69,11 @@ function reverseWords(arr, ans=[], start=0) {
    ```
    ## 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 |
    |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:
    ```
  6. carl-parrish revised this gist Dec 8, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion gistfile1.md
    Original 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('')), []); //revers words add space back
    const ans = input.reduceRight((a,b)=> a.concat((b+" ").split('')), []); //reverse words, add space back
    return ans.slice(0,-1); //trim last space.
    }
    ```
  7. carl-parrish revised this gist Dec 5, 2017. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion gistfile1.md
    Original 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:
  8. carl-parrish revised this gist Dec 5, 2017. 1 changed file with 11 additions and 1 deletion.
    12 changes: 11 additions & 1 deletion gistfile1.md
    Original 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:
    ```
  9. carl-parrish revised this gist Dec 5, 2017. 1 changed file with 1 addition and 2 deletions.
    3 changes: 1 addition & 2 deletions gistfile1.md
    Original 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);
    },[]).slice(0,-1);
    }
    ```

  10. carl-parrish revised this gist Dec 5, 2017. 1 changed file with 16 additions and 0 deletions.
    16 changes: 16 additions & 0 deletions gistfile1.md
    Original 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 |
  11. carl-parrish revised this gist Dec 5, 2017. 1 changed file with 4 additions and 2 deletions.
    6 changes: 4 additions & 2 deletions gistfile1.md
    Original 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 |

    Time: 547ms | Passed: 6 |Failed: 0 | Solution 1 |
    ------------|-----------|----------|------------|
    #### Test Case #1 :white_check_mark:
    ```
    Input: [" "," "]
  12. carl-parrish revised this gist Dec 5, 2017. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions gistfile1.md
    Original file line number Diff line number Diff line change
    @@ -46,8 +46,8 @@ function reverseWords(arr) {

    ## Results

    Time: 547ms | Passed: 6 |Failed: 0
    ------------|-----------|-----------
    Time: 547ms | Passed: 6 |Failed: 0 | Solution 1 |
    ------------|-----------|----------|------------|
    #### Test Case #1 :white_check_mark:
    ```
    Input: [" "," "]
  13. carl-parrish revised this gist Dec 4, 2017. 1 changed file with 1 addition and 2 deletions.
    3 changes: 1 addition & 2 deletions gistfile1.md
    Original 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
    - 0 ≤ arr.length ≤ 100
    - ***[output]*** array.character

    ## First Solution
  14. carl-parrish revised this gist Dec 4, 2017. 1 changed file with 9 additions and 0 deletions.
    9 changes: 9 additions & 0 deletions gistfile1.md
    Original 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) {
  15. carl-parrish revised this gist Dec 4, 2017. 1 changed file with 15 additions and 0 deletions.
    15 changes: 15 additions & 0 deletions gistfile1.md
    Original 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' ]
    ```
  16. carl-parrish revised this gist Dec 4, 2017. 1 changed file with 106 additions and 2 deletions.
    108 changes: 106 additions & 2 deletions gistfile1.md
    Original 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:
    #### Test Case #1 :white_check_mark:
    Input: [" "," "]

    Expected: [" "," "]

    Actual: [ ' ', ' ' ]

    ### Test Case #2 :white_check_mark:
    #### 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' ]
  17. carl-parrish revised this gist Dec 4, 2017. 1 changed file with 7 additions and 0 deletions.
    7 changes: 7 additions & 0 deletions gistfile1.md
    Original 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' ]
  18. carl-parrish revised this gist Dec 4, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion gistfile1.md
    Original 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
    ### Test Case #1 :white_check_mark:
    Input: [" "," "]

    Expected: [" "," "]
  19. carl-parrish revised this gist Dec 4, 2017. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions gistfile1.md
    Original 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: [ ' ', ' ' ]
  20. carl-parrish revised this gist Dec 4, 2017. 1 changed file with 3 additions and 9 deletions.
    12 changes: 3 additions & 9 deletions gistfile1.md
    Original 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:

    [ ' ', ' ' ]
    Input: [" "," "]
    Expected: [" "," "]
    Actual: [ ' ', ' ' ]
  21. carl-parrish revised this gist Dec 4, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion gistfile1.md
    Original 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 trimming last space and spliting back to chars
    return wordsArray.reduceRight((a,b)=> [...a, ...b, " "],[]).slice(0,-1); // Reverse words, break into chars & trim last space
    }
    ```

  22. carl-parrish revised this gist Dec 4, 2017. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion gistfile1.md
    Original 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:
    ## Example:

    ```
    input: arr = [ 'p', 'e', 'r', 'f', 'e', 'c', 't', ' ',
  23. carl-parrish revised this gist Dec 4, 2017. 1 changed file with 2 additions and 3 deletions.
    5 changes: 2 additions & 3 deletions gistfile1.md
    Original 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
    ### Test Case #1
    Input:

    [" "," "]
    @@ -49,4 +49,3 @@ Expected:
    Actual:

    [ ' ', ' ' ]
    ```
  24. carl-parrish revised this gist Dec 4, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion gistfile1.md
    Original 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:
  25. carl-parrish revised this gist Dec 4, 2017. 1 changed file with 2 additions and 3 deletions.
    5 changes: 2 additions & 3 deletions gistfile1.md
    Original file line number Diff line number Diff line change
    @@ -37,9 +37,8 @@ function reverseWords(arr) {

    ## Results
    ```
    Time: 547ms
    Passed: 6
    Failed: 0
    Time: 547ms | Passed: 6 |Failed: 0
    ----------------------------------
    Test Case #1
    Input:
  26. carl-parrish revised this gist Dec 4, 2017. 1 changed file with 20 additions and 3 deletions.
    23 changes: 20 additions & 3 deletions gistfile1.md
    Original 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 input = arr.join('').replace(',','').split(' '); //Split array into words
    return input.reduceRight((a,b)=> [...a, ...b, " "],[]).slice(0,-1); // Reverse words trimming last space
    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:
    [ ' ', ' ' ]
    ```
  27. carl-parrish revised this gist Dec 4, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion gistfile1.md
    Original 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(' '); //Convert arry to string to split
    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
    }
    ```
  28. carl-parrish revised this gist Dec 4, 2017. 1 changed file with 8 additions and 0 deletions.
    8 changes: 8 additions & 0 deletions gistfile1.md
    Original 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
    }
    ```
  29. carl-parrish revised this gist Dec 4, 2017. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions gistfile1.md
    Original 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:
    # 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
    ## First Solution
    ```js
    function reverseWords(arr) {
    const str = arr.join('').replace(',',''); //Convert arry to string to split
  30. carl-parrish renamed this gist Dec 4, 2017. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.