Search This Blog

Wednesday, January 30, 2013

Interesting problems from Facebook hackers cup

Hi friends,
Yesterday i participated on a coding contest.. Felt it is worth sharing :)
Hope you enjoy it too...........

BEAUTIFUL STRINGS

When John was a little kid he didn't have much to do. There was no internet, no Facebook, and no programs to hack on. So he did the only thing he could... he evaluated the beauty of strings in a quest to discover the most beautiful string in the world.

Given a string s, little Johnny defined the beauty of the string as the sum of the beauty of the letters in it.

The beauty of each letter is an integer between 1 and 26, inclusive, and no two letters have the same beauty. Johnny doesn't care about whether letters are uppercase or lowercase, so that doesn't affect the beauty of a letter. (Uppercase 'F' is exactly as beautiful as lowercase 'f', for example.)

You're a student writing a report on the youth of this famous hacker. You found the string that Johnny considered most beautiful. What is the maximum possible beauty of this string?

Input
The input file consists of a single integer m followed by m lines.

Output
Your output should consist of, for each test case, a line containing the string "Case #x: y" where x is the case number (with 1 being the first case in the input file, 2 being the second, etc.) and y is the maximum beauty for that test case.


Constraints
5 ≤ m ≤ 50
2 ≤ length of s ≤ 500

Example input

5
ABbCcc
Good luck in the Facebook Hacker Cup this year!
Ignore punctuation, please :)
Sometimes test cases are hard to make up.
So I just go consult Professor Dalves

Example output

Case #1: 152
Case #2: 754
Case #3: 491
Case #4: 729
Case #5: 646


*********************************************************************************


BALANCED SMILEYS


