Transform A,B,…AA,AB,.. to 1,2,..27,28,..

NOTE: Below code is Python 3.

base = 26
def transform(row):
    res = []
    for field in [tmp.strip() for tmp in row.split(',')]:
        ival = 0
        power = 0
        for c in field[::-1]:
            ival += pow(base,power)*(ord(c)-ord('A')+1)
            power += 1
        res.append(ival)
    return res


print(transform("A, B, Z, AA, AB, AAA"))


Output:

[1, 2, 26, 27, 28, 703]

apachectl status – How to solve lynx: command not found

apachectl status” will not work if you don’t have lynx ; text-based web browser.

You realize that when you get below message while using the command.

Error
bin/apachectl: line 95: lynx: command not found

Install the lynx and tell it to Apache httpd, he will surely understand.

Download the lynx from here. Change your working directory to the extracted folder and run the command:

./configure --prefix=<path-to-install-lynx>
make
make install

During lynx installation, it may stop with an error:

configure: error: No curses header-files found

Run the below command before proceeding:

sudo yum search ncurses-devel

Now it’s time to tell httpd about lynx.

In apachectl file (either in bin or conf directory), change the line starting with LYNX=”lynx -dump” to LYNX=”<lynx-installed-path>/lynx -dump”

apachectl status will start working..

Apache httpd: APR not found; error while installation

During Apache httpd installation if you get below error, that means Apache Portable Run-time environment was not found or not installed.

checking for APR... no
configure: error: APR not found.  Please read the documentation.

and/or

checking for APR-util... no
configure: error: APR-util not found. Please read the documentation.

So install APR and APR-util and give httpd those paths. Httpd will be happy to run for you after that… 🙂

Go further if you want to know how to install APR.

You can download APR and APR-util from here.

Then extract those to separate folders.

First change to the extract APR directory and install it. (Tip: cd command to change directory)

sudo ./configure --prefix=<apr-path>

sudo make

sudo make install

So you have a working APR. Next is APR-util.

Change to the extract APR-Util directory and install it. (Tip: cd command to change directory)

sudo ./configure --prefix=<apr-util-path> --with-prefix=<apr-path>

sudo make

sudo make instal

Install zlib packages with the command (optional; you can turn this off at your will):

yum install zlib zlib-devel

Apart from APR and APR-Util, you may need to install pcre too. So download it from here first.

Change to the directory and use the below commands to install.

sudo ./configure --prefix=<pcre-path>

sudo make

sudo make install

Now lets make httpd understand that we have a working pcre, APR and APR-util.

./configure --enable-file-cache --enable-cache --enable-disk-cache --enable-mem-cache --enable-deflate --enable-expires --enable-headers --enable-usertrack --enable-cgi --enable-vhost-alias --enable-rewrite --enable-so --prefix=<httpd-path> --with-apr=<apr-path> --with-apr-util=<apr-util-path> --with-pcre=<pcre-path>

# Rest is history make make install

Change the Java (JVM) used by tomcat.

Enterprise systems will have different versions of Java installed in it. A system administrator may have to set up Tomcat to use a particular one out of it. By default Tomcat uses the OS default; ie. the one installed into OS path. So how can we make it run with another version of Java.

This is easily achieved using the below commands(Open a terminal and run the below commands, don’t close it.):

export JAVA_HOME=<path-to-java>

or

export JRE_HOME=<path-to-java>

Run the start-up script from the same terminal and you will see from the logs that Tomcat picked up the Java from the path you set.

Notes:

JRE_HOME is set to JAVA_HOME if not set. So setting any of these variables suffice.

Shared folder for Fedora VM in VirtualBox

Hi,

I found a little hard to setup a shared folder for my VMs. So thought of sharing it..

So, assuming you already have your host machine, VirtualBox installed on it and a VM running Fedora (any linux), there we go..

Highlight your VM in VirtualBox window.

Screen Shot 2015-03-17 at 3.40.41 pmYes, it is powered off. Bring up the settings window for the VM clicking the below icon.

Screen Shot 2015-03-17 at 3.41.02 pmChoose the “Shared Folders” tab and you will see the list of folders shared with VM.Screen Shot 2015-03-17 at 3.41.22 pmClick on the plus sign you see on the left side and you get another window like the one below.

