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 40 of 377
Book Pagination in Python
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 MoreBob's Game in Python
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 MoreBig Numbers in Python
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 MoreBase 3 to integer in Python
Converting a base 3 number to decimal involves multiplying each digit by the appropriate power of 3. Python provides several methods to perform this conversion efficiently. Understanding Base 3 to Decimal Conversion In base 3, each digit position represents a power of 3. For example, "10122" in base 3 equals: 1×3⁴ + 0×3³ + 1×3² + 2×3¹ + 2×3⁰ = 81 + 0 + 9 + 6 + 2 = 98 Method 1: Using Horner's Method This efficient algorithm processes digits from left to right, accumulating the result ? def base3_to_decimal(s): ...
Read MoreAustin Powers in Python
Suppose we have a number greater than 0, we have to check whether the number is a power of two or not. A power of two is any number that can be expressed as 2n where n is a non-negative integer (1, 2, 4, 8, 16, 32, etc.). So, if the input is like 1024, then the output will be True because 1024 = 210. Approach To solve this, we will follow these steps ? While n > 1, do n := n / 2 Return true when n is same as ...
Read MoreAtbash cipher in Python
The Atbash cipher is a simple substitution cipher where each letter is mapped to its reverse position in the alphabet. In this cipher, 'a' becomes 'z', 'b' becomes 'y', and so on. Let's explore how to implement this cipher in Python. Understanding the Atbash Cipher The Atbash cipher works by reversing the alphabet: Original: a b c d e f g h i j k l m n o p q r s t u v w x y z Atbash: z y x w v u t s r q p o n m ...
Read MoreA strictly increasing linked list in Python
A strictly increasing linked list is one where each node's value is greater than the previous node's value. We need to traverse the list and check if the values are in strictly ascending order. So, if the input is like [2, 61, 105, 157], then the output will be True because each element is greater than the previous one. Algorithm To solve this, we will follow these steps − Define a function solve() that takes the head of the linked list If head.next is null, return True (single node is considered strictly increasing) If head.val ...
Read MoreA number and its triple in Python
When working with lists of numbers, you might need to check if any number in the list is exactly triple another number. This problem can be solved efficiently using a two-pointer approach after sorting the list. For example, if we have nums = [2, 3, 10, 7, 9], the output should be True because 9 is the triple of 3. Algorithm Steps To solve this problem, we follow these steps ? Sort the list to enable two-pointer technique Initialize two pointers: i = 0 and j = ...
Read MoreAncient Astronaut Theory in Python
The Ancient Astronaut Theory problem involves checking if a string follows a custom lexicographic ordering defined by an ancient astronaut dictionary. Given a dictionary string that represents the order of characters, we need to verify if the input string is sorted according to this custom order. For example, if the dictionary is "bdc", then 'b' comes before 'd', and 'd' comes before 'c' in this custom ordering. Problem Understanding Given a dictionary string and an input string, we need to check if the characters in the input string appear in the same order as defined by the ...
Read MoreAcronym in Python
An acronym is formed by taking the first letter of each word in a phrase. In Python, we can create acronyms by splitting the phrase into words and extracting the first character of each word, excluding common words like "and". Problem Statement Given a string representing a phrase, we need to generate its acronym. The acronym should be capitalized and exclude the word "and". For example, if the input is "Indian Space Research Organisation", the output should be "ISRO". Algorithm To solve this problem, we follow these steps ? Split the string into ...
Read More