Thursday, November 18, 2010

Algorithm Design: Longest Common Subsequence

A group of students in the COMP6400 course in UWI have started a blog to share the material covered in class. Hopefully we can publish everything in this location in such a way that everyone can benefit from each other. Its the study group gone WWW.

Here is an example of one post:

Algorithm Design: Longest Common Subsequence: "Here is an implementation of the Longest Common Subsequence (LCS) problem that was discussed in class. The Problem You are given a sequence..."


Wednesday, June 23, 2010

New Website

I finally managed to finish the new design of my website. Of course it is still early days so the content is a bit sparse. I have started publishing a number of tutorials and lecture notes on various topics.

Over the years I have written many tutorials on a number of subjects but unfortunately they are not in the correct format to be published on the web. The time consuming task of converting them all has begun.

This website will be my main point of contact with students this coming year. I am publishing all course outlines and handouts here.

Take a look: http://www.vincentramdhanie.com.

Reading List

I found myself reading Stieg Larsson's The Girl with the Dragon Tattoo recently and was pleasantly surprised. It was a pretty good mystery with enough action to keep you reading till the end.

At first it took some getting used to the Swedish terms and places but that really added to the appeal of the book.

There are plenty of likeable characters with unique traits. All in all a good reading experience.

I have already started on the second of the series  The Girl Who Played with Fire.



Monday, May 03, 2010

South Cluster Launches IPG

The Baha'i community of South Trinidad have launch an intensive programme of growth (IPG) on 2 May 2010. The occasion was joyous and the excitment of the friends could be felt. Over 70 persons from around the country attended the event.


Messages of congratulations from the National Spiritual Assembly of Trinidad and Tobago and the Counsellor were read.


A spiritual atmosphere was created by opening the program with a 15 minute devotional and numerous songs were sung during the program.



Many friends from other clusters offered congratulations to the Southern Cluster.


For the past few years the Baha'i friends in South have been working very hard to make a significant advance in building their capacity to effect change in the society. It is widely recognized that the present order of society is crumbling and there is very little that can be done to save it.

Any real change has to come from the fundamental understanding that we are spiritual beings and not just animals. As such our actions can reflect a nobler reality. Once this understanding is achieved an individual can start working towards building a new world civilization.

The Baha'i community is searching for the individuals in the wider community that is concerned about the breakdown in society and would like to take an active role in building the new civilization. Together these individuals will study and develop skills that are necessary to carry out this incredible task.

The Universal House of Justice has described the activities that are necessary for this work:

...meetings that strengthen the devotional character of the community; classes that nurture the tender hearts and minds of children; groups that channel the surging energies of junior youth; circles of study, open to all, that enable people of varied backgrounds to advance on equal footing and explore the application of the teachings to their individual and collective lives...
The Universal House of Justice, Ridvan 2010.
When many individuals are actively engaged in these four activities it is called an Intensive Programme of Growth (IPG). The launch of an IPG is exciting because it signals that the community have grown to the point that this process can now be self sustaining. The community now has the capacity to steadily introduce new individuals to this programme and continuously widen the circle of friends that are actively building the new world civilization.
Congratulations South Cluster.

Friday, April 02, 2010

Endevour Overpass

Sometimes you notice something that is so inconsequential that you do not even notice that you notice. There is absolutely nothing of note in this picture.
 

Looking north to the Endevour overpass.

Tuesday, March 30, 2010

Upgrading a Laptop

Upgrading is never easy. No matter how much preparation you make.


We decided to upgrade all the developers laptops on the team by increasing the RAM and moving from Windows XP to Windows 7 64 bit. We started by getting more RAM and increasing to 4 GB. Of course, because we were running Windows XP 32 bit, so we only got a usable 3.48 GB of RAM. While that is an improvement over the 2GB we had before we wanted more.

So we are now attempting to upgrade to Windows 7. I spent the better part of the last two days backing up and installing. Backing up took all night despite the use of some helpful migration tools available from Microsoft.

The installation went quite smoothly and so far the new system is working nicely. Unfortunately, I still have only 3.48 GB of RAM. It turns out that I installed the 32 bit version. Hmm. Now I will have to get the 64 bit cd and do it all over again. At least this time I have everything backed up so if the installation runs smoothly then I will not have  to spend too much time on the operation.

