Tuesday, 12 November 2013

Learn Python The Hard Way

Ex6:


In this exercise, we learnt about printing strings. When we need to print only one string, no () are needed.

But when you are printing, multiple string variables using formatting strings ()  are needed.

 

SCRAPY INSTALL IN LINUX MINT

Scrapy is a tool used for web - crawling

STEP 1:


Check python version in your machine

python -- version



If python is not installed, install it using

sudo apt-get install python



STEP 2: ( Install Dependencies )


Scrapy depends on two pacakages "python-setuptools" and "pythonX.X.X - dev "

NOTE: X.X.X - indicates the version Example: 2.7.3

sudo apt-get install python-setuptools python2.7.3-dev



STEP 3:


Install Scrapy using "easy_install" or  "python-pip"

To install using easy_install

easy_install scarpy



To install using python-pip

pip install scarpy



STEP 4:


To check whether it is installed correctly, enter scrapy in command line.

On successful installation you will get

Selection_002

Sunday, 10 November 2013

DPKG Error : Sub-process /usr/bin/dpkg returned an error code (1)

While installing some software using linux terminal, we may encounter error like this

Sub-process /usr/bin/dpkg returned an error code (1)



I got this error while installing a library file "libpam0g:i386".

Dpkg - Error

Just see the 4th line of this error.

dpkg: error processing /var/cache/apt/archives/libpam0g_1.1.3-7ubuntu3_i386.deb (--unpack):



The error can be rectified by using the command

sudo dpkg -i --force-overwrite /var/cache/apt/archives/libpam0g_1.1.3-7ubuntu3_i386.deb


Thursday, 7 November 2013

CODECHEF NOV13 PRETNUM

Problem Statement


Given a range to b, we have to all numbers which have prime number of divisors.

Eg: In the range 1 to 10 , the numbers are 2, 3, 4, 5, 7, 9

For a brief problem statement visit this link.

Monday, 4 November 2013

Hackerrank 101oct Halloween party

PROBLEM STATEMENT:


You are given a very large rectangular chocolate bar. You must cut only as 1 x 1 pieces. You are allowed to do only 'K' cuts at the maximum. What is maximum number of chocolate pieces can u cut ?

For a detailed problem statement click here

Friday, 25 October 2013

Topcoder SRM 595 DIV2 250

Problem Statement


In short, You are given a string containing only characters R,G,B. We can remove the string from either end at each move( from left or from right). Finally, the string must contain only R or G or B.

You can find a detailed problem description here.

Wednesday, 16 October 2013

CODECHEF COOK38 RRMATRIX

Problem Statement


Let 'A' be a R x C  Matrix. Elements in 'A' are filled from 1 to R*C in row-major order. 'B' is also a R x C Matrix where the elements are filled in column-major order.


Find number of cells which satisfy the property Ai,j ==  Bi,j.

For a brief problem statement click here.

Topcoder SRM 594 DIV2 250

Problem Statement


In a nutshell, Given a M x N Matrix we have to check whether it is possible to visit all the cells starting from any location.


Moving Direction - If you are in the cell (x,y), you can only move to ( (x+1)%M, (y+1)%N )

You can find a detailed problem description here.

Saturday, 14 September 2013

Bit Tricks

The basic Bit Operations are & (AND) , |(OR) , ^(X-OR), ~(Negation).

Checking nth bit set or not

Let 'x' be the given number.
 (x & (1<<n)) >> n

Setting the nth bit of number
x = ( x | (1<<n) )

Wednesday, 11 September 2013

Pop Vs Imap Protocol

Both POP3 and IMAP protocol are used for email retrieval process.

Pop Protocol



  • Pop protocol downloads the mail from the server, stores it locally and deletes from the server.

  • POP3 is useful only when you access email from only one computer.

  • POP3 is a one-way communication.

  • All mails are stored locally and without internet connection we can access them.

  • When the mail is accidentally deleted, we cannot recover it.


IMAP Protocol



  • IMAP protocol connects the remote server, caches the mail locally and then disconnects from the server.

  • IMAP is useful when you want access e-mail from multiple systems.

  • IMAP is a 2-way communication

Wednesday, 4 September 2013

Puzzle - A Bird and Two Trains

Puzzle


Consider two trains starting from two points X and Y respectively. Trains which starts from X travels at 15 Km/hr while the train which starts from Y travels at 20 Km/hr. Both trains start at the same time. A Bird also start from X at the same time. The speed of the Bird is 25 Km/hr. The Bird flies from X to Y until it meets the train started from Y. After that it changes its direction and fly towards the train started from X. When it meets X it start flying toward Y and so on until the train meets. What is the total distance covered by the bird ?


Hint : Think Simple !

Puzzle - A Gold For 7 Days

Puzzle


A loyal worker is working under you for 7(seven) days and you have only one gold bar to pay him. You must pay the worker at the end of each day. Otherwise, he wont come for next day's work. One more constraint is you have to make only two cuts in the gold bar. How can you pay him at the end of each day ?

