New Speakers! :D

I recently got new speakers.

Sony SRS D5 Speakers. They are really awesome , although the volume is not that loud, but this has really some bass.

BTW! I have recently solved “Name that Number” USACO Problem. Here is the solution:


/*
ID: gautam11
PROG: namenum
LANG: C++
*/
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define P(s) printf("%s\n",s)
#define Pint(n) printf("%d",n)
#define Sint(n) scanf("%d",&n)
#define TEST printf("Test Called!")
#define NL printf("\n")
typedef struct node{
	int value;
	struct node *next;
}node;

typedef struct BinaryTree{
	int value;
	struct BinaryTree *left;
	struct BinaryTree *right;
}BinaryTree;

using namespace std;

string getChars(char num)
{
	switch(num)
	{
		case '2': return "ABC";
		case '3': return "DEF";
		case '4': return "GHI";
		case '5': return "JKL";
		case '6': return "MNO";
		case '7': return "PRS";
		case '8': return "TUV";
		case '9': return "WXY";
	}
}

/*bool LinearSearch(string S)
{
	ifstream fin ("dict.txt");
	string temp;
	for(int i=0;!fin.eof();i++)
	{
		fin>>temp;
		if(!S.compare(temp))
			return 1;
	}
	return 0;
}*/

bool match(string N,string test)
{
	string chars;
	for(int i=0;i>N;
	string test;
	bool flag=0;
	for(int i=0;!fdict.eof();i++)
	{
		fdict>>test;
		if(test.length()==N.length() && match(N,test))
		{
			flag=1;
			fout<<test<<endl;
		}
	}
	if(!flag)
		fout<<"NONE"<<endl;
	return 0;
}


Now, I am working on Code Chef November 2012 Challenge! I’ll post the solution to the problem I am able to solve(although the chances are really low ) here.

Well, I just got Coin Flip done. Here is my code:


#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define P(s) printf("%s\n",s)
#define Pint(n) printf("%d",n)
#define Sint(n) scanf("%d",&n)
#define TEST printf("Test Called!")
#define NL printf("\n")
typedef struct node{
	int value;
	struct node *next;
}node;

typedef struct BinaryTree{
	int value;
	struct BinaryTree *left;
	struct BinaryTree *right;
}BinaryTree;

using namespace std;

int main()
{
	int T;
	Sint(T);
	while(T--)
	{
		int G;
		Sint(G);
		while(G--)
		{
			int I,Q;
			long long int N;
			scanf("%d %lld %d",&I,&N,&Q);
			if(I==Q)
				printf("%lld\n",N/2);
			else
			{
				if(N%2)
					printf("%lld\n",N/2+1);
				else
					printf("%lld\n",N/2);
			}
		}
	}
	return 0;
}

Assholes ! how come I never misses to meet one.

I can’t write anything technical this time. I’m on my way back to Hyderabad in train. Unluckily, no hot chics near by.. but a lot of assholes. one never misses to meet atleast a asshole when travelling in Indian railways. Sitting here with no work , I just can’t stop thinking why is it that almost all companies select their employees on the basis on their data structure and algorithms knowledge.I mean what about a candidate who is very good at understanding large code bases?! what about those students who are really good at scripting ? Although , I am trying to improve my algorithmic skills for this very purpose. I am thinking of applying to Android Open Source Project(AOSP). I think this will boost my resume and I’ll learn few new very interesting things.

Installing mysql server.

I am trying to clone Open Monitor – Aggregator. The Open Source Project from Umit Project.

Its been about 1.5 years since I started contributing to Umit Project.
But soon after I cloned the aggregator from git, I realized I am I don’t have mysql setup. when I used the command to enter into mysql shell:

mysql -u root -p

After entering all the passwords I could remember to have ever used, all I could get in return was :

ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)

I was really pissed then I realized ,I have never really installed mysql server since I upgraded my system to Ubuntu 12.04

so I went to su mode, typed:

 apt-get install mysql-server

Bingo ! it worked in the first ,I could get my mysql running in no time.

Then I created a database named “openmonitor” using :

 create database openmonitor;

The day CodeSprint Ended.

I participated in CodeSprint 3. Before you judge me, let me tell you, it was a complete failure. I was hardly able to solve 1 problem out of 7 and my batchmates solved 4 problems even after starting way later when I had.

I need to keep my mind focused on Coding or I can say Bye Bye to the Placements this December.

From now on , I am focusing on solving a test placement paper or a coding competition every day . I’ll make sure I do it.I don’t think I have a option though.

BTW, Quora is damn addictive. Hats off to Adam D’Angelo.

