pseudocode

Test if a range intersects another range of numbers

时光怂恿深爱的人放手 提交于 2019-12-19 10:29:39
问题 I have 2 range of numbers: $startTime to $endTime $offerStartTime to $offerEndTime Each of the above variables are integers. I want to see if the range offerStartTime to offerEndTime falls in the range startTime and endTime . For example, if the startTime and endTime range was: 10 to 20, then the following example ranges would return true : offerStartTime: 5, offerEndTime: 11 offerStartTime: 5, offerEndTime: 100 offerStartTime: 10, offerEndTime: 15 offerStartTime: 10, offerEndTime: 100

In a triangulated isometric grid, what triangle is a given point in?

痴心易碎 提交于 2019-12-19 07:51:03
问题 I have a triangulated isometric grid, like this: (source: mathforum.org) In my code, triangles are grouped by columns. As I hover the mouse, I want to calculate what triangle the mouse coordinates are in. Is there a simple algorithm to do that? 回答1: What you want to do is turn this into a grid as much as possible because grids are far easier to work with. The first thing you do is work out what column it's in. You say you store that so it should be easier by doing a simple integer division on

In a triangulated isometric grid, what triangle is a given point in?

a 夏天 提交于 2019-12-19 07:50:34
问题 I have a triangulated isometric grid, like this: (source: mathforum.org) In my code, triangles are grouped by columns. As I hover the mouse, I want to calculate what triangle the mouse coordinates are in. Is there a simple algorithm to do that? 回答1: What you want to do is turn this into a grid as much as possible because grids are far easier to work with. The first thing you do is work out what column it's in. You say you store that so it should be easier by doing a simple integer division on

Maximum Likelihood Estimate pseudocode

点点圈 提交于 2019-12-17 21:47:55
问题 I need to code a Maximum Likelihood Estimator to estimate the mean and variance of some toy data. I have a vector with 100 samples, created with numpy.random.randn(100) . The data should have zero mean and unit variance Gaussian distribution. I checked Wikipedia and some extra sources, but I am a little bit confused since I don't have a statistics background. Is there any pseudo code for a maximum likelihood estimator? I get the intuition of MLE but I cannot figure out where to start coding.

Find maximum value in an array by recursion

爱⌒轻易说出口 提交于 2019-12-17 20:18:16
问题 // Find a maximum element in the array. findMax(A) findMaxHelper(A, 0, A.length) findMaxHelper(A, left, right) if (left == right - 1) return A[left] else max1 = findMaxHelper(A, left, (right + left) / 2) max2 = findMaxHelper(A, (right + left) / 2, right) if (max1 > max2) return max1 else return max2 I am having a hard time understanding what is happening in this pseudo-code. Can someone help explain what is happening at each line. I need to understand this code before I can answer the

sorting int array with only 3 elements

北慕城南 提交于 2019-12-17 18:54:51
问题 I have this array: int [] myarray = {17, 6, 8}; What is the optimal way to sort this array, in pseudocode? Thanks! 回答1: I think this should be quite fast (ascending order): if (el1 > el2) Swap(el1,el2) if (el2 > el3) Swap(el2,el3) if (el1 > el2) Swap(el1,el2) 回答2: This code makes 2 or 3 comparisons and 4 memory records in the worst case, as opposed to another answer (always 3 comparisons and 9 memory records in the worst case). if a[0] < a[1]: if a[1] > a[2]: if a[0] < a[2]: temp = a[1] a[1]

Two Rectangles intersection

爱⌒轻易说出口 提交于 2019-12-17 04:59:15
问题 I have two rectangles characterized by 4 values each : Left position X , top position Y , width W and height H : X1, Y1, H1, W1 X2, Y2, H2, W2 Rectangles are not rotated, like so: +--------------------> X axis | | (X,Y) (X+W, Y) | +--------------+ | | | | | | | | | | +--------------+ v (X, Y+H) (X+W,Y+H) Y axis What is the best solution to determine whether the intersection of the two rectangles is empty or not? 回答1: if (X1+W1<X2 or X2+W2<X1 or Y1+H1<Y2 or Y2+H2<Y1): Intersection = Empty else

Asymptotic Complexity for an Algorithm

对着背影说爱祢 提交于 2019-12-14 03:29:42
问题 i <-- 0 while(i < n) someWork(...) i <-- i^2 done Can someone confirm that the worst case time complexity (Big-O) of this loop is O(log n) if: someWork(...) is an O(1) algorithm someWork(...) is an O(n) algorithm Also, what is the worst case time complexity (Big-O) if someWork(...) does exactly i operations? someWork(...) does more work as i increases. Your answer should be something like sigma(f(i)). Thank you very much for any help. 回答1: First: if (as mentioned) 0 <= i <= 1 holds, the

Variant of Knapsack

随声附和 提交于 2019-12-14 02:53:22
问题 I'm working on a program to solve a variant of the 0/1 Knapsack problem. The original problem is described here: https://en.wikipedia.org/wiki/Knapsack_problem. In case the link goes missing in the future, I will give you a summary of the 0/1 Knapsack problem (if you are familiar with it, jump this paragraph): Let's say we have n items, each with weight wi and value vi . We want to put items in a bag, that supports a maximum weight W , so that the total value inside the bag is the maximum

Move elements N steps to the left (challenge)

久未见 提交于 2019-12-14 02:17:41
问题 Lets say we have this array: {"a", "b", "c", "d", null, "f", "g", null, "i", "j", "k", null, "l"}; Then I already made a method to push elements to the left and leaving all the nulls at the right: void pushFromLeftToLeft() { int nullCount = 0; for (int i = 0; i < BUCKET_CAPACITY; i++) { if (data[i] == null) { nullCount++; } else if (nullCount != 0) { data[i-nullCount] = data[i]; } } // set the last elements to null for (int i = BUCKET_CAPACITY - nullCount; i < BUCKET_CAPACITY; i++) { data[i]