Found 10784 Articles for Python

Remove Consecutive Duplicates in Python

Arnab Chakraborty
Updated on 22-Sep-2020 11:09:40

3K+ Views

Suppose we have a string s, this string consisting of "R" and "L", we have to remove the minimum number of characters such that there's no consecutive "R" and no consecutive "L".So, if the input is like "LLLRLRR", then the output will be "LRLR"To solve this, we will follow these steps −seen := first character of sans := first character of sfor each character i from index 1 to end of s, doif i is not same as seen, thenans := ans + iseen := ireturn ansLet us see the following implementation to get better understanding −Example Live Democlass Solution:   ... Read More

Connell sequence in Python

Arnab Chakraborty
Updated on 22-Sep-2020 11:06:14

397 Views

Suppose we have a number n, we have to find the nth term of Connell sequence. The Connell sequence is as follows: 1. Take first odd integer: 1 2. Take next two even integers 2, 4 3. Then take the next three odd integers 5, 7, 9 4. After that take the next four even integers 10, 12, 14, 16 And so on.So, if the input is like 12, then the output will be 21To solve this, we will follow these steps −i := 1while quotient of (i *(i + 1) / 2) < n + 1, doi := i ... Read More

Common Words in Two Strings in Python

Arnab Chakraborty
Updated on 22-Sep-2020 11:04:19

5K+ Views

Suppose we have two strings s0 and s1, they are representing a sentence, we have to find the number of unique words that are shared between these two sentences. We have to keep in mind that, the words are case-insensitive so "tom" and "ToM" are the same word.So, if the input is like s0 = "i love python coding", s1 = "coding in python is easy", then the output will be 2 as there are 2 common words, ['python', 'coding']To solve this, we will follow these steps −convert s0 and s1 into lowercases0List := a list of words in s0s1List ... Read More

Column Sort of a Matrix in Python

Arnab Chakraborty
Updated on 22-Sep-2020 11:02:40

2K+ Views

Suppose we have a matrix, we have to sort each of the columns in ascending order.So, if the input is like1121316641118then the output will be1646118112131To solve this, we will follow these steps −R := row count of matrix, C := column count of matrixres := matrix of same size as given matrix and fill with 0for col in range 0 to C, dovalues := take the elements as a vector of matrix[col]for row in range 0 to R, dores[row, col] := delete last element from valuesreturn resLet us see the following implementation to get better understanding −Example Live Democlass Solution:   ... Read More

Collatz sequence in Python

Arnab Chakraborty
Updated on 22-Sep-2020 11:00:19

2K+ Views

Suppose we have a positve integer n, we have to find the length of its Collatz sequence. As we know Collatz sequence is generated sequentially where n = n/2 when n is even otherwise n = 3n + 1. And this sequence ends when n = 1.So, if the input is like n = 13, then the output will be 10 as [13, 40, 20, 10, 5, 16, 8, 4, 2, 1] these is the sequence.To solve this, we will follow these steps −if num is same as 0, thenreturn 0length := 1while num is not same as 1, donum ... Read More

Total Distance to Visit City Blocks in Python

Arnab Chakraborty
Updated on 22-Sep-2020 10:58:41

156 Views

Suppose we have a matrix of unique strings representing the city blocks, and another list of strings containing blocks to visit. If we are at block matrix[0][0], then find the total Manhattan distance required to visit every block in order.So, if the input is likeQBCDEZGGiBlock = [H, B, C]Then the output will be 6 as "h" is 2 blocks bottom(south) and 1 block right(east), "b" is 2 blocks up(north), "c" is 1 block right(east).To solve this, we will follow these steps −coords := a map with key 'start' and value (0, 0)for each row in mat, dofor each col in ... Read More

Split String of Size N in Python

Arnab Chakraborty
Updated on 22-Sep-2020 10:56:36

939 Views

Suppose we have a string s and an integer n, we have to split the s into n-sized pieces.So, if the input is like s = "abcdefghijklmn", n = 4, then the output will be ['abcd', 'efgh', 'ijkl', 'mn']To solve this, we will follow these steps −i:= 0f:= a new listwhile i < size of s, doinsert s[from index i to i+n-1] at the end of fi := i + nreturn fLet us see the following implementation to get better understanding −Example Live Democlass Solution:    def solve(self, s, n):       i=0       f=[]       while(i

Get unique values from a list in Python

Pradeep Elance
Updated on 09-Sep-2020 12:36:28

3K+ Views

A list in python is a number of items placed with in [] which may or may not have same data types. It can also contain duplicates. In this article we will see how to extract only the unique values from a list.With append()In this approach we will first create a new empty list and then keep appending elements to this new list only if it is not already present in this new list. A for loop is used along with not in condition. It checks for the existence of the incoming element and it is appended only if it ... Read More

Get last element of each sublist in Python

Pradeep Elance
Updated on 09-Sep-2020 12:33:34

2K+ Views

A list in python can also contain lists inside it as elements. These nested lists are called sublists. In this article we will solve the challenge of retrieving only the last element of each sublist in a given list.Using for loopIt is a very simple approach in which we loop through the sublists fetching the item at index -1 in them. A for loop is used for this purpose as shown below.Example Live DemoAlist = [['Mon', 1], ['Tue', 'Wed', "Fri"], [12, 3, 7]] print("Given List:", Alist) print("Lastst Items from sublists:") for item in Alist:    print((item[-1]))OutputRunning the above code gives us ... Read More

Get first element of each sublist in Python

Pradeep Elance
Updated on 09-Sep-2020 12:32:15

4K+ Views

A list in python can also contain lists inside it as elements. These nested lists are called sublists. In this article we will solve the challenge of retrieving only the first element of each sublist in a given list.Using for loopIt is a very simple approach in which we loop through the sublists fetching the item at index 0 in them. A for loop is used for this purpose as shown below.Example Live DemoAlist = [['Mon', 1], ['Tue', 'Wed', "Fri"], [12, 3, 7]] print("Given List:", Alist) print("First Items from sublists:") for item in Alist:    print((item[0]))OutputRunning the above code gives us ... Read More

Advertisements