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
Python Articles
Page 659 of 855
Python Group Anagrams from given list
In this tutorial, we are going to write a program that groups all anagrams in a list. First, let's see what are anagrams. Any two strings that have the same characters in a different order are known as anagrams. Before diving into the solution, let's see an example. Example Input and Output words = ['cat', 'dog', 'fired', 'god', 'pat', 'tap', 'fried', 'tac'] print("Input:", words) Input: ['cat', 'dog', 'fired', 'god', 'pat', 'tap', 'fried', 'tac'] Expected output: [['cat', 'tac'], ['dog', 'god'], ['fired', 'fried'], ['pat', 'tap']] Understanding the Approach ...
Read MorePython Getting sublist element till N
In this tutorial, we will learn how to extract the first element from each sublist up to the Nth sublist in a nested list. For example, if we have a list with 5 sublists and want the first element from the first 3 sublists, Python provides several approaches to accomplish this. Let's explore different methods using the following example list: programming_languages = [['Python', 'Java'], ['C', 'Pascal'], ['Javascript', 'PHP'], ['C#', 'C++'], ['React', 'Angular']] print("Original list:", programming_languages) Original list: [['Python', 'Java'], ['C', 'Pascal'], ['Javascript', 'PHP'], ['C#', 'C++'], ['React', 'Angular']] Using For Loop The ...
Read MoreProgram to check congruency of two triangles in Python
In geometry, two triangles are congruent if they have the same shape and size. We can check triangle congruency using three main criteria: SSS (Side-Side-Side), SAS (Side-Angle-Side), and ASA (Angle-Side-Angle). Note that AAA (Angle-Angle-Angle) only proves similarity, not congruency. Let's implement functions to check each congruency condition ? SSS (Side-Side-Side) Congruency Two triangles are congruent if all three corresponding sides are equal ? def side_side_side_congruent(sides_one, sides_two): # Sort both lists to compare corresponding sides sides_one_sorted = sorted(sides_one) sides_two_sorted = sorted(sides_two) ...
Read MoreProgram for longest common directory path in Python
In this tutorial, we are going to write a program that finds the longest common directory path from a given list of paths. This is useful when working with file systems or organizing directory structures. Problem Statement Given a list of file paths, we need to find the longest directory path that is common to all of them ? paths = ['home/tutorialspoint/python', 'home/tutorialspoint/c', 'home/tutorialspoint/javascript', 'home/tutorialspoint/react', 'home/tutorialspoint/django'] Expected Output: home/tutorialspoint Using os.path.commonprefix() Python's os module provides os.path.commonprefix() to find the common prefix of multiple ...
Read MoreBinary Prefix Divisible By 5 in Python
Given an array A of 0s and 1s, we need to determine which binary prefixes are divisible by 5. For each index i, we interpret the subarray from A[0] to A[i] as a binary number and check if it's divisible by 5. For example, if the input is [0, 1, 1, 1, 1, 1], the binary prefixes are: 0 (binary) = 0 (decimal) → divisible by 5 01 (binary) = 1 (decimal) → not divisible by 5 011 (binary) = 3 (decimal) → not divisible by 5 0111 (binary) = 7 (decimal) → not divisible by 5 ...
Read MorePowerful Integers in Python
A powerful integer is defined as a number that can be expressed in the form xi + yj where x and y are positive integers, and i, j are non-negative integers. Given two positive integers x and y along with a bound, we need to find all powerful integers less than or equal to the bound. Understanding Powerful Integers For example, with x = 2, y = 3, and bound = 10: 2 = 20 + 30 = 1 + 1 3 = 21 + 30 = 2 + 1 4 = 20 + 31 = ...
Read MoreDelete Columns to Make Sorted in Python
Given an array of N lowercase letter strings of equal length, we need to find the minimum number of column deletions required to make all remaining columns sorted in non-decreasing order. When we delete columns at specific indices, the remaining characters form new columns. Each remaining column must be sorted for the solution to be valid. Problem Understanding Consider the array ["abcdef", "uvwxyz"] with deletion indices {0, 2, 3}: After deletions: ["bef", "vyz"] Remaining columns: ["b", "v"], ["e", "y"], ["f", "z"] All columns are sorted in non-decreasing order Algorithm The approach involves ...
Read MoreDI String Match in Python
The DI String Match problem involves creating a permutation based on a pattern string containing "I" (increase) and "D" (decrease). Given a string S of length N, we need to return a permutation of numbers [0, 1, ..., N] that satisfies the increase/decrease pattern. Problem Understanding For a string S containing only "I" and "D" characters: If S[i] is "I", then A[i] < A[i+1] (increasing) If S[i] is "D", then A[i] > A[i+1] (decreasing) For example, with input "IDID", a valid output is [0, 4, 1, 3, 2] because: 0 < 4 (I), ...
Read MoreValid Mountain Array in Python
A valid mountain array is an array that rises to a peak and then falls, resembling a mountain shape. To be valid, it must have at least 3 elements, strictly increase to a peak, then strictly decrease. Mountain Array Requirements An array is a valid mountain if it satisfies these conditions: Array size >= 3 There exists a peak index i where: Elements strictly increase: A[0] < A[1] < ... < A[i] Elements strictly decrease: A[i] > A[i+1] > ... > A[length-1] Algorithm Approach We use a two-phase traversal approach: Ascending ...
Read MoreReorder Data in Log Files in Python
Suppose we have an array of logs. In that array each entry is a space delimited string of words. The first word in each log is an alphanumeric identifier. Then, there are different types of strings like below − Each word after the id will consist only of lowercase letters; Each word after the id will consist only of digits. We will call these two types of logs as letter-logs and digit-logs respectively. It is guaranteed that each log has at least one word after its id. ...
Read More