Matrix multiplication is a fundamental operation in linear algebra where we multiply two matrices to produce a resultant matrix. In Python, we can implement matrix multiplication using nested loops and list comprehensions. Algorithm Step 1: Input two matrices of compatible dimensions Step 2: Initialize a result matrix with zeros Step 3: Use three nested loops: - Outer loop for rows of first matrix - Middle loop for columns of second matrix - Inner loop for dot product calculation Step 4: For each position (i, j), multiply ... Read More
Modular exponentiation calculates (baseexponent) % modulo efficiently. This operation is fundamental in cryptography and number theory, involving three integers: base, exponent, and modulo. Syntax # Basic syntax result = (base ** exponent) % modulo # Using pow() function (recommended) result = pow(base, exponent, modulo) Basic Example Let's calculate (25) % 5 step by step ? base = 2 exponent = 5 modulo = 5 # Step by step calculation power_result = base ** exponent print(f"{base}^{exponent} = {power_result}") final_result = power_result % modulo print(f"{power_result} % {modulo} = {final_result}") ... Read More
String rotation involves moving characters from one end of a string to the other. Python's string slicing makes this operation simple and efficient. We can rotate strings in two directions: left rotation (anticlockwise) and right rotation (clockwise). Understanding String Rotation Given a string and a rotation distance d: Left Rotation: Move first d characters to the end Right Rotation: Move last d characters to the beginning Example Input: string = "pythonprogram" d = 2 Output: Left Rotation: thonprogrampy Right Rotation: ampythonprogr Algorithm ... Read More
Given a string that has set of words and spaces, our task is to move all spaces to front of string, by traversing the string only once. We will solve this problem quickly in Python using list comprehension. Example Input: string = "python program" Output: string = " pythonprogram" Algorithm The algorithm follows these steps: Input a string with words and spaces Traverse the input string using list comprehension to extract non-space characters Calculate the number of spaces in ... Read More
In this article, we are going to learn how to check if the number is positive, negative, odd, even, or zero. Identifying and categorizing a number based on its characteristics is a basic operation. A number can fall into multiple categories: Positive: A number is greater than 0. Negative: A number less than 0. Zero: The number that is equal to 0. Even: A number that is divisible by 2. Odd: A number that is not divisible by ... Read More
A sublist is a portion of a list containing one or more elements from the original list. Sublists can be contiguous (elements appear in the same order as the original) or non-contiguous (elements are taken from different positions). Printing Contiguous Sublists Using Nested Loops Contiguous sublists maintain the original order of elements. We can generate them using nested loops ? def print_contiguous_sublists(items): for i in range(len(items)): for j in range(i, len(items)): print(items[i:j+1]) ... Read More
In Python, Strings are one of the commonly used data types that are used to store sequences of characters. In this article, we are going to learn to concatenate strings with uncommon characters in Python. The uncommon characters are the characters that appear in one string but not in the other string. For example, if str1="ABC" and str2="BCD", the uncommon characters are 'A' and 'D', and the concatenated result is "AD". Using set.symmetric_difference() Method The set.symmetric_difference() method returns a new set that contains elements present in either of the sets, but not in both. This makes it ... Read More
Given two lists, we need to find and print all the common elements between them. Python provides several approaches to solve this problem efficiently. Examples Input : list1 = [5, 6, 7, 8, 9] list2 = [5, 13, 34, 22, 90] Output : {5} Explanation The common element between both lists is 5, which appears in both lists. Using Set Intersection Convert both lists to sets and use the intersection operator (&) to find common elements ? def find_common_elements(list1, list2): ... Read More
Here we need to calculate a series where each term is formed by repeating digit n multiple times: n + nn + nnn + ... up to m terms. For example, if n=3 and m=4, the series would be 3 + 33 + 333 + 3333. Algorithm Step 1: Input n and m values Step 2: Convert n to string for concatenation Step 3: Initialize sum with first term (n) Step 4: Build each term by concatenating n repeatedly Step 5: Convert string back to integer and add to sum Step 6: Return final sum ... Read More
In this article, we'll learn how to determine the astrological sign or zodiac sign for a given date of birth. We'll compare the user's birthdate with predefined date ranges for each zodiac sign using Python programming logic. The 12 astrological sun signs, also known as zodiac signs, are based on specific periods throughout the year. Each sign corresponds to a particular range of dates ? Aries (March 21 − April 19) Taurus (April 20 − May 20) Gemini (May 21 − June 20) Cancer (June ... Read More
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Economics & Finance