pysert (with lowercase ‘p’) is a python script capable of generating random SQL data (INSERTS) from predefined templates .

How it works:

Let’s suppose you want to in fill the ‘Employees’ table in your application with 100 random entries . The table has the following columns: (employee_id, first_name, last_name, email, job_id, salary) .

The first step will be to write the pysert template .  The template is a a simple XML file divided into two sections:

  • A declarative area – Here you define the data sets from which the data is generated ;
  • The template string  – The actual string from which the output is generated .
A possible pysert template for our ‘Employees’ table will look like this:

template.xml

Once the template is ready you can use the script to generate the results . In it’s current form the script works perfectly with both python 2.7.x and python 3.2.x series .

The generated output will be printed directly to Continue reading

If you found this article on the web I suppose you already know what a Priority Queue is . Of course it’s an abstract data type, that acts exactly like a queue, but additionally all the elements have priorities associated with them . So when you want to remove something from the Priority Queue, you will pull the highest priority element first .

To get better performance, we’ll going to implement the Priority Queue using a heap . Please follow the code comments:

pqueue.h

Continue reading

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 .

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:

Observations: Continue reading

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: Continue reading