ProblemWe are required to write a JavaScript function that takes in an array of unique words.Our function should return an array of all such index pairs, the words at which, when combined yield a palindrome word.ExampleFollowing is the code − Live Democonst arr = ["abcd", "dcba", "lls", "s", "sssll"]; const findPairs = (arr = []) => { const res = []; for ( let i = 0; i < arr.length; i++ ){ for ( let j = 0; j < arr.length; j++ ){ if (i !== j ) { let k = `${arr[i]}${arr[j]}`; let l = [...k].reverse().join(''); if (k === l) res.push( [i, j] ); } }; }; return res; }; console.log(findPairs(arr));Output[ [ 0, 1 ], [ 1, 0 ], [ 2, 4 ], [ 3, 2 ] ]
ProblemWe are required to write a JavaScript function that takes in an array of numbers in string format. Our function must return a string. The numbers correspond to the letters of the alphabet in reverse order: a=26, z=1 etc.We should also account for '!', '?' and ' ' that are represented by '27', '28' and '29' respectively.ExampleFollowing is the code − Live Democonst arr = ['5', '23', '2', '1', '13', '18', '6']; const convertToString = (arr) => { let res = ''; for (let char of arr) { if (Number(char)
ProblemWe are required to write a JavaScript function that takes in a string. Our function should turn each character into its ASCII character code and join them together to create a number. Then we should replace all instances of 7 from this number to 1 to construct another number. Finally, we should return the difference of both these numbersExampleFollowing is the code − Live Democonst str = 'AVEHDKDDS'; const ASCIIDifference = (str = '') => { return str .split('') .map(c => c.charCodeAt(0)) .join('') .split('') .map(Number) .filter(str => str === 7) .length * 6; }; console.log(ASCIIDifference(str));Output12
ProblemWe are required to write a JavaScript function that takes in an array of numbers. Our function should take each number in the array and square it if it is even, or square root the number if it is odd and then return the sum of all the new numbers rounded to two decimal places.ExampleFollowing is the code − Live Democonst arr = [45, 2, 13, 5, 14, 1, 20]; const squareAndRootSum = (arr = []) => { const res = arr.map(el => { if(el % 2 === 0){ return el * el; }else{ return Math.sqrt(el); }; }); const sum = res.reduce((acc, val) => acc + val); return sum; }; console.log(squareAndRootSum(arr));Output613.5498231854631
ProblemWe are required to write a JavaScript function that takes in a number n. Our function should that smallest number which is just greater than n and is a prime number.ExampleFollowing is the code − Live Democonst num = 101; const isPrime = (num) => { let sqrtnum = Math.floor(Math.sqrt(num)); let prime = num !== 1; for(let i = 2; i < sqrtnum + 1; i++){ if(num % i === 0){ prime = false; break; }; }; return prime; } const nextPrime = (num = 1) => { while(!isPrime(++num)){ }; return num; }; console.log(nextPrime(num));Output103
When it is required solve the maximum subarray problem using the divide and conquer method, Below is the demonstration of the same −Example Live Demodef max_crossing_sum(my_array, low, mid, high): sum_elements = 0 sum_left_elements = -10000 for i in range(mid, low-1, -1): sum_elements = sum_elements + my_array[i] if (sum_elements > sum_left_elements): sum_left_elements = sum_elements sum_elements = 0 sum_right_elements = -1000 for i in range(mid + 1, high + 1): sum_elements = sum_elements + my_array[i] if (sum_elements > sum_right_elements): ... Read More
When it is required to sort a list of tuples in increasing order based on last element of every tuple, a method is defined, that iterates over the tuple and performs a simple swap to achieve the same.Below is the demonstration of the same −Example Live Demodef sort_tuple(my_tup): my_len = len(my_tup) for i in range(0, my_len): for j in range(0, my_len-i-1): if (my_tup[j][-1] > my_tup[j + 1][-1]): temp = my_tup[j] my_tup[j]= my_tup[j + 1] ... Read More
When it is required to generate random numbers within a given range and append them to a list, a method is defined, that generates random numbers and ‘append’s them to an empty list.Below is the demonstration of the same −Example Live Demoimport random def random_gen(beg, end, my_num): my_result = [] for j in range(my_num): my_result.append(random.randint(beg, end)) return my_result my_num = 19 beg = 1 end = 20 print("The number is :") print(my_num) print("The start and end values are :") print(beg, end) print("The elements are : ") print(random_gen(beg, end, my_num))OutputThe number is : 19 The start ... Read More
When it is required to find the sum of a list where the specific element is sum of first few elements, a method is defined, that takes list as parameter. It uses list comprehension to find the cumulative sum.Below is the demonstration of the same −Example Live Demodef cumulative_sum(my_list): cumulative_list = [] my_length = len(my_list) cumulative_list = [sum(my_list[0:x:1]) for x in range(0, my_length+1)] return cumulative_list[1:] my_list = [10, 20, 25, 30, 40, 50] print("The list is :") print(my_list) print("The cumulative sum is :") print (cumulative_sum(my_list))OutputThe list is : [10, 20, 25, 30, 40, 50] The cumulative ... Read More
When it is required to find all numbers in a range where there are perfect square, and sum of digits in the number is less than 10, list comprehension is used.Below is the demonstration of the same −Example Live Demolower_limit = int(input(“Enter the lower range: “)) upper_limit = int(input(“Enter the upper range: “)) my_list = [] my_list = [x for x in range(lower_limit,upper_limit+1) if (int(x**0.5))**2==x and sum(list(map(int,str(x))))
 
 Data Structure
 Data Structure Networking
 Networking RDBMS
 RDBMS Operating System
 Operating System Java
 Java MS Excel
 MS Excel iOS
 iOS HTML
 HTML CSS
 CSS Android
 Android Python
 Python C Programming
 C Programming C++
 C++ C#
 C# MongoDB
 MongoDB MySQL
 MySQL Javascript
 Javascript PHP
 PHP