Code Comments

Code Comments

Tips and short tutorials on various programming technologies

Code Comments RSS Feed
 
 
 
 

Archive for August, 2008

Rank Numbers in Java

If you have a list of values in Java, you sometimes want to sort them. Other times, rather than sort them, you may want to determine the order that they would be sorted…or in other words rank them.
So if you create a list in this way:

ArrayList list = new ArrayList();
list.add(0);
list.add(3);
list.add(1);

…and sorted it, the list would [...] Read more »

How to Kill a Running Process in Linux

First you must determine the PID of the process you want to kill. This can be done usually pretty easily with the top command in Linux. When you type top at the command line, it shows you the processes that are consuming the most resources at the time on the system. Most likely the one [...] Read more »

Log Transformations in Python

It is very simple to do a log transformation in Python. Log transformations are sometimes performed on a set of numbers to smooth out the data (make it look more “Normal”), which can help in performing certain statistical analyses.
The default in Python is the natural log. So for demonstration purposes, I will first find e^10. [...] Read more »

What Is the R Statistical Package?

R is an open source statistical package that can be used to do complex analyses and produce publication quality graphics. It’s home page is at http://www.r-project.org.
This tool is very powerful but sometimes hard to figure out how to do simple things. For this reason, I will be posting some tips as I encounter such situations. Read more »

Rotate Axis Names on X-Axis in R

You use one of the par parameters to do this. For example:

x = c(3,4,2,5)
names(x) = c(“ABC”, “DEF”, “GHI”, “JKL”)
barplot(x, las=”2″)

See also: What Is the R Statistical Package Read more »

Copy Files Recursively in Python

import shutil
shutil.copytree(src, dst)

See also Copying Files in Python. Read more »

Copying Files in Python

To copy a file in Python, you would use the following syntax where src is the path to the file you are copying and dst is the path to which you want to copy the file.
import shutil
shutil.copy(src, dst)
If you want to copy files in a directory structure, you can use similar syntax where src is [...] Read more »

Convert Python List to a Delimited String

This is easy to do with the join command, though the syntax may not be immediately obvious.
So if you create a list:

x = ["abc","def","ghi","jkl"]

And wanted to convert that to a comma-separated list (for example, to store in a file), you would do this:

“,”.join(x)

And it would result in:

‘abc,def,ghi,jkl’

Or if you wanted it to be a tab-delimited [...] Read more »

Introduction to In-Memory Databases

I have been using a tool in programming projects for a couple years called SQLite. Essentially, this is a lightweight database with functionality similar to (but not as advanced as) relational database management systems like Oracle, SQL Server, or MySQL. One key difference is that SQLite databases reside either in memory or in a single [...] Read more »

Determine Whether a String Value is Numeric in Python

It is pretty easy to determine whether a String object is an integer in Python, but the functionality is a bit obscured by a misnomer. This can be done using a function called isdigit.
x = “123″
x.isdigit() // True
However, this has some limitations. It only works with deciphering positive integers. For example:
x = “1.23″
x.isdigit() // False
y [...] Read more »

How to Iterate Through a HashMap in Java

The HashMap class is a convenient way to store key-value pairs in Java without having to create a custom class to do this. It’s nice that you can iterate through it using the keys.

java.util.HashMap map = new java.util.HashMap();
map.put(“ABC”, “123″);
map.put(“DEF”, “456″);

for (String key : map.keySet())
  System.out.println(key + ” – ” + map.get(key));

This results in:
ABC – 123
DEF – [...] Read more »

Passing a Variable Number of Parameters to a Method in Java

If you have a list of objects that you want to pass to a method, you can always use an Array or a Collection of some sort (such as ArrayList). However, sometimes you want to be able to pass in a variable number of arguments without using these complex types in advance. This is easy [...] Read more »