language-agnostic

Better language feature than exception handling in C++?

(not sure if it's only a C++ thing) Exception handling is hard to learn in C++ and is certainly not a perfect solution but in most cases (other than some specific embedded software contexts) it's certainly the better solution we currently have for exception handling. What about the future? Are there other known ways to handle errors ...

Given a date in UTC time, how do I know if it's currently the same day anywhere else in the world?

Suppose I have a particular UTC time T. I would like a method that returns true if it is possible that at least one spot somewhere else on Earth has the same local date as T right now. def still_same_date?(t) # true # if it is the same day at some other place on Earth # false # otherwise end For example, let's say T is tod...

Pseudocode interpreter?

Like lots of you guys on SO, I often write in several languages. And when it comes to planning stuff, (or even answering some SO questions), I actually think and write in some unspecified hybrid language. Although I used to be taught to do this using flow diagrams or UML-like diagrams, in retrospect, I find "my" pseudocode language has c...

Algorithm for iterating over an outward spiral on a discrete 2D grid from the origin

For example, here is the shape of intended spiral (and each step of the iteration) y | | 16 15 14 13 12 17 4 3 2 11 -- 18 5 0 1 10 --- x 19 6 7 8 9 20 21 22 23 24 | | Where the lines are the x and y axes. Here would be the actual values the algorithm would "retur...

How to validate a Rummikub combination with Jokers

In the game of Rummikub, for those who don't know it, you have tiles in 4 colours and with 13 different numbers on them (so 4 x 13 = 52 unique tiles) which you must use to make groups. There are two kinds of groups: Different colours, same numbers (e.g. R1-B1-G1) Same colours, sequence of numbers (e.g. G6-G7-G8) I'm writing code that...

"Center of Mass" between a set of points on a Toroidally-Wrapped Map that minimizes average distance to all points

edit As someone has pointed out, what I'm looking for is actually the point minimizing total geodesic distance between all other points My map is topographically similar to the ones in Pac Man and Asteroids. Going past the top will warp you to the bottom, and going past the left will warp you to the right. Say I have two points (of ...

How do libraries in different programming languages handle Date & Time, Timestamps & Durations, Leapseconds & -years, DSTs & Timezones, ...?

Is there a standard body or a specific normative way how time-related things should be implemented in practice (like ICU for Unicode-related tasks) or is this currently a "best-effort", depending on how much effort, time and money language and library implementers want to spend? Is there a specific and complete implementation which coul...

File/Data transfer between two arbitrary sources

I'm looking for a simple way to implement this scenario: Say I have two machines I'd like to share data between. The location/addresses of these machines can change at any time. I'd like both machines to check in to a central server to announce their availability. One of the two systems wants to pull a file from the other. I know th...

"Pyramidizing" an array/list (in Ruby, but general solutions could probably be implemented)

I'm not sure what the best word to use here. By "pyramidizing", I mean: [1,2,3,4].pyramidize # => [1,1,1,1,2,2,2,3,3,4] ["a","b","c","d"].pyramidize # => ["a","a","a","a","b","b","b","c","c","d"] To represent visually, it could be thought of as: [ 1,1,1,1, 2,2,2, 3,3, 4 ] Is there a way to do this that maximizes e...

Algorithm to encrypt "numbers" on mobile device so that a wrong password will only reveal wrong numbers but no clue that it is wrong

I am looking for an algorithm to encrypt secret numbers on a mobile device with these properties: Each number has a name associated by the user which is stored in cleartext but may be used to perturb the encryption The number has to be encrypted with the password without revealing a hint about whether a given password is right or wrong...

Differences between singleton/static class/instance class with a private ctor?

The below seem to be very similar: What differences are there between a static and private ctor in a class? Furthermore, what's the difference between a singleton and an instance class with a static or private constructor? ...

Most efficient way of randomly choosing a set of distinct integers

I'm looking for the most efficient algorithm to randomly choose a set of n distinct integers, where all the integers are in some range [0..maxValue]. Constraints: maxValue is larger than n, and possibly much larger I don't care if the output list is sorted or not all integers must be chosen with equal probability My initial idea wa...

Need help with a math trick question

Possible Duplicate: Easy interview question got harder: given numbers 1..100, find the missing number(s) Hi guys, I wasn't sure where to ask this but since this is an algorithmic question here it goes. I've come face to face with a math problem and can't seem to get over it for the last couple of days. It goes like this: Y...

Uses of self referencing lists

Hello, I know it is possible to create a self referencing list in languages like Python: >>> my_list = [1,2] >>> my_list.append(my_list) >>> print my_list [1,2,[...]] >>> print my_list[0] 1 >>> print my_list[2] [1,2,[...]] What algorithms benefit from self referencing lists? I cannot think of one. Thanks. ...

Hard-coding paths in Linux

Coming from Windows background here. Is it an acceptable practice for GUI Linux applications to store their data files (not user-specific) at hard-coded locations (e. g. /etc/myapp/stuff)? I couldn't find any syscalls that would return the preferred directory for app data. Is there a convention out there as to what goes where? ...

Linear feedback shift register?

Lately I bumped repeatedly into the concept of LFSR, that I find quite interesting because of its links with different fields and also fascinating in itself. It took me some effort to understand, the final help was this really good page, much better than the (at first) cryptic wikipedia entry. So I wanted to write some small code for a p...

Measuring the average thickness of traces in an image

Here's the problem: I have a number of binary images composed by traces of different thickness. Below there are two images to illustrate the problem: First Image - size: 711 x 643 px Second Image - size: 930 x 951 px What I need is to measure the average thickness (in pixels) of the traces in the images. In fact, the average thick...

How can I reproduce a scribbly pattern like this in code?

I made this graph in wolfram alpha by accident: Can you write code to produce a larger version of this pattern? Can you make similar looking patterns? Readable code in any language is good, but something that can be run in a browser would be best (i.e. JavaScript / Canvas). If you write code in other languages, please include a scr...

Real number arithmetic in a general purpose language?

As (hopefully) most of you know, floating point arithmetic is different from real number arithmetic. It's for starters imprecise. Many numbers, especially decimals (0.1, 0.3) cannot be represented, leading to problems like this. A more thorough list can be found here. Are there any general purpose languages that have built-in support fo...

Object Oriented style programming for interaction between objects

Hi, I am trying to write a program in object-oriented style. I have some confusions when coding the interaction between two objects. Scenario: Person (John) gives Person (Betty) $ 5. Possible solutions (pseudo code): A) John.pays(Betty, 5); B) Betty.receives(John, 5); C) Bank.transfer(John, Betty, 5); D) begin transaction: John....