Found 10476 Articles for Python

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

188 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

298 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

812 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

268 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

Program to sort all vowels at beginning then the consonants, are in sorted order in Python

Arnab Chakraborty
Updated on 05-Oct-2020 08:00:46

2K+ Views

Suppose we have a lowercase alphabet string s, we have to find a string with all the vowels of s in sorted sequence followed by all consonants of s in sorted sequence.So, if the input is like "helloworld", then the output will be "eoodhlllrw", as vowels are "eo" And consonants are in sorted order "dhlllrw"To solve this, we will follow these steps −k := blank string, t := blank stringfor each character c in s, doif c is a vowel, thenk := k concatenate cotherwise, t := t concatenate creturn (k after sort and concatenate t after sorting)Let us see ... Read More

Program to encrypt a string using Vigenere cipher in Python

Arnab Chakraborty
Updated on 05-Oct-2020 07:59:32

4K+ Views

Suppose we have a lowercase alphabet string text, and have another string called key. We have to find a new string where every letter in text[i] is moved to the right side with offset key[i]. Here the offset represented by key[i]'s position in the alphabet (A=0, B=1 etc.) If the letter overflows, it gets wrapped around the other side.So, if the input is like text = "code", key = "team", then the output will be "vsdq"To solve this, we will follow these steps −cip := a new liststart := ASCII of 'a'for each l from text and k from key, ... Read More

Program to encrypt a string using Vertical Cipher in Python

Arnab Chakraborty
Updated on 05-Oct-2020 07:57:34

300 Views

Suppose we have a string s and a number n, we have to rearrange s into n rows so that s can be selected vertically (top to down, left to right).So, if the input is like s = "ilovepythonprogramming" n = 5, then the output will be ['ipnrn', 'lypag', 'otrm', 'vhom', 'eogi']To solve this, we will follow these steps −L := empty listfor i in range 0 to n - 1:insert a string by taking each nth character starting from i, and insert into Lreturn LLet us see the following implementation to get better understanding −Example Live Democlass Solution:    def ... Read More

Program to find the resolved Unix style path in Python

Arnab Chakraborty
Updated on 05-Oct-2020 07:55:08

184 Views

Suppose we have a Unix path, in a list of strings, we have to find its resolved version. As we know in Unix, ".." denotes the previous directory and "." denotes stay on the current directory. Here resolving indicates evaluation of the two symbols so that we get the final directory we're currently in.So, if the input is like path = ["usr", "..", "usr", ".", "local", "etc", "foo"], then the output will be ['usr', 'local', 'etc', 'foo'], as the part represents "/usr/../usr/./local/etc" which resolves to "/usr/local/etc/foo"To solve this, we will follow these steps −s := a new listfor each element ... Read More

Program to find the number of unique integers in a sorted list in Python

Arnab Chakraborty
Updated on 05-Oct-2020 07:52:19

272 Views

Suppose we have a list of sorted numbers called nums we have to find the number of unique elements in the list.So, if the input is like nums = [3, 3, 3, 4, 5, 7, 7], then the output will be 4, as The unique numbers are [3, 4, 5, 7]To solve this, we will follow these steps −s:= a new setcnt:= 0for each i in nums, doif i is not in s, theninsert i into scnt := cnt + 1return cntLet us see the following implementation to get better understanding −Example Live Democlass Solution:    def solve(self, nums):     ... Read More

Advertisements