Your friend John uses a lot of emoticons when you talk to him on Messenger. In addition to being a person who likes to express himself through emoticons, he hates unbalanced parenthesis so much that it makes him go :(

Sometimes he puts emoticons within parentheses, and you find it hard to tell if a parenthesis really is a parenthesis or part of an emoticon.

A message has balanced parentheses if it consists of one of the following:
- An empty string ""
- One or more of the following characters: 'a' to 'z', ' ' (a space) or ':' (a colon)
- An open parenthesis '(', followed by a message with balanced parentheses, followed by a close parenthesis ')'.
- A message with balanced parentheses followed by another message with balanced parentheses.
- A smiley face ":)" or a frowny face ":("

Write a program that determines if there is a way to interpret his message while leaving the parentheses balanced.

Input
The first line of the input contains a number T (1 ≤ T ≤ 50), the number of test cases. The following T lines each contain a message of length s that you got from John.

Output
For each of the test cases numbered in order from 1 to T, output "Case #i: " followed by a string stating whether or not it is possible that the message had balanced parentheses. If it is, the string should be "YES", else it should be "NO" (all quotes for clarity only)

Constraints
1 ≤ length of s ≤ 100

Example input

5
:((
i am sick today (:()
(:)
hacker cup: started :):)
)(

Example output
Case #1: NO
Case #2: YES
Case #3: YES
Case #4: YES
Case #5: NO


*********************************************************************************

FIND MIN


After sending smileys, John decided to play with arrays. Did you know that hackers enjoy playing with arrays? John has a zero-based index array, m, which contains n non-negative integers. However, only the first k values of the array are known to him, and he wants to figure out the rest.

John knows the following: for each index i, where k <= i < n, m[i] is the minimum non-negative integer which is *not* contained in the previous *k* values of m.

For example, if k = 3, n = 4 and the known values of m are [2, 3, 0], he can figure out that m[3] = 1.

John is very busy making the world more open and connected, as such, he doesn't have time to figure out the rest of the array. It is your task to help him.

Given the first k values of m, calculate the nth value of this array. (i.e. m[n - 1]).

Because the values of n and k can be very large, we use a pseudo-random number generator to calculate the first k values of m. Given non-negative integers a, b, c and positive integer r, the known values of m can be calculated as follows:
m[0] = a
m[i] = (b * m[i - 1] + c) % r, 0 < i < k

Input
The first line contains an integer T (T <= 20), the number of test cases. This is followed by T test cases, consisting of 2 lines each. The first line of each test case contains 2 space separated integers, n, k (1 <= k <= 105, k < n <= 109). The second line of each test case contains 4 space separated integers a, b, c, r (0 <= a, b, c <= 109, 1 <= r <= 109).

Output
For each test case, output a single line containing the case number and the nth element of m.

Example input

5
97 39
34 37 656 97
186 75
68 16 539 186
137 49
48 17 461 137
98 59
6 30 524 98
46 18
7 11 9 46

Example output

Case #1: 8
Case #2: 38
Case #3: 41
Case #4: 40
Case #5: 12

Saturday, January 26, 2013

Freeing your RAM

Hi friends,
I was working on a server and suddenly the machine seemed to kind of slow. I guessed that my RAM was full and checked it. And as i suspected its free memory was less. I was shocked just by seeing the total used memory, it was way too high. I tried to trace down the processes but nothing seemed to consume so much.

   So i was assuming that one of the open source application which i was using had some memory leakage. So i wanted to make an investigation on that. All my attempts were failure. So from the experiment i understood the following about memory management in linux.

1. When ever some process is executed after usage it is cached to local storage
2. And this cached memory is not freed automatically.

So i found it, that was the culprit. This cached memory is the one that was occupying the RAM and RAM appeared to be full. So now i would be sharing you the command that would clear the cached memory in RAM and gives your system some space to breathe.

Here it is,

# sync; echo 3 > /proc/sys/vm/drop_caches

Sunday, January 6, 2013

Getting more info on segmentation fault

Hi all,
This is just a very small post which might be useful to you.. Mostly when you develop some codes, especially in some native languages like C, C++, sometimes you face some error "Segmentation Fault (Core Dumped)". This post is all about how we get more info on this when you are working on Linux.

Has anyone wondered what is that called "Core Dumped" that appears after segmentation fault ? and where is it dumped ? :) It is not dumped anywhere unless you enable it.. So we shall learn how to enable it....

Step 1: Change your current directory to temp

# cd /tmp

Step 2: Enable core dump

# ulimit -c unlimited

Step 3: Execute your program in which you face segmentation fault

For example,

# /opt/myprogram.out

It produces,

Segmentation Fault (core dumped)

Thats it, now you have core dumped in /tmp/core file. You can open it with your favorite editor (I prefer vi editor, as other editors crash mostly when you open such binaries).

Note:
The file is a binary and it has more junk info.. parse through the file to check some errors somewhere..

Saturday, December 22, 2012

Accessing Eucalyptus 2.x DataBase


Hi,
I just thought this might be useful, if anyone who wants to play around with eucalyptus 2.x database. In these version eucalyptus uses HSQLDB as its database.

Step 1: Get into the following folder and we need to get the eucalyptus database password

<installed eucalyptus folder>/var/lib/eucalyptus/db/
cat eucalyptus_general.script | grep "CREATE USER SA PASSWORD"

Copy only the password part from it

Example if the output is

CREATE USER SA PASSWORD "81564841F531D0ED828B825158DA0C56723113C49D4E259B6C4CCC1E698934A1F3321062B5187BC76518F85C63FAA4D051A4BE072A093191759C4AF3B5E477576182C49AF8994C115963F3AEC78A706601A17AF2ABE22EC7398CBB046E705743F620ED990B0642196888A0684F49AD2EEF8D34A2F2FA1B5A0D7B231EBD07253AB98F8B9E97E22B0FD6612C37ED666A122ADFD1DCB740478F3CD46AB4F2350E956B7957DAA45EFCD30C9D04A048711A01FC1C2DA7557634C357B5AD9266A18CD4A071670E873651A7E77286E22AEE0736B892EEACE22C1A9E15AD113B9EBC43031EE0AB4856768443B4A3EC32A27CAD37627BDAFBB0C75822E7E58A7C3CD38667"

then password is

81564841F531D0ED828B825158DA0C56723113C49D4E259B6C4CCC1E698934A1F3321062B5187BC76518F85C63FAA4D051A4BE072A093191759C4AF3B5E477576182C49AF8994C115963F3AEC78A706601A17AF2ABE22EC7398CBB046E705743F620ED990B0642196888A0684F49AD2EEF8D34A2F2FA1B5A0D7B231EBD07253AB98F8B9E97E22B0FD6612C37ED666A122ADFD1DCB740478F3CD46AB4F2350E956B7957DAA45EFCD30C9D04A048711A01FC1C2DA7557634C357B5AD9266A18CD4A071670E873651A7E77286E22AEE0736B892EEACE22C1A9E15AD113B9EBC43031EE0AB4856768443B4A3EC32A27CAD37627BDAFBB0C75822E7E58A7C3CD38667

Step 2: Next we can use the hsqldb manager to access database

# <installed eucalyptus folder>/usr/share/eucalyptus
java -cp log4j-1.2.15.jar:eucalyptus-db-hsqldb-ext-2.0.2.jar:hsqldb-1.8.0.10.jar:proxool-0.9.1.jar:ehcache-core-1.7.2.jar:commons-logging-1.1.1.jar org.hsqldb.util.DatabaseManager

Note: The above line invokes the hsqldb manager. Make sure all the jar files mentioned above exists in the current folder. With each version of eucalyptus, there may be some upgrade in jar files version too. The above line is for eucalyptus 2.0.2
          Also note that the manager may still be invoked if some jar files are missing, but it may not let you log into the database.


Step 3: Fill in the following details in the manager window that pops up

Type: HSQL Database Engine In-Memory
Driver: org.hsqldb.jdbcDriver
URL: jdbc:hsqldb:hsqls://localhost:9001/eucalyptus_general
user: SA
password: <copied in step 1>

In the above URL eucalyptus_general is one of the databases used by eucalyptus. The list of database names are given below.

1. eucalyptus_storage
2. eucalyptus_auth
3. eucalyptus_config
4. eucalyptus_general
5. eucalyptus_dns
6. eucalyptus_images
7. eucalyptus_walrus
8. eucalyptus_records

Step 4: Click on "OK" to login and a window pops up where you can write sql queries to view/edit them.

Accessing Eucalyptus 3.1/3.2 DataBase

Hi Folks,
There may be a need to open up Eucalyptus 3.1/3.2 DataBase in-order either to tweak, or add new features based existing schema ( suggested for good practices :P). So let me tel you the step by step guide to open it up.

Step 1: We don't have the password generated by eucalyptus at the time of initialization of the cloud. So first we make a small tweak to find the secret password

# cd <installed eucalyptus directory>/etc/eucalyptus/cloud.d/scripts
# nano setup_db.groovy

Get to line no: 138 
Next to the line containing
LOG.debug("Postgres 9.1 command : " + args.toString( ) )

Add these lines

final File passFile1 = new File("<installed eucalyptus directory>/password.txt")
passFile1.write( getPassword() )

Step 2: Restart eucalyptus cloud

# <installed eucalyptus directory>/etc/init.d/eucalyptus-cloud restart

Step 3: Now open the file password.txt and this is your password for the database access

# cat <installed eucalyptus directory>/password.txt

Step 4: Next we can straight away try to access the database by doing the following prerequisites

# chmod 777 <installed eucalyptus directory>/var/lib/eucalyptus/db/
# chmod 777 <installed eucalyptus directory>/var/lib/eucalyptus/db/data
# chmod 777 <installed eucalyptus directory>/var/lib/eucalyptus/db/data/.s.PGSQL.8777

# su postgres
# export PGPASSWORD="<password displayed/copied in step 3>"

Step 5: Finally we can use different set of psql commands to view/use the eucalyptus database

example to list the databases present

# psql -l -p 8777 -h <installed eucalyptus directory>/var/lib/eucalyptus/db/data/ -U eucalyptus

example to open on of its database

# psql -p 8777 -h <installed eucalyptus directory>/var/lib/eucalyptus/db/data/ -U eucalyptus eucalyptus_general

Thats it this how you can play around with the eucalyptus database. (version 3.1/3.2)
This can extend to next to eucalyptus versions too.. :)

