The binary numbers are the basis of how data is stored, processed and transferred. Every integer is internally represented by using a sequence of bits (combination of 0s and 1s). In this article, we will learn how to check if binary representation of two numbers are anagram. Two numbers are said to be binary anagrams if their binary representations contain the same number of 1s and 0s. For example, the number 12 in binary is 1100 and the number 10 in binary is 1010, both have two 0s and two 1s, making them binary anagrams. Using collections.Counter() Class ... Read More
Given a number n, we can print m multiples of a step value without using any loop by implementing a recursive function. This approach uses function calls to simulate iteration. Problem Statement We need to print numbers starting from n, decreasing by a step value until we reach 0 or negative, then increasing back to the original number. Example Input: n = 15, step = 5 Output: 15 10 5 0 5 10 15 Algorithm The recursive approach works as follows: Start with the given number n and a flag ... Read More
In this article, we will explore how to find missing and additional values in two lists. This type of comparison is commonly encountered in real-world scenarios such as data validation, system synchronization, and database reconciliation. The task is to determine which elements are missing in one list and what unexpected elements were received. This can be achieved by using Python's built-in data structures like sets that allow for efficient comparison operations. Understanding the Problem When comparing two lists, we need to identify: Missing values: Elements present in the first list but absent in the second list ... Read More
A pangram is a sentence that contains every letter of the alphabet at least once. For example, "The quick brown fox jumps over the lazy dog" is a famous pangram. We can check if a sentence is a pangram using Python's set() operations. Example Input and Output Input: string = 'abc def ghi jkl mno pqr stu vwx yz' Output: Yes // contains all the characters from 'a' to 'z' Input: string = 'python program' Output: No // Does not contain all the characters from 'a' to 'z' Algorithm Here's the step-by-step approach ... Read More
Bit extraction involves extracting a specific number of bits from a given position in a number's binary representation. This is useful in low-level programming, cryptography, and data manipulation tasks. Understanding the Problem Given a number, we need to extract 'k' consecutive bits starting from a specific position 'pos' (counted from the right, starting at 0). The extracted bits are then converted back to decimal. Example Input: number=170, k=5, pos=2 Output: 21 Binary of 170: 10101010 Position 2 from right, extract 5 bits: 01010 Decimal value: 21 Algorithm The ... Read More
In Python, Lists are one of the built-in data types used for storing collections of data. Python lists are sequences of comma-separated items, enclosed in square brackets []. They are flexible, easy to manipulate and widely used in various applications. In this article, we will explore different methods to find the difference between two lists. Python provides several built-in approaches for achieving this. Using set.difference() Method The Python set.difference() method returns a new set containing elements that are present in the first set but not in any other sets provided as arguments. Syntax set.difference(*others) ... Read More
The distinct elements are the values that appear only once or uniquely in an array. When working with arrays, we often encounter repeated or duplicate values. In this article, we will explore different methods to print all distinct elements of a given integer array. Identifying and printing these distinct elements is a common task to avoid unexpected results. This can be achieved using Python built-in tools like sets, dictionaries, and loops. Using Python set() Function In this approach, we use the Python set() function, which automatically removes all duplicate values from the list because a set only ... Read More
The checkboard pattern is a square grid composed of alternating 0s and 1s, arranged in a way that no two adjacent cells have the same value. It looks like a chessboard, where black and white squares alternate in every row and column. This pattern is commonly used in chess, checkers, image processing, graphics, and visualization. In this article, we'll learn how to create a checkboard pattern of n×n using NumPy. Using numpy.indices() Method The numpy.indices() method returns a grid of indices with the given shape. It creates coordinate matrices from coordinate vectors that we can use to ... Read More
Inter-process communication (IPC) is essential when multiple processes need to share data and work together. In Python, the multiprocessing module provides various IPC mechanisms, one of which is the pipe. A pipe allows data to be transferred between processes in either two-way (duplex) or one-way mode. It is particularly useful when a parent process creates a child process and they need to exchange messages or data. Python multiprocessing.Pipe() Method The multiprocessing.Pipe() method returns a pair of connection objects connected by a pipe. This pipe enables sending and receiving data between two processes. Syntax multiprocessing.Pipe(duplex=True) ... Read More
Password validation is crucial for application security. A strong password should meet specific criteria including minimum length, mixed case letters, numbers, and special characters. Python's re module provides regular expressions to check these requirements efficiently. Password Validation Rules A valid password must satisfy the following criteria − Minimum 8 characters long At least one lowercase letter (a-z) At least one uppercase letter (A-Z) At least one digit (0-9) At least one special character (_ @ $) No whitespace characters Algorithm Step 1: Import re module for regular expressions Step 2: Get password ... Read More
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Economics & Finance