Top 10 web development presentations on Slideshare

April 29, 2008

I’ve only recently discovered Slideshare, a website for sharings slideshows and presentations. The site has lots of really good programming presentations. My top 10 web development presentations from the site are listed below, in no particular order:

AJAX Security

High Performance Javascript

High Performance AJAX applications

Metaprogramming Javascript

Enterprise PHP development

PHP tips and tricks

Go OO: Reallife design patters in PHP5

Iterators in PHP 5

Ruby on Rails 101

Django for beginners

Line number and position from absolute file position

April 24, 2008

I was once having some issues with a compiler because when it found errors rather than giving me the line number and character position of the error it would report the file position. It was a real nightmare trying to work out where the errors actually were, because my text editor only told me the line number and line position of the current character, not the overall file position.

My solution was to write the following small C program, which given a position and a filename outputs a line number and line position.

#include 

void usage()
{
	printf("file_pos  n");
}

int main(int argc, char* argv[])
{
	if(argc != 3)
	{
		usage();
		return 0;
	}

	// open a file
	char *filename = argv[2];
	FILE *fp = fopen(filename, "r");

	// seek to given position
	int position = atoi(argv[1]);
	int lineNumber = 0;
	int linePosition = 0;
	char ch;
	int i;
	for(i = 0; i < position; i++)
	{
		ch = fgetc(fp);
		if(ch == 'n')
		{
			lineNumber++;
			linePosition = 0;
		}
		else
		{
			linePosition++;
		}
	}

	printf("Position %d is line %d and character %dn", position, lineNumber, linePosition);
	return 0;
}

And here it is in action:

~$ ./file_pos 50 file_pos.c
Position 50 is line 4 and character 15

Fortunately compilers are much more friendly these days!

Python Bingo Card Generator

April 9, 2008

Here is a Python script I wrote some time ago. It generates bingo cards based on a number of variables at the top of the file, such as grid size, and number of cards.

#!/usr/bin/env python
import random

gridSize = 5
minNum = 1
maxNum = 50
cards = 40

for h in range(cards):
	card = []
	randRange = range(minNum, maxNum)
	card = random.sample(randRange, gridSize * gridSize)
	for i in range(gridSize):
		string = ""
		for j in range(gridSize):
			string +=  str(card[i + j * gridSize]) + "t"
		print string	

	print "n"

Below if the output when cards is set to 2:

~$ ./bingo.py
11      39      48      19      36
3       12      29      32      31
8       16      5       34      22
14      47      21      26      23
4       13      42      44      28

28      8       15      16      42
10      35      43      38      41
27      17      21      1       4
9       47      11      30      40
49      44      6       45      18