Why I fail to submit my phrase to engineyard contest

23 07 2009

There is a programming contest which is conducted by engineyard. The contest is interesting so I planned to participate. I wrote program but could not submit the phrase to it. When I analyze why I could not submit, these are the mistake that I did.

1. I calculated wrong time of submission. I am living in IST and the content submission time is based on PST. I am not very familiar with these timezones, so I calculated wrong time. By the time I wanted to submit, the submission time is closed. I learned calculate the time properly and keep alerts

2. Lazy in writing program. I wrote program which will just take the 12 random words from the dictionary and calculate the hamming distance as specified in the contest. There are 2 more things I missed I can add 5 letter word at the end and each letter in the word can be of any case(upper or lower). I wrote these 2 case after the contest time start. I learned finish the task at that time, don’t postpone any piece of work to another day.

3. Lazy in knowing about SHA1 algorithm. I know that taking random words from the dictionary does not work. I know I need to know more about SHA1 but could not find or spend time on knowing more about SHA1. I depend on luck I know this is not correct, still I am lazy and couldn’t read about SHA1. I leaned that do not depend on luck just know more and work.

htmlcutstring python package

20 07 2009

I released php, javascript implementation for cut html string, these programs cut the html string by keeping html tags as it is. Now I released same in python with the name htmlcutstring. Check this at http://pypi.python.org/pypi/htmlcutstring/1.0 .

It is easy to extract an excerpt of a text string with a given length limit. But if you want to extract an excerpt from HTML, the tags that may exist in the text string make it more complicated.

This module provides a solution to extract excerpts from HTML documents with a given text length limit without counting the length of any HTML tags.

This module is used to cut the string which is having html tags. It does not count the html tags, it just count the string inside tags and keeps the tags as it is.

ex: If the string is “welcome to <b>Python World</b> <br> Python is bla”. and If we want to cut the string of 16 charaters then output will be “welcome to <b>Python</b>”.

Here while cutting the string it keeps the tags for the cutting string and skip the rest and without distorbing the div structure.

obj = HtmlCutString("welcome to <b>Python World</b> <br> Python is",16)
newCutString = obj.cut()

newCutString = cutHtmlString("welcome to <b>Python World</b> <br> Python is",16)

steps to create mysql database

17 07 2009

Creating a database in mysql is easy with just CREATE TABLE command. For any application just creating database is not enough. We have to create one user and we need to give privileges for that user on the database. These are the commands that will create database, user and give permissions for the user to the database.

Go to mysql prompt

shell>mysql -u root -p

Use -p option if password is set for the root.

Create database

mysql> CREATE DATABASE databasename;

Create user

mysql> CREATE USER username

Grant permissions for the user

mysql> GRANT ALL PRIVILEGES ON databasename.* to username@localhost IDENTIFIED BY 'password';

Flush privileges


Quit mysql prompt

mysql> \q

Note: Untill we flush the privileges we can’t get the new privileges that we assigned.

That’s it we created database, user and gave permissions to user to the database. Now use the following command to go to database with the username

shell> mysql -u username -p databasename

strlen vs mb_strlen function in php

7 06 2009

I wrote a simple class in php called cut html string . This class with cut the html string without considering html tags but the output will have the html tags as it is. Here I used strlen function to find the length of string. This function return the number of bytes of the string. If string has some utf8 special characters then it returns wrong value, because those special characters will take more bytes. I want to find the number of characters in the string. mb_strlen function returns the number of characters in string. A multi-byte character is counted as 1. I prefer to use mb_strlen function to find the character count of a string.

Cut html string by keeping html tags as it is

12 04 2009

With the use of WYSIWYG editor user can enter html content. While displaying teaser(short content) of the content on home page or dashboard it is hard to cut the string properly. Regarding this I did not see any available code. I need to do this in javascript and PHP. I implemented same logic in both javascript and php. I hope it would be helpful for others. The code for javascript and php is available at http://code.google.com/p/cut-html-string/

Using PHP code

You can download PHP code from this link
Download code and extract it. Include the cutstring.php file and use like this

$data = "<span style='color:green;'>aa<em>BB</em><ul><li>one</li><li>two</li><li>three</li></ul></span>";
$wanted_count = 10;
$cutstrObj = new HtmlCutString($data,$wanted_count);
$new_string = $cutstrObj->cut();
echo $new_string;

Update: We can directly use cut_html_string function

$data = "<span style='color:green;'>aa<em>BB</em><ul><li>one</li><li>two</li><li>three</li></ul></span>";
$wanted_count = 10;
$new_string = cut_html_string($data,$wanted_count);
echo $new_string;

Update 2: Above code will output the following string

<span style="color:green;">aa<em>BB</em><ul><li>one</li><li>two</li></ul></span>

Using Javascript code

You can downlod javascript code from this link
Download code and extract it. Include the cutstring.js file in your html file and use like this.

var data = '<span style="font-weight:bold">aa<em>BB</em></span><span id="1">Test <b>Cutstring</b></span> bb';
var wanted_count = 10;
var cutstrObj = new CutString(data,wanted_count);
var newStr = cutstrObj.cut();

update: Now we can directly use cutHtmlString function

var data = '<span style="font-weight:bold">aa<em>BB</em></span><span id="1">Test <b>Cutstring</b></span> bb';
var wanted_count = 10;
var newStr = cutHtmlString(data, wanted_count);

Update 2: Above code will log the following output

<span style="font-weight: bold;">aa<em>BB</em></span><span id="1">Test <strong>C</strong></span>

First I implemented it easily in javascript using DOM. But it took lot of time in PHP to implement same logic. Finally I implemented same logic using DOMDocument in php.