Programming Articles

Page 536 of 2547

Split String of Size N in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 1K+ Views

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 More

Changing Directions in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 1K+ Views

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 More

Cell fusion in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 249 Views

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

camelCase in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 9K+ Views

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 More

Caesar Cipher in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 1K+ Views

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 More

Buying Cars in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 451 Views

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 More

Boss Fight in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 757 Views

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

Book Pagination in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 1K+ Views

Book pagination is a common requirement when displaying large amounts of text content in manageable chunks. In Python, we can implement pagination by calculating the starting index based on the page number and page size, then extracting the appropriate slice of data. Given a list of strings representing a book, a page index (0-indexed), and a page size, we need to return the list of words on that specific page. If the page is out of bounds, we return an empty list. Problem Example If we have: book = ["hello", "world", "programming", "language", "python", "c++", "java"] ...

Read More

Bob's Game in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 753 Views

Suppose we have a friend named Bob, and he is playing a game with himself. He gives himself a list of numbers called nums. Now in each turn, Bob selects two elements of the list and replaces them with one positive integer with the same sum as the numbers he selected. Bob declares victory when all of the numbers in the array are even. We have to find the minimum number of turns required by Bob so he can declare victory. If there is no such solution, then return -1. So, if the input is like [2, 3, 4, ...

Read More

Big Numbers in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 460 Views

In Python, working with big numbers (arbitrary precision integers) is seamless because Python automatically handles integers of any size. Unlike many other programming languages that have fixed-size integer types, Python's int type can grow as large as your system's memory allows. Python's Built-in Big Number Support Python automatically converts integers to arbitrary precision when they exceed the typical 32-bit or 64-bit limits ? # Large numbers are handled automatically big_number = 12345678901234567890123456789 print("Big number:", big_number) print("Type:", type(big_number)) # Arithmetic operations work seamlessly result = big_number * 999999999999999999999 print("Multiplication result:", result) Big number: ...

Read More
Showing 5351–5360 of 25,466 articles
« Prev 1 534 535 536 537 538 2547 Next »
Advertisements