ROT13 “Encryption”

ROT13 is a very simple “encryption” achieved by shifting each letter with the 13th letter further in the alphabet (with wrap-around). That is, in English alphabet, A becomes N, B becomes O, N becomes A, etc.

This encryption (or a more general form of it, where rotation can be by any number, with or without wrap-around) was once used for military communications by Julius Caesar.

It’s used today in the Internet (and once, in Usenet) mostly to hide spoilers (similar to Continue reading →

Define A Macro In C With A Variable Number Of Arguments

Say you want to wrap a function like printf() (which receives a variable number of arguments) with a macro. This is how to do it:


#define PRINT_DEBUG(fmt, args...)  printf("DBG:"fmt, ##args)

That is, declare them as args… and use them with ##args.

Note: The word args has nothing special, you could similarly use somethingelse… and ##somethingelse. Just in case you were wondering ;-)

Finding The Median Of Two Sorted Arrays

The reason I’m writing about this problem, is not because of this specific problem itself, but because it shows that sometimes it’s simpler to solve a more general problem, where the original problem is a private case, and then applying the solution to this private case.

Maybe this is counterintuitive at first: How can happen that solving the general case is simpler? As engineers, we’re used to think in the terms of trade-offs, either you solve a general problem (win) with great effort (lose), or you solve a private and specific case (lose) with little effort (win). It doesn’t feel right to win twice: Both solve the more general problem, and do it with less effort.

This intuition seems to not always be correct, as in the case we’re discussing here. Moreover, many times, understanding a more general problem gives a better insight into the nature of the problem than a private case.

The message I’m trying to pass in this article, is: If you’re facing a problem, and something in the problem feels too constrained, don’t hesitate to think of a more general problem, by relaxing some constraints and unbinding parameters.

Let’s dive into our specific problem:

Continue reading →

Replace Text Enclosed in Quotes or Brackets in VIM

This command, to change text enclosed between quotes/double quotes/any type of brackets, can be really time saving:
Say you have this code:

void f() {
    some_code_here();
    and_maybe_more_code();
    return;
}

Place the vim cursor anywhere inside the function, and (in command mode) type ci{ (meaning: Change Inner block enclosed between curly brackets).
The code within the brackets (In this case, the whole body of the function) will be deleted, and you’ll find yourself in insert mode where you can type the new code.

Try it, you’ll love it ;-)

How To Print With Color In Linux Terminal

The proper (and slower ?) way to do this is using ncurses or similar libraries. Here We’ll talk about a “quick and dirty” method using control characters.

There are special characters, referred to as “control characters“, which, when read by the the console, make it change the way it displays text.

The general format of these control characters is:

ESC[{attr1};…;{attrn}m

Where:

Continue reading →

Free Online Courses From Top Universities

Many top universities have opened their courses to be freely viewed online by the public (video, sometimes also slides, lecture notes and exercises).