Screen Shot 2015-03-17 at 3.41.44 pmYou may choose the folder of the host machine you want to share and tick the “Auto-mount” option. Click OK.

Screen Shot 2015-03-17 at 4.12.42 pm

The folder you chose to share is now visible and we are done with the VirtualBox settings. Click OK to come out of it.

Screen Shot 2015-03-17 at 4.13.05 pm

Now boot up the VM. We need to install a few packages in Fedora for this. You may use below command to install the same.

Screen Shot 2015-03-17 at 5.11.50 pmThis will install latest gcc and kernel-devel packages. Guest addition may get arrogant, at times and gives error that it can’t find a particular version of kernel-devel. I will explain how to solve that later.

Now we can install “Guest Addition”

Click on the “Devices” menu and select the below highlighted option.Screen Shot 2015-03-17 at 4.15.35 pmSome OS automatically run the autorun script from CD. Otherwise open a file-browser and open the CD contents. You will see “autorun.sh” file there. Drag and drop it to Terminal. It will first authenticate you and start installing “Guest Addition”.

Screen Shot 2015-03-17 at 4.59.33 pmScreen Shot 2015-03-17 at 4.53.26 pm

You will get below output 99% of the times and that means just a reboot and you have a host-folder available in VM.

Screen Shot 2015-03-17 at 5.26.14 pm

The shared folder will be auto-mounted in /media.

Screen Shot 2015-03-17 at 5.30.02 pmThe 1% I left earlier is the case when “Guest Addition” fails to install with an error about the kernel-devel version. You may get below screen in such cases.

Screen Shot 2015-03-17 at 4.54.16 pmIn such cases you get a command from the above output.

yum install kernel-devel-3.17.4-301.fc21.x86_64

Just run the below part out of it and do the “Guest Addition” installation again and you are good to go..

yum install kernel-devel-3.17.4

Now let me take you through another issue you may face. Your folder will get listed in /media. But you may not have permission to open it. In my case, “Shared” is the shared-folder. /media/sf_Shared is the path in VM where it is mounted.

ls /media/sf_Shared

The above command will give an output like this:Screen Shot 2015-03-18 at 9.48.20 pm

This is because of the difference of UID in host and VM machines.

Screen Shot 2015-03-18 at 9.24.30 pm

The easiest way to sort this out is to add the user account to vboxsf.

usermod -a -G vboxsf

I went for a reboot (of VM) after that and things worked well.

May be you want to do this too.

Create a link in your home directory to access the shared directory mounted in /media. Make sure your home directory does not have “Shared” folder already.

ln -s /media/sf_Shared ~/Shared

So now you may use ~/Shared as your window to the host-machine..

Clone vdi image (.vdi) without error

Clone vdi image (.vdi) without the UID error.

Use the below command via terminal.

VBoxManage clonevdi <ImageYouWantToCopy.vdi> <DestinationImage.vdi>

If you get below error, that means one of your VM is using the .vdi image. Shutdown that VM and run the command again.

VBoxManage: error: Failed to lock source media 'ImageYouWantToCopy.vdi'

VBoxManage: error: Details: code VBOX_E_INVALID_OBJECT_STATE (0x80bb0007), component Medium, interface IMedium, callee nsISupports

VBoxManage: error: Context: "CloneTo(dstDisk, ComSafeArrayAsInParam(l_variants), NULL, progress.asOutParam())" at line 740 of file VBoxManageDisk.cpp

Google Code jam Solutions: Problem A. Store Credit

Problem

You receive a credit C at a local store and would like to buy two items. You first walk through the store and create a list L of all available items. From this list you would like to buy two items that add up to the entire value of the credit. The solution you provide will consist of the two integers indicating the positions of the items in your list (smaller number first).

Input

The first line of input gives the number of cases, N. N test cases follow. For each test case there will be:

  • One line containing the value C, the amount of credit you have at the store.
  • One line containing the value I, the number of items in the store.
  • One line containing a space separated list of I integers. Each integer P indicates the price of an item in the store.
  • Each test case will have exactly one solution.

Output

