Skip to content

Instantly share code, notes, and snippets.

@wmjd
Last active April 18, 2019 22:32
Show Gist options
  • Select an option

  • Save wmjd/da6eeb32c99ddcff74541c185d9f6134 to your computer and use it in GitHub Desktop.

Select an option

Save wmjd/da6eeb32c99ddcff74541c185d9f6134 to your computer and use it in GitHub Desktop.

Revisions

  1. wmjd revised this gist Apr 18, 2019. 1 changed file with 7 additions and 7 deletions.
    14 changes: 7 additions & 7 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -62,13 +62,13 @@ function isIncreasingIfRemoveOne(next, prev, dec){
    console.log(isIncreasingIfRemoveOne(1,0,0));

    //current favorite : )
    const v4 = (n, p, d) =>
    (d > 1) ?
    const isIncIfRemOne = (next, prev, dec) =>
    (dec > 1) ?
    "reject" :
    (arr.length === n) ?
    (arr.length === next) ?
    "accept" :
    (arr[n] < arr[p]) ?
    v4(n+1,p,d+1) :
    v4(n+1,n,d) ;
    (arr[next] < arr[prev]) ?
    isIncIfRemOne(next+1, prev, dec+1) :
    isIncIfRemOne(next+1, next, dec) ;

    console.log(v5(1,0,0));
    console.log(isIncIfRemOne(1,0,0));
  2. wmjd revised this gist Apr 18, 2019. 1 changed file with 8 additions and 5 deletions.
    13 changes: 8 additions & 5 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -62,10 +62,13 @@ function isIncreasingIfRemoveOne(next, prev, dec){
    console.log(isIncreasingIfRemoveOne(1,0,0));

    //current favorite : )
    const v5 = (n, p, d) =>
    (d > 1) ? "reject" :
    (arr.length === n) ? "accept" :
    (arr[n] < arr[p]) ? v5(n+1,p,d+1)
    : v5(n+1,n,d);
    const v4 = (n, p, d) =>
    (d > 1) ?
    "reject" :
    (arr.length === n) ?
    "accept" :
    (arr[n] < arr[p]) ?
    v4(n+1,p,d+1) :
    v4(n+1,n,d) ;

    console.log(v5(1,0,0));
  3. wmjd revised this gist Apr 18, 2019. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -65,7 +65,7 @@ console.log(isIncreasingIfRemoveOne(1,0,0));
    const v5 = (n, p, d) =>
    (d > 1) ? "reject" :
    (arr.length === n) ? "accept" :
    (arr[n] < arr[p]) ?
    v5(n+1,p,d+1) : v5(n+1,n,d);
    (arr[n] < arr[p]) ? v5(n+1,p,d+1)
    : v5(n+1,n,d);

    console.log(v5(1,0,0));
  4. wmjd revised this gist Apr 18, 2019. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -62,10 +62,10 @@ function isIncreasingIfRemoveOne(next, prev, dec){
    console.log(isIncreasingIfRemoveOne(1,0,0));

    //current favorite : )
    const v4 = (n, p, d) =>
    const v5 = (n, p, d) =>
    (d > 1) ? "reject" :
    (arr.length === n) ? "accept" :
    (arr[n] < arr[p]) ?
    v4(n+1,p,d+1) : v4(n+1,n,d);
    v5(n+1,p,d+1) : v5(n+1,n,d);

    console.log(v4(1,0,0));
    console.log(v5(1,0,0));
  5. wmjd revised this gist Apr 18, 2019. 1 changed file with 10 additions and 1 deletion.
    11 changes: 10 additions & 1 deletion gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -59,4 +59,13 @@ function isIncreasingIfRemoveOne(next, prev, dec){
    }
    }

    console.log(isIncreasingIfRemoveOne(1,0,0));
    console.log(isIncreasingIfRemoveOne(1,0,0));

    //current favorite : )
    const v4 = (n, p, d) =>
    (d > 1) ? "reject" :
    (arr.length === n) ? "accept" :
    (arr[n] < arr[p]) ?
    v4(n+1,p,d+1) : v4(n+1,n,d);

    console.log(v4(1,0,0));
  6. wmjd revised this gist Apr 18, 2019. 1 changed file with 23 additions and 1 deletion.
    24 changes: 23 additions & 1 deletion gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -37,4 +37,26 @@ const bar = (i, dec) =>
    (i === arr.length) ? "succeed" :
    bar(i+1, (arr[i+1] < arr[i]) ? dec+1 : dec);

    console.log(bar(0,0));
    console.log(bar(0,0));

    /* v4
    this is a soln to a slightly different problem.
    for example, [0,1,2,0,1] only decreases once but removing one elt won't make strictly increasing
    */

    function isIncreasingIfRemoveOne(next, prev, dec){
    if(dec > 1){
    console.log(next,prev,dec);
    return "reject" ;
    }
    if(next === arr.length){
    return "accept" ;
    }
    if(arr[next] < arr[prev]){
    return isIncreasingIfRemoveOne(next+1, prev, dec+1) ;
    }else{
    return isIncreasingIfRemoveOne(next+1, next, dec) ;
    }
    }

    console.log(isIncreasingIfRemoveOne(1,0,0));
  7. wmjd revised this gist Apr 18, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    //see if sequence of nums are in increasing order, but seq is allowed to decrease once.
    //see if sequence of nums is in increasing order, but seq is allowed to decrease once.
    //some shared stuff first:
    var arr = [0,1,2,3,5,4,5,7];
    //currently these two functions are only used in v1 (v2-3 prints value of entire function)
  8. wmjd revised this gist Apr 18, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    //see if sequence of nums are in increasing order, but it is allowed to decrease once.
    //see if sequence of nums are in increasing order, but seq is allowed to decrease once.
    //some shared stuff first:
    var arr = [0,1,2,3,5,4,5,7];
    //currently these two functions are only used in v1 (v2-3 prints value of entire function)
  9. wmjd revised this gist Apr 18, 2019. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,4 @@
    //see if sequence of nums are in increasing order, but it is allowed to decrease once.
    //some shared stuff first:
    var arr = [0,1,2,3,5,4,5,7];
    //currently these two functions are only used in v1 (v2-3 prints value of entire function)
  10. wmjd revised this gist Apr 18, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    //some shared stuff first:
    var arr = [0,1,2,3,5,4,5,7];

    //currently these two functions are only used in v1 (v2-3 prints value of entire function)
    const s = () => console.log("success");
    const f = () => console.log("failure");

  11. wmjd revised this gist Apr 18, 2019. 1 changed file with 18 additions and 15 deletions.
    33 changes: 18 additions & 15 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -5,32 +5,35 @@ const s = () => console.log("success");
    const f = () => console.log("failure");

    //v1 imperative style
    let dec = 0;
    for(var i = 0; i < arr.length -1; i++){
    if(arr[i+1] < arr[i]){
    if(++dec > 1){break ;}
    function exeImperative(){
    let dec = 0;
    for(var i = 0; i < arr.length -1; i++){
    if(arr[i+1] < arr[i]){
    if(++dec > 1){break ;}
    }
    }
    if(i === arr.length -1){
    s();
    }else{
    f();
    }
    }
    if(i === arr.length -1){
    s();
    }else{
    f();
    }
    exeImperative();

    //v2 transformed to functional style: recursion, no (re)assignments
    const foo = function(i, dec){
    if(dec > 1){return f()}
    if(i === arr.length){return s()}
    if(dec > 1){return "fail"}
    if(i === arr.length){return "succeed"}
    if(arr[i+1]<arr[i]){return foo(i+1, dec+1)}
    else{return foo(i+1, dec)}
    };

    foo_intermediate(0,0);
    console.log(foo(0,0));

    //v3 transformed to single statement : )
    const bar = (i, dec) =>
    (dec > 1) ? f() :
    (i === arr.length) ? s() :
    (dec > 1) ? "fail" :
    (i === arr.length) ? "succeed" :
    bar(i+1, (arr[i+1] < arr[i]) ? dec+1 : dec);

    bar(0,0);
    console.log(bar(0,0));
  12. wmjd revised this gist Apr 18, 2019. 1 changed file with 5 additions and 5 deletions.
    10 changes: 5 additions & 5 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -18,19 +18,19 @@ if(i === arr.length -1){
    }

    //v2 transformed to functional style: recursion, no (re)assignments
    const foo_intermediate = function(i, dec){
    const foo = function(i, dec){
    if(dec > 1){return f()}
    if(i === arr.length){return s()}
    if(arr[i+1]<[i]){return foo(i+1, dec+1)}
    if(arr[i+1]<arr[i]){return foo(i+1, dec+1)}
    else{return foo(i+1, dec)}
    };

    foo_intermediate(0,0);

    //v3 transformed to single statement : )
    const foo = (i, dec) =>
    const bar = (i, dec) =>
    (dec > 1) ? f() :
    (i === arr.length) ? s() :
    foo(i+1, (arr[i+1] < arr[i]) ? dec+1 : dec);
    bar(i+1, (arr[i+1] < arr[i]) ? dec+1 : dec);

    foo(0,0);
    bar(0,0);
  13. wmjd revised this gist Apr 18, 2019. 1 changed file with 1 addition and 2 deletions.
    3 changes: 1 addition & 2 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -8,8 +8,7 @@ const f = () => console.log("failure");
    let dec = 0;
    for(var i = 0; i < arr.length -1; i++){
    if(arr[i+1] < arr[i]){
    dec++;
    if(dec > 1){break ;}
    if(++dec > 1){break ;}
    }
    }
    if(i === arr.length -1){
  14. wmjd revised this gist Apr 18, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -18,7 +18,7 @@ if(i === arr.length -1){
    f();
    }

    //v2 transformed to functional style: recursion, no assignments
    //v2 transformed to functional style: recursion, no (re)assignments
    const foo_intermediate = function(i, dec){
    if(dec > 1){return f()}
    if(i === arr.length){return s()}
  15. wmjd revised this gist Apr 18, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -28,7 +28,7 @@ const foo_intermediate = function(i, dec){

    foo_intermediate(0,0);

    //v3 transformed to single expression : )
    //v3 transformed to single statement : )
    const foo = (i, dec) =>
    (dec > 1) ? f() :
    (i === arr.length) ? s() :
  16. wmjd revised this gist Apr 18, 2019. 1 changed file with 20 additions and 20 deletions.
    40 changes: 20 additions & 20 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -4,25 +4,7 @@ var arr = [0,1,2,3,5,4,5,7];
    const s = () => console.log("success");
    const f = () => console.log("failure");

    /////////////////////////////////////////////////
    const foo = (i, dec) =>
    (dec > 1) ? f() :
    (i === arr.length) ? s() :
    foo(i+1, (arr[i+1] < arr[i]) ? dec+1 : dec);

    foo(0,0);

    /////////////////////////////////////////////////
    const foo_intermediate = function(i, dec){
    if(dec > 1){return f()}
    if(i === arr.length){return s()}
    if(arr[i+1]<[i]){return foo(i+1, dec+1)}
    else{return foo(i+1, dec)}
    };

    foo_intermediate(0,0);

    /////////////////////////////////////////////////
    //v1 imperative style
    let dec = 0;
    for(var i = 0; i < arr.length -1; i++){
    if(arr[i+1] < arr[i]){
    @@ -34,4 +16,22 @@ if(i === arr.length -1){
    s();
    }else{
    f();
    }
    }

    //v2 transformed to functional style: recursion, no assignments
    const foo_intermediate = function(i, dec){
    if(dec > 1){return f()}
    if(i === arr.length){return s()}
    if(arr[i+1]<[i]){return foo(i+1, dec+1)}
    else{return foo(i+1, dec)}
    };

    foo_intermediate(0,0);

    //v3 transformed to single expression : )
    const foo = (i, dec) =>
    (dec > 1) ? f() :
    (i === arr.length) ? s() :
    foo(i+1, (arr[i+1] < arr[i]) ? dec+1 : dec);

    foo(0,0);
  17. wmjd revised this gist Apr 18, 2019. No changes.
  18. wmjd created this gist Apr 18, 2019.
    37 changes: 37 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,37 @@
    //some shared stuff first:
    var arr = [0,1,2,3,5,4,5,7];

    const s = () => console.log("success");
    const f = () => console.log("failure");

    /////////////////////////////////////////////////
    const foo = (i, dec) =>
    (dec > 1) ? f() :
    (i === arr.length) ? s() :
    foo(i+1, (arr[i+1] < arr[i]) ? dec+1 : dec);

    foo(0,0);

    /////////////////////////////////////////////////
    const foo_intermediate = function(i, dec){
    if(dec > 1){return f()}
    if(i === arr.length){return s()}
    if(arr[i+1]<[i]){return foo(i+1, dec+1)}
    else{return foo(i+1, dec)}
    };

    foo_intermediate(0,0);

    /////////////////////////////////////////////////
    let dec = 0;
    for(var i = 0; i < arr.length -1; i++){
    if(arr[i+1] < arr[i]){
    dec++;
    if(dec > 1){break ;}
    }
    }
    if(i === arr.length -1){
    s();
    }else{
    f();
    }