Tuesday, December 11, 2012

Reverse proxy using apache2

Hi all, once again a step by step instruction on how to make your apache2 software work as a reverse proxy as well.

The steps here are for debian or ubuntu based distro. Replacing some system commands for other distros must give you a similar response.

Step 1: Install apache2

# apt-get install apache2

Step 2: Install modprxoy

# apt-get install libapache2-mod-proxy-html

Step 3: Edit httpd.conf

# nano /etc/apache2/httpd.conf

and paste the following contents

  LoadModule proxy_module /usr/lib/apache2/modules/mod_proxy.so
  LoadModule proxy_http_module /usr/lib/apache2/modules/mod_proxy_http.so
  Include /etc/apache2/proxy.conf

The above step loads modproxy modules into apache2 and includes the reverse proxy mapping file

Step 4: Create a file proxy.conf

# nano /etc/apache2/proxy.conf

and paste the following data (with editting path for your application)

  ProxyPass  /<name to map> http://<host ip>:<port>/<application>
  ProxyPassReverse  /<name to map> http://<host ip>:<port>/<application>

Step 5: Restart apache2

# /etc/init.d/apache2 restart

Thats it your application which was accessed via specific port is now accessible via apache2 default port with the mapped name. :)

Thursday, October 25, 2012

Accessing XCP through libvirt API

