Saturday, November 15, 2008

Sorting Dictionary object in .Net

A few days ago I came across a sorting-dictionary problem at work. The easiest way to have a sorted dictionary in .Net is to use the SortedDictionary(TKey, TValue) item. Every time you enter a keyvaluepair in the SortedDictionary item, it will be sorted automatically.

But I had to sort an existing Dictionary(TKey, TValue) item, that was provided by some other program. Initially, I was hoping that there is some Dictionary.Sort() function available that I could use but there isn't.

After some research I found that you could use the List.Sort() function to sort the dictionary by keys. But that is not all. You also need to add a delegate in the List.Sort() function to carry out the sorting process.

Here is the code.

//Code takes a Dictionary<double,int> item, sorts it and return the new Dictionary<double,int> item

private static Dictionary<double,int> Sort(Dictionary<double,int> Dict)
{
//Create a List object from the Dictionary item
List<keyvaluepair<double,int>> result = new List<keyvaluepair<double,int>>(Dict);


//Use the delegate in the List.Sort function to compare the keys for sorting
result.Sort(
delegate(
KeyValuePair<double, int> first,
KeyValuePair<double, int> second)
{
return first.Key.CompareTo(second.Key);
}
);

//Add the List items into a new dictionary item
Dictionary<double,int> sortedDict = new Dictionary<double,int>();

foreach (KeyValuePair<double, int> kvp in result)
{

double key = kvp.Key;
int value = kvp.Value;

sortedDict.Add(key, value);
}

return sortedDict;
}



If there is a better way to achieve this, please let me know.

Tuesday, September 9, 2008

New Semester, New Challenges

The stint at ESRI ended this august. The Fall'08 semester has started and I have already started to wish that humans didn't have to sleep. Distributed Computing and Information Retrieval will be my focus of study this fall. While I like the Distributed Computing course and the technology that we will use :WCF, I am more excited about IR. Hope this will be an amazing learning experience.

Tuesday, April 29, 2008

Internship @ ESRI

I have been hunting for internships for a while. Due to the course load and other mundane activities I started my hunt a little late and hence was always under the impression that probably its too late now. But some companies contacted me and finally I got a full-time summer internship offer from ESRI . I am really excited about that and I hope that it will be one rewarding and learning experience.

Sunday, February 17, 2008

IR or Spam Filter

I haven't been updating my blog lately. The commencement of the spring 2008 semester has to do something with that but the real reason is that I have been busy researching some interesting topics for my AI project.

Last semester I wanted to create an Image Spam Filter for my "Data mining & Pattern Recognition" class. My theory was to apply OCR techniques to capture text from the image, after which it really becomes a text spam-filter problem. I was thinking of using Neural Networks for Image Text recognition and Bayes' Theorem for spam-filtering. But my idea was unanimously rejected by my group and instead we developed a Handwriting Recognition system (which was still a better project to work on than our previous plan to tell time by reading an image of analogue clock.)

So now I have a chance to have another go at my Image Spam Filter project. But I am still debating about it. The reason is that I am also fond of Information Retrieval problems. I am thinking of working on a Search engine and automatic indexing of a technical book/manual. Since this is an individual project I can do whatever I want (Of course my professor has to approve it.)

If I think about it, I find both, IR and Spam Filter, interesting. So it is not a clash of interest but more of what will I gain from them and what I want to do in future.

While I try to analyze this, feel free to give me your suggestions. Maybe you can give me an insight which may eventually help me reach a decision.

Monday, December 24, 2007

Single-row functions in ORACLE

Oracle 9i has many old and new single-row functions that are very useful. Some of them that I find very interesting are:



NVL(x1, x2)
NULLs are of great importance and I am pretty sure that I will get atleast one question on NULLs in the test. One of the ways that NULL values are handled in ORACLE is by using the function NVL(x1,x2). This function takes 2 values x1 and x2 and return x2 if x1 is NULL else it returns x1. Eg., the following query will return the employee name and salary+bonus for each employee:

SELECT ename, salary + NVL(bonus,0) FROM emp

NVL2(x1,x2,x3)
NVL2 is also used to handle NULL values. It takes 3 values (x1,x2,x3) and return x3 if x1 is NULL and x2 if x1 is not NULL. So we can rewrite the above query as follows:

SELECT ename, NVL2(bonus, salary+bonus, salary) from EMP

DECODE
Decode is used to incorporate IF.. THEN.. ELSE functionality in Oracle. Eg., the following query will display the employee name and the department name:

SELECT ename,
DECODE(deptno
, 10,'Accounting'
,20,'Administration'
,'Other')
FROM emp

- Raza

Datatype and Operators

Oracle has some interesting data types and operators. I don't use all of them while writing SQL queries but nevertheless they are helpful in database programming. Here are some of them:

Number(p,s)
'Number' is used for any numeric datatype. The s is for precision and s is for scale. p has a range from 1 to 38, while s can take values from -84 to 127. For example Number(4,2) can take values from -99.99 to 99.99. At first this datatype looks simple but it gets interesting when some different combinations of p and s are used. Here are some of them:

If s is greater than p, then the precision defines the maximum number of digits to the right of the decimal point after (s-p) zeros. For examples, Number(3,5) can take values from -0.00999 to 0.00999. Any number out of this range will generate error, e.g. 0.999 or 0.01 will result in raising an exception.

If s is negative then s zeros are added to the left of the decimal and the number is rounded. So if the datatype is Number(4,-2) and the value is 12355.5 then it will be changed to 12300. However 123555.5 will raise an error becuse the value should not have more than (s+p) digitgs.

Operators
I have used many operators in SQL queries but some of them that I don't use often are as follows:

ANY (or SOME) is used to compare a value to each value in a list or subquery. It must always be preceded by a comparision operator:

SELECT empno, empname FROM emp
WHERE deptno <=ANY(10,20,25) or, SELECT empname, salary FROM emp WHERE salary >=ANY( SELECT salary FROM emp
WHERE deptn0=10);


ALL operator is used to compare a value to every value in a list or subquery. It must be preceded by a comparision operator.

SELECT empno FROM emp
WHERE salary > ALL(SELECT salary FROM emp
WHERE deptno=10);

NOTE: '=ANY' is equivalent to IN and '!=ALL' is equivalent to NOT IN

EXISTS is also an interesting operator that I don't remember using in my academic or professional life but it can be very helpful under some circumstances. EXISTS is always followed by a subquery and is evaluated to TRUE if the subquery returns atleast one row.

SELECT e.empno, e.ename FROM emp e
WHERE EXISTS ( SELECT 1 FROM dept d
WHERE d.deptno= e.deptno
AND d.dname='Administration');

I hope you found this helpful. I'll keep adding useful things as I come across during my study.

- Raza





Oracle 9i Certification

I started using Oracle almost 8 years ago and I was hooked. I really liked the field of databases and to this day I like to write SQL qeries more than anything else. I have been using Oracle every now and then but I never got the chance to explore it in depth. So in order to increase my knowledge I made the decision to pursue Oracle 9i certification. I have a long way to go but the first step is to prepare for the 1Z0-007 Exam. I have the book and other material, so all I need to do is.... STUDY. I am hoping to take this test before January 18 when my Spring '08 semester starts .