Friday, April 24, 2009

Generate Power Ball numbers




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79

class Lottery
attr_reader :lotteryArray, :maxNumber, :powerBall, :maxPB
#attr_writer :maxNumber

def initialize(arraySize, maxNumber, powerBall, maxPB)
@lotteryArray = Array.new(arraySize)
@maxNumber = maxNumber
@powerBall = powerBall
@maxPB = maxPB
end

def getUniqueLottery
loop do
checkDuplicate = 0
numRand = getRandomNumber(@maxNumber)

for i in 0...@lotteryArray.length
if numRand == @lotteryArray[i] || numRand == 0
checkDuplicate = 1
break
end
end

if checkDuplicate == 0
return numRand
end

end # end of loop
end # getUniqueLottery

def buildLottery
for i in 0...@lotteryArray.length
@lotteryArray[i] = getUniqueLottery
end
end # buildLottery

def sortLottery
return @lotteryArray.sort
end

def printLottery
a = sortLottery
a.each do |num|
printf "%2s ", num
end

if @powerBall == "Y"
printf "\tPB: %2s \n", getPowerBall
end
end # printLottery

def getRandomNumber(number)
randNum = rand(number)
end # getRandomNumber

def getPowerBall
randNum = 0
until randNum > 0
randNum = getRandomNumber(@maxPB)
end
return randNum
end # getPowerBall
end

# To run program
#
# ruby -w lottery.rb 5
#
# where TotalTickets is number of tickets that generates

# start program
lotto = Lottery.new(5, 56, "Y", 43)

#for i in 1..$TotalTickets.to_i
for i in 0...ARGV[0].to_i
lotto.buildLottery
lotto.printLottery
end

Thursday, April 23, 2009

Syntax Color Highlight

After many tries, finally I got it to work. This is a ruby code to print out a string.
I am using coderay to do color highlighting.

To install coderay

sudo gem install coderay

To generate html type:

coderay -ruby -page < yourRubyProgram.rb > yourRubyProgram.rb.html




puts "To seek elegance rather than luxury"

Thursday, April 16, 2009

Install Oracle 11g on Ubuntu 8.10

Follow this link to install Oracle 11g on Ubuntu 8.10

Parsing US Census Data

The goal is to get State, State Code, Place FIPS code, City Name, Lat, and Lon from places2k.txt.
http://www.census.gov/geo/www/gazetteer/gazette.html

Here is the layout of place2k.txt

Columns 1-2: United States Postal Service State Abbreviation
* Columns 3-4: State Federal Information Processing Standard (FIPS) code
* Columns 5-9: Place FIPS Code
* Columns 10-73: Name
...
* Columns 144-153: Latitude (decimal degrees) First character is blank or "-" denoting North or South latitude respectively
* Columns 154-164: Longitude (decimal degrees) First character is blank or "-" denoting East or West longitude respectively

luan@luan-desktop:~/app/software/census_data$ head -5 places2k.txt
AL0100124Abbeville city 2987 1353 40301945 120383 15.560669 0.046480 31.566367 -85.251300
AL0100460Adamsville city 4965 2042 50779330 14126 19.606010 0.005454 33.590411 -86.949166
AL0100484Addison town 723 339 9101325 0 3.514041 0.000000 34.200042 -87.177851
AL0100676Akron town 521 239 1436797 0 0.554750 0.000000 32.876425 -87.740978
AL0100820Alabaster city 22619 8594 53023800 141711 20.472605 0.054715 33.231162 -86.823829

luan@luan-desktop:~/app/software/census_data$ cut -c -2,3-4,5-9,10-73,144-153,154-164 --output-delimiter="|" places2k.txt | head -5
AL|01|00124|Abbeville city | 31.566367| -85.251300
AL|01|00460|Adamsville city | 33.590411| -86.949166
AL|01|00484|Addison town | 34.200042| -87.177851
AL|01|00676|Akron town | 32.876425| -87.740978
AL|01|00820|Alabaster city | 33.231162| -86.823829

You can write C or Java to parse the above data, but it would take more time to write code for it.
If cut command does not serve your need, you should look at awk programming language before jump into C or Java.

Install ruby 1.9 on Ubuntu 9.04

First, remove any ruby version on the system:

sudo apt-get remove --purge ruby1.8 ruby1.9

-- package build dependencies (ruby)

sudo apt-get build-dep ruby1.9

-- get ruby 1.9 source
-- if you can't get it, try to download from
-- http://www.ruby-lang.org/en/

wget -c ftp://ftp.ruby-lang.org/pub/ruby/1.9/ruby-1.9.1-p0.tar.gz

-- extract ruby source using tar

tar -xvzf ruby-1.9.1-p0.tar.gz

-- go to extracted ruby code dir

cd ruby-1.9.1-p0/

-- execute configure
./configure --prefix=/opt/ruby-1.9.1-p0 --enable-pthread --enable-shared

-- execute make command
make

-- install process
sudo make install

Then I add the following line to the bottom of $HOME/.bash_profile

luan@luan-desktop:~$ cat .bash_profile
export OC4J_HOME=/opt/oc4j

export JAVA_HOME=/opt/java
export ECLIPSE_HOME=/opt/eclipse
export GROOVY_HOME=/opt/groovy

export GRAILS_HOME=/opt/grails
export TNS_ADMIN=/opt/oracle
export ORACLE_HOME=/u01/app/oracle/product/11.1.0/db_1
export RUBY_HOME=/opt/ruby

export NLS_LANG=AMERICAN_AMERICA.UTF8
export PATH=$ORACLE_HOME/bin:$RUBY_HOME/bin:$JAVA_HOME/bin:$OC4J_HOME/bin:$ECLIPSE_HOME:$GROOVY_HOME/bin:$GRAILS_HOME/bin:$PATH

export AWT_TOOLKIT=MToolkit

Create a link:

luan@luan-desktop:/opt$ ln -s /opt/ruby-1.9.1-p0 /opt/ruby

luan@luan-desktop:/$ cd

luan@luan-desktop:/$ s
ource .bash_profile

luan@luan-desktop:/$ ruby -v
ruby 1.9.1p0 (2009-01-30 revision 21907) [i686-linux]


If you are using ruby gem, you need to type:
sudo /opt/ruby/bin/gem