Found 10476 Articles for Python

Program to find the K-th last node of a linked list in Python

Arnab Chakraborty
Updated on 09-Oct-2020 14:29:45

1K+ Views

Suppose we have a singly linked list, we have to check find the value of the kth last node (0-indexed). We have to solve this in single pass.So, if the input is like node = [5, 4, 6, 3, 4, 7], k = 2, then the output will be 3, as The second last (index 3) node has the value of 3.To solve this, we will follow these steps −klast := nodelast := nodefor i in range 0 to k, dolast := next of lastwhile next of last is not null, dolast := next of lastklast := next of klastreturn ... Read More

Program to find maximum sum of popped k elements from a list of stacks in Python

Arnab Chakraborty
Updated on 09-Oct-2020 14:25:24

333 Views

Suppose we have a list of stacks and an integer k. We have to find the maximum possible sum that can be achieved from popping off exactly k elements from any combination of the stacks.So, if the input is like stacks = [[50, -4, -15], [2], [6, 7, 8]], k = 4, then the output will be 39, as we can pop off all 3 elements from the first stack and pop the last element of the last stack to get -15 + -4 + 50 + 8 = 39.To solve this, we will follow these steps −Define a function ... Read More

Program to find a list of numbers where each K-sized window has unique elements in Python

Arnab Chakraborty
Updated on 09-Oct-2020 14:17:28

280 Views

Suppose we have a list of numbers called nums and another number k, we have to find a list of count of distinct numbers in each window of size k.So, if the input is like nums = [2, 2, 3, 3, 4], k = 2, then the output will be [1, 2, 1, 2], as the windows are [2, 2], [2, 3], [3, 3], and [3, 4].To solve this, we will follow these steps −c := make a dictionary of elements in nums and their frequenciesans := a new listfor i in range k to size of nums, doinsert size ... Read More

Program to find the perimeter of an island shape in Python

Arnab Chakraborty
Updated on 09-Oct-2020 14:13:04

499 Views

Suppose we have a binary matrix where 0 shows empty cell and 1 shows a block that forms a shape, now we have to find the perimeter of the shape. The shape will not hold any hole inside it.So, if the input is like0000000111001100111000000then the output will be 14.To solve this, we will follow these steps −d := 0perimeter := 0height := row count of matrixlength := column count of matrixfor each row in matrix, doc := 0for each val in row, doif val is same as 1, thensurround := 4if c is not same as length - 1, thenif ... Read More

Program to interleave list elements from two linked lists in Python

Arnab Chakraborty
Updated on 09-Oct-2020 14:01:44

525 Views

Suppose we have two linked lists l1 and l2, we have to return one linked list by interleaving elements of these two lists starting with l1. If there are any leftover nodes in a linked list, they should be appended to the list.So, if the input is like l1 = [5, 4, 6, 3, 4, 7] l2 = [8, 6, 9], then the output will be [5, 8, 4, 6, 6, 9, 3, 4, 7]To solve this, we will follow these steps −ans := l1while l2 is not null, doif ans is not null, thenif next of ans is not ... Read More

Program to count n digit integers where digits are strictly increasing in Python

Arnab Chakraborty
Updated on 09-Oct-2020 13:45:58

252 Views

Suppose we have a number n, we have to find the number of n-digit positive integers such that the digits are in strictly increasing order.So, if the input is like n = 3, then the output will be 84, as numbers are 123, 124, 125, ..., 678,789To solve this, we will follow these steps −if n < 9 is non-zero, thenreturn Combination (9Cn)otherwise,return 0Let us see the following implementation to get better understanding −Example Live Demofrom math import factorial as f class Solution:    def solve(self, n):       if n < 9:          return f(9) / f(n) / f(9 - n)       else:          return 0 ob = Solution() print(ob.solve(3))Input3Output84

Program to check whether we can reach last position from index 0 in Python

Arnab Chakraborty
Updated on 09-Oct-2020 13:40:03

210 Views

Suppose we have a list of numbers called nums where each number shows the maximum number of jumps we can make; we have to check whether we can reach to the last index starting at index 0 or not.So, if the input is like nums = [2, 5, 0, 2, 0], then the output will be True, as we can jump from index 0 to 1, then jump from index 1 to end.To solve this, we will follow these steps−n := size of numsarr := an array of size n and fill with falsearr[n - 1] := Truefor i in ... Read More

Program to find minimum number of heights to be increased to reach destination in Python

Arnab Chakraborty
Updated on 08-Oct-2020 14:52:04

211 Views

Suppose we have a matrix M where M[r][c] represents the height of that cell. If we are currently at top left corner and want to go to the bottom right corner. We can move to adjacent cells (up, down, left, right) only if that the height of that adjacent cell is less than or equal to the current cell's height. We can increase the height of any number of cells before we move, so we have to find the minimum total height that needs to be increased so that we can go to the bottom right cell.So, if the input ... Read More

How to use for loop to print all the elements of a list in R?

Nizamuddin Siddiqui
Updated on 08-Oct-2020 14:41:45

2K+ Views

Applying for loop to a vector or a list is no different, we can simply use in the usual manner. For example, if we have a list called List and we want to print all the elements of the list then we can use the code for(i in List){print(i)}, here i refers to the vectors in the List.Example Live DemoList

Program to group a set of points into k different groups in Python

Arnab Chakraborty
Updated on 08-Oct-2020 14:28:00

1K+ Views

Suppose we have a list of points and a number k. The points are in the form (x, y) representing Cartesian coordinates. We can group any two point p1 and p2 if the Euclidean distance between them is

Advertisements