Hint:   Barter System

Puzzle - Three Ants Problem

Puzzle


Three ants are sitting in the corners of the equilateral triangle. All ants start at same time and move at same speed constantly. The speeds of three ants are equal. Each ant picks up a direction randomly at first and moves in that direction. What is the probability that none of the ants will collide with each other ?

Tuesday, 3 September 2013

Puzzle - Red and Blue Marble

Puzzle


We have 50 Red , 50 Blue Marbles and 2 jars. One of the jar is chosen at random and then one marble is chosen from that jar at random. How would we maximize the chance of choosing red marble ? What is the probability of choosing red marble ?. All 100 marbles should be placed in the jar.

Monday, 2 September 2013

Unique Binary Search Trees

Problem Statement


Given n, how many structurally unique BST's (binary search trees) that store values 1...n?

For example,
Given n = 3, there are a total of 5 unique BST's.
   1         3     3      2      1
\ / / / \ \
3 2 1 1 3 2
/ / \ \
2 1 2 3

Path Sum in Binary Tree

Problem Statement


Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum.

For example:
Given the below binary tree and sum = 22,
              5
/ \
4 8
/ / \
11 13 4
/ \ \
7 2 1

return true, as there exist a root-to-leaf path 5->4->11->2 which sum is 22.

Addition in Linked List

Problem Statement


You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.

Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8

Sunday, 1 September 2013

Flatten Binary Tree to Linked List

Problem Statement


Given a binary tree, flatten it to a linked list in-place.

For example,
Given
         1
/ \
2 5
/ \ \
3 4 6

The flattened tree should look like:
   1
\
2
\
3
\
4
\
5
\
6

Saturday, 31 August 2013

Remove nth Node in List

Problem Statement


Given a linked list, remove the nth node from the end of list and return its head

Saturday, 24 August 2013