Saturday, March 27, 2010

Mastermind in SML

Functional languages are making a comeback. For a while they were in the exclusive domain of academia but recently there has been increased interest in functional programming in the wider industry. I will leave it up to WikiPedia to explain if you never heard of this before.

Here is a simple program written in SML that implements a Mastermind game.

(*
This program implements a mastermind game.
The computer is the board master and the user
has to make guesses until...

The tokens "g", "r", "y", "b" are used to represent

green, red, yellow and blue respectively.

Author: Vincent Ramdhanie
Date: 11 February 2010
*)


(*Start by opening the TextIO library for input and output of text data.*)
open TextIO;

(*Next we need a random number generator*)
val ran = Random.rand(1, 20);
fun random() = Random.randRange(1, 4) ran;

(* member returns true if c is a member of the list, false otherwise. Of course there is a built in member function. But we are trying to learn the language here are we not?*)

fun member(c, []) = false |
    member(c, h::t) = c = h orelse member(c, t);
(*inPosition returns true is c is in the ith position ina given list*)
fun inPosition(c, i, []) = false |
    inPosition(c, 0, h::_) = c = h |
    inPosition(c, i, _::t) = inPosition(c, i - 1, t);


(*We need a datatype to track character correctness*)
datatype correct = A | B | C;


(*This function takes a character and a position and returns a correct value*)
fun charCorrect(c, i, L) =
    if member(c, L) then
        if inPosition(c, i, L) then
            A
        else
            B
    else
        C;

