Wednesday, December 24, 2008

Numerology in Erlang

After a long time, again with the same problem but using a different language. I have been watching the talks on Erlang - about its strength and stability. I thought why not give it a try.

This is the same numerology problem written in Erlang.


-module(numero).
-export([value/1,start/1]).

start(Args) -> value(lists:nth(1,Args)).

value(NAME) -> io:write(sum(0,lists:sum([val(A) || A <- NAME]))),io:nl(),halt().

sum(SUM,NUM) when NUM > 0 -> Q = NUM,
R = Q rem 10,
NEWQ = Q div 10,
sum(SUM + R,NEWQ);
sum(SUM,0) ->
if
SUM > 9 -> sum(0,SUM);
true -> SUM
end.


val(C) ->
if
(($A =:= C) or ($J =:= C) or ($I =:= C) or ($Y =:= C) or ($Q =:= C)) -> 1;
(($B =:= C) or ($K =:= C) or ($R =:= C)) -> 2;
(($S =:= C) or ($C =:= C) or ($L =:= C) or ($G =:= C)) -> 3;
(($T =:= C) or ($D =:= C) or ($M =:= C)) -> 4;
(($E =:= C) or ($N =:= C) or ($X =:= C) or ($H =:= C)) -> 5;
(($U =:= C) or ($V =:= C) or ($W =:= C)) -> 6;
(($O =:= C) or ($Z =:= C)) -> 7;
(($F =:= C) or ($P =:= C)) -> 8;
true -> 0
end.

Saturday, September 27, 2008

Netbeans setting jvmargs for SHIFT-F6

In Netbeans there is an option to run the current file. It works fine until we need the option to set a JVM argument.
I hit the same problem and was searching the build xml for target that runs the file and customize it. But accidentally found a property runmain.jvmargs in project.properties file and set the JVM args that i wanted and it worked.

--> {project}/nbproject/project.properties (runmain.jvmargs)

This also works for web projects.

Sunday, July 13, 2008

Numerology with Ruby

This is a simple Numerology calculator written in ruby. Just a pass time program while learning ruby.
This will give you the value of the given name and date (date of birth)
Usage:
./numerology.rb <your_name> <date> <month> <year>


#!/usr/bin/env ruby
class Numerology
@name
@date
@month
@year
$map = {'A' => 1, 'J' =>1, 'I' =>1, 'Y' => 1, 'Q' => 1,
'B' => 2, 'K' => 2, 'R' => 2,
'S' =>3, 'C' =>3, 'L' =>3, 'G' => 3,
'T' => 4, 'D' => 4, 'M' => 4,
'E' =>5, 'N' => 5, 'X' =>5, 'H' => 5,
'U' => 6, 'V' => 6, 'W'=>6,
'O' =>7, 'Z' => 7,
'F' =>8, 'P' => 8 ,
' ' => '' #for removing spaces
}

def initialize(name,date,month,year)
if name.nil? or name.empty?
raise "Please provide a name"
end
@name = name.upcase
@date = date.to_i
@month = month.to_i
@year = year.to_i
if @date == 0 or @month == 0 or @year == 0
raise "Please provide a valid date"
end
end

def calculate
puts "Name : #{@name} \n"
puts "Date : #{@date}/#{@month}/#{@year} \n"
sum = 0

#old version
#~ Range.new(0,@name.length).each do|n|
#~ #puts "char at #{n} = #{@name[n]}"
#~ #print @name[n]
#~ case @name[n]
#~ when ?A , ?J , ?I , ?Y , ?Q then sum = sum + 1
#~ when ?B , ?K , ?R then sum = sum + 2
#~ when ?S , ?C, ?L, ?G then sum = sum +3
#~ when ?T, ?D, ?M then sum = sum +4
#~ when ?E, ?N, ?X, ?H then sum = sum + 5
#~ when ?U, ?V, ?W then sum = sum + 6
#~ when ?O, ?Z then sum = sum + 7
#~ when ?F, ?P then sum = sum +8
#~ end
#~ while sum > 9
#~ sum = sum_digits(sum)
#~ end
#~ end

