Found 10784 Articles for Python

Program to find how many ways we can climb stairs in Python

Arnab Chakraborty
Updated on 05-Oct-2020 12:25:32

279 Views

Suppose we have a staircase with n steps, and we can climb up either 1 or 2 steps at a time. We have to define a function that returns the number of unique ways we can climb the staircase.The order of the steps should not be changed, so each different order of steps counts as a way. If the answer is very large then mod the result by 10^9 + 7So, if the input is like n = 5, then the output will be 8, as there are 8 unique ways −1, 1, 1, 1, 12, 1, 1, 11, 2, ... Read More

Program to find minimum number of busses are required to pass through all stops in Python

Arnab Chakraborty
Updated on 05-Oct-2020 12:35:51

1K+ Views

Suppose we have a list of numbers called nums and that is showing the bus stops on a line where nums[i] shows the time a bus must arrive at station i. Now that buses can only move forward, we have to find the minimum number of buses that are needed to pass through all the stops.So, if the input is like nums = [1, 2, 7, 9, 3, 4], then the output will be 2, as one bus can take stops [1, 2, 3, 4] and another can do [7, 9].To solve this, we will follow these steps−ans := 0seen ... Read More

Program to add two binary strings, and return also as binary string in C++

Arnab Chakraborty
Updated on 05-Oct-2020 12:19:16

855 Views

Suppose we have two binary strings a and b, we have to add these binary numbers and find their sum, also as a string.So, if the input is like a = "10110", b = "10010", then the output will be "101000".To solve this, we will follow these steps −ret := empty stringna := size of a, nb := size of bi := na - 1, j := nb - 1carry := 0while (i >= 0 or j >= 0), do:addA := (if i >= 0, then a[i] - ASCII of '0', otherwise 0)addB := (if j >= 0, then b[j] ... Read More

Program to find maximum amount we can get by taking different items within the capacity in Python

Arnab Chakraborty
Updated on 05-Oct-2020 12:15:03

480 Views

Suppose we have two lists called weights and values which are of same length and another number called capacity k. Here weights[i] and values[i] shows the weight and value of the ith item. Now, we can take at most k capacity weights, and that we can only take at most one copy of each item, we have to find the maximum amount of value we can get.So, if the input is like weights = [2, 3, 4], values = [2, 6, 4], capacity = 6, then the output will be 8To solve this, we will follow these steps −n:= size ... Read More

Program to find the sum of elements that forms a Z shape on matrix in Python

Arnab Chakraborty
Updated on 05-Oct-2020 12:12:57

327 Views

Suppose we have one n x n matrix M, we have to find the sum of all elements that form a Z shape in the matrix.So, if the input is like432918256then the output will be 23, as elements are [4+3+2+1+2+5+6] = 23.To solve this, we will follow these steps −n := row count of matrixif n

Program to check we can spell out the target by a list of words or not in Python

Arnab Chakraborty
Updated on 05-Oct-2020 12:23:00

136 Views

Suppose we have a list of numbers called nums and another number k. If we start at index k and at any index i, we can go either left or right by exactly nums[i] number of steps. We have to check whether we can reach the end of the list or not.So, if the input is like nums = [0, 0, 2, 1, 3, 3, 1, 1] k = 2, then the output will be True, as if we start at index 2, then jump to index 4 and then jump to the last index 7.To solve this, we will ... Read More

Program to find the maximum profit we can get by buying on stock market multiple times in Python

Arnab Chakraborty
Updated on 05-Oct-2020 12:08:38

219 Views

Suppose we have a list of prices representing the stock prices of a company in chronological sequence, we have to find the maximum profit we could have made from buying and selling that stock any number of times. We have to keep in mind that we must buy before we can sell it.So, if the input is like prices = [10, 50, 30, 40, 60], then the output will be 70, as We can buy at 10, sell at 50, buy at 30, and sell at 60.To solve this, we will follow these steps −prev_price := infinityprofit := 0for each ... Read More

Program to check whether given graph is bipartite or not in Python

Arnab Chakraborty
Updated on 05-Oct-2020 12:05:32

633 Views

Suppose we have one undirected graph, we have to check whether the graph is bipartite or not. As we know a graph is bipartite when we can split the nodes of the graph into two sets A and B such that every edge {u, v} in the graph has one node u in A and another node v in B.So, if the input is likeThen the output will be True, [0, 4] are in set A and [1, 2, 3] are in set B, and all edges are from A to B or B to A, not A to A ... Read More

Program to find the maximum profit we can get by buying on stock market once in Python

Arnab Chakraborty
Updated on 05-Oct-2020 12:07:00

1K+ Views

Suppose we have a list of prices representing the stock prices of a company in chronological sequence, we have to find the maximum profit we could have made from buying and selling that stock only once. We have to keep in mind that we must buy before we can sell it.So, if the input is like prices = [10, 12, 9, 6, 8, 12], then the output will be 6, as we can buy at 6 and sell at 12.To solve this, we will follow these steps −max_profit := 0min_stock := infinityfor each price in prices, domax_profit := maximum of ... Read More

Program to find the number of possible position in a line in Python

Arnab Chakraborty
Updated on 05-Oct-2020 08:03:13

200 Views

Suppose we have a number n, and p and q. Now suppose we are standing in a line of n people. We do not know which position we are in, but we know there are at least p people in front of us and at most q people behind us. We have to find the number of possible positions we could be in.So, if the input is like n = 10, p = 3, q = 4, then the output will be 5, as there are 10 people and at least 3 are in front and at most 4 are ... Read More

Advertisements