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
Server Side Programming Articles
Page 532 of 2109
Column 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 MorecamelCase in Python
CamelCase is a naming convention where the first word starts with a lowercase letter and each subsequent word starts with an uppercase letter, with no spaces between words. In Python, we can convert a list of words to camelCase format using string manipulation. So, if the input is like ["Hello", "World", "Python", "Programming"], then the output will be "helloWorldPythonProgramming". Algorithm Steps To solve this, we will follow these steps − Initialize an empty string For each word in the list − ...
Read MoreCaesar Cipher in Python
The Caesar Cipher is a simple encryption technique where each letter in a string is shifted by a fixed number of positions in the alphabet. When a letter goes past 'z', it wraps around to 'a'. For example, if we have the string "hello" and shift by 3 positions, 'h' becomes 'k', 'e' becomes 'h', and so on, resulting in "khoor". Algorithm Steps To implement the Caesar cipher, we follow these steps ? Convert each character to its position in the alphabet (0-25) Add the shift value ...
Read MoreBuying Cars in Python
Suppose we have a list of prices of cars for sale, and we also have a budget k, we have to find the maximum number of cars we can buy. So, if the input is like [80, 20, 10, 30, 80], k = 85, then the output will be 3 as we can buy three cars with prices 20, 10, 30. Algorithm To solve this, we will follow these steps − Initialize count := 0 Sort the list of prices in ascending order For i in range 0 to size of prices, do If prices[i]
Read MoreBoss Fight in Python
In this problem, we have a fighters list representing fighters (where 1 = fighter) and a bosses matrix where each row represents a boss formation (where 1 = boss). Fighters can defeat a boss row only if they have more fighters than that boss row has bosses. We need to return the remaining undefeated boss rows. Problem Understanding Given fighters = [0, 1, 1] (2 fighters total) and the boss matrix: Boss Matrix: [0, 0, 0] → 0 bosses (defeated: 2 > 0) ...
Read More