Skip to content

Instantly share code, notes, and snippets.

@vitaliiznak
Last active March 22, 2018 15:18
Show Gist options
  • Select an option

  • Save vitaliiznak/1bf50f6ff582495be9a5cb05388dc281 to your computer and use it in GitHub Desktop.

Select an option

Save vitaliiznak/1bf50f6ff582495be9a5cb05388dc281 to your computer and use it in GitHub Desktop.

Revisions

  1. vitaliiznak revised this gist Mar 22, 2018. 1 changed file with 1 addition and 3 deletions.
    4 changes: 1 addition & 3 deletions bfs_search_excersice.js
    Original file line number Diff line number Diff line change
    @@ -49,7 +49,6 @@ function countStepsImperatively(startCoords, digitsSumLimit = 21) {

    while (queue.length > 0) {
    let coords = queue.pop();
    console.log(JSON.stringify(coords));
    ++steps;

    coords.forEach((coord, index, coordsArray) => {
    @@ -143,12 +142,11 @@ console.log(
    /**
    * return 13, number of points in 2 dimensional space
    * points are - [0,0] [0,-1] [0,-2] [-1,-1] [1,-1] [0,1] [0,2] [-1,1] [1,1] [-1,0] [-2,0] [1,0] [2,0]
    */
    */

    console.log(
    "countStepsImperatively([0,0], 21) return",
    countStepsImperatively([0, 0], 21)
    );
    //a lot of points I will not paste it here
    //return 287881

  2. vitaliiznak revised this gist Mar 22, 2018. 1 changed file with 8 additions and 2 deletions.
    10 changes: 8 additions & 2 deletions bfs_search_excersice.js
    Original file line number Diff line number Diff line change
    @@ -49,6 +49,7 @@ function countStepsImperatively(startCoords, digitsSumLimit = 21) {

    while (queue.length > 0) {
    let coords = queue.pop();
    console.log(JSON.stringify(coords));
    ++steps;

    coords.forEach((coord, index, coordsArray) => {
    @@ -137,12 +138,17 @@ console.log(

    console.log(
    "countStepsImperatively([0,0], 1) return",
    countStepsImperatively([0, 0], 1)
    countStepsImperatively([0, 0], 2)
    );
    //return 5, trivial points with coordinate [0,0], [0,1], [0,-1], [-1,0] in 3 dimensional space
    /**
    * return 13, number of points in 2 dimensional space
    * points are - [0,0] [0,-1] [0,-2] [-1,-1] [1,-1] [0,1] [0,2] [-1,1] [1,1] [-1,0] [-2,0] [1,0] [2,0]
    */

    console.log(
    "countStepsImperatively([0,0], 21) return",
    countStepsImperatively([0, 0], 21)
    );
    //a lot of points I will not paste it here
    //return 287881

  3. vitaliiznak revised this gist Mar 22, 2018. 1 changed file with 7 additions and 6 deletions.
    13 changes: 7 additions & 6 deletions bfs_search_excersice.js
    Original file line number Diff line number Diff line change
    @@ -130,18 +130,19 @@ function countStepsRecursively(
    }

    console.log(
    "countStepsImperatively([0,0], 1) gives",
    "countStepsImperatively([0,0], 1) return",
    countStepsImperatively([0], 1)
    );
    //gives 3, trivial points with coordinates [0],[-1],[1] in 1 dimensional space
    //return 3, trivial points with coordinates [0],[-1],[1] in 1 dimensional space

    console.log(
    "countStepsImperatively([0,0], 1) gives",
    "countStepsImperatively([0,0], 1) return",
    countStepsImperatively([0, 0], 1)
    );
    //gives 3, trivial points with coordinate [0,0], [0,1], [0,-1], [-1,0] in 3 dimensional space
    //return 5, trivial points with coordinate [0,0], [0,1], [0,-1], [-1,0] in 3 dimensional space

    console.log(
    "countStepsImperatively([0,0], 21) gives",
    countStepsImperatively([0, 0, 0], 21)
    "countStepsImperatively([0,0], 21) return",
    countStepsImperatively([0, 0], 21)
    );
    //return 287881
  4. vitaliiznak revised this gist Mar 22, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion bfs_search_excersice.js
    Original file line number Diff line number Diff line change
    @@ -32,7 +32,7 @@ const coordsDigitsCalcSum = coords =>
    .reduce((acc, val) => acc + val, 0);

    /**
    * Represents a book.
    *
    * @constructor
    * @param {[number,...]} coords - The coordinatesof a starting point for an arbytrary dimension
    * f.e [0,0] - 2 dimesnional space, [0,0,0] -threee dimensional space
  5. vitaliiznak revised this gist Mar 22, 2018. 1 changed file with 6 additions and 0 deletions.
    6 changes: 6 additions & 0 deletions bfs_search_excersice.js
    Original file line number Diff line number Diff line change
    @@ -78,6 +78,12 @@ function countStepsImperatively(startCoords, digitsSumLimit = 21) {
    return steps;
    }

    /**
    * For relatively large nambers you will recive a stack overflow error in your machine
    * @param {*} coords
    * @param {*} digitsSumLimit
    * @param {*} processedNodes
    */
    function countStepsRecursively(
    coords,
    digitsSumLimit = 21,
  6. vitaliiznak revised this gist Mar 22, 2018. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions bfs_search_excersice.js
    Original file line number Diff line number Diff line change
    @@ -127,13 +127,13 @@ console.log(
    "countStepsImperatively([0,0], 1) gives",
    countStepsImperatively([0], 1)
    );
    //gives 3, trivial points with coordinates [0],[-1],[1] in 1 dimension space
    //gives 3, trivial points with coordinates [0],[-1],[1] in 1 dimensional space

    console.log(
    "countStepsImperatively([0,0], 1) gives",
    countStepsImperatively([0, 0], 1)
    );
    //gives 3, trivial points with coordinate [0,0], [0,1], [0,-1], [-1,0] in 3 dimension space
    //gives 3, trivial points with coordinate [0,0], [0,1], [0,-1], [-1,0] in 3 dimensional space

    console.log(
    "countStepsImperatively([0,0], 21) gives",
  7. vitaliiznak revised this gist Mar 22, 2018. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions bfs_search_excersice.js
    Original file line number Diff line number Diff line change
    @@ -127,15 +127,15 @@ console.log(
    "countStepsImperatively([0,0], 1) gives",
    countStepsImperatively([0], 1)
    );
    //gives 3, trivial points with coordinates [0],[-1],[1]
    //gives 3, trivial points with coordinates [0],[-1],[1] in 1 dimension space

    console.log(
    "countStepsImperatively([0,0], 1) gives",
    countStepsImperatively([0, 0], 1)
    );
    //gives 3, trivial points with coordinate [0,0], [0,1], [0,-1], [-1,0]
    //gives 3, trivial points with coordinate [0,0], [0,1], [0,-1], [-1,0] in 3 dimension space

    console.log(
    "countStepsImperatively([0,0], 21) gives",
    countStepsImperatively([0, 0], 21)
    countStepsImperatively([0, 0, 0], 21)
    );
  8. vitaliiznak revised this gist Mar 22, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion bfs_search_excersice.js
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    /**
    * TASK DESCRIPTIONJ:
    * TASK DESCRIPTION
    *
    * Consider 2 dimensional space, with discreate coordinates
    * - a given starting point ex. (0, 0)
  9. vitaliiznak revised this gist Mar 22, 2018. 1 changed file with 27 additions and 0 deletions.
    27 changes: 27 additions & 0 deletions bfs_search_excersice.js
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,23 @@
    /**
    * TASK DESCRIPTIONJ:
    *
    * Consider 2 dimensional space, with discreate coordinates
    * - a given starting point ex. (0, 0)
    * - you allow to move as (x+1, y), (x-1,y), (x, y+1) , (x,y-1)
    * - RESTRICTION: you can not step into the point if the absolute number of a coordinates digits are grater than some N
    * f.e if N=21 and a point is (59, -79), sum of digits in coordinates os (5 + 9 + 7 + 9) = 30 > 27 (N)
    * the point is prohibited
    * - you can not step over the points
    *
    * WHAT TO DO:
    * Create a function which recives as input
    * - arbitrary starting point for arbitrary space dimension
    * - sum digits limit
    * And outputs the number of points accessible from a starting point
    *
    *
    * */

    const coordsDigitsCalcSum = coords =>
    coords
    .reduce(
    @@ -11,6 +31,13 @@ const coordsDigitsCalcSum = coords =>
    )
    .reduce((acc, val) => acc + val, 0);

    /**
    * Represents a book.
    * @constructor
    * @param {[number,...]} coords - The coordinatesof a starting point for an arbytrary dimension
    * f.e [0,0] - 2 dimesnional space, [0,0,0] -threee dimensional space
    * @param {number} number
    */
    function countStepsImperatively(startCoords, digitsSumLimit = 21) {
    const queue = [];

  10. vitaliiznak created this gist Mar 22, 2018.
    114 changes: 114 additions & 0 deletions bfs_search_excersice.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,114 @@
    const coordsDigitsCalcSum = coords =>
    coords
    .reduce(
    (acc, el) =>
    Math.abs(el)
    .toString(10)
    .split("")
    .map(num => Number(num))
    .concat(acc),
    []
    )
    .reduce((acc, val) => acc + val, 0);

    function countStepsImperatively(startCoords, digitsSumLimit = 21) {
    const queue = [];

    let startCoordsDigitsSum = coordsDigitsCalcSum(startCoords);
    if (startCoordsDigitsSum > digitsSumLimit) return 0;
    let steps = 0;
    queue.push(startCoords);
    const processedNodes = { [startCoords]: [startCoords] };

    while (queue.length > 0) {
    let coords = queue.pop();
    ++steps;

    coords.forEach((coord, index, coordsArray) => {
    const newCoordsPlus = coordsArray.slice();
    newCoordsPlus[index] = coord + 1;
    let coordsPlusDigitsSum = coordsDigitsCalcSum(newCoordsPlus);
    if (
    coordsPlusDigitsSum <= digitsSumLimit &&
    !processedNodes[newCoordsPlus]
    ) {
    queue.push(newCoordsPlus);
    processedNodes[newCoordsPlus] = newCoordsPlus;
    }

    const newCoordsMinus = coordsArray.slice();
    newCoordsMinus[index] = coord - 1;
    let coordsMinusDigitsSum = coordsDigitsCalcSum(newCoordsMinus);
    if (
    coordsMinusDigitsSum <= digitsSumLimit &&
    !processedNodes[newCoordsMinus]
    ) {
    queue.push(newCoordsMinus);
    processedNodes[newCoordsMinus] = newCoordsMinus;
    }
    });
    }
    return steps;
    }

    function countStepsRecursively(
    coords,
    digitsSumLimit = 21,
    processedNodes = {}
    ) {
    if (processedNodes[coords]) {
    return 0;
    }
    processedNodes[coords] = coords;
    const digitsSum = coordsDigitsCalcSum(coords);

    if (digitsSum > digitsSumLimit) {
    return 0;
    }
    const [x, y] = coords;
    return (
    1 +
    coords
    .map((coord, index, coordsArray) => {
    const newCoordsPlus = coordsArray.slice();
    newCoordsPlus[index] = coord + 1;

    const newCoordsMinus = coordsArray.slice();
    newCoordsMinus[index] = coord - 1;
    return (
    (processedNodes[newCoordsPlus]
    ? 0
    : countStepsRecursively(
    newCoordsPlus,
    digitsSumLimit,
    processedNodes
    )) +
    (processedNodes[newCoordsMinus]
    ? 0
    : countStepsRecursively(
    newCoordsMinus,
    digitsSumLimit,
    processedNodes
    ))
    );
    })
    .reduce((acc, val) => acc + val, 0)
    );
    }

    console.log(
    "countStepsImperatively([0,0], 1) gives",
    countStepsImperatively([0], 1)
    );
    //gives 3, trivial points with coordinates [0],[-1],[1]

    console.log(
    "countStepsImperatively([0,0], 1) gives",
    countStepsImperatively([0, 0], 1)
    );
    //gives 3, trivial points with coordinate [0,0], [0,1], [0,-1], [-1,0]

    console.log(
    "countStepsImperatively([0,0], 21) gives",
    countStepsImperatively([0, 0], 21)
    );