(*Remove all newLine characters from a char list*)
fun removeNewLine([]) = [] |
    removeNewLine(#"\n"::_) = [] |
    removeNewLine(h::t) = h::removeNewLine(t);

(*Remove newline characters from a string *)
fun trimString(s) = implode(removeNewLine(explode(s)));

(*Ensure that only the characters #"g", #"r", #"b" and #"y" are in a string*)
local val chars = [#"g", #"r", #"b", #"y"] in
fun limitChars([]) = true |
    limitChars(h::t) = member(h, chars) andalso limitChars(t)
end;

fun limitString(s) = limitChars(explode(s));

(* Generate Random characters*)
fun randomChar() =
    let
        val r = random()
    in
        case r of
            1 => #"g" |
            2 => #"b" |
            3 => #"r" |
            4 => #"y"
    end;

(*Generate just n characters*)
fun nRandomChars(0) = [] |
    nRandomChars(n) = randomChar()::nRandomChars(n - 1);

(*Create a string of 4 characters*)
fun randomCharString() = implode(nRandomChars(4));

(*Some user interaction functions*)

(*This function gets an input line from the user*)
fun inLine() = valOf(inputLine(stdIn));

(*Prompt the user for input*)
fun prompt(msg) = (print(msg ^ "\r\n");inLine())

(*The Yes No datatype*)
datatype answer = Yes | No;

(*The Valid Invalid datatype*)
datatype valids = Valid | Invalid;

(*Prompt the user for a Yes No value*)
fun yesNoPrompt(msg) =
    let
        val ans = prompt(msg)
    in
        if ans = "Yes\n" then
            Yes
        else
            No
    end;

(* Get a cleaned up guess*)
fun guessPrompt() =
    let
        val guess = prompt("Please enter 4 character guess using the characters g, r, b and y")
    in
        trimString(guess)
    end;


(* Validate Guess*)
fun getValidGuess() =
    let
        val inp = guessPrompt()
        val lim = limitString(inp)
    in
        if (size(inp)  = 4) andalso lim then
            inp
        else
            (print("Sorry, That input is invalid. Please try again.\n\n");getValidGuess())
    end;

(*  Print a report*)
fun printResult([]) = print("_______________________\n") |
    printResult(h::t) =
    if h = A then
        (print("Correct Position\n");printResult(t))
    else
        if h = B then
            (print("Incorrect Position\n");printResult(t))
        else
            (print("Incorrect Token\n");printResult(t))

(*Determine the quality of a guess*)
fun processGuessAux(original, _, []) = [] |
    processGuessAux(original,i, h::t) =  charCorrect(h, i, original)::processGuessAux(original,i + 1, t);

fun processGuess(original, guess) =
    if original = guess then
        true
    else
        (printResult(processGuessAux(explode(original), 0, explode(guess)));false)
   

(*A run of the Game*)
fun gameRun(original) =
    let
        val guess = getValidGuess()
                val result = processGuess(original, guess)
    in
        if result then
            print("Congratulations. You Win!")
        else
            gameRun(original)
    end;

(*Game Control*)
fun playAgain() =
    let
        val ans = yesNoPrompt("Do you want to play again? (Yes/No):\n")
    in
        if ans = Yes then
            play()
        else
            print("Thank You for playing.")
    end
and
   play() =
    let
        val original = randomCharString()
    in
        (gameRun(original);playAgain())
    end;

Friday, March 05, 2010

Collatz Conjecture

In today's xkcd comic, the topic was Collatz Conjecture. This is the first I have ever heard of this so of course I looked it up on Wikipedia. Turns out it was proposed by Lothar Collatz in 1937.

 
 Simply put, given any natural number n. If n is even, we halve it (n / 2), otherwise we multiply by 3 and add 1 to obtain 3n + 1. Rinse, repeat. The conjecture is that for all numbers this process converges to 1.

The Wikipedia article gives several implementations of this algorithm so I opened up my editor and tried out an SML implementation. Without exhaustive testing, it seems to be working so far.
Here is the code:

(*
  Implements Collatz Conjecture.
  Accepts: an integer n.
  Returns: The list of integers produced by following collatz sequence until the sequence converges to 1.
  Pre: n >= 1
*)
fun collatz(1) = [1] |
    collatz(n) =
        let
            val even = n mod 2 = 0
        in
           if even then
              n::collatz(n div 2)
           else
              n::collatz(3*n + 1)
        end;

Wednesday, January 20, 2010

Increasing Memory For Servers in Netbeans 6.8

A frequent problem that developers working with Java web applications face is running out of heapspace while testing their applications. This is mainly due to the fact that the amount of heap memory reserved by the JVM for the application is quickly consumed by repeated undeploy/deploy cycles. The onmly work around that I know to date is to increase the available heap space. This gives you at least temporary relief since you are only postponing the inevitable out of memory error.

In Netbeans 6.8 it is very simple to increase the memory available for a server.

Step1.
On the Services tab expand the Servers node. Right click on the name of the server that you are using and select Properties.


Figure 1: Selecting Server Properties Window

Step 2
Select the Platform tab and enter the following under VM Options:

-Xms512M -Xmx512M

These options specify that there will be a minimum of 512 MB available for the server and a maximum of 512 MB available. You can of course use different numbers to suit your needs. Other VM Options can be found here.


Figure 2: The VM OPtions for the Server

Now you need to restart the server for the settings to take effect.



Thursday, January 07, 2010

Making 2 jQuery UI DatePickers Interact

Using the jQuery DatePicker is easy and provides a very functional calendar popup. What if you want to have two datepickers but you want the second one to respond to the selections in the first one? So every time you select a date in datepicker1, then datepicker2 immediately selects a date that is 7 days ahead.

Using the built in Date functions in JavaScript you can obtain the Date from the first picker when it is changed and set the date on the second picker after adding the required number of days.

<!DOCTYPE html>
    <html>
        <head>
            <link type="text/css" href="http://jqueryui.com/latest/themes/base/ui.all.css" rel="stylesheet" />
            <script type="text/javascript" src="http://jqueryui.com/latest/jquery-1.3.2.js"></script>
            <script type="text/javascript" src="http://jqueryui.com/latest/ui/ui.core.js"></script>
            <script type="text/javascript" src="http://jqueryui.com/latest/ui/ui.datepicker.js"></script>
            <script type="text/javascript">
           $(document).ready(function(){
              $("#datepicker1").datepicker();
              $("#datepicker2").datepicker();
              $("#datepicker1").change(function() {
              var date2 = $("#datepicker1").datepicker("getDate");
              date2.setDate(date2.getDate()+7);
              $("#datepicker2").datepicker("setDate", date2);
              });
                            });
             </script>
        </head>
         <body style="font-size:62.5%;">

              <div type="text" id="datepicker1"></div>
              <div type="text" id="datepicker2"></div>

          </body>
     </html>