ProblemWe are required to write a JavaScript function that takes in a sting and returns true if all the characters in the string appear only once and false otherwise.ExampleFollowing is the code − Live Democonst str = 'thisconaluqe'; const allUnique = (str = '') => { for(let i = 0; i < str.length; i++){ const el = str[i]; if(str.indexOf(el) !== str.lastIndexOf(el)){ return false; }; }; return true; }; console.log(allUnique(str));Outputtrue
ProblemA Boggle board is a 2D array of individual characters, e.g. −const board = [ ["I", "L", "A", "W"], ["B", "N", "G", "E"], ["I", "U", "A", "O"], ["A", "S", "R", "L"] ];We are required to write a JavaScript function that takes in boggle board and a string and checks whether that string is a valid guess in the boggle board or not. Valid guesses are strings which can be formed by connecting adjacent cells (horizontally, vertically, or diagonally) without reusing any previously used cells.For example, in the above board "LINGO", and "ILNBIA" would all be valid ... Read More
ProblemWe are required to write a JavaScript function that takes in a number n. Our function should find the absolute difference between the sum and the product of all the digits of that number.ExampleFollowing is the code − Live Democonst num = 434312; const sumProductDifference = (num = 1) => { const sum = String(num) .split('') .reduce((acc, val) => acc + +val, 0); const product = String(num) .split('') .reduce((acc, val) => acc * +val, 1); const diff = product - sum; return Math.abs(diff); }; console.log(sumProductDifference(num));Output271
ProblemWe are required to write a JavaScript function that takes in an array of integers (positives and negatives) and our function should return an array, where the first element is the count of positives numbers and the second element is sum of negative numbers.ExampleFollowing is the code − Live Democonst arr = [1, 2, 1, -2, -4, 2, -6, 2, -4, 9]; const posNeg = (arr = []) => { const creds = arr.reduce((acc, val) => { let [count, sum] = acc; if(val > 0){ count++; }else if(val < 0){ sum += val; }; return [count, sum]; }, [0, 0]); return creds; }; console.log(posNeg(arr));Output[ 6, -16 ]
Amicable numbers are two different numbers so related that the sum of the proper divisors of each is equal to the other number. When it is required to check if two numbers are amicable numbers, a method can be defined that iterates over the number, and uses the modulus operator. Another method is defined that calls the previously defined function to determine if two numbers are amicable or not.Below is the demonstration of the same −Example Live Demoimport math def divided_sum_val(my_val) : res = 0 for i in range(2, int(math.sqrt(my_val)) + 1) : ... Read More
When it is required to compute a polynomial equation when the coefficients of the polynomial are stored in a list, a simple ‘for’ loop can be used.Below is the demonstration of the same −Example Live Demomy_polynomial = [2, 5, 3, 0] num = 2 poly_len = len(my_polynomial) my_result = 0 for i in range(poly_len): my_sum = my_polynomial[i] for j in range(poly_len - i - 1): my_sum = my_sum * num my_result = my_result + my_sum print("The polynomial equation for the given list of co-efficients is :") print(my_result)OutputThe polynomial equation for the given list of co-efficients ... Read More
Strong number is a number whose sum of all digits’ factorial is equal to the number ‘n’. Factorial implies when we find the product of all the numbers below that number including that number and is denoted by ! (Exclamation sign), For example: 5! = 5x4x3x2x1 = 120. When it is required to check if a number is a strong number or not, the remainder/modulus operator and the ‘while’ loop can be used.Below is the demonstration of the same −Example Live Demomy_sum=0 my_num = 296 print("The number is") print(my_num) temp = my_num while(my_num): i=1 fact=1 remainder = my_num%10 ... Read More
A number is said to be a Perfect Number when that is equal to the sum of all its positive divisors except itself. When it is required to check if a number is a perfect number, a simple ‘for’ loop can be used.Below is the demonstration of the same −Example Live Demon = 6 my_sum = 0 for i in range(1, n): if(n % i == 0): my_sum = my_sum + i if (my_sum == n): print("The number is a perfect number") else: print("The number is not a perfect number")OutputThe number is a perfect numberExplanationThe ... Read More
When it is required to print the pascal’s triangle for a specific number of rows, where the number is entered by the user, a simple ‘for’ loop is used.Below is the demonstration of the same −Example Live Demofrom math import factorial input = int(input("Enter the number of rows...")) for i in range(input): for j in range(input-i+1): print(end=" ") for j in range(i+1): print(factorial(i)//(factorial(j)*factorial(i-j)), end=" ") print()OutputEnter the number of rows...6 1 1 1 1 2 1 1 3 3 1 ... Read More
When it is required to convert binary code to gray code, a method is defined that performs the ‘xor’ operation.Below is the demonstration of the same −Example Live Demodef binary_to_gray_op(n): n = int(n, 2) n ^= (n >> 1) return bin(n)[2:] gray_val = input('Enter the binary number: ') binary_val = binary_to_gray_op(gray_val) print('Gray codeword is :', binary_val)OutputEnter the binary number: 101100110 Gray codeword is : 111010101ExplanationA method named ‘binary_to_gray_op’ is defined, that takes the binary number as its parameter.It performs the ‘xor’ operation.It returns the converted output.The input of binary number is taken from the user.The function ... Read More