Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
Articles on Trending Technologies
Technical articles with clear explanations and examples
How to expand a data frame rows by their index position in R?
To expand a data frame rows by its index position in R, we can follow the below steps −First of all, create a data frame.Then, use rep and seq_len function with nrow to expand the data frame rows by their index position.Create the data frameLet's create a data frame as shown below −x1
Read MorePython Program to Convert a given Singly Linked List to a Circular List
When it is required to convert a singly linked list into a circular linked list, a method named ‘convert_to_circular_list’ is defined that ensures that the last elements points to the first element, thereby making it circular in nature.Below is a demonstration of the same −Exampleclass Node: def __init__(self, data): self.data = data self.next = None class LinkedList_struct: def __init__(self): self.head = None self.last_node = None def add_elements(self, data): if self.last_node is None: self.head = Node(data) ...
Read MorePython Program to Find Nth Node in the Inorder Traversal of a Tree
When it is required to find the ‘n’th node using inorder traversal of a binary tree, a binary tree class is created with methods to set the root element, add elements to left or right, perform in order traversal and so on. An instance of the class is created, and it can be used to access the methods.Below is a demonstration of the same −Exampleclass BinaryTree_struct: def __init__(self, key=None): self.key = key self.left = None self.right = None def set_root(self, key): self.key = key ...
Read MoreSwitching positions of selected characters in a string in JavaScript
ProblemWe are required to write a JavaScript function that takes in a string that contains only the letter ‘k’, ‘l’ and ‘m’.The task of our function is to switch the positions of k with that of l leaving all the instances of m at their positions.ExampleFollowing is the code −const str = 'kklkmlkk'; const switchPositions = (str = '') => { let res = ""; for(let i = 0; i < str.length; i++){ if (str[i] === 'k') { res += 'l'; } else if (str[i] === 'l') { res += 'k'; } else { res += str[i]; }; }; return res; }; console.log(switchPositions(str));OutputFollowing is the console output −llklmkll
Read MoreFinding smallest sum after making transformations in JavaScript
ProblemWe are required to write a JavaScript function that takes in an array of positive integers. We can transform its elements by running the following operation on them as many times as required −if arr[i] > arr[j] then arr[i] = arr[i] - arr[j]When no more transformations are possible, our function should return its sum.ExampleFollowing is the code −const arr = [6, 9, 21]; const smallestSum = (arr = []) => { const equalNums = arr => arr.reduce((a, b) => { return (a === b) ? a : NaN; }); if(equalNums(arr)){ return ...
Read MoreNumber of carries required while adding two numbers in JavaScript
ProblemWe are required to write a JavaScript function that takes in two numbers.Our function should count the number of carries we are required to take while adding those numbers as if we were adding them on paper.Like in the following image while adding 179 and 284, we had used carries two times, so for these two numbers, our function should return 2.ExampleFollowing is the code −const num1 = 179; const num2 = 284; const countCarries = (num1 = 1, num2 = 1) => { let res = 0; let carry = 0; while(num1 + num2){ ...
Read MoreRemoving the second number of the pair that add up to a target in JavaScript
ProblemWe are required to write a JavaScript function that takes in an array of numbers and a target sum.Our function should remove the second number of all such consecutive number pairs from the array that add up to the target number.ExampleFollowing is the code −const arr = [1, 2, 3, 4, 5]; const target = 3; const removeSecond = (arr = [], target = 1) => { const res = [arr[0]]; for(i = 1; i < arr.length; i++){ if(arr[i] + res[res.length-1] !== target){ res.push(arr[i]); }; }; return res; }; console.log(removeSecond(arr, target));OutputFollowing is the console output −[ 1, 3, 4, 5 ]
Read MoreFinding the immediate bigger number formed with the same digits in JavaScript
ProblemWe are required to write a JavaScript function that takes in a number n. Our function should rearrange the digits of the numbers such that we form the smallest number using the same digits but just bigger than the input number.For instance, if the input number is 112. Then the output should be 121.ExampleFollowing is the code −const num = 112; const findNextBigger = (num = 1) => { const sortedDigits = (num = 1) => { return String(num) .split('') .sort((a, b) => b - a); }; let max = sortedDigits(num).join(''); max = Number(max); for(let i = num + 1; i
Read MoreInterchanging first letters of words in a string in JavaScript
ProblemWe are required to write a JavaScript function that takes in a string that contains exactly two words.Our function should construct and return a new string in which the first letter of the words are interchanged with each other.ExampleFollowing is the code −const str = 'hello world'; const interchangeChars = (str = '') => { const [first, second] = str.split(' '); const fChar = first[0]; const sChar = second[0]; const newFirst = sChar + first.substring(1, first.length); const newSecond = fChar + second.substring(1, second.length); const newStr = newFirst + ' ' + newSecond; return newStr; }; console.log(interchangeChars(str));OutputFollowing is the console output −wello horld
Read MoreNumber difference after interchanging their first digits in JavaScript
ProblemWe are required to write a JavaScript function that takes in an array of exactly two numbers. Our function should return the absolute difference between the numbers after interchanging their first digits.For instance, for the array [105, 413], The difference will be: |405 - 113| = 292ExampleFollowing is the code −const arr = [105, 413]; const interchangedDigitDiff = (arr = []) => { arr = arr.map(String); const [first, second] = arr; const fChar = first[0]; const sChar = second[0]; const newFirst = sChar + first.substring(1, first.length); const newSecond = fChar + second.substring(1, second.length); ...
Read More