For each test case, output one line containing “Case #x: ” followed by the indices of the two items whose price adds up to the store credit. The lower index should be output first.

Limits

5 ≤ C ≤ 1000
1 ≤ P ≤ 1000

Small dataset

N = 10
3 ≤ I ≤ 100

Large dataset

N = 50
3 ≤ I ≤ 2000

Sample

Input Output
3
100
3
5 75 25
200
7
150 24 79 50 88 345 3
8
8
2 1 9 4 4 56 90 3
Case #1: 2 3
Case #2: 1 4
Case #3: 4 5

C++ Solution:

/*
Author      :   Sreejith Sreekantan
Description :   https://code.google.com/codejam/contest/351101/dashboard#s=p0

*/

#include 
#include 
#include 
#include 

using namespace std;

int main(int argc, char const *argv[])
{

    int numOfTestInstances;
    cin >> numOfTestInstances;
    std::vector itemWeight;
    for (int testInstanceNum = 0; testInstanceNum < numOfTestInstances; ++testInstanceNum)
    {
        int limit;
        cin >> limit;

        int numOfItems;
        cin >> numOfItems;

        itemWeight.clear();
        itemWeight.reserve(numOfItems);

        for (int itemNum = 0; itemNum < numOfItems; ++itemNum)
        {
            cin >> itemWeight[itemNum];
        }

        cout << "Case #" << testInstanceNum + 1 << ": ";

        for (int i = 0; i < numOfItems; ++i)
        {
            for (int j = i + 1; j < numOfItems; ++j)
            {
                if (itemWeight[i] > limit)
                {
                    break;
                }
                if (itemWeight[j] > limit)
                {
                    continue;
                }
                if (itemWeight[i] + itemWeight[j] == limit)
                {
                    if (i < j)
                    {
                        cout << i + 1 << " " << j + 1 << endl;
                    }
                    else
                    {
                        cout << j + 1 << " " << i + 1 << endl;
                    }
                }
            }
        }

    }
    return 0;
}

Python Solution:

#! /usr/bin/python

numOfInstances = int(raw_input())

for x in xrange(1,numOfInstances+1):
    limit = int(raw_input())
    numOfItems = int(raw_input())
    d = dict()
    items = [int(x) for x in raw_input().split()]
    for y in xrange(1,numOfItems+1):
        if items[y-1] not in d:
            d[items[y-1]] = set() 
        d[items[y-1]].add(y)

     print d

    for x in d.keys():
        if x <= limit:
            index = d[x].pop()
            if (limit-x) in d and len(d[limit-x])>0:
                index2=d[limit-x].pop()
                d[limit-x].add(index2)
                print min(index, index2), max(index, index2)
            d[x].add(index)


Google Code jam Solutions: Problem B. Reverse Words

Problem

Given a list of space separated words, reverse the order of the words. Each line of text contains L letters and W words. A line will only consist of letters and space characters. There will be exactly one space character between each pair of consecutive words.

Input

The first line of input gives the number of cases, N.
N test cases follow. For each test case there will a line of letters and space characters indicating a list of space separated words. Spaces will not appear at the start or end of a line.

Output

For each test case, output one line containing “Case #x: ” followed by the list of words in reverse order.

Limits

Small dataset

N = 5
1 ≤ L ≤ 25

Large dataset

N = 100
1 ≤ L ≤ 1000

Sample

Input Output
3
this is a test
foobar
all your base
Case #1: test a is this
Case #2: foobar
Case #3: base your all

C++ Solution:

/*
    Author      :   Sreejith Sreekantan
    Description :   Problem B. Reverse Words https://code.google.com/codejam/contest/351101/dashboard#s=p1
                    
*/

#include 
#include 
#include 
#include 
#include 
#include 

using namespace std;


int main(int argc, char const *argv[])
{
    
    int numOfTestInstances;
    cin >> numOfTestInstances;
    
    for (int testInstanceNum = 0; testInstanceNum < numOfTestInstances; ++testInstanceNum)
    {
        istringstream in;
        string s;
        cin >> ws;
        getline(cin ,s);
        replace(s.begin(), s.end(), ' ', '\n');
        in.str(s);
        
        stack stack_s_rev;

        while (in >> s)
        {
            stack_s_rev.push(s);
        }
        cout << "case #" << testInstanceNum+1 << ": ";
        while(!stack_s_rev.empty())
        {
            cout << stack_s_rev.top() << " ";
            stack_s_rev.pop();
        }
        cout << endl;

    }
    return 0;
}


