language-agnostic

Should we compare floating point numbers for equality against a *relative* error?

北慕城南 提交于 2020-01-09 02:21:13
问题 So far I've seen many posts dealing with equality of floating point numbers. The standard answer to a question like "how should we decide if x and y are equal?" is abs(x - y) < epsilon where epsilon is a fixed , small constant. This is because the "operands" x and y are often the results of some computation where a rounding error is involved, hence the standard equality operator == is not what we mean, and what we should really ask is whether x and y are close , not equal. Now, I feel that if

Sum array values with sum equals X

﹥>﹥吖頭↗ 提交于 2020-01-07 08:46:35
问题 I have an integer collection. I need to get all possibilites that sum of values are equal to X. I need something like this. It can be written in: delphi, c#, php, RoR, python, cobol, vb, vb.net 回答1: That's a subset sum problem. And it is NP-Complete. The only way to implement this would be generate all possible combinations and compare the sum values. Optimization techniques exists though. Here's one in C#: static class Program { static int TargetSum = 10; static int[] InputData = new[] { 1,

Find all duplicates and missing values in a sorted array

一个人想着一个人 提交于 2020-01-06 22:46:10
问题 Suppose you have a sorted range (x to y) of values in an array. x = 3; y = 11; array == 3, 4, 5, 6, 7, 8, 9, 10, 11 But it is possible that some values are duplicated and some are missing, so you might have: array == 4, 5, 5, 5, 7, 8, 9, 10, 10 What's the best way in your language to find all duplicates and missing values so you get: resultMissingValuesArray == 3, 6, 11 resultDuplicatesArray == 5, 5, 10 Here's some C++ code to get you started: #include <vector> #include <iostream> #include

Clean algorithm to generate all sets of the kind (0) to (0,1,2,3,4,5,6,7,8,9)

心不动则不痛 提交于 2020-01-06 21:51:11
问题 Basically, I'd like a set of sets that contains from (0..9), then (0, 1..9), (1, 2..9)..(8,9), and so on and so forth up until (0,1,2,3,4,5,6,7,8,9). I know this can be accomplished by nesting for loops in the manner below, but I'm wondering if there's a neater way of doing it? Preferably something that could be accomplished within C#, but I'm interested in any algorithm. for (int i = 0; i < max; i++) { yield {i}; for (int j = i + 1; j < max; j++) { yield {i, j}; for (int k = j + 1; k < max;

How to implement data execution (as if it were script source code) [closed]

[亡魂溺海] 提交于 2020-01-06 14:10:46
问题 Closed . This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post. Closed 5 years ago . Suppose there is a script language called ScriptCode with the capabilities of execute code in same language. //This ficticius program executes a simple constant code. main() { ScriptCode sc=new ScriptCode ( "print \"Hello\""); execute(sc); print (" world"); } //This ficticius program would read 10

Generating Uniform Random Deviates within a given range

一个人想着一个人 提交于 2020-01-06 04:53:20
问题 I'd like to generate uniformly distributed random integers over a given range. The interpreted language I'm using has a builtin fast random number generator that returns a floating point number in the range 0 (inclusive) to 1 (inclusive). Unfortunately this means that I can't use the standard solution seen in another SO question (when the RNG returns numbers between 0 (inclusive) to 1 (exclusive) ) for generating uniformly distributed random integers in a given range: result=Int((highest -

How to know if your Unit Test is “right-sized”?

和自甴很熟 提交于 2020-01-06 04:20:25
问题 One thing that I've always noticed with my unit tests is that they get to be kind of verbose; seeing as they could also be not verbose enough, how do you get a sense of when your unit tests are the right size? I know of a good quote for this and it's: "Perfection is achieved, not when there is nothing left to add, but when there is nothing left to remove." - Antoine de Saint-Exupery. 回答1: One reason why them become verbose is that they're testing multiple things. I try to make each unit test

Consistency guarantee of file system regarding sequential write

会有一股神秘感。 提交于 2020-01-05 15:11:31
问题 My program (only 1 process and 1 thread) sequentially write n consecutive chunks of data to a file on a HDD (regular kind of HDD) using plain old write system call. It's like some kind of append-only log file. After a system crash (power failure, not HDD failure), I read back and verified that chunks[i] (0 < i < n) had been entirely written down to disk (by checking length). May be the content of the chunk is not checksum correct but still the whole chunks[i] stably sit on the surface of the

Calculation of all possible mutations of a nonogram [closed]

亡梦爱人 提交于 2020-01-04 15:25:13
问题 Closed . This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post. Closed 5 years ago . I need to build a nonogram solver according to a very specific recipe. For each row, I need to calculate all possible mutations, and then check if that row would still make the puzzle valid. For those unknown with nonograms, here's a link. A row is nothing more than a boolean array, where 'true'

Function/Class Comment Formatting Conventions

五迷三道 提交于 2020-01-04 07:00:32
问题 Who has the most readable and useful function/class commenting convention? I'm not looking for something that generates docs, but I'm considering adopting something like JavaDoc because all the info is there. /** * This function processes data * * @param p1 the first piece of data * @param p2 the second piece of data * @return true if the processing was successful, else false */ function ProcessData(p1, p2){ or some other hand crafted thing? ///////////////////////////////// // This function