screenshot-from-2015-10-31-182914

URL shortening in Django with bit.ly

Building an application which has features to invite people or maybe share some data through those “big” “lengthy” URL is a disgrace for a developer and is a things of past.

In python along with bit.ly,believe me it becomes lot much easy although I had to struggle to write the most crisp peace of code which code serve the purpose.

Following are the steps :

1.) Signup with bit.ly and navigate to http://dev.bitly.com/

2.) Click My Apps . Register your app and generate generic access token.

3.) Use following code snippet in your code to generate the shortened bit.ly in your code:

import urllib
import urllib2
import json


link = "http://www.example.com/foo/bar/zoo/hello/"
values = {'access_token' : BITLY_ACCESS_TOKEN,
          'longUrl'      : link}
url = "https://api-ssl.bitly.com/v3/shorten"

data = urllib.urlencode(values)
req = urllib2.Request(url,data)
response = (urllib2.urlopen(req).read()).replace('\/', '/')
bitly_url = (json.loads(response))['data']['url']
print bitly_url

You don’t need to create a whole new app in your  Django project just for shortening the URL.

But if you want the all the features of the bit.ly API, start by reading the documentation at dev.bitly.com/get_started.html, and then download the official python client.

Louis Van Gaal tactical philosophy

Have not read an article like this in ages…….complete logical maths involved and sheer brilliance of a visionist
People might call him arrogant but he is confident….
‪#‎GGMU‬

Defending With The Ball

‘The national coach (me) sees opportunities because he believes in vision and structure. When I say that, I mean MY vision, and my structure. Yes, that is arrogant. I bet you hear that more, that people find me arrogant. It does not bother me.  I am NOT arrogant. But I do believe in my vision. So the people who work in my organization have something to hold on to. That is what I believe in.’

Football dominance comes in cycles, and although the inability to keep hold of key players disrupted the progress and dominance Van Gaal was on the verge of achieving in Amsterdam, his Ajax team are widely regarded as one of the greatest teams to have played the game.

“Ajax are not just the team of the Nineties, they are approaching football Utopia. Their concept of the game is exquisite yet they have a physical superiority as…

View original post 1,736 more words

String vs StringBuffer in JAVA

SITUATION:In this present world,the performance of any application is very important and it’s then only when factors like RUNNING TIME and MEMORY occupied plays very critical role in deciding that factor.While working on one such deliverable i too came accross such condition where i need to read some million odd files and each containing some million odd records and do some operation on the data and overwrite or make a copy of the updated info and show to the user so i used STRINGS approach as any nooby will use and what i came accross was that it was taking almost like an hour to do it so i just being guided by seniors to use StringBuffer class instead of String.When i ran it after making the required changes it took only like 3-4 seconds which was like a miracle.Now that really makes a lot of difference and made me to get the details why did it happen and stuff and share it with everyone.

MECHANISM: So the reason why it happened was that String object is immutable whereas StringBuffer/StringBuilder objects are mutable.So what it means is that whenever u create any string object and assign any value on it and do any operation on it and try to update it,what it does is create a new String object and assign the updated value and doesnt assign it the previous one.So what was happening that for every file and every updation an object was being created in background so in all 1,000,000*1,000,000 many objects were created which was causing that much time to do whole process whereas what happens in StringBuffer is when u do any operation on an object then it will upadate it then and there only without taking any time

Finally, whats the difference between StringBuffer and StringBuilder?

StringBuffer and StringBuilder have the same methods with one difference and that’s of synchronization. StringBuffer is synchronized( which means it is thread safe and hence you can use it when you implement threads for your methods) whereas StringBuilder is not synchronized( which implies it isn’t thread safe).

So, if you aren’t going to use threading then use the StringBuilder class as it’ll be more efficient than StringBuffer due to the absence ofsynchronization.

TimeStamp

While working in college we dnt get much chance to deal with all the datatypes in sql or some of the importatnt ones which you get it corporate world while working on some projects.One such is TIMESTAMP which is really different from the normal DATE datatype and its format is like yyyy/MM/dd hh24:mi:ss.SSSSSS.

PROBLEM:  So the problem now is how to deal with this format coz it always use to give errors like: Error: ORA-01830: date format picture ends before converting entire input string or ORA-01843 “not a valid month” and ORA-01861 “literal does not match format string” .So the problem occur due to the SSSSSS part in the end.

SOLUTION: So i came accross many solutions on net of which the few are:

1.instead of using to_date and use to_timestamp('2010-12-14:09:56:53', 'YYYY-MM-DD:HH24:MI:SS')

2.to_date(?,’DD/MM/YYYY HH24:MI:SS’) -in place of ? just put the new entry that you want to insert.

