Daily Coding Problem: Problem #890 (09/08/2021)

/*
There is an N by M matrix of zeroes. Given N and M, write a function to count the number
of ways of starting at the top-left corner and getting to the bottom-right corner. 
You can only move right or down.
For example, given a 2 by 2 matrix, you should return 2, since there are two ways to 
get to the bottom-right:
Right, then down
Down, then right
Given a 5 by 5 matrix, there are 70 ways to get to the bottom-right.
0    0    0    0    0 
0    0    0    0    0   
0    0    0    0    0
0    0    0    0    0
0    0    0    0    0
*/

function numero_Caminos(n, m)
{
    if (n == 1 || m == 1){
        return 1;
    }    
    return numero_Caminos(n - 1, m) + numero_Caminos(n, m - 1);
}

console.log(numero_Caminos(5,5)) // 70
console.log(numero_Caminos(3,3)) // 6
console.log(numero_Caminos(2,2)) // 2

Daily Coding Problem: Problem #888 (09/06/2021)

/*
Given a list of points, a central point, and an integer k, find the nearest k points
from the central point.
For example, given the list of points [(0, 0), (5, 4), (3, 1)], the central point (1, 2),
and k = 2, return [(0, 0), (3, 1)]*/

function getKPoints(x, k){
    var arrIni = x.sort();
    var cont = 0;
    var arrEnd= new Array(k);
    for (var i = 0; i < k; i++) {
        var point = arrIni[i];
        arrEnd[i] = arrIni[i];
    }
    return arrEnd;
}

var x = [[0,0],[5,4],[3,1],[2,0],[1,0],[3,0]]
console.log(x);
var rslt = getKPoints(x, 2);
console.log(rslt); //[ [ 0, 0 ], [ 1, 0 ] ]

Daily Coding Problem: Problem #887 (09/06/2021)

/*
The ancient Egyptians used to express fractions as a sum of several terms where each 
numerator is one. For example, 4 / 13 can be represented as 1 / 4 + 1 / 18 + 1 / 468.
Create an algorithm to turn an ordinary fraction a / b, where a < b, into an Egyptian
fraction.*/

function dividirFraction(numerador, denominador) {
    var ar = new Array(numerador);
    var first = denominador + numerador - 1;
    ar[0] = first;

    // Loop to find the N - 1
    // fraction
    for (var i = 1; i < numerador; i++) {
      ar[i] = first * --first;
    }

    // Loop to print the Fractions
    for (var j = 0; j < numerador; j++) {
      if (ar[j] % numerador === 0) {
        console.log("1/" + ar[j] / numerador + ", ");
      } else {
        console.log(numerador + "/" + ar[j] + ", ");
      }
    }
  }

  dividirFraction(3,4); //1/2, 1/10, 3/20,
  dividirFraction(4,2); //4/5, 1/5, 1/3, 4/6,
  dividirFraction(4,13); //1/4, 1/60, 4/210, 4/182,

Daily Coding Problem: Problem #886 (09/04/2021)

/*
Problem: The edit distance between two strings refers to the minimum number of 
character insertions, deletions, and substitutions required to change one string 
to the other. For example, the edit distance between “kitten” and “sitting” is 
three: substitute the “k” for “s”, substitute the “e” for “i”, and append a “g”.*/
//Levenshtein Distance
const str1 = 'hitting';
const str2 = 'kitten';
const str3 = "perrito";
const str4 = "gatita";
function getLevenshteinDistance(str1, str2){
    
    const ruta = Array(str2.length + 1).fill(null).map(() =>
    Array(str1.length + 1).fill(null));
    for (let i = 0; i <= str1.length; i += 1) {
      ruta[0][i] = i;
    }
    for (let j = 0; j <= str2.length; j += 1) {
      ruta[j][0] = j;
    }
    for (let j = 1; j <= str2.length; j += 1) {
      for (let i = 1; i <= str1.length; i += 1) {
         const indicator = str1[i - 1] === str2[j - 1] ? 0 : 1;
         ruta[j][i] = Math.min(
            ruta[j][i - 1] + 1, // deletion
            ruta[j - 1][i] + 1, // insertion
            ruta[j - 1][i - 1] + indicator, // substitution
         );
      }
    }
    return ruta[str2.length][str1.length];
    
}
console.log(getLevenshteinDistance(str1, str2)); //3
console.log(getLevenshteinDistance(str3, str4)); //5

Daily Coding Problem: Problem #885 (09/04/2021)

Example: Given the array ['G', 'B', 'R', 'R', 'B', 'R', 'G'], 
it should become ['R', 'R', 'R', 'G', 'G', 'B', 'B'].

//var Arr = ['G', 'B', 'R', 'R', 'B', 'R', 'G']
var Arr = ['G', 'B', 'R', 'R', 'B', 'R', 'G','B','R','G']

console.log(Arr);
function organizeRGB(Arr){
    for(i=0;i <= Arr.length; i++){  
        for(j=i;j < Arr.length; j++){
            if(Arr[i] == 'R'){
                break;
            }
            else if(Arr[i] != 'R' && Arr[j] == 'R'){
                var R = Arr[i];
                Arr[i] = Arr[j];
                Arr[j] = R;
            }
            else if(Arr[i] != 'G' && Arr[j] == 'G'){
                var G = Arr[i];
                Arr[i] = Arr[j];
                Arr[j] = G;
            }
        }
    }
    return Arr;
}
var newArr = organizeRGB(Arr);
console.log(newArr);

/*
Input Array:  ['G','B','R','R','B','R','G','B','R','G']
Output Array: ['R','R','R','R','G','G','G','B','B','B']
*/