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
-
Economics & Finance
Articles by Arnab Chakraborty
Page 127 of 377
Count Elements x and x+1 Present in List in Python
Suppose we have a list of numbers called nums, we have to find the number of elements x there are such that x + 1 exists as well. So, if the input is like [2, 3, 3, 4, 8], then the output will be 3 because: 2 has 3 (2+1) in the list 3 has 4 (3+1) in the list (counted twice since 3 appears twice) 4 doesn't have 5 in the list 8 doesn't have 9 in the list Algorithm To solve this, we will follow these steps − Create a ...
Read MoreCell Count After Removing Corner Diagonals in Python
Suppose we have a number n representing the length of an n × n board. We need to remove all cells that are diagonal to one of the four corners and count the remaining cells. In this problem, we identify cells that lie on the main diagonal (top-left to bottom-right) or anti-diagonal (top-right to bottom-left) and mark them as removed. The remaining cells represent our answer. Problem Visualization For n = 4, the board looks like this after removing diagonal cells ? XOOX OXXO OXXO XOOX Here, X represents removed diagonal ...
Read MoreConnell sequence in Python
The Connell sequence is a mathematical sequence that alternates between groups of consecutive odd and even integers, with each group size increasing by one. Understanding this pattern helps us find the nth term efficiently. Understanding the Connell Sequence Pattern The sequence follows this specific pattern ? Take first 1 odd integer: 1 Take next 2 even integers: 2, 4 Take next 3 odd integers: 5, 7, 9 Take next 4 even integers: 10, 12, 14, 16 Continue alternating with increasing group sizes The complete sequence looks like: 1, 2, 4, 5, 7, 9, 10, ...
Read MoreCommon Words in Two Strings in Python
Finding common words between two strings is a frequent programming task. This involves splitting strings into words, handling case-insensitivity, and finding intersections between word sets. For example, if we have s0 = "i love python coding" and s1 = "coding in python is easy", the common words are python and coding, so the count is 2. Algorithm Steps To find common words between two strings, follow these steps: Convert both strings to lowercase for case-insensitive comparison Split each string into a list of words using split() Convert word lists to sets and find their intersection ...
Read MoreColumn Sort of a Matrix in Python
Matrix column sorting involves sorting each column of a matrix independently in ascending order. This is useful for organizing data where each column represents different attributes that need to be ordered separately. Problem Statement Given a matrix, we need to sort each column in ascending order while keeping the column structure intact. Example Input and Output Input Matrix: 11 21 31 6 6 4 1 11 8 Expected Output: 1 6 4 6 11 8 11 21 31 ...
Read MoreCollatz sequence in Python
The Collatz sequence (also known as the 3n+1 problem) is a mathematical sequence where we repeatedly apply rules to a positive integer until it reaches 1. For any positive integer n: If n is even: n = n/2 If n is odd: n = 3n + 1 Continue until n = 1 For example, if n = 13, the sequence is: [13, 40, 20, 10, 5, 16, 8, 4, 2, 1] with a length of 10. Algorithm Steps To find the length of a Collatz sequence ? If num is 0, return ...
Read MoreTotal Distance to Visit City Blocks in Python
Suppose we have a matrix of unique strings representing the city blocks, and another list of strings containing blocks to visit. If we are at block matrix[0][0], then find the total Manhattan distance required to visit every block in order. The Manhattan distance between two points (x1, y1) and (x2, y2) is calculated as |x1 - x2| + |y1 - y2|. Example Problem Consider this city block matrix ? Q B C D E Z G H I Blocks to visit: ["H", "B", "C"] Starting from ...
Read MoreSplit String of Size N in Python
Splitting a string into chunks of equal size is a common task in Python programming. This involves breaking down a string into smaller substrings, each containing exactly n characters (except possibly the last chunk). For example, if we have s = "abcdefghijklmn" and n = 4, the output will be ['abcd', 'efgh', 'ijkl', 'mn']. Using a While Loop The most straightforward approach uses a while loop to iterate through the string and extract chunks ? def split_string(s, n): i = 0 chunks = [] ...
Read MoreChanging Directions in Python
In Python, we can determine how many times a list changes direction by identifying peaks and valleys. A direction change occurs when the slope switches from increasing to decreasing (peak) or decreasing to increasing (valley). Given a list like [2, 4, 10, 18, 6, 11, 13], the output should be 2 because the direction changes at index 3 (18 is a peak) and at index 4 (6 is a valley). Algorithm To solve this problem, we follow these steps: Initialize a counter to track direction changes Iterate through the list from index 1 to length-2 ...
Read MoreCell fusion in Python
Cell fusion is a problem where cells of different sizes interact according to specific rules. Given a list of cell sizes, we simulate iterations where the two largest cells interact: if they're equal, both die; otherwise, they merge into a new cell with size floor((a + b) / 3). For example, with cells [20, 40, 40, 30], the two largest cells (40, 40) are equal so they die, leaving [20, 30]. These merge to form floor((20 + 30) / 3) = 16. Algorithm We use a max heap to efficiently find the two largest cells in each ...
Read More