Found 10784 Articles for Python

Program to find minimum number of rocketships needed for rescue in Python

Arnab Chakraborty
Updated on 21-Oct-2020 10:23:11

133 Views

Suppose we have a list of numbers called weights this is representing peoples' weights and a value limit determines the weight limit of one rocket ship. Now each rocketship can take at most two people. We have to find the minimum number of rocket ships it would take to rescue everyone to Planet.So, if the input is like weights = [300, 400, 300], limit = 600, then the output will be 2, as it will take one rocket ship to take the two people whose weights are 300 each, and another to take the person whose weight is 400.To solve ... Read More

Program to reverse linked list by groups of size k in Python

Arnab Chakraborty
Updated on 20-Oct-2020 10:55:37

317 Views

Suppose we have a singly linked list, and another value k, we have to reverse every k contiguous group of nodes.So, if the input is like List = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], k = 3, then the output will be [3, 2, 1, 6, 5, 4, 9, 8, 7, 10, ]To solve this, we will follow these steps −tmp := a new node with value 0next of tmp := nodeprev := null, curr := nulllp := temp, lc := currcnt := kwhile curr is not null, doprev := nullwhile cnt > 0 and curr ... Read More

Program to reverse the directed graph in Python

Arnab Chakraborty
Updated on 20-Oct-2020 10:53:25

912 Views

Suppose we have a directed graph, we have to find its reverse so if an edge goes from u to v, it now goes from v to u. Here input will be an adjacency list, and if there are n nodes, the nodes will be (0, 1, ..., n-1).So, if the input is likethen the output will beTo solve this, we will follow these steps −ans := a list of n different lists, where n is number of verticesfor each index i, and adjacent list l in graph, dofor each x in l, doinsert i at the end of ans[x]return ... Read More

Program to reverse a linked list in Python

Arnab Chakraborty
Updated on 20-Oct-2020 10:51:14

416 Views

Suppose we have a linked list, we have to reverse it. So if the list is like 2 -> 4 -> 6 -> 8, then the new reversed list will be 8 -> 6 -> 4 -> 2.To solve this, we will follow this approach −Define one procedure to perform list reversal in recursive way as solve(head, back)if head is not present, then return headtemp := head.nexthead.next := backback := headif temp is empty, then return headhead := tempreturn solve(head, back)Let us see the following implementation to get better understanding −Exampleclass ListNode:    def __init__(self, data, next = None):   ... Read More

Program to find string after removing consecutive duplicate characters in Python

Arnab Chakraborty
Updated on 20-Oct-2020 10:48:27

320 Views

Suppose we have a string s, we repeatedly delete the first consecutive duplicate characters. We have to find the final string.So, if the input is like s = "xyyyxxz", then the output will be "z", as "yyy" are the first consecutive duplicate characters which will be deleted. So we have "xxxz". Then "xxx" will be deleted to end up with "z".To solve this, we will follow these steps −stack := a new stacki := 0while i < size of s, doif stack is not empty and top of stack is same as s[i], thenx := delete last element from stackwhile ... Read More

Program to count minimum invalid parenthesis to be removed to make string correct in Python

Arnab Chakraborty
Updated on 20-Oct-2020 10:45:21

364 Views

Suppose we have a string of parentheses; we have to write a function to compute the minimum number of parentheses to be removed to make the string correct (each open parenthesis is eventually closed).So, if the input is like "(()))(", then the output will be 2, as the correct string is "(())", remove ")(".To solve this, we will follow these steps −total := 0, temp := 0for each p in s, doif p is same as "(", thentotal := total + 1otherwise when p is same as ")" and total is not 0, thentotal := total - 1otherwise, temp := ... Read More

Program to remove duplicate entries in a linked list in Python

Arnab Chakraborty
Updated on 20-Oct-2020 10:34:42

166 Views

Suppose we have a linked list of numbers, we have to remove those numbers which appear multiple times in the linked list (hold only one occurrence in the output), we also have to maintain the order of the appearance in the original linked list.So, if the input is like [2 -> 4 -> 6 -> 1 -> 4 -> 6 -> 9], then the output will be [2 -> 4 -> 6 -> 1 -> 9].To solve this, we will follow these steps −if node is not null, thenl := a new settemp := nodeinsert value of temp into lwhile ... Read More

Program to update elements in a given range in Python

Arnab Chakraborty
Updated on 20-Oct-2020 10:31:20

246 Views

Suppose we have a list of numbers called nums and a list of operations. Here each operation has three fields [L, R, X], this indicated that we should increment by X all the elements from indices L to R (inclusive). We have to apply all operations and return the final list.So, if the input is like nums = [8, 4, 2, -9, 4] operations = [ [0, 0, 3], [1, 3, 2], [2, 3, 5] ], then the output will be [11, 6, 9, -2, 4], as the initial list was [8, 4, 2, -9, 4].Performing first operation [0, 0, ... Read More

Program to find how many total amount of rain we can catch in Python

Arnab Chakraborty
Updated on 20-Oct-2020 07:45:17

94 Views

Suppose we have an array of n non-negative integers. These are representing a height where the width of each bar is 1, we have to compute how much water it is able to catch after raining. So the map will be like −Here we can see there are 8 blue boxes, so the output will be 8.To solve this, we will follow these steps −Define a stack st, water := 0 and i := 0while i < size of heightif is stack is empty or height[stack top] >= height[i], then push i into stack, increase i by 1otherwisex := stack ... Read More

Program to find maximum product of contiguous subarray in Python

Arnab Chakraborty
Updated on 20-Oct-2020 07:43:33

1K+ Views

Suppose we have an array called nums, we have to find the product of elements of a contiguous subarray within an array (containing at least one number) which has the largest product. So if the array is [1, 9, 2, 0, 2, 5], the output will be 18, as contiguous subarray [1, 9, 2] has max product.To solve this, we will follow these steps −max_list := list of size nums, and fill with 0min_list := list of size nums, and fill with 0min_list := list of size nums, and fill with 0for i in range 1 to length of numsmax_list[i] ... Read More

Advertisements