pseudocode

From milliseconds to hour, minutes, seconds and milliseconds

假装没事ソ 提交于 2019-11-28 04:30:47
I need to go from milliseconds to a tuple of (hour, minutes, seconds, milliseconds) representing the same amount of time. E.g.: 10799999ms = 2h 59m 59s 999ms The following pseudo-code is the only thing I could come up with: # The division operator below returns the result as a rounded down integer function to_tuple(x): h = x / (60*60*1000) x = x - h*(60*60*1000) m = x / (60*1000) x = x - m*(60*1000) s = x / 1000 x = x - s*1000 return (h,m,s,x) I'm sure it must be possible to do it smarter/more elegant/faster/more compact. Here is how I would do it in Java: int seconds = (int) (milliseconds /

Interview puzzle: Jump Game

不羁的心 提交于 2019-11-28 04:01:18
Jump Game: Given an array, start from the first element and reach the last by jumping. The jump length can be at most the value at the current position in the array. The optimum result is when you reach the goal in minimum number of jumps. What is an algorithm for finding the optimum result? An example: given array A = {2,3,1,1,4} the possible ways to reach the end (index list) are 0,2,3,4 (jump 2 to index 2, then jump 1 to index 3 then 1 to index 4) 0,1,4 (jump 1 to index 1, then jump 3 to index 4) Since second solution has only 2 jumps it is the optimum result. Overview Given your array a

Algorithm to solve the points of a evenly-distributed / even-gaps spiral?

浪尽此生 提交于 2019-11-28 00:41:02
问题 First, just to give a visual idea of what I'm after, here's the closest result (yet not exactly what I'm after) image that I've found: Here's the entire site-reference: http://www.mathematische-basteleien.de/spiral.htm BUT, it doesn't exactly solve the problem I'm after. I would like to store an array of points of a very specific spiral algorithm. The points are evenly distributed The 360 degree cycles have an even gap If I'm not mistaken, the first two points would be: point[ 0 ] = new Point

Object.hashCode() algorithm

谁都会走 提交于 2019-11-27 23:33:00
问题 I'm looking for the algorithm of Object.hashCode() . This code is native in Object.java. Is this because (a) the code is in assembly-- never was in Java or any other HLL at all or (b) it simply isn't disclosed ? In either case, I am looking to get hold of the algorithm (pseudo-code or some detailed explanation) of "how hashCode() is calculated"-- what are the params going into its calculation and the calculation itself? Please note: It's the hashCode() of Object i'm looking for-- not another

transitive reduction algorithm: pseudocode?

我与影子孤独终老i 提交于 2019-11-27 18:45:35
I have been looking for an algorithm to perform a transitive reduction on a graph, but without success. There's nothing in my algorithms bible (Introduction To Algorithms by Cormen et al) and whilst I've seen plenty of transitive closure pseudocode, I haven't been able to track down anything for a reduction. The closest I've got is that there is one in "Algorithmische Graphentheorie" by Volker Turau (ISBN:978-3-486-59057-9), but unfortunately I don't have access to this book! Wikipedia is unhelpful and Google is yet to turn up anything. :^( Does anyone know of an algorithm for performing a

Pathfinding (routing, trip planning, …) algorithms on graphs with time restrictions

蓝咒 提交于 2019-11-27 17:15:45
I have a database of bus/train/... stops and the arrival/departure times on each date and so on. I'm looking for a way to do a search for the fastest(shortest/cheapest/least transitions) trip between two locations. I would like to have arbitrary locations in the future, using OpenStreetMap data to do walking between stops and from stops to start/end, however for the time being I just want to find path between two stops in the database. The problem is I can't seem to find much info about this subject, for example this Wikipedia page has a lot of text with absolutely no useful information in it.

Static (Lexical) Scoping vs Dynamic Scoping (Pseudocode)

感情迁移 提交于 2019-11-27 16:53:43
Program A() { x, y, z: integer; procedure B() { y: integer; y=0; x=z+1; z=y+2; } procedure C() { z: integer; procedure D() { x: integer; x = z + 1; y = x + 1; call B(); } z = 5; call D(); } x = 10; y = 11; z = 12; call C(); print x, y, z; } From my understanding, the result of this program when run using static scoping is: x=13, y=7, and z=2. However, when it is run using dynamic scoping , the result is: x=10, y=7, and z=12. These results are the ones that our professor gave us. However, I cannot understand for the life of me how he has reached these results. Could someone possibly walk

Check which side of a plane points are on

倖福魔咒の 提交于 2019-11-27 13:42:18
问题 I'm trying to take an array of 3D points and a plane and divide the points up into 2 arrays based on which side of the plane they are on. Before I get to heavily into debugging I wanted to post what I'm planning on doing to make sure my understanding of how to do this will work. Basically I have the plane with 3 points and I use (pseudo code): var v1 = new vector(plane.b.x-plane.a.x, plane.b.y-plane.a.y, plane.b.z-plane.a.z); var v2 = new vector(plane.c.x-plane.a.x, plane.c.y-plane.a.y, plane

Create sine lookup table in C++

这一生的挚爱 提交于 2019-11-27 12:24:30
问题 How can I rewrite the following pseudocode in C++? real array sine_table[-1000..1000] for x from -1000 to 1000 sine_table[x] := sine(pi * x / 1000) I need to create a sine_table lookup table. 回答1: You can reduce the size of your table to 25% of the original by only storing values for the first quadrant, i.e. for x in [0,pi/2]. To do that your lookup routine just needs to map all values of x to the first quadrant using simple trig identities: sin(x) = - sin(-x), to map from quadrant IV to I

Algorithm for sampling without replacement?

蓝咒 提交于 2019-11-27 11:36:58
I am trying to test the likelihood that a particular clustering of data has occurred by chance. A robust way to do this is Monte Carlo simulation, in which the associations between data and groups are randomly reassigned a large number of times (e.g. 10,000), and a metric of clustering is used to compare the actual data with the simulations to determine a p value. I've got most of this working, with pointers mapping the grouping to the data elements, so I plan to randomly reassign pointers to data. THE QUESTION: what is a fast way to sample without replacement, so that every pointer is