Google Code jam Solutions: Problem A. Alien Language

Problem

After years of study, scientists at Google Labs have discovered an alien language transmitted from a faraway planet. The alien language is very unique in that every word consists of exactly L lowercase letters. Also, there are exactly D words in this language.

Once the dictionary of all the words in the alien language was built, the next breakthrough was to discover that the aliens have been transmitting messages to Earth for the past decade. Unfortunately, these signals are weakened due to the distance between our two planets and some of the words may be misinterpreted. In order to help them decipher these messages, the scientists have asked you to devise an algorithm that will determine the number of possible interpretations for a given pattern.

A pattern consists of exactly L tokens. Each token is either a single lowercase letter (the scientists are very sure that this is the letter) or a group of unique lowercase letters surrounded by parenthesis ( and ). For example: (ab)d(dc) means the first letter is either a or b, the second letter is definitely d and the last letter is either d or c. Therefore, the pattern (ab)d(dc) can stand for either one of these 4 possibilities: add, adc, bdd, bdc.

Input

The first line of input contains 3 integers, L, D and N separated by a space. D lines follow, each containing one word of length L. These are the words that are known to exist in the alien language. N test cases then follow, each on its own line and each consisting of a pattern as described above. You may assume that all known words provided are unique.

Output

For each test case, output

Case #X: K

where X is the test case number, starting from 1, and K indicates how many words in the alien language match the pattern.

Limits

 

Small dataset

1 ≤ L ≤ 10
1 ≤ D ≤ 25
1 ≤ N ≤ 10

Large dataset

1 ≤ L ≤ 15
1 ≤ D ≤ 5000
1 ≤ N ≤ 500

Sample

Input Output
3 5 4
abc
bca
dac
dbc
cba
(ab)(bc)(ca)
abc
(abc)(abc)(abc)
(zyx)bc
Case #1: 2
Case #2: 1
Case #3: 3
Case #4: 0

C++ Solution:

/*
    Author      :   Sreejith Sreekantan
    Description :   Problem A. Alien Language (https://code.google.com/codejam/contest/90101/dashboard#s=p0)

*/

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

using namespace std;


#define rep(v,N) for(int v=0; v<n; v++)="" #define="" <span="" class="hiddenSpellError" pre="define " data-mce-bogus="1">rep2(v,M,N) for(int v=M; v<n; v++)="" #define="" for(v,c)="" for(__typeof(c.begin())="" v="C.begin();" v!="C.end();" ++v)="" vi="" std::vector<int="">
#define vll std::vector
#define pb push_back
#define Sort(C) std::sort(C.begin(), C.end())
#define RSort(C) std::sort(C.rbegin(), C.rend())
#define Copy(ans,out) copy(ans.begin(),ans.end(), ostream_iterator<__typeof(ans[0])>(out, " "))

// #define SMALL
#define LARGE

int main(int argc, char const *argv[])
{
    freopen("a.in", "rt", stdin);
    // freopen("a.out", "wt", stdout);
#ifdef SMALL
    freopen("as.in", "rt", stdin);
    freopen("as.out", "wt", stdout);
#endif

#ifdef LARGE
    freopen("al.in", "rt", stdin);
    freopen("al.out", "wt", stdout);
#endif

    unsigned int L, D, N;
    cin >> L >> D >> N;

    std::vector dict(D);
    std::vector inp(D);
    rep(i, D)
    {
        cin >> dict[i];
    }
    rep(i, N)
    {
        cin >> inp[i];
        replace(inp[i].begin(), inp[i].end(), '(', '[');
        replace(inp[i].begin(), inp[i].end(), ')', ']');
        // cout << inp[i]<< endl;
        int c = 0;
        rep(j, D)
        {
            if (regex_match(dict[j], regex(inp[i])) ) c++;
        }
        cout << "Case #" << i+1 << ": " << c << endl;
        
    }
    return 0;
}