and similarly like this many more but no one was of use so that encouraged me to share the solution with you.

The best way to is to take the substring of the new entry from 0 to indexof(.) so what you get is  yyyy/MM/dd HH24:MI:ss and just put it place and the compiler will take care of the rest .Hope it will be beneficial for you and you dont have to scroll through thousands of web pages and still unable to get the right solution to the problem.

Interview Q-Reverse string without reversing words of the string.

This was one of the string manipulation question that was asked to one of my friend in TECH ROUND 1 of CISCO INTERNSHIP HIRING PROCESS so thought of sharing with you guys and following the approach that i suggested him.

EXPLANATION:Basically if you are a given a string “hi friends how are you”.The output was suppose to be “you are how friends hi”.

APPROACH:The approach that I suggested to him was :

1.To reverse the words individually first without reversing the string.

2.Now reverse the whole string which will restore the reversed words to the original ones and reverse the string too and hence MISSION ACCOMPLISHED..:)

CODE:

void reverse(char *begin, char *end)
{
char temp;
while (begin < end)
{
temp = *begin;
*begin++ = *end;
*end– = temp;
}
}

void reverseWords(char *s)
{
char *word_begin = NULL;
char *temp = s;
while( *temp )                           //swapping the words
{
if (( word_begin == NULL ) && (*temp != ‘ ‘) )
{
word_begin=temp;
}
if(word_begin && ((*(temp+1) == ‘ ‘) || (*(temp+1) == ”)))
{
reverse(word_begin, temp);
word_begin = NULL;
}
temp++;
}
reverse(s, temp-1);
}

k-th smallest element in binary search tree

Approach-Basically there are two approaches to solve this first one is to find by INORDER TRAVERSAL of binary search tree which takes O(N) time but the better way is to use the binary tree property of left key being smaller and right key being bigger than root and calculating the size of the subtree which takes O(LogN) time.

STEPS: Now, suppose we are at node T:

1. k == num_elements(left subtree of T), then the answer we’re looking for is the value in node T
2. k > num_elements(left subtree of T) then obviously we can ignore the left subtree, because those elements will also be smaller than the kth smallest. So, we reduce the problem to finding the k – num_elements(left subtree of T) smallest element of the right subtree.
3. k < num_elements(left subtree of T), then the kth smallest is somewhere in the left subtree, so we reduce the problem to finding the kth smallest element in the left subtree.

CODE:

int size(node *root)
{
if(root==NULL)
return 0;
else
return (size(root->left)+1+sizeof(root->right));
}

int smallest(int k,node *root)
{
int count;
count=size(root->left)+1;
if(k==count)
return (root->data);
else if(k<count)
return smallest(k,root->left);
else
return smallest(k-count,root->right);
}

Swapping two nodes of a singly linked lists (SET 2)

Approach: There are two methods to swap two nodes of a linked list:one by swapping their values and the other by swapping their pointers.The first method has already been discussed in previous post.In this SET i have swapped the pointers of two nodes.

Steps: 1.Pass the address of the nodes to the swap function which you want to swap.

2.Now take five pointers-two pointing to the nodes which you want to swap and two pointing to their previous nodes respectively and one temporary pointer pointing to start.

3.Now point the temporary node pointer to the second nodes next pointer.

4.Swap the previous node’s pointers of the two nodes and swap their link’s also.

CODE:

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>

struct node{
int data;
node *link;
}*start=NULL;

node* insert(int data)
{
node *temp=(struct node*)malloc(sizeof(struct node));
temp->data=data;
if(start==NULL)
{
temp->link=NULL;
start=temp;
}
else
{
node *p=start;
while(p->link!=NULL)
{
p=p->link;
}
p->link=temp;
temp->link=NULL;
}
return temp;
}

node* swap(node **first,node **second)
{
node *p,*p1,*r,*r1,*temp;
p=p1=r=r1=temp=start;
while(p!=*first)
{
p1=p;
p=p->link;
}
while(r!=*second)
{
r1=r;
r=r->link;
}
temp=r->link;
r->link=p->link;
p->link=temp;
p1->link=r;
r1->link=p;
return start;
}

void display(node *start)
{
node *p=start;
while(p!=NULL)
{
printf(“%d—–>”,p->data);
p=p->link;
}
printf(“\n”);
}

int main()
{
start=insert(1);
node *node1=start;
node *node2=insert(2);
node *node3=insert(3);
node *node4=insert(4);
node *node5=insert(5);
printf(“before swapping the list\n”);
display(start);
start=swap(&node2,&node4);
printf(“after swapping the list\n”);
display(start);
getch();
return 0;
}

OUTPUT :

Image