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
When it is required to print all the permutations of a string in the lexicographic order without using recursion, a method is defined, that takes the string as the parameter. It uses a simple ‘for’ loop to iterate over the string elements and uses ‘while’ condition to check for certain constraints.Below is the demonstration of the same −Example Live Demofrom math import factorial def lex_permutation(my_string): for i in range(factorial(len(my_string))): print(''.join(my_string)) i = len(my_string) - 1 while i > 0 and my_string[i-1] > my_string[i]: i -= 1 my_string[i:] = reversed(my_string[i:]) if i > 0: ... Read More
When it is required to form a new string that is made from the first two and last two characters of a given string, a counter can be defined, and indexing can be used to access specific range of elements.Below is the demonstration of the same −Example Live Demomy_string = "Hi there how are you" my_counter = 0 for i in my_string: my_counter = my_counter + 1 new_string = my_string[0:2] + my_string [my_counter - 2: my_counter ] print("The string is ") print(my_string) print("The new string is ") print(new_string)OutputThe string is Hi there how are you The new ... Read More
When it is required to create a mirror copy of a tree, and display it using breadth first search, a binary tree class is created with methods set the root element, insert element to left, insert element to right, search for a specific element, and perform post order traversal and so on. An instance of the class is created, and it can be used to access the methods.Below is the demonstration of the same −Example Live Democlass BinaryTree_struct: def __init__(self, key=None): self.key = key self.left = None self.right = None ... Read More
When it is required to implement depth first search using post order traversal, a tree class is created with methods to add element, search for a specific element, and perform post order traversal and so on. An instance of the class is created, and it can be used to access the methods.Below is the demonstration of the same −Example Live Democlass Tree_Struct: def __init__(self, key=None): self.key = key self.children = [] def add_elem(self, node): self.children.append(node) def search_elem(self, key): if self.key == key: ... Read More