algorithm

How to check if a List Argument is None

百般思念 提交于 2021-01-29 09:11:56
问题 I am doing this problem set, and I cannot get passed the Test Case for when the List is None. I have tested for almost all the cases, and I have noticed that other users got it right with a code that is very similar to mine, I need help debugging and checking if I am missing a test case in my Conditionals. The test cases that come back negative are testing to see if the List is None, which I found out by testing in the Python interpreter that it does not necessarily imply empty. Here is the

How to remove not equals Objects using ArrayList in Java

做~自己de王妃 提交于 2021-01-29 08:22:06
问题 If I have an ArrayList of books and I want to delete a certain number of books only if they are different (using equals). How can I approach the problem? For example, if I have 3 different books and my quantitytoremove is 3.. then those 3 books have to be deleted. Assuming quantitytoremove can't be higher than the number of DIFFERENT books inside the array. I tried something like this but I know there are issues with what I wrote. Is there a function available with streams or else that can

How to extract specific numbers based on the conditions in python

若如初见. 提交于 2021-01-29 08:06:59
问题 I have a bunch of lines created by connecting some points. I have the number number of lines as a numpy array: line_no_A= np.arange (17, 31) sep_A=23 These lines are arranged in two perpendicular direction, shown as blue and red lines. From the sep , direction of lines changes. I have also the number of points createing these lines: point_rep_A=np.array([4, 3, 3, 1]) Now, I want to extract some specific lines. I uploaded a fig and shown the specifics line with blue circles. To find the red

Efficient Algorithm for comparing and iterate through two large arrays

让人想犯罪 __ 提交于 2021-01-29 07:54:35
问题 I have this problem: I want to iterate and compare each item of array1 against each item of array2, both arrays have the same lenght. When the value of both items coincide I store the value j in my indexOfInterest[] array for later use. This is the code that I use, works well but its extremely slow comparing large arrays (thousands of items), can anyone help me in implement an efficient algorithm for this task. int i,j; int counter = [array1 count]; int indexOfInterest[counter]; for (i = 0; i

L1-Norm minimization

匆匆过客 提交于 2021-01-29 07:46:16
问题 I am trying to minimize the following function using Linear programming. I am unable to include the image of my objective function. Click this Objective Function to view what I am trying to optimize. My question is there any library or function in python which can do this optimization for me or should I be writing the code? 回答1: import cvxpy as cp import numpy as np N=10 M=100 U = np.random.random((M,N)) m = np.random.random(M) t = cp.Variable(M) x = cp.Variable(N) prob = cp.Problem(cp

Java program to create a Descending Order using bucket sort

我与影子孤独终老i 提交于 2021-01-29 07:11:55
问题 I was making a program to take 10 user input values(1-100) and use bucket sort to sort it according to the user preference whether by ascending or descending order. I was able to create the ascending order. This is my code for ascending, but how could I make it descending? public class BucketSort{ Scanner in = new Scanner(System.in); public void bucketSort(int array[], int numBuckets){ System.out.println("Choose Sorting Order:"); System.out.println("[A] Ascending \n[D] Descending \n Enter

search a list in another list using Python

时光怂恿深爱的人放手 提交于 2021-01-29 06:55:43
问题 I am trying to write the sublist search algorithm using Python. For reference : https://www.geeksforgeeks.org/sublist-search-search-a-linked-list-in-another-list/ Here is my code: def sublist(arr1,arr2): i = 0 j = 0 k = 0 if len(arr1) == 0: print('List1 Empty') if len(arr2) == 0: print('List 2 Empty') for j in range(0,len(arr2)): for i in range(0,len(arr1)): if arr1[i] != arr2[j]: break while arr1[i] == arr2 [j]: if i == len(arr1) - 1: return True i = i + 1 j = j + 1 if i == len(arr1): return

how to build heap tree?

▼魔方 西西 提交于 2021-01-29 06:20:53
问题 I understand the algorithm of building heap tree (max or min) but i don't understand the code of it: First: How does this loop build a max heap?, why we started the i with n/2-1 ? // Build heap (rearrange array) for (int i = n / 2 - 1; i >= 0; i--) heapify(arr, n, i); and this is the Heapify function: Secondly: how did we assume that largest is "i" ? Third: why we heapify again in the last line? // To heapify a subtree rooted with node i which is // an index in arr[]. n is size of heap void

All possible paths from top left to bottom right of a mXn matrix

久未见 提交于 2021-01-29 05:43:33
问题 I was going through this leetcode problem for going from top left to bottom right. How many possible unique paths are there? I was able to understand this method of dynamic programming by storing the result for every index. public int uniquePaths(int m, int n) { int count[][] = new int[m][n]; for (int i = 0; i < m; i++) count[i][0] = 1; for (int j = 0; j < n; j++) count[0][j] = 1; for (int i = 1; i < m; i++) { for (int j = 1; j < n; j++) { count[i][j] = count[i-1][j] + count[i][j-1]; //+

Graphic: Rerouting problem test in python language

邮差的信 提交于 2021-01-29 05:37:07
问题 Your company has N servers. The information flows from one server to another through a connection. If the information flows from server i to server j , then connection (i) = j. It's possible for some server connection(i) = i, meaning information doesn't flow further. You are given an array connection consisting of N integers. You are tasked with making minimum number of changes to connection array values so that the information from all server can reach at exactly one server in the whole