# Algorithm practice

What is a algorithm practice How do you make a algorithm practice? This script and codes were developed by Rafael Abensur on 16 November 2021, Tuesday.

How do I make an algorithm practice?
Algorithm practice Previews

## Algorithm practice HTML Codes

``````<!DOCTYPE html>
<html >
<meta charset="UTF-8">
<title>algorithm practice</title>

<body>

<script src="js/index.js"></script>

</body>
</html>``````

## Algorithm practice JS Codes

``````'use strict';

console.clear();
console.group('Algorithm practice');
var test = function test(opt) {
return console.log(('\n  in:   ' + opt.input + '\n  exp:  ' + opt.exp + '\n  out:  ' + opt.output).replace('\n', ''));
};

/**
Reverse Polish Notation
Reverse Polish notation is a mathematical notation in which every operator
follows its operands. For example, the Unix expression 5 * 3 +2 is
represented in Reverse Polish notation as 5 3 * 2 +. Unix style pipelines
were inspired by this notation as well as a number of popular HP calculators
in the 1960s and 1970s. Your task is to write a Reverse Polish calculator
function that reads a Reverse Polish calculation from STDIN and prints the
results to STDOUT. It should handle addition (“ + ”), subtraction (“ - “),
division (“ / ”), and multiplication (“ * ”) of positive 32 bit integers.

Input format
Input will be a single Reverse Polish expression, E, containing integers
and symbols separated by spaces and terminated by a new line.

Output format
Output should be a single integer by a new line
**/

var calcPostfix = function calcPostfix(postfixExpression) {
return postfixExpression.split(' ').reduce(function (pile, token) {
if (isNaN(token)) {
var first = pile.pop();
pile.push(eval(pile.pop() + token + ' ' + first));
} else {
pile.push(token);
}
return pile;
}, []).pop();
};

console.groupCollapsed('Reverse Polish Notation');
test({ input: "3 3 +", exp: '6', output: calcPostfix("3 3 +") });
test({ input: "2 2 3 + *", exp: '10', output: calcPostfix("2 2 3 + *") });
test({ input: "2 2 3 3 / * /", exp: '1', output: calcPostfix("2 2 3 3 / * /") });
console.groupEnd();

/**
Valid DNA String
You are a scientist who has discovered a new alien lifeform with six new
nucleotide bases in three paired combinations: U and Z, V and Y, and W and X;
For a DNA strand to be valid, each pair base must be matched: e. g. each U
must be matched by a trailing Z. Additionally, the interval between the bases
must be balanced. As an example:
UVWXYZ – This is a valid DNA strand
UVWYXZ – This is not a valid DNA strand

Your task is to write a function that, given a list of DNA strings, evaluates
each one and prints “YES” if the strand is valid and “NO” if it is invalid.

Input format
The first line of STDIN will contain as integer N. The next N lines will
each contain a DNA string.
**/

var isValidDNA = function isValidDNA(DNA) {
var pairs = 'UZ;ZU;VY;YV;WX;XW'.split(';');
var size = DNA.length;
var half = size / 2;

return DNA.slice(0, half).split('').reduce(function (matches, leftN, index) {
var rightN = DNA.charAt(size - index - 1);
return matches + Number(pairs.indexOf(leftN + '' + rightN) > -1);
}, 0) === half;
};

console.groupCollapsed('Valid DNA String');
test({ input: "UVWXYZ", exp: 'YES',
output: isValidDNA("UVWXYZ") ? 'YES' : 'NO' });
test({ input: "UVWYXZ", exp: 'NO',
output: isValidDNA("UVWYXZ") ? 'YES' : 'NO' });
test({ input: "UUUZZZUUUZZZ", exp: 'YES',
output: isValidDNA("UUUZZZUUUZZZ") ? 'YES' : 'NO' });
console.groupEnd();

/**
Water fill
Given an integer matrix represented a square of land, imagine that each
integer represents the location’s height above sea level with zero
representing a location covered by water.
Write a function to calculate the size of each body of water in the plot. The
size of a water body is the total number of connected water cells (cells can
be connected horizontally, vertically and diagonally). Your function should
output on new lines the size of each body of water orders from smallest to
largest.

Input format
The first line of input contains the number of lines, L. Each line consists
of L integers separated by spaces.
Output format
After sorting the bodies of water by size, print each size on a new line
from smallest to largest.
**/

var waterFill = function waterFill(string) {
var matrix = string.split('\n').map(function (line) {
return line.split(' ');
});
var bodies = [];

var countConections = function countConections(row, col) {
if (matrix[row][col] == 0) {
matrix[row][col] = null;
return findConections(row, col).reduce(function (sum, coord) {
return sum + countConections(coord, coord);
}, 1);
}
return 0;
};

var findConections = function findConections(row, col) {
return [[row - 1, col], [row + 1, col], [row, col - 1], [row, col + 1], [row - 1, col - 1], [row - 1, col + 1], [row + 1, col - 1], [row + 1, col + 1]].filter(function (coord) {
return matrix[coord] && matrix[coord][coord];
});
};

for (var row in matrix) {
for (var col in matrix[row]) {
if (matrix[row][col] == 0) {
bodies.push(countConections(+row, +col));
}
}
}

return bodies.sort(function (a, b) {
return a > b;
}).join('\n');
};

console.groupCollapsed('Water fill');
test({
input: '\n4\n0 2 1 0\n0 1 0 1\n1 1 0 1\n0 1 0 1',
exp: '\n1\n2\n4',
output: '\n' + waterFill('4\n0 2 1 0\n0 1 0 1\n1 1 0 1\n0 1 0 1')
});
test({
input: '\n0 0 0 0\n1 1 1 1\n0 0 1 1\n1 1 0 0',
exp: '\n4\n4',
output: '\n' + waterFill('0 0 0 0\n1 1 1 1\n0 0 1 1\n1 1 0 0')
});
test({
input: '\n0 0 1 1\n1 1 1 1\n0 0 1 1\n1 1 0 1',
exp: '\n2\n3',
output: '\n' + waterFill('0 0 1 1\n1 1 1 1\n0 0 1 1\n1 1 0 1')
});
console.groupEnd();``````
Do you want hide your ip address?Surf anonymously, prevent hackers from acquiring your IP address, send anonymous email, and encrypt your Internet connection. High speed, ultra secure, and easy to use. Instant setup.