language-agnostic

Best dynamic data structure for 2d circle nearest neighbor

喜你入骨 提交于 2019-12-21 04:12:09
问题 The title is most of the problem. I have a set of circles, each given by a center C and radius r. The distance between two circles is the Euclidean distance between their centers minus both their radii. For circles a and b, d_ab = |C_a - C_b| - r_a - r_b. Note this can be negative if the circles overlap. Then what is the quickest data structure for finding the nearest (minimum distance) neighbor of a given circle in the set? Adding and deleting circles with "find nearest" queries interleaved

Testing with random inputs best practices

只谈情不闲聊 提交于 2019-12-21 04:08:04
问题 NOTE : I mention the next couple of paragraphs as background. If you just want a TL;DR, feel free to skip down to the numbered questions as they are only indirectly related to this info. I'm currently writing a python script that does some stuff with POSIX dates (among other things). Unit testing these seems a little bit difficult though, since there's such a wide range of dates and times that can be encountered. Of course, it's impractical for me to try to test every single date/time

How does the verbosity of identifiers affect the performance of a programmer?

廉价感情. 提交于 2019-12-21 04:01:15
问题 I always wondered: Are there any hard facts which would indicate that either shorter or longer identifiers are better? Example: clrscr() opposed to ClearScreen() Short identifiers should be faster to read because there are fewer characters but longer identifiers often better resemble natural language and therefore also should be faster to read. Are there other aspects which suggest either a short or a verbose style? EDIT: Just to clarify: I didn't ask: "What would you do in this case?". I

How do I use a Trie for spell checking

五迷三道 提交于 2019-12-21 03:57:29
问题 I have a trie that I've built from a dictionary of words. I want to use this for spell checking( and suggest closest matches in the dictionary , maybe for a given number of edits x). I'm thinking I'd use levenshtein distance between the target word and words in my dictionary, but is there a smart way to traverse the trie without actually running the edit distance logic over each word separately? How should I do the traversal and the edit distance matching? For e.g, if I have words MAN, MANE,

Check if two arrays are cyclic permutations

假装没事ソ 提交于 2019-12-21 03:57:23
问题 Given two arrays, how do you check if one is a cyclic permutation of the other? For example, given a = [1, 2, 3, 1, 5] , b = [3, 1, 5, 1, 2] , and c = [2, 1, 3, 1, 5] we have that a and b are cyclic permutations but c is not a cyclic permutation of either. Note: the arrays may have duplicate elements. 回答1: The standard trick here is to concatenate one of the arrays with itself, and then try to find the 2nd array in the concatenated array. For example, 'a' concatenated with itself is: [1, 2, 3

Are exception hierarchies really useful?

感情迁移 提交于 2019-12-21 03:55:16
问题 It dawned on me that I've never seen a single exception hierarchy for which creating subclasses but catching the parent class was actually useful (except, of course, for the base Exception class, that has to be derived). Are exception hierarchies really useful, xor should all exceptions be derived from language's base exception class? 回答1: Exception hierarchies are useful for grouping related exceptions together, when you need different granularity of catching in different places. Putting all

testing command line utilities

匆匆过客 提交于 2019-12-21 03:53:33
问题 I'm looking for a way to run tests on command-line utilities written in bash, or any other language. I'd like to find a testing framework that would have statements like setup: command = 'do_awesome_thing' filename = 'testfile' args = ['--with', 'extra_win', '--file', filename] run_command command args test_output_was_correct assert_output_was 'Creating awesome file "' + filename + '" with extra win.' test_file_contains_extra_win assert_file_contains filename 'extra win' Presumably the base

Should I ever use continue inside a switch statement?

不想你离开。 提交于 2019-12-21 03:09:11
问题 I noticed you can indeed use the continue keyword in a switch statement, but on PHP it doesn't do what I expected. If it fails with PHP, who knows how many other languages it fails too? If I switch between languages a lot, this can be a problem if the code doesn't behave like I expect it to behave. Should I just avoid using continue in a switch statement then? PHP (5.2.17) fails: for($p = 0; $p < 8; $p++){ switch($p){ case 5: print"($p)"; continue; print"*"; // just for testing... break; case

Lookup structure for handling future events (time based)

我们两清 提交于 2019-12-21 02:40:59
问题 I am looking for an efficient data structure, that'll allow me to cue events ... that is, i will be having an app, where at any time in execution, it is possible, that an event will be raised for a future point in execution ... something like: t=20: in 420 seconds, A occurs t=25: in 13 seconds, B occurs t=27: in 735 seconds, C occurs ... so i'd like to have a data structure, where i can put in any event in any time in the future and where i can get and (by doing so) remove all due events ...

Algorithm to generate all multiset size-n partitions

依然范特西╮ 提交于 2019-12-21 02:21:28
问题 I've been trying to figure out a way to generate all distinct size-n partitions of a multiset, but so far have come up empty handed. First let me show what I'm trying to archieve. Let's say we have an input vector of uint32_t : std::vector<uint32_t> input = {1, 1, 2, 2} An let's say we want to create all distinct 2-size partitions. There's only two of these, namely: [[1, 1], [2, 2]], [[1, 2], [1, 2]] Note that order does not matter, i.e. all of the following are duplicate, incorrect solutions