Finding Length, Width, and Height of Sheet Using JavaScript

AmitDiwan
Updated on 17-Apr-2021 13:10:53

288 Views

ProblemWe are required to write a JavaScript function that takes in the length, height and width of a room.Our function should calculate the number of sheets required to cover the whole room given that the width of one single sheet is .52 units and length is 10 units.For the sake of convenience, we should return the number of rolls such that the length is 15% more than the required length.ExampleFollowing is the code − Live Democonst findSheet = (length, width, height) => {    if(length === 0 || width === 0){       return 0;    }    const roomArea ... Read More

Order Tuples Using External List in Python

AmitDiwan
Updated on 17-Apr-2021 13:07:40

349 Views

When it is required to order the tuples using an external list, the list comprehension, and ‘dict’ method can be used.Below is the demonstration of the same −Example Live Demomy_list = [('Mark', 34), ('Will', 91), ('Rob', 23)] print("The list of tuple is : ") print(my_list) ordered_list = ['Will', 'Mark', 'Rob'] print("The ordered list is :") print(ordered_list) temp = dict(my_list) my_result = [(key, temp[key]) for key in ordered_list] print("The ordered tuple list is : ") print(my_result)OutputThe list of tuple is : [('Mark', 34), ('Will', 91), ('Rob', 23)] The ordered list is : ['Will', 'Mark', 'Rob'] The ordered tuple list is ... Read More

Remove Tuples of Length K in Python

AmitDiwan
Updated on 17-Apr-2021 13:07:20

528 Views

When it is required to remove the tuples of a specific length ‘K’, list comprehension can be used.Below is the demonstration of the same −Example Live Demomy_list = [(32, 51), (22, 13 ), (94, 65, 77), (70, ), (80, 61, 13, 17)] print("The list is : " ) print(my_list) K = 1 print("The value of K is ") print(K) my_result = [ele for ele in my_list if len(ele) != K] print("The filtered list is : ") print(my_result)OutputThe list is : [(32, 51), (22, 13), (94, 65, 77), (70, ), (80, 61, 13, 17)] The value of K is ... Read More

Counting Specific Digits in Squares of Numbers using JavaScript

AmitDiwan
Updated on 17-Apr-2021 13:06:36

367 Views

ProblemWe are required to write a JavaScript function that takes in an integer n (n >= 0) and a digit d (0

Expanding Binomial Expression Using JavaScript

AmitDiwan
Updated on 17-Apr-2021 13:06:16

348 Views

ProblemWe are required to write a JavaScript function that takes in an expression in the form (ax+b)^n where a and b are integers which may be positive or negative, x is any single character variable, and n is a natural number. If a = 1, no coefficient will be placed in front of the variable.Our function should return the expanded form as a string in the form ax^b+cx^d+ex^f... where a, c, and e are the coefficients of the term, x is the original one-character variable that was passed in the original expression and b, d, and f, are the powers ... Read More

Obtain Maximum Number by Rotating Digits in JavaScript

AmitDiwan
Updated on 17-Apr-2021 13:05:55

109 Views

ProblemWe are required to write a JavaScript function that takes in a positive integer n and returns the maximum number we got doing only left rotations to the digits of the number.ExampleFollowing is the code − Live Democonst num = 56789; const findMaximum = (num = 1) => {    let splitNumbers = num.toString().split("");    let largestNumber = num;    for(let i = 0; i < splitNumbers.length - 1; i++) {       splitNumbers.push(splitNumbers.splice(i, 1)[0]);       let newNumber = Number(splitNumbers.join(""));       if(newNumber >= largestNumber) {          largestNumber = newNumber;       }    };    return largestNumber; }; console.log(findMaximum(num));Output68957

Sum of Perimeter of All Squares in a Rectangle Using JavaScript

AmitDiwan
Updated on 17-Apr-2021 13:05:35

322 Views

Problem Suppose there are 5 squares embedded inside a rectangle like this −Their perimeter will be −4 + 4 + 8 + 12 + 20 = 48 unitsWe are required to write a JavaScript function that takes in a number n and return the sum of the perimeter if there are n squares embedded.ExampleFollowing is the code − Live Democonst num = 6; const findPerimeter = (num = 1) => {    const arr = [1,1];    let n = 0;    let sum = 2;    for(let i = 0 ; i < num-1 ; i++){       n = arr[i] + arr[i+1];       arr.push(n);       sum += n;    };    return sum * 4; }; console.log(findPerimeter(num - 1));Output80

Moving Every Alphabet Forward by 10 Places in JavaScript

AmitDiwan
Updated on 17-Apr-2021 13:05:17

251 Views

ProblemWe are required to write a JavaScript function that takes in a string of English alphabets. Our function should push every alphabet forward by 10 places. And if it goes past 'z', we should start again at 'a'.ExampleFollowing is the code − Live Democonst str = 'sample string'; const moveStrBy = (num = 10) => {    return str => {       const calcStr = (ch, code) => String       .fromCharCode(code + (ch.charCodeAt(0) - code + num) % 26);       const ACode = 'A'.charCodeAt(0);       const aCode = 'a'.charCodeAt(0);       return str.replace(/[a-z]/gi, ch => (          ch.toLowerCase() == ch          ? calcStr(ch, aCode)          : calcStr(ch, ACode)       ));    }; }; const moveByTen = moveStrBy(); console.log(moveByTen(str));Outputckwzvo cdbsxq

Extract Digits from Tuple List in Python

AmitDiwan
Updated on 17-Apr-2021 13:04:43

686 Views

When it is required to extract digits from a list of tuple, list comprehension can be used.Below is the demonstration of the same −Example Live Demomy_list = [(67, 2), (34, 65), (212, 23), (17, 67), (18, )] print("The list is : ") print(my_list) N = 2 print("The value of N is ") print(N) my_result = [sub for sub in my_list if all(len(str(ele)) == N for ele in sub)] print("The extracted tuples are : " ) print(my_result)OutputThe list is : [(67, 2), (34, 65), (212, 23), (17, 67), (18, )] The value of N is 2 The extracted tuples ... Read More

Join Tuples if Similar Initial Element in Python

AmitDiwan
Updated on 17-Apr-2021 13:04:17

524 Views

When it is required to join tuples if they contain a similar initial element, a simple ‘for’ loop and an ‘of’ condition can be used. To store elements to one list, the ‘extend’ method can be used.Below is the demonstration of the same −Example Live Demomy_list = [(43, 15), (66, 98), (64, 80), (14, 9), (47, 17)] print("The list is : ") print(my_list) my_result = [] for sub in my_list:    if my_result and my_result[-1][0] == sub[0]:       my_result[-1].extend(sub[1:])    else:       my_result.append([ele for ele in sub]) my_result = list(map(tuple, my_result)) print("The extracted elements ... Read More

Advertisements