Monday, March 25, 2013

Installing node.js in Linux

I tested these steps in RHEL 5.4 and Ubuntu 12.10.

  1. wget http://nodejs.org/dist/v0.10.1/node-v0.10.1.tar.gz
  2. tar -xvf node-v0.10.1.tar.gz
  3. cd node-v0.10.1
  4. ./configure --prefix=<a directory to which you have write and execute access>
  5. make
  6. make install
  7. Create a file, say Test.js and add the following lines to it
    var http = require('http');

    http.createServer(function (request, response) {
      response.writeHead(200, {'Content-Type': 'text/plain'});
      response.end('Hello World\n');
    }).listen(8124);

    console.log('Server running at http://127.0.0.1:8124/');
  8. node Test.js
  9. Open a browser and visit http://127.0.0.1:8124/. If all went fine, you should see Hello World in the browser.
It is as simple as that. I did not hit any road block at all. They all went fine.

Sunday, October 21, 2012

Next Palindrome after a 1000000 digit number - SPOJ - PALIN

I always have afraid of the problems which require string processing, to be solved. This is one such problem. The problem description as stated in SPOJ (http://www.spoj.pl/problems/PALIN/) and my solution to this problem is http://ideone.com/osqLhp (I am sure, I can improve this code)

A positive integer is called a palindrome if its representation in the decimal system is the same when read from left to right and from right to left. For a given positive integer K of not more than 1000000 digits, write the value of the smallest palindrome larger than K to output. Numbers are always displayed without leading zeros.


Input

The first line contains integer t, the number of test cases. Integers K are given in the next t lines.

Output

For each K, output the smallest palindrome larger than K.

Example

Input:
2
808
2133

Output:
818
2222


We know that, there is no integral data type which is as sophisticated as to accommodate a 1000000 digits number (a million digits in a number). So only option is to use string. And we also know what palindrome is. A number or a string of characters, is the same when expressed from left to right and right to left. For example, AMMA, MADAM, MALAYALAM, 11, 505, 797979797, ...

Lets assume the input number as N. If we read through the problem, we are asked to find the next (greater than the number provided) palindrome which exists after the number N. As we see in the examples, 818 is the next big palindrome after 808 and 2222 is the next big palindrome after 2133. If the numbers are small, we can try brute-force approach.

Brute force approach:


  1. Read the number N
  2. Increment by 1
  3. Check whether it is a palindrome or not
  4. If Yes, Print the number and exit
  5. If No, goto step 2

This approach is quite simple and easy to implement. But, this may not work always when we have time constraints. If the number N becomes as big as a 1000000 digit number, program with this approach would produce output after a very long time. We all want our programs to produce outputs so quickly, don't we? Let us look at the approach which I followed to solve this problem.

My approach:

  1. Read the number as a string and store it in STR and the number of digits as N
  2. Split STR into two halves STR1 and STR3, if the number of digits in STR is odd, the middle digit will be in STR2 otherwise it is empty
  3. If the Reverse of STR1 is greater than STR3, Print STR1 + STR2 + Reverse (STR1) and exit
  4. make all digits of STR3 as '0'
  5. If N is even, 
    1. increment STR1
    2. Goto step 1 with input (Incremented STR1 + STR3)
  6. If N is odd, 
    1. increment STR2
      1. If Incremented STR2 is 10, 
        1. set STR2 as "0"
        2. increment STR1
        3. Goto step 1 with input (Incremented STR1 + STR2 + STR3)
      2. If Incremented STR2 is lesser than 10
        1. Goto step 1 with input (STR1 + STR2 + STR3)
The observation which I made after analyzing few examples is, if the second half of the number is smaller than the reverse of the first half of the number, just replacing the second half with the reverse of the first half produces the required output. If it is not smaller than the reverse of the first half, increment the first half and then replace the second half with the incremented first half. This is the basic idea but the above algorithm includes all the corner cases.

My solution:

Infix Expression to Postfix (RPN) Expression Conversion - SPOJ - ONP

This was one of our lab exercises in my Engineering first year. That was Data Structures lab. Guess what, this program was agreed as the toughest program of all the programs, by the whole class (our class strength was 63), which we were supposed to finish in that semester and the teacher had the whole program written on the blackboard and later we typed, compiled and executed on the computers. I remember the days, in which I used to memorize the program which solved the same problem for the lab exams. Nostalgic!!! Now, this took me lesser than 15 minutes to get "accepted" in SPOJ :)

