language-agnostic

How to handle multiple simultaneous elastic collisions?

只谈情不闲聊 提交于 2020-02-26 10:04:25
问题 I'm computing the result by colliding pairs of 2D convex objects (without rotation), using the basic equations on wikipedia. However, when there are dependencies, like two objects hitting another object at the same time: Such as here, with objects 1 and 2 hitting 3 at the exact same time, the pair-wise approach fails. Depending on the order I compute the collisions (1-3 first or 2-3 first), I will get different results. Repeated iteration through the collisions will still give order dependent

Code Golf: Quickly Build List of Keywords from Text, Including # of Instances

丶灬走出姿态 提交于 2020-02-12 08:36:25
问题 Locked . This question and its answers are locked because the question is off-topic but has historical significance. It is not currently accepting new answers or interactions. I've already worked out this solution for myself with PHP, but I'm curious how it could be done differently - better even. The two languages I'm primarily interested in are PHP and Javascript, but I'd be interested in seeing how quickly this could be done in any other major language today as well (mostly C#, Java, etc).

Is it possible to create an algorithm which generates an autogram?

牧云@^-^@ 提交于 2020-02-03 18:58:35
问题 An autogram is a sentence which describes the characters it contains, usually enumerating each letter of the alphabet, but possibly also the punctuation it contains. Here is the example given in the wiki page. This sentence employs two a’s, two c’s, two d’s, twenty-eight e’s, five f’s, three g’s, eight h’s, eleven i’s, three l’s, two m’s, thirteen n’s, nine o’s, two p’s, five r’s, twenty-five s’s, twenty-three t’s, six v’s, ten w’s, two x’s, five y’s, and one z. Coming up with one is hard,

Sorting k-sorted arrays with a min heap

霸气de小男生 提交于 2020-02-03 08:12:14
问题 A good solution to sorting k-sorted arrays(each element is at most k away from its target position) is, 1) Create a Min Heap of size k+1 with first k+1 elements. This will take O(k) time. 2) One by one remove min element from heap, put it in result array, and add a new element to heap from remaining elements. overall complexity will be O(k) + O((n-k)*logK) I can't understand the relevance of the array being k-sorted to using the heap technique. Won't this work even when the array is not k

Sorting k-sorted arrays with a min heap

情到浓时终转凉″ 提交于 2020-02-03 08:09:32
问题 A good solution to sorting k-sorted arrays(each element is at most k away from its target position) is, 1) Create a Min Heap of size k+1 with first k+1 elements. This will take O(k) time. 2) One by one remove min element from heap, put it in result array, and add a new element to heap from remaining elements. overall complexity will be O(k) + O((n-k)*logK) I can't understand the relevance of the array being k-sorted to using the heap technique. Won't this work even when the array is not k

Does a deep copy operation recursively copies subvariables which it doesn't own?

。_饼干妹妹 提交于 2020-02-03 05:42:05
问题 Given an object that has a variable which it doesn't own; that is, the variable is composed by aggregation instead of composition. Will a deep copy operation copy the variable or only the link to it? 回答1: I like the distinction that you are making here between the role of composition and aggregation in the context of a deep copy. I am going to go against the other answer and say: no, an object should not deep-copy another object that it doesn't own. One would expect a deep copy of an object

What exactly is the 'Saff Squeeze' method of finding a bug?

自作多情 提交于 2020-01-30 19:46:47
问题 I have read Kent Beck's original blog post on the Saff Squeeze method. I have also read this InfoQ post that elaborates a bit more on the topic but does not provide any examples. I know that it is essentially a way of homing in on a bug without relying on the debugger. However I find Kent's example to be not that clear. Can someone more enlightened educate me on how to use this approach with a clear, concrete example? It'll hopefully serve as a learning resource for anyone else researching

What exactly is the 'Saff Squeeze' method of finding a bug?

我与影子孤独终老i 提交于 2020-01-30 19:46:06
问题 I have read Kent Beck's original blog post on the Saff Squeeze method. I have also read this InfoQ post that elaborates a bit more on the topic but does not provide any examples. I know that it is essentially a way of homing in on a bug without relying on the debugger. However I find Kent's example to be not that clear. Can someone more enlightened educate me on how to use this approach with a clear, concrete example? It'll hopefully serve as a learning resource for anyone else researching

How is it possible for a program to contain exclusively thread-safe classes, but not be thread-safe?

天大地大妈咪最大 提交于 2020-01-30 06:03:46
问题 I am reading "java concurrency in practice", and the author says: "A program that consists entirely of thread-safe classes may not be thread-safe". How is this possible? I don't seem to understand, can someone provide an example? 回答1: An example would be individual methods on a class that are thread safe, but they are not atomic if you invoke more than one. E.g. if (!threadSafeCollection.contains(thing)) { threadSafeCollection.add(thing); } This may yield incorrect results if another thread

Given n numbers find minimum perimeter triangle

余生颓废 提交于 2020-01-30 02:34:34
问题 Encountered this problem in coding contest. Could think only O(n^2 log(n)) solution. I guess the expected was O(n log n). I am given n numbers, I have to find 3 numbers that follow triangle inequality and have the smallest sum. I hope this is quite easy to understand. Eg. 10,2,5,1,8,20 Answer is 23 = (5+8+10) 回答1: The longest side should be the successor of the second longest; otherwise, we could shrink the longest and thus the perimeter. Now you can use your binary search to find the third