Find All Nodes Reachable from a Node Using BFS in a Graph

AmitDiwan
Updated on 17-Apr-2021 11:50:18

793 Views

When it is required to find the sum of all the nodes of a tree, a class is created, and it contains methods to set the root node, add elements to the tree, search for a specific element, and add elements of the tree to find the sum and so on. An instance of the class can be created to access and use these methods.Below is a demonstration of the same −Example Live Demofrom collections import deque def add_edge(v, w):    global visited_node, adj    adj[v].append(w)    adj[w].append(v) def BFS_operation(component_num, src):    global visited_node, adj    queue ... Read More

Display Nodes of a Tree Using BFS Traversal in Python

AmitDiwan
Updated on 17-Apr-2021 11:49:49

909 Views

When it is required to display the nodes of a tree using the breadth first search traversal, a class is created, and it contains methods to set the root node, add elements to the tree, search for a specific element, perform ‘bfs’ (breadth first search) and so on. An instance of the class can be created to access and use these methods.Below is a demonstration of the same −Example Live Democlass Tree_struct:    def __init__(self, data=None):       self.key = data       self.children = []    def set_root(self, data):       self.key = data   ... Read More

Find the Sum of All Nodes in a Binary Tree using Python

AmitDiwan
Updated on 17-Apr-2021 11:49:29

251 Views

When it is required to find the sum of all the nodes of a tree, a class is created, and it contains methods to set the root node, add elements to the tree, search for a specific element, and add elements of the tree to find the sum and so on. An instance of the class can be created to access and use these methods.Below is a demonstration of the same −Example Live Democlass Tree_struct:    def __init__(self, data=None):       self.key = data       self.children = []    def set_root(self, data):       self.key = ... Read More

Filter String to Contain Unique Characters in JavaScript

AmitDiwan
Updated on 17-Apr-2021 11:49:16

789 Views

ProblemWe are required to write a JavaScript function that takes in a string str. Our function should construct a new string that contains only the unique characters from the input string and remove all occurrences of duplicate characters.ExampleFollowing is the code − Live Democonst str = 'hey there i am using javascript'; const removeAllDuplicates = (str = '') => {    let res = '';    for(let i = 0; i < str.length; i++){       const el = str[i];       if(str.indexOf(el) === str.lastIndexOf(el)){          res += el;          continue;       };    };    return res; }; console.log(removeAllDuplicates(str));OutputFollowing is the console output −Ymungjvcp

Separate Data Types from Array into Groups in JavaScript

AmitDiwan
Updated on 17-Apr-2021 11:42:09

602 Views

ProblemWe are required to write a JavaScript function that takes in an array of mixed data types. Our function should return an object that contains data type names as key and their value as array of elements of that specific data type present in the array.ExampleFollowing is the code − Live Democonst arr = [1, 'a', [], '4', 5, 34, true, undefined, null]; const groupDataTypes = (arr = []) => {    const res = {};    for(let i = 0; i < arr.length; i++){       const el = arr[i];       const type = typeof el;   ... Read More

Mathematics Summation Function in JavaScript

AmitDiwan
Updated on 17-Apr-2021 11:40:30

2K+ Views

ProblemWe are required to write a JavaScript function that takes in a number n. Our function should return the sum of all the natural numbers from 1 to n including both 1 and nExampleFollowing is the code − Live Democonst num = 34; const summation = (num = 1) => {    let res = 0;    for(let i = 1; i

Create All Possible Unique Permutations of a String in JavaScript

AmitDiwan
Updated on 17-Apr-2021 11:39:18

1K+ Views

ProblemWe are required to write a JavaScript function that takes in a string str. Our function should create all permutations of the input string and remove duplicates, if present. This means, we have to shuffle all letters from the input in all possible orders.ExampleFollowing is the code − Live Democonst str = 'aabb'; const permute = (str = '') => {    if (!!str.length && str.length < 2 ){       return str    }    const arr = [];    for (let i = 0; i < str.length; i++){       let char = str[i]       ... Read More

Returning Number with Increasing Digits in JavaScript

AmitDiwan
Updated on 17-Apr-2021 11:37:15

184 Views

ProblemWe are required to write a JavaScript function that takes in a number n. Our function should return it with its digits in descending order. Essentially, we should rearrange the digits to create the highest possible number.ExampleFollowing is the code − Live Democonst num = 5423267; const arrangeInDescending = (num = 1) => {    const str = String(num);    const arr = str.split('');    arr.sort((a, b) => {       return +b - +a;    });    const newStr = arr.join('');    const res = Number(newStr);    return res; }; console.log(arrangeInDescending(num));OutputFollowing is the console output −7654322

Currified Function that Multiplies Array Elements in JavaScript

AmitDiwan
Updated on 17-Apr-2021 11:35:29

221 Views

ProblemWe are required to write a JavaScript function that takes in an array and returns another function which in turn takes in a number which returns a new array which is the product of corresponding elements of the input array to the first function and the number provided to the second function.ExampleFollowing is the code − Live Democonst arr = [2, 5, 2, 7, 8, 4]; const num = 4; const produceWith = (arr = []) => (num) => {    const res = arr.map(el => {       return el * num;    });    return res; }; console.log(produceWith(arr)(num));OutputFollowing is the console output −[ 8, 20, 8, 28, 32, 16 ]

Returning the Expanded Form of a Number in JavaScript

AmitDiwan
Updated on 17-Apr-2021 11:33:22

384 Views

ProblemWe are required to write a JavaScript function that takes in a number and returns a string of the expanded form of the number, indicating the place value of each number.ExampleFollowing is the code − Live Democonst num = 56577; const expandedForm = (num = 0) => {    const str = String(num);    let res = '';    let multiplier = Math.pow(10, str.length - 1);    for(let i = 0; i < str.length; i++){       const el = +str[i];       const next = +str[i + 1];       if(el){          res += (el * multiplier);       };       if(next){          res += ' + ';       };       multiplier /= 10;    };    return res; }; console.log(expandedForm(num));OutputFollowing is the console output −50000 + 6000 + 500 + 70 + 7

Advertisements