<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:blogger='http://schemas.google.com/blogger/2008' xmlns:georss='http://www.georss.org/georss' xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-3851373114383745907</id><updated>2024-09-20T16:13:26.514-07:00</updated><category term="strings"/><category term="recursion"/><category term="binary tree"/><category term="linked list"/><category term="arrays"/><category term="atoi"/><category term="strtok"/><category term="tree"/><category term="balanced"/><category term="binary search"/><category term="bit magic"/><category term="commafy"/><category term="count leaves"/><category term="exponentiation"/><category term="expression evaluation"/><category term="height of tree"/><category term="isBST"/><category term="isNumber"/><category term="itoa"/><category term="keypad"/><category term="level order"/><category term="lowercase"/><category term="matrix"/><category term="permutations"/><category term="queue"/><category term="reverse"/><category term="stack"/><category term="unsolved"/><category term="uppercase"/><title type='text'>Code Arena</title><subtitle type='html'></subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://programming-puzzle.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3851373114383745907/posts/default?redirect=false'/><link rel='alternate' type='text/html' href='http://programming-puzzle.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Unknown</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>18</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-3851373114383745907.post-7465895167441316460</id><published>2014-04-08T21:54:00.001-07:00</published><updated>2014-04-08T21:54:31.540-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="arrays"/><category scheme="http://www.blogger.com/atom/ns#" term="bit magic"/><title type='text'>Find single number!!</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
Given an array containing integers, where each number occurs three times except for one number which occurs only once, find the number which occurs only once.
&lt;br/&gt;
Couldnt think of a way to use XOR for this. XOR works fine when all numbers appear twice and only one of them appears once.
&lt;br/&gt;
But we can still use some bit magic for this question.
&lt;br/&gt;
Lets consider ith bit of all numbers. If we count the total number of one&#39;s in the ith bit for all numbers and later divide this number by 3, we can get the ith bit of the desired number. So lets say if we have total of 7 elements for eg: {10, 13, 10, 13, 10, 4, 13}. Now if we count total number of ones for LSB in the whole array, we will get 3(for 3 13s, for every other number LSB is 0). Now if we find remainder of the count when divided by 3, we will get the LSB of the desired number. Why? Because every other number appears three times, hence all other ones will appear three times and when taking a remainder will cancel out. 
&lt;br/&gt;
We can do the same thing for all 32 bits :). Below is the code. 
&lt;pre class = &quot;prettyprint&quot;&gt;
int singleNumber(int A[], int n) {
    int num = 0;
    int temp = 0;
    for(int i = 0; i &amp;lt; 32; i++)
    {
        for(int j = 0; j &amp;lt; n; j ++)
        {
            if((A[j] &amp;gt;&amp;gt; i) &amp; 1)
                temp ++;
        }
        temp = temp %3;
        num = num | (temp&amp;lt;&amp;lt;i);
        temp = 0;
    }
    return num;
}

&lt;/pre&gt;
&lt;br /&gt;&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://programming-puzzle.blogspot.com/feeds/7465895167441316460/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://programming-puzzle.blogspot.com/2014/04/find-single-number.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3851373114383745907/posts/default/7465895167441316460'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3851373114383745907/posts/default/7465895167441316460'/><link rel='alternate' type='text/html' href='http://programming-puzzle.blogspot.com/2014/04/find-single-number.html' title='Find single number!!'/><author><name>Unknown</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3851373114383745907.post-735969669384599596</id><published>2014-02-01T17:44:00.000-08:00</published><updated>2014-02-01T17:45:03.196-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="linked list"/><title type='text'>Zip of a linked list</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;br /&gt;
Given a linked list &lt;1, 2, 3, 4, 5, 6&gt;, zip of this linked list is defined as 1, 6 , 2, 5 , 3, 4. And the task is to achieve desired linked list using O(1) space.
&lt;br/&gt;
This can be performed by a simple algorithm:
&lt;ul&gt;
&lt;li&gt; Split the list from the middle into two lists. We are splitting the list into two and not creating a new linked list hence maintaining O(1) space&lt;/li&gt;
&lt;li&gt;Now we have two lists : 1, 2, 3 and 4, 5, 6. Reverse the second list&lt;/li&gt;
&lt;li&gt;This gives us two lists 1, 2, 3 and 6, 5, 4&lt;/li&gt;
&lt;li&gt;Now merge the lists picking one node from each list as a time&lt;/li&gt;
&lt;/ul&gt;

Below is how we can do the same using STL lists.
&lt;pre class = &quot;prettyprint&quot;&gt;

list&amp;lt;int&amp;gt; l; //original list
list&amp;lt;int&amp;gt; nl; //new empty list
int size = l.size();

//Step 1 of splitting into two
for(int i = 0 ; i &amp;lt; size/2; i++)
{
    nl.push_back(l.back());
    l.pop_back();
}

//At this point we have 1, 2, 3 and 6, 5, 4
size = l.size();
list&amp;lt;int&amp;gt;::iterator it = l.begin();
it++;
while(i)
{
    l.insert(it, nl.front());
    nl.pop_front();
    i--;
}
&lt;/pre&gt;
&lt;br/&gt;&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://programming-puzzle.blogspot.com/feeds/735969669384599596/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://programming-puzzle.blogspot.com/2014/02/zip-of-linked-list.html#comment-form' title='5 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3851373114383745907/posts/default/735969669384599596'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3851373114383745907/posts/default/735969669384599596'/><link rel='alternate' type='text/html' href='http://programming-puzzle.blogspot.com/2014/02/zip-of-linked-list.html' title='Zip of a linked list'/><author><name>Unknown</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>5</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3851373114383745907.post-1980060147413941928</id><published>2013-09-24T22:39:00.000-07:00</published><updated>2013-09-24T22:39:28.257-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="binary search"/><title type='text'>Count number of occurences of a number in a sorted array</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;br /&gt;
We will use the fact that the array is sorted. Hence we can use binary search. The only trick is that we do not stop when we find the first occurrence of the number, but continue doing the binary search to the left and right of that number.
&lt;pre class = &quot;prettyprint&quot;&gt;
int count(int* arr, int num, int start, int end)
{
    if(start &amp;gt; end)
    return 0;
    int mid = (start + end)/2;
    if(arr[mid] == num)
        return 1 + count(arr, num, start, mid-1) + count(arr, num, mid+1, end);
    else if(arr[mid] &amp;gt; num)
        return count(arr, num, start, mid-1);
    else
        return count(arr, num, mid+1, end);
}
&lt;/pre&gt;
&lt;br/&gt;&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://programming-puzzle.blogspot.com/feeds/1980060147413941928/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://programming-puzzle.blogspot.com/2013/09/count-number-of-occurences-of-number-in.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3851373114383745907/posts/default/1980060147413941928'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3851373114383745907/posts/default/1980060147413941928'/><link rel='alternate' type='text/html' href='http://programming-puzzle.blogspot.com/2013/09/count-number-of-occurences-of-number-in.html' title='Count number of occurences of a number in a sorted array'/><author><name>Unknown</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3851373114383745907.post-4130835034496776041</id><published>2012-11-06T21:56:00.000-08:00</published><updated>2012-11-06T21:57:56.213-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="balanced"/><category scheme="http://www.blogger.com/atom/ns#" term="binary tree"/><category scheme="http://www.blogger.com/atom/ns#" term="count leaves"/><category scheme="http://www.blogger.com/atom/ns#" term="height of tree"/><category scheme="http://www.blogger.com/atom/ns#" term="isBST"/><category scheme="http://www.blogger.com/atom/ns#" term="recursion"/><category scheme="http://www.blogger.com/atom/ns#" term="tree"/><title type='text'>Useful tree functions!!</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;b&gt;1) Check if a given tree is a BST?&lt;/b&gt;
&lt;br/&gt;
Checking that a given binary tree is a BST requires fulfillment of the property that value of root node is greater than all nodes in its left subtree and its less than all nodes in its right subtree.
&lt;pre class = &quot;prettyprint&quot;&gt;
bool isBST(tree* root,int min, int max)
{
    if(root==NULL)
        return true;
    if((root-&amp;gt;data &amp;gt;=min &amp;&amp; root-&amp;gt;data&amp;lt;max) || (root-&amp;gt;data &amp;gt;min &amp;&amp; root-&amp;gt;data&amp;lt;=max))
        return isBST(root-&amp;gt;lchild, min, root-&amp;gt;data) &amp;&amp; isBST(root-&amp;gt;rchild, root-&amp;gt;data, max);
    else
        return false;
}
&lt;/pre&gt;
&lt;b&gt;2) Find height of given binary tree.&lt;/b&gt;
&lt;br/&gt;
To calculate height of the tree, we find the height of the left subtree and the right subtree recursively and report the maximum of the two.
&lt;pre class = &quot;prettyprint&quot;&gt;
int height(tree* root)
{
    if(root==NULL)
        return 0;
    
    return (1+ max(height(root-&amp;gt;lchild), height(root-&amp;gt;rchild)));
}
&lt;/pre&gt;
&lt;b&gt;3) Is the given binary tree balanced?&lt;/b&gt;
&lt;br/&gt;
To check that a tree is balanced, we check at every node that its right subtree and its right subtree do not differ in height by more than 1. The property should hold true at all levels and for all nodes in order for tree to be balanced.
&lt;pre class = &quot;prettyprint&quot;&gt;
bool isBalanced(tree* root)
{
    if(root!=NULL)
    {
        int l = height(root-&amp;gt;lchild);
        int r = height(root-&amp;gt;rchild);
        
        if(abs(l-r)&amp;gt;1)
            return false;
        else
        {
            isBalanced(root-&amp;gt;lchild);
            isBalanced(root-&amp;gt;rchild);
        }
        return true;
    }
&lt;/pre&gt;
&lt;b&gt;4) Count leaves in a binary tree.&lt;/b&gt;
&lt;br/&gt;
Recursion is pretty similar to height calculation. We find number of leaves in the left subtree and sum that with number of leaves in the right subtree.
&lt;pre class = &quot;prettyprint&quot;&gt;
int countLeaves(struct tree* root)
{
    if(root==NULL)
        return 0;
    if(root-&amp;gt;lchild==NULL &amp;&amp; root-&amp;gt;lchild==NULL)
        return 1;
    else
        return countLeaves(root-&amp;gt;lchild) + countLeaves(root-&amp;gt;rchild);
}
&lt;/pre&gt;
&lt;b&gt;5) Given the root node, copy the binary tree.&lt;/b&gt;
&lt;pre class = &quot;prettyprint&quot;&gt;
tree* copy(tree* t1)
{
    if(t1)
    {
        tree* temp = new tree;
        
        temp-&amp;gt;lchild=copy(t1-&amp;gt;lchild);
        temp-&amp;gt;data=t1-&amp;gt;data;
        temp-&amp;gt;rchild=copy(t1-&amp;gt;rchild);
        return temp;
    }
    return NULL;
}
&lt;/pre&gt;
&lt;br /&gt;&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://programming-puzzle.blogspot.com/feeds/4130835034496776041/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://programming-puzzle.blogspot.com/2012/11/useful-tree-functions.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3851373114383745907/posts/default/4130835034496776041'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3851373114383745907/posts/default/4130835034496776041'/><link rel='alternate' type='text/html' href='http://programming-puzzle.blogspot.com/2012/11/useful-tree-functions.html' title='Useful tree functions!!'/><author><name>Unknown</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3851373114383745907.post-1739021440177582312</id><published>2012-10-24T21:51:00.000-07:00</published><updated>2012-10-24T21:51:23.424-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="binary tree"/><category scheme="http://www.blogger.com/atom/ns#" term="recursion"/><title type='text'>Is there a path from root to leaf that sums up to a value in a binary tree!!</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
The problem can be solved using two simple recursive calls. As we need to find a path that starts from root and ends at leaf, we can check at every call if we hit a node that is a leaf and if the sum requirement at that point matches the value of that node, then we declare success. And if any such path exists we return true and hence the &#39;OR&#39; operator in the return statement.
&lt;pre class = &quot;prettyprint&quot;&gt;
bool sumPath(node* root, int sum)
{
    if(root == NULL)
        return false;
    if(root-&amp;gt;lchild == NULL &amp;&amp; root-&amp;gt;rchild==NULL &amp;&amp; root-&amp;gt;data == sum)
        return true;
    return (sumPath(root-&amp;gt;lchild, sum - root-&amp;gt;data) || sumPath(root-&amp;gt;rchild, sum - root-&amp;gt;data));
}
&lt;/pre&gt;
&lt;br /&gt;&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://programming-puzzle.blogspot.com/feeds/1739021440177582312/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://programming-puzzle.blogspot.com/2012/10/is-there-path-from-root-to-leaf-that.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3851373114383745907/posts/default/1739021440177582312'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3851373114383745907/posts/default/1739021440177582312'/><link rel='alternate' type='text/html' href='http://programming-puzzle.blogspot.com/2012/10/is-there-path-from-root-to-leaf-that.html' title='Is there a path from root to leaf that sums up to a value in a binary tree!!'/><author><name>Unknown</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3851373114383745907.post-4188758613346291228</id><published>2012-09-07T21:14:00.001-07:00</published><updated>2012-09-07T21:14:08.877-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="expression evaluation"/><category scheme="http://www.blogger.com/atom/ns#" term="stack"/><title type='text'>Evaluate a parenthesized expression!!</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;br /&gt;
The problem can be solved by using two stacks. One for the numbers and another one for the operators. When we get a left parenthesis we ignore it. When we get an operand we push it to the number stack and when we get a operator, we push it to the operator stack. When we encounter a right parenthesis, we pop two elements from number stack, pop a operator from operator stack, perform operation on the numbers and push back the result to the number stack.
&lt;pre class = &quot;prettyprint&quot;&gt;
#include&amp;lt;iostream&amp;gt;
#include&amp;lt;stack&amp;gt;
using namespace std;
int main()
{
    stack&amp;lt;int&amp;gt; val;
    stack&amp;lt;char&amp;gt; op;
    string expression;
    cin&amp;gt;&amp;gt;expression;
    for(int i=0;i&amp;lt;expression.length();i++)
    {
        if((expression[i] - &#39;0&#39; &amp;gt;=0) &amp;&amp; (expression[i] - &#39;0&#39; &amp;lt;=9))
            val.push(expression[i] - &#39;0&#39;);
        else if (expression[i] == &#39;(&#39;)
            continue;
        else if (expression[i] == &#39;-&#39; || expression[i] == &#39;+&#39; || expression[i] == &#39;*&#39; || expression[i] == &#39;/&#39;)
            op.push(expression[i]);
        else if(expression[i] == &#39;)&#39;)
        {
            if (op.empty() || val.empty())
            {
                cout&amp;lt;&amp;lt;&quot;InCorrect number of paranthesis &quot;&amp;lt;&amp;lt;endl;
                return -1;
            }
            else
            {
                int num1 = val.top();
                val.pop();
                if(val.empty())
                {
                    cout&amp;lt;&amp;lt;&quot;InCorrect number of paranthesis &quot;&amp;lt;&amp;lt;endl;
                    return -1;
                }
                int num2 = val.top();
                val.pop();
                char oper = op.top();
                op.pop();
                switch(oper)
                {
                    case &#39;+&#39; : val.push(num1 + num2);
                    break;
                    case &#39;-&#39; : val.push(num1 - num2);
                    break;
                    case &#39;*&#39; : val.push(num1*num2);
                    break;
                    case &#39;/&#39; : val.push(num1/num2);
                    break;
                }
            }
        }
        else
            continue;
    }
    cout&amp;lt;&amp;lt;val.top()&amp;lt;&amp;lt;endl;
    return 0;
}
&lt;/pre&gt;&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://programming-puzzle.blogspot.com/feeds/4188758613346291228/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://programming-puzzle.blogspot.com/2012/09/evaluate-parenthesized-expression.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3851373114383745907/posts/default/4188758613346291228'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3851373114383745907/posts/default/4188758613346291228'/><link rel='alternate' type='text/html' href='http://programming-puzzle.blogspot.com/2012/09/evaluate-parenthesized-expression.html' title='Evaluate a parenthesized expression!!'/><author><name>Unknown</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3851373114383745907.post-5833103929083266982</id><published>2012-06-10T11:21:00.001-07:00</published><updated>2012-09-07T21:09:12.966-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="keypad"/><category scheme="http://www.blogger.com/atom/ns#" term="recursion"/><category scheme="http://www.blogger.com/atom/ns#" term="strings"/><title type='text'>Print all possible combinations of strings that can be made using a keypad given a number</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;br /&gt;
Consider a phone keypad where every number has few english alphabet characters associated with it. When a user types some numbers on screen , print all possible permutations of string possible using the characters associated with those numbers.
Example: if 1 corresponds to A B C and 2 corresponds to DEF. Then number &quot;12&quot; might represent any of the following :
AD, AE, AF, BD, BE, BF, CD, CE, CF.
&lt;br/&gt;
The problem is very similar to &lt;a href=&quot;http://programming-puzzle.blogspot.com/2012/05/print-given-string-in-all-combinations.html&quot;&gt;this problem.&lt;/a&gt;
&lt;br/&gt;
The key steps in writing an this function are:
&lt;ul&gt;
&lt;li&gt; Initialize a keypad array of strings with alphabets corresponding to each number.&lt;/li&gt;
&lt;li&gt; Now one by one choose each of the characters corresponding to the number and recurse.&lt;/li&gt;
&lt;li&gt; Make sure to keep a check for the digit 9 as it will have only 2 characters.&lt;/li&gt;
&lt;/ul&gt;
&lt;pre class = &quot;prettyprint&quot;&gt;
string keypad[9] = {&quot;ABC&quot;, &quot;DEF&quot;, &quot;GHI&quot;, &quot;JKL&quot;, &quot;MNO&quot;, &quot;PQR&quot;, &quot;STU&quot;, &quot;VWX&quot;, &quot;YZ&quot;};

void keys(char* num, char *s, int start, int end)
{
    if(start == end)
    {
        cout&amp;lt;&amp;lt;s&amp;lt;&amp;lt;endl;
        return;
    }
    
    s[start] = keypad[(num[start] - &#39;0&#39; - 1)][0];
    keys(num, s,start+1,end);
    s[start] = keypad[(num[start] - &#39;0&#39; - 1)][1];
    keys(num, s,start+1,end);
    if((num[start] - &#39;0&#39; - 1) &amp;lt; 8)
    {
        s[start] = keypad[(num[start] - &#39;0&#39; - 1)][2];
        keys(num, s,start+1,end);
    }
}
&lt;/pre&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programming-puzzle.blogspot.com/feeds/5833103929083266982/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://programming-puzzle.blogspot.com/2012/06/print-all-possible-combinations-of.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3851373114383745907/posts/default/5833103929083266982'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3851373114383745907/posts/default/5833103929083266982'/><link rel='alternate' type='text/html' href='http://programming-puzzle.blogspot.com/2012/06/print-all-possible-combinations-of.html' title='Print all possible combinations of strings that can be made using a keypad given a number'/><author><name>Unknown</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3851373114383745907.post-7379128399303375921</id><published>2012-06-03T23:59:00.001-07:00</published><updated>2014-02-04T23:08:11.957-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="permutations"/><category scheme="http://www.blogger.com/atom/ns#" term="recursion"/><category scheme="http://www.blogger.com/atom/ns#" term="strings"/><title type='text'>Function to print all permutations of a string!!</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;br /&gt;
Write a function to print all permutations of a string. Characters in the string do not repeat themselves.
&lt;br/&gt;
The problem is pretty similar to &lt;a href=&quot;http://programming-puzzle.blogspot.com/2012/05/print-given-string-in-all-combinations.html&quot;&gt; this problem.&lt;/a&gt;
&lt;br/&gt;
Solution is a simple recursive approach where we call the function again and again once with each arrangement of  characters. These arrangements are achieved by swapping start with every other character and calling the function again and again with different values of start.
&lt;pre class=&quot;prettyprint&quot;&gt;
void perm(string s, int start, int end)
{
    //If we have already reached the end of string, print it
    if(start == end)
    {
        cout&amp;lt;&amp;lt;s&amp;lt;&amp;lt;endl;
        return;
    }
    char temp;
    for(int i=start;i&amp;lt;end;i++)
    {
        SWAP(s[start],s[i],temp);
        perm(s,start+1,end);
        SWAP(s[i],s[start],temp);
    }
}
&lt;/pre&gt;
&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programming-puzzle.blogspot.com/feeds/7379128399303375921/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://programming-puzzle.blogspot.com/2012/06/function-to-print-all-permutations-of.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3851373114383745907/posts/default/7379128399303375921'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3851373114383745907/posts/default/7379128399303375921'/><link rel='alternate' type='text/html' href='http://programming-puzzle.blogspot.com/2012/06/function-to-print-all-permutations-of.html' title='Function to print all permutations of a string!!'/><author><name>Unknown</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3851373114383745907.post-4679902609683384455</id><published>2012-06-02T17:49:00.000-07:00</published><updated>2012-06-02T17:51:30.173-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="arrays"/><category scheme="http://www.blogger.com/atom/ns#" term="linked list"/><category scheme="http://www.blogger.com/atom/ns#" term="matrix"/><category scheme="http://www.blogger.com/atom/ns#" term="unsolved"/><title type='text'>Unsolved!!</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
Here are some questions which I will try in coming days and have not solved them. Will take them of this list as I solve each one of them :).&lt;br /&gt;
&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;
Given a linked list structure where every node represents a linked list and contains two pointers of its type: (i) pointer to next node in the main list. (ii) pointer to a linked list where this node is the head. Write a function to flatten the list into a single linked list.&lt;/li&gt;
&lt;br/&gt;
&lt;li&gt;You are given an array of integers A[1..n] and a maximum sliding window of size w. Output an array B where B[i] is the maximum in A[i, i+w-1].&lt;/li&gt;
&lt;br/&gt;
&lt;li&gt;You have an array of 0s and 1s and you want to output all the intervals (i, j) where the number of 0s and numbers of 1s are equal.&lt;/li&gt;
&lt;br/&gt;
&lt;li&gt;Given a binary matrix of NxN of integers ,return only unique rows of binary arrays eg: 01001 10110 01001 11100 ans: 01001 10110 11100&lt;/li&gt;
&lt;br/&gt;
&lt;li&gt;Given an array of integers like ar[]= {1,3,2,4,5,4,2}. Create another array ar_low[] such that ar_low[i] = # of elements lower than or equal to ar[i] in ar[i+1:n-1]. So the output for above array should be {0,2,1,2,2,1,0} &lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programming-puzzle.blogspot.com/feeds/4679902609683384455/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://programming-puzzle.blogspot.com/2012/06/unsolved.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3851373114383745907/posts/default/4679902609683384455'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3851373114383745907/posts/default/4679902609683384455'/><link rel='alternate' type='text/html' href='http://programming-puzzle.blogspot.com/2012/06/unsolved.html' title='Unsolved!!'/><author><name>Unknown</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3851373114383745907.post-265923520246122352</id><published>2012-05-30T20:33:00.000-07:00</published><updated>2014-02-04T23:02:51.802-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="linked list"/><category scheme="http://www.blogger.com/atom/ns#" term="recursion"/><category scheme="http://www.blogger.com/atom/ns#" term="reverse"/><title type='text'>Reverse a Linked List!!</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;br /&gt;
Iterative version
&lt;pre class=&quot;prettyprint&quot;&gt;
void reverse()
{
    if(head == NULL)
        return;
    list *first = NULL;
    list *second = head;
    list *third = second-&amp;gt;next;
    while(third!=NULL)
    {
        second-&amp;gt;next=first;
        first = second;
        second = third;
        third = third-&amp;gt;next;
    }
    second-&amp;gt;next = first;
    head=second;
}
&lt;/pre&gt;
Recursive version
&lt;pre class=&quot;prettyprint&quot;&gt;
void rReverse(list *n)
{
    if (n == NULL)
        return;
    if(n-&amp;gt;next ==NULL)
    {
        head = n;
        return ;
    }
    rReverse(n-&amp;gt;next);
    n-&amp;gt;next = n;
}
&lt;/pre&gt;
&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programming-puzzle.blogspot.com/feeds/265923520246122352/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://programming-puzzle.blogspot.com/2012/05/reverse-linked-list.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3851373114383745907/posts/default/265923520246122352'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3851373114383745907/posts/default/265923520246122352'/><link rel='alternate' type='text/html' href='http://programming-puzzle.blogspot.com/2012/05/reverse-linked-list.html' title='Reverse a Linked List!!'/><author><name>Unknown</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3851373114383745907.post-661325659883679955</id><published>2012-05-27T22:40:00.001-07:00</published><updated>2014-02-04T23:03:09.898-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="lowercase"/><category scheme="http://www.blogger.com/atom/ns#" term="recursion"/><category scheme="http://www.blogger.com/atom/ns#" term="strings"/><category scheme="http://www.blogger.com/atom/ns#" term="uppercase"/><title type='text'>Print given string in all combinations of uppercase and lowercase characters.</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;br /&gt;
Given a string we need to print the string with all possible combinations of the uppercase and lowercase characters in the string.
&lt;br/&gt;
So given a string &quot;abc&quot;, we need to print the following:
&lt;br/&gt;
ABC
&lt;br/&gt;
ABc
&lt;br/&gt;
AbC
&lt;br/&gt;
Abc
&lt;br/&gt;
aBC
&lt;br/&gt;
aBc
&lt;br/&gt;
abC
&lt;br/&gt;
abc
&lt;br/&gt;
Solution is a simple recursive approach where we call the function again and again once with a character in lower case and another time with the same character in upper case. The code is similar to what we would write for all permutations of a string.
&lt;pre class=&quot;prettyprint&quot;&gt;
void lowerUpper(char *s, int start, int end)
{
    if(start == end)
    {
        cout&amp;lt;&amp;lt;s&amp;lt;&amp;lt;endl;
        return;
    }
    //Change next character to upper case
    s[start] = toupper(s[start]);
    lowerUpper(s,start+1,end);

    //Change the same character as changed earlier to lower case
    s[start] = tolower(s[start]);
    lowerUpper(s,start+1,end);
}
&lt;/pre&gt;
&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programming-puzzle.blogspot.com/feeds/661325659883679955/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://programming-puzzle.blogspot.com/2012/05/print-given-string-in-all-combinations.html#comment-form' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3851373114383745907/posts/default/661325659883679955'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3851373114383745907/posts/default/661325659883679955'/><link rel='alternate' type='text/html' href='http://programming-puzzle.blogspot.com/2012/05/print-given-string-in-all-combinations.html' title='Print given string in all combinations of uppercase and lowercase characters.'/><author><name>Unknown</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3851373114383745907.post-4323985251457006299</id><published>2012-05-22T15:00:00.000-07:00</published><updated>2012-05-22T15:00:30.629-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="binary tree"/><category scheme="http://www.blogger.com/atom/ns#" term="level order"/><category scheme="http://www.blogger.com/atom/ns#" term="queue"/><category scheme="http://www.blogger.com/atom/ns#" term="tree"/><title type='text'>Level Order in a Binary Tree!!</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;br /&gt;
Write a function to do level order in a binary tree with a slight modification. We need to print each level&#39;s data in a separate line.
&lt;br/&gt;
The key steps in writing such a level order function is to identify where each level ends.
&lt;br/&gt;
&lt;br/&gt;
In order to do that we follow the following steps:
&lt;ul&gt;
&lt;li&gt; We use a dummy node to identify a level end and put a dummy node as soon as we are done reading that level.&lt;/li&gt; 
&lt;li&gt; First we enter root in the queue. As root is the only node at its level, we enter a dummy node after it to signal end of level.&lt;/li&gt;
&lt;li&gt; So when we read back nodes from queue, as soon as we see a dummy node we know that this level has ended&lt;/li&gt;
&lt;li&gt; When we remove a node from queue, we enter all its children in the queue.&lt;/li&gt;
&lt;li&gt; And as soon as we encounter a dummy node(signaling end of current level), we enter another dummy node in the queue(this signals that we have encountered all nodes in previous level and entered their children in the queue, so this level ends here). &lt;/li&gt;
&lt;li&gt; As we need to print each level in new line, we enter a new line when we see a dummy node&lt;/li&gt;
&lt;/ul&gt;
Consider Node to be a class having 3 members, a left and right Node pointer and an integer data field and all members are public.
&lt;pre class=&quot;prettyprint&quot;&gt;
void printLevel(Node* root)
{
    queue&amp;lt;Node*&amp;gt; q;
    Node *dummy = NULL;
    if(root == NULL)
        return;
    //Enter root in the queue
    else
    {
        q.push(root);
        q.push(dummy);
    }
    //If queue is empty then we are done.
    while(!q.empty())
    {
        Node* temp = q.front();
        q.pop();
        //If its a dummy node
        if(temp == NULL)
        {
            cout&amp;lt;&amp;lt;endl;
            q.push(dummy);
        }
        //Else enter node&#39;s children in the queue
        if(temp-&amp;gt;left!=NULL)
            q.push(temp-&amp;gt;left);
        if(temp-&amp;gt;right!=NULL)
            q.push(temp-&amp;gt;right);
        //Print out node&#39;s data.
        cout&amp;lt;&amp;lt;temp-&amp;gt;data&amp;lt;&amp;lt;&quot; &quot;;
    }
}
&lt;/pre&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programming-puzzle.blogspot.com/feeds/4323985251457006299/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://programming-puzzle.blogspot.com/2012/05/level-order-in-binary-tree.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3851373114383745907/posts/default/4323985251457006299'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3851373114383745907/posts/default/4323985251457006299'/><link rel='alternate' type='text/html' href='http://programming-puzzle.blogspot.com/2012/05/level-order-in-binary-tree.html' title='Level Order in a Binary Tree!!'/><author><name>Unknown</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3851373114383745907.post-4379933132332260552</id><published>2012-05-17T18:12:00.000-07:00</published><updated>2012-05-17T18:12:19.570-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="strings"/><category scheme="http://www.blogger.com/atom/ns#" term="strtok"/><title type='text'>Check if string is valid IPv4 ip address</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;br /&gt;
Given a string, we need to print whether the string is a valid IPv4 ip address or not. For example, 12.2.3.4 is valid IP address and 1222.2.22.33 is not.
&lt;br/&gt;
The key steps in writing an verifyIP function are:
&lt;ul&gt;
&lt;li&gt; Length of the string should be less than equal to 15. As each sub part of the IP can be at max 3 digits and we can have at max 3 dots.&lt;/li&gt;
&lt;li&gt; The string should have 4 parts separated by 3 dots.&lt;/li&gt;
&lt;li&gt; Each part should be greater than 0 and less than 256.&lt;/li&gt;
&lt;/ul&gt;

&lt;pre class=&quot;prettyprint&quot;&gt;
void verifyIP(char *s )
{
    int count=0;
    char *str = new char[strlen(s)];
    strcpy(str,s);

    //Check if length of string is greater than 15.
    if(strlen(str)&amp;gt;15)
    {
        cout&amp;lt;&amp;lt;&quot;Not an IP Address&quot;&amp;lt;&amp;lt;endl;
        return;
    }
    
    char *ip=strtok(str,&quot;.&quot;);
    
    while(ip!=NULL)
    {
        count++;

        //Check that each subpart is less than 256
        if(atoi(ip)&amp;gt;255)
        {
            cout&amp;lt;&amp;lt;&quot;Not an IP address&quot;&amp;lt;&amp;lt;endl;
            return;
        }
        ip=strtok(NULL,&quot;.&quot;);
    }

    //Check that we have 4 parts in the ip
    if(count==4)
        cout&amp;lt;&amp;lt;&quot;Correct IP Address&quot;&amp;lt;&amp;lt;endl;
    else
        cout&amp;lt;&amp;lt;&quot;Not an IP Address&quot;&amp;lt;&amp;lt;endl;
}
&lt;/pre&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programming-puzzle.blogspot.com/feeds/4379933132332260552/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://programming-puzzle.blogspot.com/2012/05/check-if-string-is-valid-ipv4-ip.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3851373114383745907/posts/default/4379933132332260552'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3851373114383745907/posts/default/4379933132332260552'/><link rel='alternate' type='text/html' href='http://programming-puzzle.blogspot.com/2012/05/check-if-string-is-valid-ipv4-ip.html' title='Check if string is valid IPv4 ip address'/><author><name>Unknown</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3851373114383745907.post-5448522507835751822</id><published>2012-05-16T22:44:00.000-07:00</published><updated>2012-05-16T22:48:39.386-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="commafy"/><category scheme="http://www.blogger.com/atom/ns#" term="itoa"/><category scheme="http://www.blogger.com/atom/ns#" term="strings"/><title type='text'>Commafy()</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;br /&gt;
Given a number, convert it to a string and put commas in it after every 3 digits. So given a number say 100054, function should return 100,054 or for a number like 3040005, function should return 3,040,005.
&lt;br/&gt;
The key steps in writing an commafy function are:
&lt;ul&gt;
&lt;li&gt; Check if number is 0. In this case return string &quot;0&quot;. &lt;/li&gt;
&lt;li&gt; Check if number is less than zero. Save this information in a variable and make the number positive. &lt;/li&gt;
&lt;li&gt; Extract each digit from the number and add a comma after every 3 digits. &lt;/li&gt;
&lt;li&gt; Make sure to append &#39;-&#39; sign before returning result if the number was negative.&lt;/li&gt;
&lt;li&gt; Remember to reverse the string before returning as we extracted digits LSB first and hence generated string is reverse of expected&lt;/li&gt;
&lt;/ul&gt;
&lt;pre class=&quot;prettyprint&quot;&gt;
const char* commafy(int num)
{
    char *text = new char(10);

    //Check for num == 0
    if(num==0)
        return &quot;0\0&quot;;

    int i=0, sign = 1, count = 0;
    
    //Check if number &amp;lt; 0, Save this info in variable sign and make the number positive
    if(num&amp;lt;0)
    {
        sign = -1;
        num = num*-1;
    }
    
    //Extract each digit from right and add comma after every 3 digits.
    while(num&amp;gt;0)
    {
        count++;
        if(count &amp;gt;1 &amp;&amp; (count%3 ==1))
        text[i++] = &#39;,&#39;;
        
        text[i++] = &#39;0&#39; + num%10;
        num=num/10;
        
    }
    
    //Add &#39;-&#39; if number was negative.
    if(sign == -1)
        text[i++] = &#39;-&#39;;
    
    //Add null terminator
    text[i] = &#39;\0&#39;;
   
    //Reverse string before returning 
    int len = strlen(text);
    for(int i=0;i&amp;lt;(len/2);i++)
    {
        char temp = text[i];
        text[i] = text[len-i-1];
        text[len-i-1] = temp;
    }
    
    return text;
}
&lt;/pre&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programming-puzzle.blogspot.com/feeds/5448522507835751822/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://programming-puzzle.blogspot.com/2012/05/commafy.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3851373114383745907/posts/default/5448522507835751822'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3851373114383745907/posts/default/5448522507835751822'/><link rel='alternate' type='text/html' href='http://programming-puzzle.blogspot.com/2012/05/commafy.html' title='Commafy()'/><author><name>Unknown</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3851373114383745907.post-8739709458488885494</id><published>2012-05-07T23:22:00.002-07:00</published><updated>2012-05-08T18:12:04.649-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="atoi"/><category scheme="http://www.blogger.com/atom/ns#" term="isNumber"/><category scheme="http://www.blogger.com/atom/ns#" term="strings"/><title type='text'>Implement isNumber()</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;br /&gt;
The code is similar to atop but here we also need to check if number is a double. We need to make sure that the string has at most one decimal point. 
&lt;br/&gt;
The key steps in writing an isNumber function are:
&lt;ul&gt;
&lt;li&gt; Check for sign(-) after we&#39;ve checked for whitespace &lt;/li&gt;
&lt;li&gt; Check if each character lies between 0 &amp; 9 &lt;/li&gt;
&lt;li&gt; Make note of first decimal encountered.&lt;/li&gt;
&lt;li&gt; Return false if we encounter a second decimal point &lt;/li&gt;
&lt;li&gt; Return false as soon as we see a non numeric character &lt;/li&gt;
&lt;li&gt; Return true otherwise. &lt;/li&gt;
&lt;/ul&gt; 
&lt;pre class=&quot;prettyprint&quot;&gt;
bool isNumber(string toTest)
{
    int index =0, decimal = 0;

    //Checking for sign
    if(toTest[index] == &#39;-&#39;)
        index++;
    
    while(index&amp;lt;toTest.length())
    {
        int num = toTest[index] - &#39;0&#39;;

        //Check for numerical digit
        if(num &amp;gt;=0 &amp;&amp; num &amp;lt;=9)
        {
            index++;
            continue;
        }

        //Check if character is a decimal point and if its the first decimal point.
        else if(toTest[index] == &#39;.&#39;)
        {

            //Number cannot have 2 decimal points so return false.
            if(decimal)
                return false;
            else
                decimal = 1;
            index++;
        }
        else
        return false;
    }
    return true;
}
&lt;/pre&gt;
&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programming-puzzle.blogspot.com/feeds/8739709458488885494/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://programming-puzzle.blogspot.com/2012/05/implement-isnumber.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3851373114383745907/posts/default/8739709458488885494'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3851373114383745907/posts/default/8739709458488885494'/><link rel='alternate' type='text/html' href='http://programming-puzzle.blogspot.com/2012/05/implement-isnumber.html' title='Implement isNumber()'/><author><name>Unknown</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3851373114383745907.post-3489664043442424696</id><published>2012-05-07T21:26:00.001-07:00</published><updated>2012-05-08T18:06:28.335-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="strings"/><category scheme="http://www.blogger.com/atom/ns#" term="strtok"/><title type='text'>Implement strtok()</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;br /&gt;
strtok takes in a string and one or more tokens, and it returns a substring which is split using all the tokens provided. The tricky part is that once you call strtok with a string, next time you can call it and pass NULL instead of any string and it will assume the string you passed in previous call.
&lt;br/&gt;
The key steps in writing an atoi function are:
&lt;ul&gt;
&lt;li&gt; Make a static copy of the string passed into the function for reuse on next function call.&lt;/li&gt;
&lt;li&gt; If new string is passed to function, make a copy of it else reuse the last sent string.&lt;/li&gt;
&lt;li&gt; Check for tokens in the string to split on. Make sure to keep track of end of string.&lt;/li&gt;
&lt;li&gt; Update the saved copy of string as well as the string passed into the function.&lt;/li&gt;
&lt;/ul&gt;

&lt;pre class=&quot;prettyprint&quot;&gt;

char* new_strtok(char* str, const char* tokens)
{
    //Making a static string to be used again on next function call.
    static char *temp;
    
    //If string passed to function is not null, copy it to our static variable
    if(str!=NULL)
    {
        temp=(char*)malloc(strlen(str));
        strcpy(temp,str);
    }

    //If the string passed is NULL and even the copy is NULL, we are done and return NULL.
    else if(temp==NULL)
    return NULL;

    //If only the string passed is NULL and the copy still has data, work with it.
    else
    {
        str=temp;
    }

    int chars=0, len = strlen(tokens), flag=0;
    
    //Run the loop till we find a token or our copy is fully parsed.
    while(*temp)
    {
        for(int i=0;i&amp;lt;len;i++)
        {
            if(*temp==tokens[i])
            {
                if(chars==0)
                {
                    flag=1;
                    str++;
                }
                else
                {
                    temp++;
                    str[chars]=&#39;\0&#39;;
                    return str;
                }
            }
        }
        if(flag==0)
            chars++;
        temp++;
        flag=0;
    }
    temp=NULL;
    str[chars]=&#39;\0&#39;;
    return str;
}

&lt;/pre&gt;
&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programming-puzzle.blogspot.com/feeds/3489664043442424696/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://programming-puzzle.blogspot.com/2012/05/implement-strtok.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3851373114383745907/posts/default/3489664043442424696'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3851373114383745907/posts/default/3489664043442424696'/><link rel='alternate' type='text/html' href='http://programming-puzzle.blogspot.com/2012/05/implement-strtok.html' title='Implement strtok()'/><author><name>Unknown</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3851373114383745907.post-4566217885177050579</id><published>2012-05-07T21:24:00.001-07:00</published><updated>2012-11-07T17:03:00.823-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="exponentiation"/><category scheme="http://www.blogger.com/atom/ns#" term="recursion"/><title type='text'>\[x^y\]</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;br /&gt;
Implement a function to calculate exponentiation x^y quickly. x and y are assumed to be integers.
&lt;br/&gt;
&lt;br/&gt;
The key steps in writing an exponentiation function are:
&lt;ul&gt;
&lt;li&gt; Return 1 if exponent(power) is 0 because \[x^0 = 1\] &lt;/li&gt;
&lt;li&gt; Return x if exponent(power) is 1 because \[x^1 = x\] &lt;/li&gt;
&lt;li&gt; Check if exponent is &amp;lt;0. If it is, then call the function again with 1/x and -y because \[x^{-y} = 1/x^y\] &lt;/li&gt;
&lt;li&gt; Call function again using  x*x and y/2 if exponent is even. Say y = 4, we can write \[x^4 = [x^2]^2\] which is essentially calling function again using x*x and y/2.&lt;/li&gt;
&lt;li&gt; If exponent is odd, say y = 2n+1, we can write \[x^y = x^{2n+1} = x * x^{2n}\]Now 2n is even and we can use the even case again by just multiplying x to it. &lt;/li&gt;
&lt;/ul&gt;
&lt;pre class=&quot;prettyprint&quot;&gt;
int power(int x, int y)
{
    if( x == 0)
        return x;

    //Checking if exponent == 0
    if(y==0)
        return 1;

    //Checking if exponent == 1
    else if(y==1)
        return x;

    //Checking if exponent &amp;lt; 0
    else if(y&amp;lt;0)
        return power(1/x, -1*y);

    //Checking if exponent is even and calling function again using x*x and y/2
    else if(y%2==0)
        return power(x*x, y/2);

    //Checking if exponent is odd and calling function again by making exponent even by taking out one x and multiplying it separately and then using the even case again. 
    else
        return x*power(x*x, y/2);
}
&lt;/pre&gt;
This method runs in O(log y) time.
&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programming-puzzle.blogspot.com/feeds/4566217885177050579/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://programming-puzzle.blogspot.com/2012/05/xy.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3851373114383745907/posts/default/4566217885177050579'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3851373114383745907/posts/default/4566217885177050579'/><link rel='alternate' type='text/html' href='http://programming-puzzle.blogspot.com/2012/05/xy.html' title='\[x^y\]'/><author><name>Unknown</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3851373114383745907.post-3762915008174465713</id><published>2012-05-07T21:21:00.000-07:00</published><updated>2012-05-08T11:38:25.982-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="atoi"/><category scheme="http://www.blogger.com/atom/ns#" term="strings"/><title type='text'>Implement atoi()</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;br /&gt;
The key steps in writing an atoi function are:
&lt;ul&gt;
&lt;li&gt; Check for whitespace at the start of the string &lt;/li&gt;
&lt;li&gt; Check for sign(-) after we&#39;ve checked for whitespace &lt;/li&gt;
&lt;li&gt; Check if each character lies between 0 &amp; 9 &lt;/li&gt;
&lt;li&gt; Break as soon as we see a non numeric character &lt;/li&gt;
&lt;li&gt; Make sure to multiply by sign before returning result &lt;/li&gt;
&lt;/ul&gt;
&lt;pre class=&quot;prettyprint&quot;&gt;

int atoi(const char *text)
{
    if(text==NULL || strlen(text)==0)
    return 0;
    
    int len = strlen(text), i = 0, sign = 1, num = 0;
    
    //Checking for whitespace
    while(text[i] == &#39; &#39; || text[i] == &#39;\t&#39; || text[i] == &#39;\n&#39;)
    i++;
    
    //Checking for sign
    if(text[i] == &#39;-&#39;)
    {
        sign=-1;
        i++;
    }
    
    //Checking for numeric characters
    while(i&amp;lt;len)
    {
        if((text[i] - &#39;0&#39;) &amp;gt;=0 &amp;amp;&amp;amp; (text[i] - &#39;0&#39;) &amp;lt;=9)
        {
            num = num*10 + (text[i] -&#39;0&#39;);
            i++;
        }
        //Stopping when we see non numeric character
        else
        break;
    }
    
    // multiply the number by sign before returning
    return num*sign;
}
&lt;/pre&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programming-puzzle.blogspot.com/feeds/3762915008174465713/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://programming-puzzle.blogspot.com/2012/05/implement-atoi.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3851373114383745907/posts/default/3762915008174465713'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3851373114383745907/posts/default/3762915008174465713'/><link rel='alternate' type='text/html' href='http://programming-puzzle.blogspot.com/2012/05/implement-atoi.html' title='Implement atoi()'/><author><name>Unknown</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry></feed>