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
Python Articles
Page 168 of 852
Remove Duplicates from Sorted Array in Python
Suppose we have a sorted list A. We have to return the length of the array after removing all duplicate entries. We have to perform this in O(1) extra space. So we have to do the operation in-place.For an example, suppose A = [1, 1, 2, 2, 2, 3, 3, 3, 3, 4, 5, 5, 5, 6] Then the output will be 6, as there are six distinct elements.To solve this, follow these steps −If the list is empty, return 0otherwise, initially take prev = first element of A. And define length = 0for i := 1 to n-1, doif ...
Read MoreNumber of operations required to make all array elements Equal in Python
We have given an array of elements, and we have to make them all equal by incrementing the elements by 1. We are allowed to increment n - 1 element at each step. Our goal is to calculate the total number of operations required to make all the array elements equal.For example, if you take the list [1, 2, 3], it took three operations to make all the elements equal. One solution to the problem is. Find the most significant number at each step and increment the rest of the elements by 1. Let's write the code.Exampledef main(): # ...
Read MoreReverse Vowels of a String in Python
Suppose we have a lowercase string. Our task is to reverse the vowels present in the string. So if the string is “hello”, then the string after vowel reversal will be “holle”. For string “programming”, it will be “prigrammong”To solve this, we will follow these steps −Take the string and make a list of vowels, and store their indices as wellreverse the vowel listset idx := 0for i := 0 to length of given string – 1if i is in index list −put vowels[i] into final stringidx := idx + 1otherwise put string[i] into final stringreturn the list as a ...
Read MoreReverse Only Letters in Python
Suppose we have a string S, we have to find the reversed string where all characters that are not a letter will not change their positions and all letters reverse their positions. So if the given string is "a-bC-dEf-ghIj", then output will be "j-Ih-gfE-dCba"To solve this, we will follow these steps −We will use the regular expression library to solve thisif S is empty, then return Sstr := an empty string, index1 := 0 and index2 := length of S – 1while index1 < length of Sif index2 >= 0 and S[index1] is alphabet and S[index2] is alphabetstr := str ...
Read MoreRemove All Adjacent Duplicates In String in Python
Suppose we have a string S of lowercase letters; a duplicate removal operation will be performed. This will be done by choosing two adjacent and equal letters, and removing them.We will repeatedly remove duplicates from S until no duplicates are remaining.Return the string after all such duplicate removals have been completed. It is guaranteed that the answer is unique.Suppose the string is “abbacaca”, then answer will be “caca”. At first delete duplicates bb, then string is “aacaca”, then remove aa, then string is “caca”, then no such duplicates are there.To solve this, we will follow these steps −Define an array ...
Read MoreNested list comprehension in python
A nested list is a list within a list. Python provides features to handle nested list gracefully and apply common functions to manipulate the nested lists. In this article we will see how to use list comprehension to create and use nested lists in python.Creating a MatrixCreating a matrix involves creating series of rows and columns. We can use for loop for creating the matrix rows and columns by putting one python list with for loop inside another python list with for loop.Examplematrix = [[m for m in range(4)] for n in range(3)] print(matrix)Running the above code gives us the ...
Read MoreGreatest Common Divisor of Strings in Python
Suppose there are two strings A and B. We can say that A is divisible by B, when A is created by concatenating B one or more times. So if A = “abcabc”, and B = “abc”, then A is divisible by B. In this section, we will see what is the greatest common divisor of a String. So return the largest string that divides both of the strings. So if two strings are “ABABAB”, and “ABAB”, then GCD will be “AB”To solve this, we will follow these steps −temp := shorter string between A and Bm := length of ...
Read MoreHeight Checker in Python
Suppose a set of students have to be arranged in non-decreasing order of their heights for a photograph. If we have an array of students, we have to return the minimum number of students that are not present in correct position. So if the array is like [1, 1, 4, 2, 1, 3], then output will be 3. So students with height 4, 3 and the last 1 are not standing in the correct position.To solve this, we will follow these steps −answer := 0let x := Array in sorted formley y := Arrayfor i := 0 to size of ...
Read MoreFind minimum of each index in list of lists in Python
In some problems we need to identify the minimum of each element in a list. But in solving the matrix operations, we need to find the minimum of each column in the matrix. That needs us to find the minimum value from list of lists. Because each column of a matrix is a list of lists.Using min() and zip()In the below example we use the min() and zip(). Here the zip() function organizes the elements at the same index from multiple lists into a single list. Then we apply the min() function to the result of zip function using a ...
Read MoreContains Duplicate in Python
Suppose we have a list of numbers. We have to check whether the list is holding some duplicate elements or not. So if the list is like [1, 5, 6, 2, 1, 3], then it will return 1 as there are two 1s, but if the list is [1, 2, 3, 4], then it will be false, as there is no duplicate present.To solve this, we will follow this approach −We know that the set data structure only holds unique data. But the list can fold duplicate contents. So if we convert the list into the set, its size will ...
Read More