C Basic Concepts



  1. We cannot apply indirection operator to void pointer. (101)

  2. Check question 33,47,49,54,67,79,85,90,94,99,101,104,106, 112, 117, 121,152,160,174,51 again

  3. We cannot apply bitwise shift operator or mod operation to float variables

  4. If the structure is globally declared all the variables are initialized to zero.

  5. When passing array to function, function parameter is implemented as pointer only(94)

  6. Function name preceded by 'pascal' keyword refers to pascal passing mechanism (left to right) while in 'cdecl' variables are passed from right to left (111)


  7. int* arr[8]; // An array of int pointers.
    int (*arr)[8]; // A pointer to an array of integers (http://stackoverflow.com/questions/859634/c-pointer-to-array-array-of-pointers-disambiguation)


  8. Macros can be re-define anywhere in the program

  9. In C, Unary + is a dummy operator.

  10. Enumeration constants cannot be modified(113)

  11. We cannot create a variable of type void.

  12. If a name is not known pre-processor treats it as equal to zero (119)

  13. & ( Address of ) Operator is not applicable for register variable (139)

  14. Small-endian ( Lower order bytes are stored in higher memory address and Higher order bytes are stored in Lower memory address). Eg:  300 Binary Representation is - 00000001 00101100. In memory it is stored as 00101100 00000001.

  15. Forward declaration is not allowed in c (154,155)

  16. The values of any type tend to rotate after a particular value Eg; for 'char' after 127  it moves to -128.

  17. Character constants are stored in code/data area. They are not stored in stack. So there might not be dangling pointer issue

  18. Initializing  structure variable inside structure is not allowed

  19. Arrays are non-modifiable Lvalue

  20. When two strings are placed together they are concatenated,this is know as stringization

  21. Empty object size is always one.

  22. Function overloading cannot be resolved based on return type alone.

  23. Expression will not be evaluated inside the sizeof operator

  24. http://stackoverflow.com/questions/501816/why-does-cout-print-char-arrays-differently-from-other-arrays

  25. http://www.geeksforgeeks.org/storage-for-strings-in-c/

  26. http://www.geeksforgeeks.org/swap-strings-in-c/

  27. In C, when we initialize less no of elements in an array all uninitialized elements become ‘′ in case of char and 0 in case of integers. ( http://geeksquiz.com/string/ Question 8 )

  28. In C, struct and union types cannot have static members.

  29. http://geeksquiz.com/structure-union/ (last Question )

  30. http://geeksquiz.com/dynamic-memory-allocation/ (Third Question)

  31. In C, static variables can only be initialized using constant literals. This is allowed in C++ though (http://www.geeksforgeeks.org/g-fact-80/)

  32. http://www.geeksforgeeks.org/are-array-members-deeply-copied/

  33. In C and C++ arguments are passed by value by default

  34. http://www.learncpp.com/cpp-tutorial/123-virtual-destructors-virtual-assignment-and-overriding-virtualization/


Constant Object



  1. The member variables cannot be changed.

  2. It can access only constant member functions and constant member variable int that class.


Constant Member Function



  1. The state of the object cannot be changed inside the constant member function

  2. Variable declared as mutable can be changed.


Static variables



  1. Created and initialized only once.

  2. Shared among all members.


Static Functions



  1. Can access only static members of the class

  2. Cannot access other members.

  3. Can be called without using objects and can be called using objects also

Sunday, 4 August 2013

Loop in Linked List

Hi to all,


This is my first article in this blog. The article briefly describes How to Find a Loop in Linked List and Finding the start of the Loop.


To find the Loop in linked list, we can use Floyd's Cycle Detection Algorithm. The algorithm is also Tortoise - Hare Algorithm. 



Concepts Behind Floyd's:


Imagine a circular track and two runners. One runner is twice as fast as the other. Both of them start at same point  'P'. The only point they will meet in the circular track again is the start point 'P'.


If the slow runner starts at "S" and fast runner starts at "K" distance away from the point "S", then they will meet in the circular track at "K" distance before the start point "S".


See this diagram for clear understanding,


first


The fast runner 'F' is quarter distance ahead of 'S'.  'S' is the starting point. They both meet quarter distance before the starting point 'S'.


The point we derive from this is :




If  the faster runner is 'K' step ahead of the slow runner at the start of the race, then they meet first at 'K' Steps before the starting point.



You can have a better understanding , if u are able to crack this spoj problem .


Monday, 22 July 2013

My first Post

[sourcecode language="cpp"]
#include<iostream>
using namespace std;
int main()
{
printf("Hello World");
}
[/sourcecode]

Wednesday, 9 January 2013

Static Data Member

Today, I am going to write a post on static data members in C++. I am writing this post after feeling so bad about a debugging competition which i failed once again.

Let’s see about static first.

“static”  means fixed literally. Its value is not lost even after the scope is lost. static variables.

Eg:
[sourcecode language="cpp" padlinenumbers="true"]
#include&lt;iostream&gt;
using namespace std;
int func()
{
static int a;
a++;
return a;
}
int main()
{
cout&lt;&lt; func()&lt;&lt;&quot; &quot;&lt;&lt;func()&lt;&lt;&quot; &quot;&lt;&lt;func();

}
[/sourcecode]


Output:

static

Even though the variable is declared inside func(), it is not intialized again and again. It is initialized only once.  But why the output is 3,2,1 and not 1,2,3 ?

The reason is “ Stack Evaluation”. Stack means LIFO(Last in First Out). So, the third func() will be executed first, then second, and finally first.

Rest we can see tomorrow :)

Sunday, 6 January 2013

Scribd API

This is going to my first post. I am currently building a site for checking plagiarism among projects uploaded in my site.

When building a site, i encountered a problem for viewing files of different formats like “.docx”, .”pptx”, “.doc”, “.ppt”, .pdf” and many. First i tried to read contents from a word document (“doc”) which i was able to succeed using anti-word. Then i tried to read contents form “.docx” and then i came to know that “.docx” is a zip and we can read contents from “word/document.xml” using PHP.

PHP has facility to read a .zip file. But in all these cases, i was able to get only the plain text and could display with their formatting. Indeed, i don’t know about and i couldn’t find a solution. Then in web i goggled for “ embedding the files “ in our webpage. Through that i came know about “scribd API” and several other API’s for “embedding the files”.

About Scribd API


I honestly don’t know much about this API. I just saw in http://www.scribd.com/developers how to integrate in our website. For using API, we need to signup for a free API account. After signing up, under the account setting in the API tab look up your “API key” and “API Secret” id.

Second Step:

Now download Scribd.php  from the site. There is a nice tutorial available in scribd developers site also about using scribd. php. Here, i will just say how to upload the doc using scribd API and how to retrieve using javascript.

Uploading a doc using Scribd API:


1) include scribd.php in your php file

2) Create a scribd object using “scribd(api_key,api_secret)”

3) Then use scribd->upload(“filename”,”file_ext”,”access”,”rev_id”);

4) On successful upload scribd API will return “doc_id” and “access_key”
[sourcecode language="php"]</pre>
<?php

require_once 'scribd.php';
<pre>$api=&quot;API Key&quot;;
$secret=&quot;API Secretj&quot;;

$scribd=new Scribd($api,$secret);</pre>
$api="7fs5j960mfy4seft72lbj";
$secret="7fs5j960mfy4seft72lbj";

$scribd=new Scribd($api,$secret);
//Object Created

//print_r($scribd);

$doc_type=$fileExt;
$access=null;
$rev_id=null;

$data=$scribd->upload($fileLoc,$doc_type,$access,$rev_id);
<pre>
/* $fileExt - Type of file to be uploaded
$fileLoc - Completer path of file
*/

?>
[/sourcecode]


5) After uploading $doc will contain an array consisting of “doc_id” and “access_key”.

Note: file Name should be complete. Your PHP version must support curl. Download the latest version of PHP.