For Now, I am solving this problem Transformation (http://ace.delos.com/usacoprob2?a=UwGQkWxSfYb&S=transform) .

WTF! I am confused now , how do we pass a 2D array of size N*N into a function ? I guess, I’ll just use dynamic allocation.

YAY! I got it working , in the 2nd Time, I did a stupid mistake in the first time. Here is my code(Include Necessary library,I’m new to blogging, don’t know how to post codešŸ˜› ):

/*
ID: gautam11
PROG: transform
LANG: C++
*/
using namespace std;

bool Compare(char **Tiles,char **modTiles,int N)
{
	for(int i=0;i<N;i++)
		for(int j=0;j<N;j++)
			if(Tiles[i][j]!=modTiles[i][j])
				return 0;
	return 1;
}

char** RotateBy90(char **Tiles,int N)
{
	char **modTiles;
	modTiles=(char **)malloc(sizeof(char *)*N);
	for(int i=0;i<N;i++)
		modTiles[i]=(char *)malloc(sizeof(char)*N);

	for(int i=0;i<N;i++)
		for(int j=0;j<N;j++)
			modTiles[i][j]=Tiles[N-1-j][i];

	return modTiles;
}
char** Reflection(char **Tiles,int N)
{
	char **modTiles;
	modTiles=(char **)malloc(sizeof(char *)*N);
	for(int i=0;i<N;i++)
		modTiles[i]=(char *)malloc(sizeof(char)*N);

	for(int i=0;i<N;i++)
		for(int j=0;j<N;j++)
			modTiles[i][j]=Tiles[i][N-1-j];

	return modTiles;
}
int main()
{
	freopen ("transform.in", "r", stdin);
	freopen ("transform.out", "w", stdout);
	int N;
	Sint(N);
	char **Tiles;
	Tiles=(char **)malloc(sizeof(char *)*N);
	for(int i=0;i<N;i++)
		Tiles[i]=(char *)malloc(sizeof(char)*N);
	char **finalTiles;
	finalTiles=(char **)malloc(sizeof(char *)*N);
	for(int i=0;i<N;i++)
		finalTiles[i]=(char *)malloc(sizeof(char)*N);
	for(int i=0;i<N;i++)
		scanf("%s",Tiles[i]);
	for(int i=0;i<N;i++)
		scanf("%s",finalTiles[i]);
	char **modTiles;
	modTiles=RotateBy90(Tiles,N);
	if(Compare(modTiles,finalTiles,N))
	{
		printf("1\n");
		return 0;
	}
	modTiles=RotateBy90(modTiles,N);
	if(Compare(modTiles,finalTiles,N))
	{
		printf("2\n");
		return 0;
	}
	modTiles=RotateBy90(modTiles,N);
	if(Compare(modTiles,finalTiles,N))
	{
		printf("3\n");
		return 0;
	}
	modTiles=Reflection(Tiles,N);
	if(Compare(modTiles,finalTiles,N))
	{
		printf("4\n");
		return 0;
	}
	for(int i=0;i<3;i++)
	{
		modTiles=RotateBy90(modTiles,N);
		if(Compare(modTiles,finalTiles,N))
		{
			printf("5\n");
			return 0;
		}
	}
	if(Compare(Tiles,finalTiles,N))
	{
		printf("6\n");
		return 0;
	}
	printf("7\n");	
	return 0;
}

How to Run C/C++ code in Android.

Hi,

I am out of station these days so thought of writing my first blog.

As most of you are aware that Android has Linux kernel,so what was always there in my mind is that there must be some way to run C code in Android, although its most of the application is written in Java.

I was not able to find a perfect method on internet so found a method myself and thought of sharing it.

This thing is possible with the help of Android-NDK.

You can download it here:Ā http://developer.android.com/sdk/ndk/index.html

After downloading NDK you’ll find a very nice tool ndk-build, its nothing but a shell script.

This tool allow us to compile our C/C++ code into binary which can later be pushed into android and executed using SDK tool adb.

Android SDK can be downloaded from:Ā http://developer.android.com/sdk/index.html

I assume while writing this post that you have basic knowledge of Android Development ,C/C++ ,eclipse and how to setup Android SDK .

Method is as follows:

  • Create a new Android Project in Eclipse.
  • Create a folder name “jni” the root directory of the project.
  • In the folder jni ,you need to make Android.mk (Android make file) as:
    LOCAL_PATH := $(call my-dir)
    
    include $(CLEAR_VARS)
    
    LOCAL_MODULE    := hello-world
    LOCAL_SRC_FILES := hello-world.c
    
    include $(BUILD_EXECUTABLE)

    Its not hard to understand every line of this make file:

    LOCAL_PATH := $(call my-dir)

    Android.mk file must start with theĀ definition of LOCAL_PATH variable (yes, this is a variable and anything like this you see in Android.mk file are variables, you can define your own variable but more on that later.)
    This line uses build-in function my-dir that returns the value of path of current directory ie. the directory containing Android.mk.

    include $(CLEAR_VARS)

    This line clears all the LOCAL_XXX variable except LOCAL_PATH.

    LOCAL_MODULE    := hello-world
    LOCAL_SRC_FILES := hello-world.c

    These are most important lines.

    LOCAL_MODULE , defines the name of the compiled code (ie. executable) that you wish.It should not contain any spaces and must be unique.

    LOCAL_SRC_FILES, it tells the name of C/C++ files that you wish to compile for android.

    include $(BUILD_EXECUTABLE)

    This is in-build function to make executable for android.

  • Now in the jni folder make file hello-world.c (in my case) as:
    #include<stdio.h>
    void main()
    {
     printf("Hello to Android World\n");
     return;
    }

    I assume you understand above code.

  • Now come to root Directory of the folder in terminal (Linux) or in Command Prompt(Windows).
    Windows people have to do little more effort, they have to download GNUMakefile from:Ā http://www.gnu.org/software/make/
    Now, set the GNUMAKE environment variable by running following command:
  • export GNUMAKE=path/to/your/GNUMAKE

    Prefer not having spaces in the path for simplicity.

  • Now comes the final step of compiling it with ndk-build.Just go to the root of the project and run the command ndk-build as:
    /path/to/you/NDK-Directory/ndk-build
  • That you have binary, Finally what remains is to push the file into Android, using adb.
    You can find this tool inside Android-SDK/platform-tools.
    Start your Android Emulator or connect you Android Phone.
    Run following commands :

    sudo ./adb kill-server

    Above command kills any server if its running.

    sudo ./adb push ./hello-world /data/local

    This command pushes binary hello-world into Android directory /data/local.

    sudo ./adb shell

    Wait for 2 sec till this “#” or “$” sign appears.
    Now, this is Android shell prompt.

    cd data/local/
    ./hello-world

    We enter into the directory containing our binary and execute it.

    This should display the output, which in our case is:

    Hello to Android world

Hope you have enjoyed this post, feel free to ask doubts or point error(Grammatical or Technical).