language-agnostic

Which Regular Expression Algorithm does Javascript use for Regex?

I was reading this article today on two different regular expression algorithms. According to the article old Unix tools like ed, sed, grep, egrep, awk, and lex, all use what's called the Thompson NFA algorithm in their regular expresssions... However newer tools like Java, Perl, PHP, and Python all use a different algorithm for thei...

function trying to put dot after n characters

I am trying to write a function, it has 2 parameters one is of string and another is of number datatype, my function has to place a dot after every N characters, where N is provided at run time (some number provided through number datatype). Can anybody help me out please? ...

Database query time complexity

I'm pretty new to databases, so forgive me if this is a silly question. In modern databases, if I use an index to access a row, I believe this will be O(1) complexity. But if I do a query to select another column, will it be O(1) or O(n)? Does the database have to iterate through all the rows, or does it build a sorted list for each col...

DB Design: Favor Abstraction or Foreign-Key Constraints ?

Say we have this scenario: Artist ==< Album ==< Track //ie, One Artist can have many albums, and one album can have many tracks In this case, all 3 entities have basically the same fields: ID Name A foreign of the one-many relationship to the corresponding children (Artist to Album and Album to Track A typical solution to the pr...

Blocking access for a given geographic location

What is the most reliable way to prevent users from a geographic location to access a web available application? I understand that IPs are related to geo positioning and I also understand that the most naive way is to get the HTTP request header IP address and take it from there. It's obvious that naive methods, like the one described ...

Are there any patterns/algorithms for dealing with localisable mnemonics?

I work on a web application product which allows mnemonics (i.e. an underscore below the character 'C', to allow a keyboard combination and the key C to trigger the "Close" button). Forms are created by different developers and they can each statically set mnemonics for buttons. Forms can be nested, so it is not necessarily known at ...

Looking for an algorithm to spit out a sequence of numbers in a (pseudo) random order

Suppose I have a sequence of numbers: {n, n+1, n+2, ... n + m} Without storing the numbers ahead of time I want to create a function f(), which given the sequence {1,2,3,...m} will spit out the original set in a random (or at least pseudo random) order. For example assume my sequence is {10, 11, 12, 13, 14, 15, 16, 17} f(1) could ...

What is the proper method of constraining a pseduo-random number to a smaller range?

What is the best way to constrain the values of a PRNG to a smaller range? If you use modulus and the old max number is not evenly divisible by the new max number you bias toward the 0 through (old_max - new_max - 1). I assume the best way would be something like this (this is floating point, not integer math) random_num = PRNG() / ma...

Is there a way I can make two reads atomic?

I'm running into a situation where I need the atomic sum of two values in memory. The code I inherited goes like this: int a = *MemoryLocationOne; memory_fence(); int b = *MemoryLocationTwo; return (a + b) == 0; The individual reads of a and b are atomic, and all writes elsewhere in the code to these two memory locations are also loc...

How do you calculate the total number of all possible unique subsets from a set with repeats?

Given a set** S containing duplicate elements, how can one determine the total number all the possible subsets of S, where each subset is unique. For example, say S = {A, B, B} and let K be the set of all subsets, then K = {{}, {A}, {B}, {A, B}, {B, B}, {A, B, B}} and therefore |K| = 6. Another example would be if S = {A, A, B, B}, the...

What's your most reused class?

Every programmer ends up with a set of utility classes after a while. Some of them are true programming pearls and they are reused in several of your projects. For example, in java: class Separator { private String separator; private boolean called; public Separator(String aSeparator) { separator =...

Scope & Communication Between Objects

I'm working in Delphi (and Delphi terminology), but my question is language neutral. Suppose you have a class, TClient, which sends messages to another class, TFacade. I assume TClient instances would have a private reference variable of type TFacade. A method somewhere in TClient would create an instance of TFacade and assign it to th...

GUI Pattern Question: PropertyChangeListener vs. Specialized View Interface

I'd like to pair a Model with it's View through an interface. I want to control when and how often the view is updated. So something like PropertyChangeListener wouldn't work well (where an event is fired after each property is set). I'm not developing for a specific GUI framework. The goal here is the ability to swap out different GUI ...

Good implementations of reinforced learning?

For an ai-class project I need to implement a reinforcement learning algorithm which beats a simple game of tetris. The game is written in Java and we have the source code. I know the basics of reinforcement learning theory but was wondering if anyone in the SO community had hands on experience with this type of thing. What would your ...

More TCP and POSIX sockets listen() and accept() semantics

Situation: The server calls listen() (but not accept()!). The client sends a SYN to the server. The server gets the SYN, and then sends a SYN/ACK back to the client. However, the client now hangs up / dies, so it never sends an ACK back to the server. The connection is in the SYN_SENT state. Now another client sends a SYN, gets a SYN/AC...

listen() without calling bind()

I tried the following: int sockfd = socket(...); listen(sockfd, 10); accept(sockfd, ...); None of the calls failed, and the program just started blocking as if I had called bind(). What will happen in this case? Will it just be impossible to ever receive a connection, since it has no local address or port? Or was it implicitly assigne...

Exceptions over remote methods

What are the best practices for exceptions over remote methods? I'm sure that you need to handle all exceptions at the level of a remote method implementation, because you need to log it on the server side. But what should you do afterwards? Should you wrap the exception in a RemoteException (java) and throw it to the client? This woul...

The fastest possible way to catch the screen? Any Language

Hi there, I need to take print-screens of a Windows application very very fast to make a video out of it... I have been using C# all the way, but I'm open to any language in which this process may be faster. I have used many techniques: .net functions: Bitmap.CopyFromScreen() GDI Direct3d/DirectX The fastest I've got was using GDI ...

How to represent a path for a genetic algorithm?

I want to use a GA to determine the optimal path from A to B, satisfying certain conditions (length, number of turns, etc.) An example of a path is: Up 4, Left 8, Down 3, Right 3, Down 1, Left 10, Up 4, Left 1, Up 3 The problem is, I don't really know a good way to represent information such as this in a good way for use in a GA, espec...

The 80 column limit, still useful?

Related: While coding, how many columns do you format for? Is there a valid reason for enforcing a maximum width of 80 characters in a code file, this day and age? I mostly use C, however this question is language agnostic. Its also subjective, so I'll tag it as such. Many individual projects set their own various coding s...