#replace the chararcters with thier numberic equivalent
#map contains key value pairs - A : 1, B : 2....
#replace each char (key in map) in the string with the value from the map
$map.each do |k,v|
@name.gsub!(k, v.to_s)
end

#find the sum of digits until the sum is single digit
sum = @name.to_i
while sum > 9
sum = sum_digits(sum)
end

#calculate sum value for date of birth
dateSum = @date + @month + @year
while dateSum > 9
dateSum = sum_digits(dateSum)
end

print "Name value :#{sum}\n"
print "Date value :#{dateSum}\n"

end

private
#sum the digits of the given number
def sum_digits(num)
sum = 0
quotient = num
while quotient > 0
#puts "sum : #{sum}, reminder: #{reminder}, quotient: #{quotient}"
reminder = quotient % 10
quotient = quotient / 10
sum = sum + reminder
end
return sum
end
end


if __FILE__ == $0
begin
num = Numerology.new(ARGV[0],ARGV[1],ARGV[2],ARGV[3])
num.calculate
rescue => ex
puts "Error : #{ex.message}"
end
end

Friday, March 28, 2008

Komodo may be the choice now....

I have been looking for an open source feature rich IDE for dynamic languages such as Python, ruby and Javascript. Now that ActiveState has released an opensource version of its Komodo, that may be become a primary choice for dynamic languages.

http://www.activestate.com/Products/komodo_ide/komodo_edit.mhtml

No more pydev - a huge junk of java for editing python files.

Thanks a lot ActiveState....

Sunday, February 10, 2008

GWT - FlexTable with frozen header

DataGrids displaying a huge set of data with header for each column would be better usable with its header row frozen.
Plain HTML tables, with frozen header are very simple to create.

  1. Create THEAD and TBODY elements.
  2. For TBODY element's style set overflow: auto or scroll;
  3. Set fixed size for the TBODY element
In GWT, a table created using either using Grid or FlexTable, creates only TBODY element and no THEAD element.
So to render a table with frozen header, in GWT we'll have to extend either Grid or FlexTable with a THEAD element and a few getter and setter methods for accessing the THEAD columns.



package name.aanand.magix.client;

import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Widget;


public class DataGrid extends FlexTable {

private Element head;
private Element headerTr;

public DataGrid() {
super();
head = DOM.createTHead();
headerTr = DOM.createTR();
DOM.insertChild(this.getElement(), head, 0);
DOM.insertChild(head, headerTr, 0);
Element tBody = getBodyElement();
DOM.setElementAttribute(tBody, "style", "overflow:auto;text-align: left;");
DOM.setElementAttribute(head, "style", "text-align: left;");

}

public void setHeight(String height) {
DOM.setElementAttribute(getBodyElement(), "height", height);
}

public void setHeader(int column,String text){
prepareHeader(column);
if (text != null) {
DOM.setInnerText(DOM.getChild(headerTr, column), text);
}
}

private void prepareHeader(int column) {
if (column < 0) {
throw new IndexOutOfBoundsException(
"Cannot create a column with a negative index: " + column);
}
int cellCount = DOM.getChildCount(headerTr);
int required = column + 1 - cellCount;
if (required > 0) {
addCells(head, 0, required);
}
}


public void setHeaderWidget(int column, Widget widget) {
prepareHeader(column);
if (widget != null) {
widget.removeFromParent();
// Physical attach.
DOM.appendChild(DOM.getChild(headerTr, column), widget.getElement());

adopt(widget);
}
}

private native void addCells(Element table, int row, int num)/*-{
var rowElem = table.rows[row];
for(var i = 0; i < num; i++){
var cell = $doc.createElement("td");
rowElem.appendChild(cell);
}
}-*/;