Found 33676 Articles for Programming

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

234 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

How to find the groupwise mean and save it in a data frame object in R?

Nizamuddin Siddiqui
Updated on 07-Oct-2020 16:16:25

437 Views

We often need groupwise mean in data analysis, especially in situations where analysis of variance techniques is used because these techniques helps us to compare different groups based on their measures of central tendencies and measures of variations. It can be done by using aggregate function so that the output can be saved in a data frame object. In the below examples, we can see how it can be done and also check the final object type.ExampleConsider the below data frame − Live Demoset.seed(109) Salary

How to concatenate vector with defined name to a list in R?

Nizamuddin Siddiqui
Updated on 07-Oct-2020 16:54:09

139 Views

The concatenation of vector to a list simply means adding an external vector to the list we already have. For this purpose, we need to defined the vector with list function so that the R program understands that we are adding a list object to another list, otherwise the vector entries will be considered as separate vectors to be added in the list. To understand this in a better way, check out the below examples.Example Live DemoConsider the below list −List1

Program to check robot can reach target position or not in Python

Arnab Chakraborty
Updated on 07-Oct-2020 13:47:26

442 Views

Suppose we have a robot, that is currently sitting in at position (0, 0) (Cartesian plane). If we have list of its moves that it can make, containing N(North), S(South), W(West), and E(East). We have to check whether it can reach at destination coordinate (x, y).So, if the input is like moves = ['N', 'N', 'E', 'E', 'S'], (x, y) = (2, 1), then the output will be True, To solve this, we will follow these steps −temp_coord := [0, 0]for each move in moves, doif move is same as "N", thentemp_coord[1] := temp_coord[1] + 1otherwise when move is same ... Read More

Program to check we can reach leftmost or rightmost position or not in Python

Arnab Chakraborty
Updated on 07-Oct-2020 13:44:57

371 Views

Suppose we have a string containing letters of three types, R, B, and dot(.). Here R stands for our current position, B stands for a blocked position, and dot(.) stands for an empty position. Now, in one step, we can move to any adjacent position to our current position, as long as it is valid (empty). We have to check whether we can reach either the leftmost position or the rightmost position.So, if the input is like s = "...........R.....BBBB.....", then the output will be True, as R can reach left most position, because there is no block.To solve this, ... Read More

Program to find how many years it will take to reach t amount in Python

Arnab Chakraborty
Updated on 07-Oct-2020 13:43:46

227 Views

Suppose we have some parameters P, O, E, T. If we have P dollars in principal that we want to invest the stock market. The stock market alternates between first returning E and then O percent interest per year, we have to check how many years it would take to reach at least T dollars.So, if the input is like P = 200, O = 10, E = 25, T = 300, then the output will be 3 as in the first year we will get interest 25%, so end up with 200+50 = 250, then next year we will ... Read More

Program to reverse the position of each word of a given string in Python

Arnab Chakraborty
Updated on 07-Oct-2020 13:42:28

653 Views

Suppose we have a string of words delimited by spaces; we have to reverse the order of words.So, if the input is like "Hello world, I love python programming", then the output will be "programming python love I world, Hello"To solve this, we will follow these steps −temp := make a list of words by splitting s using blank spacetemp := reverse the list tempreturn a string by joining the elements from temp using space delimiter.Let us see the following implementation to get better understanding −Example Live Democlass Solution:    def solve(self, s):       temp = s.split(' ')   ... Read More

Program to check if we reverse sublist of one list to form second list or not in Python

Arnab Chakraborty
Updated on 07-Oct-2020 13:41:16

224 Views

Suppose we have two lists of numbers called A, and B. We have to take some sublist in A and reverse it. Then check whether it is possible to turn A into B or not. We can take sublist and reverse it any number of times.So, if the input is like A = [2, 3, 4, 9, 10], B = [4, 3, 2, 10, 9], then the output will be True as we can reverse [2, 3, 4] and [9, 10].To solve this, we will follow these steps −res := a map, initially emptyfor each n in nums, dores[n] := ... Read More

Program to check the string is repeating string or not in Python

Arnab Chakraborty
Updated on 07-Oct-2020 13:39:32

162 Views

Suppose we have a string, we have to check whether it's a repeating string or not.So, if the input is like string = "helloworldhelloworld", then the output will be TrueTo solve this, we will follow these steps −n := size of sDefine a function findFactors() . This will take nf := a new seti := 1while i * i

Program to count k length substring that occurs more than once in the given string in Python

Arnab Chakraborty
Updated on 07-Oct-2020 13:36:43

217 Views

Suppose we have a string s and a number k, we have to find the number of k-length substrings of s, that occur more than once in s.So, if the input is like s = "xxxyyy", k = 2, then the output will be 2To solve this, we will follow these steps −seen := a new listfor i in range 0 to size of s - k, dot := substring of s [from index i to i + k - 1]insert t at the end of seenmp := a map for all distinct element in seen and their occurrencesreturn sum ... Read More

Advertisements