enrii.blog

A passionate programmer’s findings in the world of internet.

Archive for the 'Programmers should read' Category

Programming Brain Teaser

Wednesday, July 9th, 2008

I joined the fun to work on a programming brain teaser by Dustin Diaz (read the original post for details). The general rule is as follows:

Group together all duplicate items that occur anytime beyond twice by wrapping them with a tag, naturally "bookending" them.

Here's my solution in PHP:

$arr = array ('a', 'b', 'c', 'c', 'd', 'e', 'e',
'e', 'e', 'e', 'f', 'e', 'f', 'e',
'f', 'a', 'a', 'a', 'f', 'f', 'f');

$count = 0;
$prev = '';
foreach ($arr as $item) {
	if ($prev == $item) $count ++;
	if ($count == 2 && $prev == $item) echo '<strong>';
	if ($count >= 2 && $prev != $item) echo '</strong>';
	echo $item;
	if ($prev != $item) $count = 0;
	$prev = $item;
}
if ($count >= 2) echo '</strong>';

Give your comments, if any.

Update: After pointed out by gbyeow in the comment, I agree that it could be optimized. I was too overwhelmed by achieving the result that I forgot to optimize after I completed the code.

The optimized version:

$arr = array ('a', 'b', 'c', 'c', 'd', 'e', 'e',
'e', 'e', 'e', 'f', 'e', 'f', 'e',
'f', 'a', 'a', 'a', 'f', 'f', 'f');

$count = 0;
$prev = '';
foreach ($arr as $item) {
	if ($prev == $item) {
		$count ++;
		if ($count == 2) echo '<strong>';
	}
	if ($prev != $item) {
		if ($count >= 2) echo '</strong>';
		$count = 0;
	}
	echo $item;
	$prev = $item;
}
if ($count >= 2) echo '</strong>';

HTML Linking - Common Mistake

Sunday, December 2nd, 2007

At times, I was wondering why would an author link to an invalid site in their new article. It could be caused by typing mistakes but the author should always try to click on their own link before they publish the article.

I found one common mistake that lots of people would make. Google has a goodbad example on this.

At this What is Web History? help page, if you point to the link in the page (refer the following image), you will see a weird link at status bar.

Incorrect link

What I saw in status bar:

Status bar of incorrect link

If the author used the following code to link, he will get the problem.

<a href="www.enrii.com">enrii.blog</a>

The author should include "http://" to avoid the problem.

<a href="http://www.enrii.com">enrii.blog</a>

So, web programmers, take note about this!

eGenting Programming Competition 2007

Wednesday, July 25th, 2007

eGenting Programming Competition 2007

The annually organised eGenting Programming Competition is here again. If you have not heard of it before, there are basically 2 categories:

  1. Graduate category for under 26 years old
  2. Professional category for 26 years old and above

The prizes are very attractive, with cash prizes and job offer for graduate category, and computers and cruise trip for professional category.

Competition Details

  • Competition date : 22 September 2007
  • Time : 10.00 am to 6.00 pm
  • Venue : Resort Convention Centre, Genting Highlands, Malaysia
  • Nature : Open-book handwritten programming task

Registration is closing on 31 August 2007. Make sure you don't miss this great chance to win those fabulous prizes and even job offers.

Official website: eGenting Programming Competition

Setup CVSNT for Personal Use

Thursday, June 21st, 2007

After developed a small system, I find it hard to keep track all versions of my source files, especially when there are bug fixes and enhancement requests. I have to create a whole new folder to keep track all changes in the past to allow me rollback to a specific version that I need. I'm sure there are version control system that could help me. In my day work, my company is using SCCS/CSSC but it requires Linux environment.

I asked my friends who do programming in Windows environment for suggestions. Most of them confirmed that CVSNT is the thing I need.

There are lots of good and detail guide out there to setup CVSNT, but I there is none that is simple and straight forward, or it's probably too complicated for my simple mind. Let me try to put up a simple guide, with my own experience.

(more...)

Page Break for Printed Web Pages

Sunday, May 6th, 2007

After I get my invoice to be printed differently from screen, I need to have page breaks for invoices that have more than 1 page. The way to achieve it, is to use CSS, again.

It's just as simple as putting the following code at wherever you need the page break:

<div style="page-break-after: always;"></div>

Also note that there's also page-break-before which you can specify a page break before some tags.