Found 10476 Articles for Python

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

428 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

250 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

275 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

213 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

Program to find first positive missing integer in range in Python

Arnab Chakraborty
Updated on 08-Oct-2020 10:28:27

344 Views

Suppose we have a list of sorted list of distinct integers of size n, we have to find the first positive number in range [1 to n+1] that is not present in the array.So, if the input is like nums = [0, 5, 1], then the output will be 2, as 2 is the first missing number in range 1 to 5.To solve this, we will follow these steps −target := 1for each i in arr, doif i is same as target, thentarget := target + 1return targetLet us see the following implementation to get better understanding −Example Live Democlass Solution: ... Read More

Program to find lowest possible integer that is missing in the array in Python

Arnab Chakraborty
Updated on 08-Oct-2020 10:25:57

187 Views

Suppose we have a list of numbers called nums, we have to find the first missing positive number. In other words, the lowest positive number that does not present in the array. The array can contain duplicates and negative numbers as well.So, if the input is like nums = [0, 3, 1], then the output will be 2To solve this, we will follow these steps −nums := a set with all positive numbers present in numsif nums is null, thenreturn 1for i in range 1 to size of nums + 2, doif i is not present in nums, thenreturn iLet ... Read More

Program to find minimum amount needed to be paid all good performers in Python

Arnab Chakraborty
Updated on 08-Oct-2020 10:20:11

372 Views

Suppose we have given a list of numbers called ratings, and this is showing the performance scores of coders. Now the manager wants to give Rs 1000 to every coder except if two coders are adjacent, they would like to pay the better performing coder at least Rs 1000 higher than the worse performing one. We have to find the minimum amount the manager can pay following above constraints.So, if the input is like ratings = [1, 2, 5, 1], then the output will be 7000, as the minimum we can pay for each respective coder is [1000, 2000, 3000, ... Read More

Program to check a number can be written as a sum of distinct factorial numbers or not in Python

Arnab Chakraborty
Updated on 08-Oct-2020 10:17:12

235 Views

Suppose we have a positive number n, we have to check whether n can be written as the sum of unique positive factorial numbers or not.So, if the input is like n = 144, then the output will be True, as 4! + 5! = 24 + 120 = 144To solve this, we will follow these steps −fact := 1res := a new listx := 2while fact = res[i], thenn := n - res[i]return true when n is same as 0Let us see the following implementation to get better understanding −Example Live Democlass Solution: def solve(self, n):    fact = 1 ... Read More

Program to find the sum of the absolute differences of every pair in a sorted list in Python

Arnab Chakraborty
Updated on 07-Oct-2020 14:31:59

231 Views

Suppose we have a list of sorted numbers called nums, we have to find the sum of the absolute differences between every pair of numbers in the given list. Here we will consider (i, j) and (j, i) are different pairs. If the answer is very large, mod the result by 10^9+7.So, if the input is like nums = [2, 4, 8], then the output will be 24, as |2 - 4| + |2 - 8| + |4 - 2| + |4 - 8| + |8 - 2| + |8 - 4|.To solve this, we will follow these steps −m ... Read More

Advertisements