Triangular NumberTriangular number is the number of points that can fill an equilateral triangle.For instance − 9 is a triangular number which makes an equilateral triangle with each side of 4 units.ProblemWe are required to write a JavaScript function that takes in a number and returns true if its a triangular number, false otherwise.ExampleFollowing is the code − Live Democonst num = 9; const isTriangular = (num = 1) => { let i = 4; if(num === 1){ return true; }; if(num === 3){ return true; }; while(((3 * 1) - 3)
ProblemWe are required to write a JavaScript function that takes in the 1-based month index and return the quarter, which the month falls in.ExampleFollowing is the code − Live Democonst month = 7; const findQuarter = (month = 1) => { if (month
ProblemWe are required to write a JavaScript function that takes in a number n and converts it to the corresponding string without using the inbuilt functions String() or toString() or using string concatenation.ExampleFollowing is the code − Live Democonst num = 235456; const convertToString = (num) => { let res = ''; while(num){ res = (num % 10) + res; num = Math.floor(num / 10); }; return res; }; console.log(convertToString(num));OutputFollowing is the console output −235456
Doubleton NumberWe will call a natural number a "doubleton number" if it contains exactly two distinct digits. For example, 23, 35, 100, 12121 are doubleton numbers, and 123 and 9980 are not.ProblemWe are required to write a JavaScript function that takes in a number and return true if it is a doubleton number, false otherwise.ExampleFollowing is the code − Live Democonst num = 121212; const isDoubleTon = (num = 1) => { const str = String(num); const map = {}; for(let i = 0; i < str.length; i++){ const el = str[i]; ... Read More
ProblemWe are required to write a JavaScript function that takes in a string. Our function should convert every character of the string to the hex value of its ascii code, then the result should be the sum of the numbers in the hex strings ignoring the letters present in hex.ExampleFollowing is the code − Live Democonst str = "Hello, World!"; const toHexAndSum = (str = '') => { return str .split('') .map(c=>c.charCodeAt()) .map(n=>n.toString(16)) .join('') .split('') .filter(c=>'123456789'.includes(c)) .map(d=>parseInt(d)) .reduce((a, b)=>a+b, 0) }; console.log(toHexAndSum(str));OutputFollowing is the console output −91Read More
Lucas NumbersLucas numbers are numbers in a sequence defined like this −L(0) = 2 L(1) = 1 L(n) = L(n-1) + L(n-2)ProblemWe are required to write a JavaScript function that takes in a number n and return the nth lucas number.ExampleFollowing is the code − Live Democonst num = 21; const lucas = (num = 1) => { if (num === 0) return 2; if (num === 1) return 1; return lucas(num - 1) + lucas(num - 2); }; console.log(lucas(num));OutputFollowing is the console output −24476
In this tutorial, we are going to find the permutation of a string using the inbuilt function of Python called permutations. The method permutations is present in the itertools module.Procedure To Find The Permutation Of A StringImport the itertools module.Initialize the string.Use the itertools.permutations method to find the permutation of the string.In the third step, the method returns an object and convert it into a list.List contains a permutation of string as tuples.ExampleLet's see the program.## importing the module import itertools ## initializing a string string = "XYZ" ## itertools.permutations method permutaion_list = list(itertools.permutations(string)) ## printing the obj in list print("-----------Permutations Of String ... Read More
When it is required to print all possible combination of digits when the input is taken from the user, nested loop is used.Below is a demonstration of the same −Example Live Demofirst_num = int(input("Enter the first number...")) second_num = int(input("Enter the second number...")) third_num = int(input("Enter the third number...")) my_list = [] print("The first number is ") print(first_num) print("The second number is ") print(second_num) print("The third number is ") print(third_num) my_list.append(first_num) my_list.append(second_num) my_list.append(third_num) for i in range(0, 3): for j in range(0, 3): for k in range(0, 3): if(i!=j&j!=k&k!=i): ... Read More
When it is required to print all the permutations of a string in lexicographic order using recursion, a method is defined, that uses the ‘for’ loop to iterate over the sequence of elements, and use the ‘join’ method to join the elements.Below is the demonstration of the same −Example Live Demofrom math import factorial def lexicographic_permutation_order(s): my_sequence = list(s) for _ in range(factorial(len(my_sequence))): print(''.join(my_sequence)) next = next_in_permutation(my_sequence) if next is None: my_sequence.reverse() else: my_sequence = next def ... Read More
When it is required to print the nodes in the left subtree, a class can be created that consists of methods can be defined to set the root node, perform in order traversal, insert elements to the right of the root node, to the left of the root node, and so on. An instance of the class is created, and the methods can be used to perform the required operations.Below is a demonstration of the same −Example Live Democlass BinaryTree_struct: def __init__(self, data=None): self.key = data self.left = None self.right = ... Read More
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP