Find Length of Largest Subset Where One Element is Divisible by Another in Python

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

365 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

Find Number of Sublists with Exactly K Different Words in Python

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

127 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

Find How Long It Will Take to Reach Messages in a Network in Python

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

188 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

Find Steps to Change One Word to Another in Python

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

258 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

Check Odd Length Cycle in a Graph using Python

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

477 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

Get Indices of List After Deleting Elements in Ascending Order in Python

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

201 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

Show Decimal Number from Rational Number Representation in C++

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

428 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

Broadcasting in NumPy for Python

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

187 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

Add Specific Tint to Grayscale Images in Scikit-Learn Python

AmitDiwan
Updated on 11-Dec-2020 11:33:00

373 Views

The values of ‘R’, ‘G’, and ‘B’ are changed and applied to the original image to get the required tint.Below is a Python program that uses scikit-learn to implement the same. Scikit-learn, commonly known as sklearn is a library in Python that is used for the purpose of implementing machine learning algorithms −Exampleimport matplotlib.pyplot as plt from skimage import data from skimage import color path = "path to puppy_1.jpg" orig_img = io.imread(path) grayscale_img = rgb2gray(orig_img) image = color.gray2rgb(grayscale_img) red_multiplier = [0.7, 0, 0] yellow_multiplier = [1, 0.9, 0] fig, (ax1, ax2) = plt.subplots(ncols=2, figsize=(8, 4), sharex=True, sharey=True) ax1.imshow(red_multiplier * image) ... Read More

Hysteresis Thresholding in Python using Scikit-Learn

AmitDiwan
Updated on 11-Dec-2020 11:31:58

2K+ Views

Hysteresis refers to the lagging effect of a result. With respect to threshold, hysteresis refers to the areas that are above a specific low threshold value or above high threshold values. It refers to areas that are highly-confident in nature.With the help of hysteresis, the noise outside the edges of the object in the image can be ignored.Let us see how hysteresis threshold can be achieved using scikit-learn library:Exampleimport matplotlib.pyplot as plt from skimage import data, filters fig, ax = plt.subplots(nrows=2, ncols=2) orig_img = data.coins() edges = filters.sobel(orig_img) low = 0.1 high = 0.4 lowt = (edges > low).astype(int) hight ... Read More

Advertisements