Found 10784 Articles for Python

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

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

153 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

143 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

125 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

560 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

How to find the statistical summary of an R data frame with all the descriptive statistics?

Nizamuddin Siddiqui
Updated on 08-Oct-2020 14:36:44

321 Views

When we find statistical summary of an R data frame, we only get the minimum value, first quartile, median, mean, third quartile, and maximum value but in descriptive there are many other useful measures such as variance, standard deviation, skewness, kurtosis, etc. Therefore, we can use basicStats function of fBasics package for this purpose.Loading fBasics package −library(fBasics)Consider mtcars data in base R −Example Live Demodata(mtcars) head(mtcars, 20)Output          mpg    cyl     disp    hp    drat    wt qsec vs am gear carb Mazda RX4         21.0    6 160.0 110    3.90 ... Read More

Program to count number of strings we can make using grammar rules in Python

Arnab Chakraborty
Updated on 08-Oct-2020 14:22:32

177 Views

Suppose we have a number n, we have to find the number of strings of length n can be generated using the following rules −Each character is a lower case vowel [a, e, i, o, u]"a" may only be followed by one "e""e" may only be followed by any of "a" and "i""i" may not be followed by another "i""o" may only be followed by any of "i" and "u""u" may only be followed by one "a"If the result is very large, mod the result by 10^9 + 7.So, if the input is like n = 2, then the output ... Read More

Program to implement the fractional knapsack problem in Python

Arnab Chakraborty
Updated on 08-Oct-2020 14:10:45

2K+ Views

Suppose we have two lists, weights and values of same length and another value capacity. The weights[i] and values[i] represent the weight and value of ith element. So if we can take at most capacity weights, and that we can take a fraction of an item's weight with proportionate value, we have to find the maximum amount of value we can get (rounded down to the nearest integer)So, if the input is like weights = [6, 7, 3] values = [110, 120, 2] capacity = 10, then the output will be 178.To solve this, we will follow these steps −res ... Read More

Program to check given graph is a set of trees or not in Python

Arnab Chakraborty
Updated on 08-Oct-2020 10:42:45

199 Views

Suppose we have a graph, represented as a list of edges. We have to check whether the graph is a collection of trees (forest) or not.So, if the input is likethen the output will be TrueTo solve this, we will follow these steps −Define a function dfs() . This will take node, previf node in seen, thenreturn Falseinsert node into seenfor each adjacent node n in e[node], doif n is not same as prev, thenif dfs(n, node) is false, thenreturn Falsereturn TrueFrom the main method, do the following −e := an empty mapfor each start node u and end node ... Read More

Program to find correct order of visited cities in C++

Arnab Chakraborty
Updated on 08-Oct-2020 10:32:22

151 Views

Suppose we have a list of airline tickets represented by pairs of departure and arrival airports like [from, to], we have to reconstruct the itinerary in correct order. All of the tickets belong to a man who departs from KLK. So, the itinerary must begin with JFK.So if the input is like [["MUC", "LHR"], ["KLK ", "MUC"], ["SFO", "SJC"], ["LHR", "SFO"]], then the output will be ["KLK ", "MUC", "LHR", "SFO", "SJC"].To solve this, we will follow these steps −Define array ret and a map called graph.Define a method called visit. This will take airport name as inputwhile size of ... Read More

Advertisements