Found 26504 Articles for Server Side Programming

Program to count number of ways we can fill 3 x n box with 2 x 1 dominos in Python

Arnab Chakraborty
Updated on 12-Dec-2020 09:18:55

255 Views

Suppose we have a number n, we have to find the number of ways we can fill a (3 x n) block with 1 x 2 dominos. We can rotate the dominos when required. If the answer is very large then return this mod 10^9 + 7.So, if the input is like n = 4, then the output will be 11.To solve this, we will follow these steps −m = 10^9 + 7if n is odd, thenreturn 0cs := 1, os := 0for i in range 2 to n, increase by 2, docs := 3 * cs + osos := ... Read More

Program to find nth term of a sequence which are divisible by a, b, c in Python

Arnab Chakraborty
Updated on 12-Dec-2020 09:17:05

380 Views

Suppose we have four numbers n, a, b, and c. We have to find the nth (0 indexed) term of the sorted sequence of numbers divisible by a, b or c.So, if the input is like n = 8 a = 3 b = 7 c = 9, then the output will be 18, as The first 9 terms of the sequence are [1, 3, 6, 7, 9, 12, 14, 15, 18].To solve this, we will follow these steps −if minimum of a, b, c is same as 1, thenreturn nab := lcm(a, b), bc := lcm(b, c), ca := ... Read More

Program to find length of the largest subset where one element in every pair is divisible by other in Python

Arnab Chakraborty
Updated on 12-Dec-2020 09:13:56

358 Views

Suppose we have a list of unique numbers called nums, so we have to find the largest subset such that every pair of elements in the subset like (i, j) satisfies either i % j = 0 or j % i = 0. So we have to find the size of this subset.So, if the input is like nums = [3, 6, 12, 24, 26, 39], then the output will be 4, as the largest valid subset is [3, 6, 12, 24].To solve this, we will follow these steps −dp := a list of size nums and fill with 1sort the list numsn := size of numsif n

Program to find number of sublists that contains exactly k different words in Python

Arnab Chakraborty
Updated on 12-Dec-2020 09:11:43

120 Views

Suppose we have a list of words and another value k. We have to find the number of sublists in the given words such that there are exactly k different words.So, if the input is like words = ["Kolkata", "Delhi", "Delhi", "Kolkata"] k = 2, then the output will be 5, as the following sublists have 2 unique words: ["Kolkata", "Delhi"], ["Delhi", "Kolkata"], ["Kolkata", "Delhi", "Delhi"], ["Delhi", "Delhi", "Kolkata"], ["Kolkata", "Delhi", "Delhi", "Kolkata"], but not ["Delhi", "Delhi"] as there is only one unique word.To solve this, we will follow these steps −Define a function work() . This will take words, ... Read More

Program to find how long it will take to reach messages in a network in Python

Arnab Chakraborty
Updated on 12-Dec-2020 09:09:08

182 Views

Suppose we have a number and a list of edges. These n different nodes labeled as 0 to N. These nodes are forming a network. Each edge is in the form (a, b, t) of an undirected graph, this is representing if we try to send message from a to b or b to a, it will take t time. When a node receives a message, it immediately floods the message on to a neighboring node. If all nodes are connected, we have to find how long it will take for every node to receive a message that starts at ... Read More

Program to find number of steps required to change one word to another in Python

Arnab Chakraborty
Updated on 12-Dec-2020 09:08:44

253 Views

Suppose we have a list of words called dictionary and we have another two strings start and end. We want to reach from start to end by changing one character at a time and each resulting word should also be in the dictionary. Words are case-sensitive. So we have to find the minimum number of steps it would take to reach at the end. If it is not possible then return -1.So, if the input is like dictionary = ["may", "ray", "rat"] start = "rat" end = "may", then the output will be 3, as we can select this path: ... Read More

Program to check whether odd length cycle is in a graph or not in Python

Arnab Chakraborty
Updated on 12-Dec-2020 09:02:11

470 Views

Suppose we have an undirected graph we have to check whether we can find an odd length cycle inside it or not.So, if the input is like adj_list = [[1, 2], [0, 3, 4], [0, 3, 4], [1, 2, 4], [1, 2, 3]]then the output will be True as there are odd length cycles like [0, 1, 3, 4, 2], [1, 3, 4], [2, 3, 4].To solve this, we will follow these steps −Define a function dfs() . This will take node, iif node is in path, thenreturn true when (i - path[node]) is oddif node is visited, thenreturn Falsemark ... Read More

Program to get indices of a list after deleting elements in ascending order in Python

Arnab Chakraborty
Updated on 12-Dec-2020 08:58:42

194 Views

Suppose we have a list of distinct values and we want to remove each number in non-decreasing order. We have to find the indices of numbers in order of their deletion.So, if the input is like nums = [4, 6, 2, 5, 3, 1], then the output will be [5, 2, 3, 0, 1, 0] as we delete 1, so array is [4, 6, 2, 5, 3], then remove 2, array is [4, 6, 5, 3], then remove 3 we get [4, 6, 5], then remove 4 we get [6, 5], remove 5, [6] and finally remove 6.To solve this, we will follow these steps −Define a function my_sort() . This will take indsif size of inds

Program to show decimal number from rational number representation in C++

Arnab Chakraborty
Updated on 12-Dec-2020 08:54:06

420 Views

Suppose we have two numbers called numerator and denominator representing a rational number in the form (numerator / denominator). We have to find its decimal representation as a string. If there are some recurring numbers, then surround them with brackets.So, if the input is like numerator = 164 denominator = 3, then the output will be "54.(6)".To solve this, we will follow these steps −if numerator is same as 0, then −return "0"Define an array ansif numerator < 0 and denominator > 0 or numerator > 0 and denominator < 0, then −insert '-' at the end of ansdivisor := ... Read More

Discuss broadcasting in Numpy in Python?

AmitDiwan
Updated on 11-Dec-2020 11:34:09

178 Views

NumPy refers to ‘Numerical’ ‘Python’. It is a library that contains multidimensional array objects and multiple methods that help in processing the arrays.NumPy can be used to perform a wide variety of operations on arrays. It is used in conjunction with packages like SciPy, Matplotlib and so on. NumPy+Matplotlib can be understood as an alternative to MatLab. It is an open-source package, which means it can be used by anyone. Standard Python distribution doesn’t include NumPy package by default. The package has to be separately installed using the installer ‘pip’.For Windows, it has been shown below −pip install numpyOnce this ... Read More

Advertisements