"filter function usually exists in some high level programming that adopt FP paradigm.
It taks a sequence of items and a predicate function, and returns (conceptually) a subset of the items that satisfy the predicate.
Adopt this kind of operation (filter, map, reduce, take, pairwise ...) can help writting
cleaner code, and reduce the usage of mutable part in the program, lower the
possibility of making human mistake.
Take Python for example (although const-ness is not exists in Python),
assu"
Weida H. - "filter function usually exists in some high level programming that adopt FP paradigm.
It taks a sequence of items and a predicate function, and returns (conceptually) a subset of the items that satisfy the predicate.
Adopt this kind of operation (filter, map, reduce, take, pairwise ...) can help writting
cleaner code, and reduce the usage of mutable part in the program, lower the
possibility of making human mistake.
Take Python for example (although const-ness is not exists in Python),
assu"See full answer
"We can use a pool of memory where each index we can store the parent of current index then we can use same lca approach for array instead of pointers."
Sourav K. - "We can use a pool of memory where each index we can store the parent of current index then we can use same lca approach for array instead of pointers."See full answer
"class Node
{
int val;
Node left, right;
Node(int v)
{
val = v;
left = right = null;
}
}
class BinaryTree
{
Node root1, root2;
boolean identicalTrees(Node a, Node b)
{
if (a == null && b == null)
return true;
if (a != null && b != null)
return (a.val == b.val
&& identicalTrees(a.left, b.left)
&& identicalTrees(a.right, b.right));
"
Tushar A. - "class Node
{
int val;
Node left, right;
Node(int v)
{
val = v;
left = right = null;
}
}
class BinaryTree
{
Node root1, root2;
boolean identicalTrees(Node a, Node b)
{
if (a == null && b == null)
return true;
if (a != null && b != null)
return (a.val == b.val
&& identicalTrees(a.left, b.left)
&& identicalTrees(a.right, b.right));
"See full answer
"let str = 'this is a test of programs';
let obj={};
for (let s of str )
obj[s]?(obj[s]=obj[s]+1):(obj[s]=1)
console.log(JSON.stringify(obj))"
Anonymous Emu - "let str = 'this is a test of programs';
let obj={};
for (let s of str )
obj[s]?(obj[s]=obj[s]+1):(obj[s]=1)
console.log(JSON.stringify(obj))"See full answer
"function constructTree(n, matrix) {
let parent = [];
let child = [];
let root = null;
for (let i = 0; i < n; i++) {
for (let j = 0; j < n; j++) {
if (matrixi === 1) {
parent.push(i);
child.push(j);
}
}
}
for (let i = 0; i < n; i++) {
if (parent.indexOf(i) === -1) {
root = i;
}
}
let node = new Node(root);
for (let i = 0; i < n; i++) {
if (i !== root) {
constructTreeUtil(node, parent[i], child[i]);
}
}
return node;
}"
Ugo C. - "function constructTree(n, matrix) {
let parent = [];
let child = [];
let root = null;
for (let i = 0; i < n; i++) {
for (let j = 0; j < n; j++) {
if (matrixi === 1) {
parent.push(i);
child.push(j);
}
}
}
for (let i = 0; i < n; i++) {
if (parent.indexOf(i) === -1) {
root = i;
}
}
let node = new Node(root);
for (let i = 0; i < n; i++) {
if (i !== root) {
constructTreeUtil(node, parent[i], child[i]);
}
}
return node;
}"See full answer
"import java.util.Arrays;
import java.util.stream.Collectors;
class Main
{
// Recursive function to print all combinations of numbers from \i\ to \n\
// having sum \n. The index\ denotes the next free slot in the output array \out\
public static void printCombinations(int i, int n, int[] out, int index)
{
// if the sum becomes n, print the combination
if (n == 0)
{
System.out.println(Arrays.stream(out).limit(index)
.boxed().collect(Collectors.toList()));
}
// start from the previous e"
Relynn may silver B. - "import java.util.Arrays;
import java.util.stream.Collectors;
class Main
{
// Recursive function to print all combinations of numbers from \i\ to \n\
// having sum \n. The index\ denotes the next free slot in the output array \out\
public static void printCombinations(int i, int n, int[] out, int index)
{
// if the sum becomes n, print the combination
if (n == 0)
{
System.out.println(Arrays.stream(out).limit(index)
.boxed().collect(Collectors.toList()));
}
// start from the previous e"See full answer