Here is the problem statement, as stated in SPOJ (http://www.spoj.pl/problems/ONP/). My solution to the same http://ideone.com/wCfryC

Transform the algebraic expression with brackets into RPN form (Reverse Polish Notation). Two-argument operators: +, -, *, /, ^ (priority from the lowest to the highest), brackets ( ). Operands: only letters: a,b,...,z. Assume that there is only one RPN form (no expressions like a*b*c).

Input

t [the number of expressions <= 100]
expression [length <= 400]
[other expressions]
Text grouped in [ ] does not appear in the input file.

Output

The expressions in RPN form, one per line.

Example

Input:
3
(a+(b*c))
((a+b)*(z+x))
((a+t)*((b+(a+c))^(c+d)))

Output:
abc*+
ab+zx+*
at+bac++cd+^*

Store and read static files in Android

Recently I was writing an Android application, in which I had to read data from a static text file. I did not know where to keep the file and how to refer that file in the program (in windows/*nix OS we used to provide path to the file in file system). After surfing the internet enough, found a solution to this problem.

Note: This post helps you to read from a static application specific internal files.

1) The IDE which I used to develop Android applications is Eclipse and I assume that you already have an Android project opened and the static file to be read.

2) The name of the static file which I am going to use in this example is dictionary.txt

3) In the directory structure of an Android project, there will be a directory called "res". Create a directory called "raw" inside the "res" directory and import the file to be read in that directory. All the files added to the raw folder will be read-only. (And I don't think we can refer them as files anymore. So that data can be read as shown in step 6). The directory structure might look like this

4) After adding the file, just Build the Project and make sure that build is successful.

5) Now, in the "gen" directory, inside your package, Open the R.java file. You might get to see something similar to this

    public static final class raw {
        public static final int dictionary=0x......;
    }


6) Now, this static file will be included in the ".apk" file. As to read from the file, the following piece of code would do that job. You may have to add the appropriate try-catch blocks.

BufferedReader filereader = new BufferedReader(
   new InputStreamReader (ctx.getResources().openRawResource 
   (R.raw.dictionary)));

String lineread = "";
while ((lineread = filereader.readLine()) != null)
{
   System.err.println ("Read " + lineread);
}
filereader.close();

7) In order to view the files in development environment or emulator itself, there is a perspective called DDMS in Eclipse, just switch to that and have your Android Virtual Device started.


8) The application specific files can be found in data/data/<your package name>/files

Saturday, October 20, 2012

Making Lync 2010 to work in Android

Lately I downloaded Lync 2010 from Android Market. But I was not able to make it to connect to my corporate network. I was sure that there is some setting which has to be done properly but not sure what that is. After surfing internet for a while, landed on this page. http://support.microsoft.com/kb/2636313

This is how I made it to work.

1) Downloaded and installed Lync 2010 from Google Play Store in my Android Gingerbread.
2) Opened Lync 2010
3) Typed my username and password properly. The username should be typed completely, for example, someone@example.com
4) Now press the menu button to be able to select the "Options" option.
5) It will open up a page like this
6) Most of the cases, Lync detects the server details automatically. So lets not worry about the server settings now. Press the downward arrow which corresponds to the User name box, which will pull up a simple page like this.
7) This is the part which was driving me crazy and took three days to figure out. You have to provide the username along with the complete domain name. The most important thing is, you need to use a backward slash (\) to separate the domain name and username. My gingerbread did not show (\) character on this page. So I had to copy it from the browser where I was able to type that character. For example, domain\someone

I helped many of my friends using Lync this way, on Android and it works on iTouch also.