Libvirt has enhanced its accesability to XCP or XenServer via its API. Inorder to use this feature you must compile libvirt from source with this feature enables. below are the steps to compile libvirt and its dependencies.

Step 1: There are two modes of accessing XCP via libvirt.
            * From the same machine where XCP is installed
            * On a remote machine

            But i would advice you to install libvirt on the same machine where XCP is installed as some native codes might require direct or local access.

Now,

Log in to the machine where you want to install libvirt.

Step 2: First you need to download and install xenapi package. Download xenapi package (libxenserver) from the following URL

ftp://193.166.3.2/pub/NetBSD/packages/distfiles/libxenserver-5.6.100-1-src.tar.bz2

Step 3: Open a terminal and extract the source

# cd <directory where it is downloaded>
# tar jxvf libxenserver-5.6.100-1-src.tar.bz2
# cd libxenserver

Step 4: Compile libxenserver. To compile you must have installed the following dependencies

* libxml2-dev
* libcurl3-dev
* zlib1g

in a debian/ubuntu based machine you can install like this

# apt-get install libxml2-dev
# apt-get install libcurl3-dev
# apt-get install zlib1g

There is a small change you have to make in the 'Makefile'

# vi Makefile

find the line (line no: 65)

$(TEST_PROGRAMS): test/%: test/%.o libxenserver.so
        $(CC) $(LDFLAGS) -o $@ $< -L . -lxenserver

and replace it as

$(TEST_PROGRAMS): test/%: test/%.o libxenserver.so
        $(CC) -o $@ $< -L . -lxenserver $(LDFLAGS)

Also find (line no: 73)


$(INSTALL_PROG) libxenserver.so.$(MAJOR).$(MINOR) $(DESTDIR)/usr/$(LIBDIR)

replace with

$(INSTALL_DATA) libxenserver.so.$(MAJOR).$(MINOR) $(DESTDIR)/usr/$(LIBDIR)

and then compile

# make LIBDIR=lib/
# ar rcs libxenserver.a
# make install LIBDIR=lib/

Step 5: Download libvirt from

http://libvirt.org/sources/libvirt-0.10.2.tar.gz

Step 6: Configure and compile libvirt

# cd <directory where it is downloaded>
# tar zxvf libvirt-0.10.2.tar.gz
# cd libvirt-0.10.2
# ./configure --with-esx --with-xenapi=<path to libxenserver that you installed earlier>  --prefix=/opt/libvirt

Here --prefix=/opt/libvirt is optional if you dont want it to install in system folders like /usr /bin ,etc

At the end of the above step, we must be able to identify that if libxenserver is included or not, from the output

NOTE:

1. While configuring if you find some errors that some package is missing, then install that package and reconfigure again.

eg:

configure: error: You must install device-mapper-devel/libdevmapper >= 1.0.0 to compile libvirt

Here you must install the package

libdevmapper-dev

Installation always require a development version, even though if it does not point it

2. Even though we have already installed libxenserver, sometimes the compiler cannot find LINK to libxenserver files

you can try the following solutions

# ldconfig

If this does not work, change the following code in "configure" file

# vi configure

Search for the line

        LIBXENSERVER_LIBS="$LIBXENSERVER_LIBS -lxenserver"

Scroll below to find "else" part like this

        if test "$with_xenapi" = "yes"; then
            fail=1
        fi
            with_xenapi=no

Replace it with

        with_xenapi=yes
        LIBXENSERVER_LIBS="$LIBXENSERVER_LIBS -lxenserver"

And then run the above command again.

Next to compile,

# make
# make install

You can also check the api capabilities with respect to xenapi from the following URL.

http://libvirt.org/hvsupport.html

This concludes our step by step installation of libvirt with xenapi support. :)