1. Use a portable shebang

Use:

bash can have various locations Eg.: /sbin/bash , /usr/local/bin/bash , /usr/bin/bash

Note: This applies for other scripting languages as well, not only for bash .

Implementing a generic Priority Queue in C (using heaps)

This article considers you are already familiar with the concept of a Priority Queue.

It’s a Queue with a twist. You push() elements like in a standard Queue, but when it comes to pop() them out you take the element with the “highest” priority.

The implementation is going to use a Binary Heap.

The following code is also available on github:

pqueue.h

Disable/Enable all triggers in the schema (Oracle)

Normally If you want to enable/disable a trigger you would use the ALTER statement:

But today I had to disable/enable all triggers in the schema, in order to freely insert and update some data . For this task I’ve written a short PL/SQL procedure:

The idea is pretty simple, we iterate over all the user-defined triggers (see user_triggers), building on-the-way statements that are going to be “EXECUTED IMMEDIATELY” .

To execute the procedure you can do something like this:

Hope it’s going to be useful .

Credit Card Validation (Python 3.x)

If you ever wondered how Credit Card Numbers, IMEI Numbers or Canadian Social Insurance Numbers are validated you can take a look at this Programming Praxis article . It’s all about a simple, tiny, patented (now public domain) algorithm invented by IBM’s computer scientist Hans Peter Luhn .

The validation is pretty simple, and works this way:

1. Given a number we will consider the last digit a check-digit .
2. Starting from the check digit backwards we will multiply by 2 every even digit .
3. We will sum all digits (both doubled and undoubled) .
4. If the sum is a multiple of 10 then the number is valid, else is invalid .

Example:

 5 4 3 2 9 8 3 7 6 5 8 3 4 9 (1+6) 3 (1+4) 6 = 50 (which is a muliple of 10, thus the number is valid)

I’ve written the implementation of this simple algorithm using python 3.x . The solution can become rather elegant if we use the functional programming features that python offers us:

And the output:

Bytelandian gold coins

A nice programming challenge (easy/medium difficulty) comes from http://www.codechef.com/ and it is being called: “Bytelandian gold coins”.

From this exercise I’ve learnt that the most elegant solutions are recursive .

In this challenge our task is to resolve the currency issues in a imaginary country, Byteland:

Each Bytelandian gold coin has an integer number written on it. A coin n
can be exchanged in a bank into three coins: n/2, n/3 and n/4.
But these numbers are all rounded down (the banks have to make a profit).

You can also sell Bytelandian coins for American dollars. The exchange
rate is 1:1. But you can not buy Bytelandian coins.

You have one gold coin. What is the maximum amount of American dollars
you can get for it?

The input will contain several test cases (not more than 10). Each
testcase is a single line with a number n, 0 < = n <= 1 000 000 000. It is the number written on your coin. For each test case output a single line, containing the maximum amount of American dollars you can make.

(…more here)

My first attempt (which seemed natural at that point) was working “flawlessly” on my local machine but codechef was insistingly reporting Time Limit Exceed: Read More