Friday, July 25, 2014

Couple of interview questions

Write a function that takes an integer (i) and prints out the first 'i' rows of Pascal's Triangle (eg. i =5).

            line = 5;
            var pascalDepth = parseInt(line, 10); //The input integer is provided on a single line as a text value, so this handles it.
            var testTriangle = pascalTriangle(pascalDepth);
            var output = [];
            maxlength = testTriangle.length;
            var html = "<table>"
            for(var i = 0; i < maxlength; i++){
                html += "<tr>";
                html += "<td>";
                html += testTriangle[i];
                html += "</td>";
                html += "</tr>";
            }
            html+= "</table>";

            document.write(html);

            function pascalTriangle(totalLines, triangle) {
                if (typeof triangle === 'undefined') {
                    triangle = [];
                }

                if (triangle.length === totalLines) {
                    return triangle;
                }

                if (triangle.length > 0) {
                    var triangleLine = [];
                    for (var i = 0; i < triangle.length; i++) {
                        if (typeof triangle[triangle.length - 1][i - 1] === 'undefined') {
                            triangleLine.push(1);
                        } else {
                            triangleLine.push((triangle[triangle.length - 1][i - 1] + triangle[triangle.length - 1][i]));
                        }
                    }
                    triangleLine.push(1);
                    triangle.push(triangleLine);
                } else {
                    triangle.push([1]);
                }
                return pascalTriangle(totalLines, triangle);
            }
-----------------------------------------------------------------------------------------

Write a function that prints out a breakdown of an integer into a sum of numbers that have just one non-zero digit.  

eg., given 43018 it should print 40000 + 3000 + 10 + 8.

            var arr = [];
            var i =1;               
            var a = 40118;
            var input = 40118;
            while(a > 0)
            {
            if(a%(Math.pow(10,i)) > 0)
              arr.push(a%(Math.pow(10,i)));       
             
              a = a-a%(Math.pow(10,i++));
            }
            arr.sort(function(a,b){ return b-a;});
            document.write(input +" = " +arr.join(" + "));

Thursday, July 17, 2014

Creating a URL shortner

Here is the actual code i have created using php its very simple and easy to use.

Here is the live Working url : http://shorturl-kartheekgj.rhcloud.com/


Please find the code at:   https://github.com/kartheekgj/shorturl



Wednesday, May 7, 2014

Swap 2 numbers without using third variable

This is a very very very simple question generally asked as first level of logical questions

Swap 2 numbers without using third variable


function fnSwap(a,b){
   console.log("Initial value of a: "+ a);
   console.log("Initial value of b: "+ b);

   // swaping the values of a and b
    b = a+b
    a = b-a
    b = b-a
    console.log("Swaped value of a: "+ a);
   console.log("Swaped value of b: "+ b);
}

Input:

fnSwap(10,20);


output:

Initial value of a: 10
Initial value of b: 20
Swaped value of a: 20
Swaped value of b: 10

Note:
It works with console if you are working with firefox install firebug to see output in console
if you install chrome/IE you can use f12 for console

Friday, February 14, 2014

Change your Facebook Username

This is a little hacky type of method but it works

Step 1 
  • Go to account settings from your
  • Click on ‘Security’
  • Then click the option ‘Deactivate your account.’
  • Give explanation as “Its temporary”.

Step 2
  • Then log in again with your email and password.
  • Done
  • Your account is re-activated.
Step 3
  • Go to account settings from the button on top right.
  • Edit username.
  • Done. Your account username is changed.

Friday, June 28, 2013

Create QR code generator using php-curl


    $url = "https://chart.googleapis.com/chart?";
    $strPost = "cht=qr&chs=177x177&chl=http://opendummies.blogspot.in/";
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    // curl_setopt($ch, CURLOPT_PROXY, $proxy);
    curl_setopt($ch, CURLOPT_HEADER, 1);
    curl_setopt($ch, CURLOPT_HTTPGET,1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($ch, CURLOPT_HTTPHEADER, Array("Content-Type: image/jpeg"));
    curl_setopt($ch, CURLOPT_HEADER, FALSE);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $strPost); // add POST fields
    curl_setopt($ch, CURLOPT_POST, 1);
    header('Content-Type: image/jpeg');
    echo $result = curl_exec($ch);
    curl_close($ch);
    

output:

Tuesday, June 25, 2013

URL shortner using Bitly - PHP

For creating a shorten url you need to create an account in the following link:

http://dev.bitly.com/my_apps.html

Now go to 

https://bitly.com/a/oauth_apps 

Here you need to create a accesstoken if you are creating for the first time it prompts for the password.

Then you get a alpha numeric value as accesstoken section.


Now use the following Code:

    $url = "https://api-ssl.bitly.com/v3/shorten";
    $strPost = "longUrl=http%3A%2F%2Fgoogle.com%2F&   access_token=YOUR ACCESSTOKEN";
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    // curl_setopt($ch, CURLOPT_PROXY, $proxy);
    curl_setopt($ch, CURLOPT_HEADER, 1);
    curl_setopt($ch, CURLOPT_HTTPGET,1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($ch, CURLOPT_HTTPHEADER, Array("Content-Type: application/json"));
    curl_setopt($ch, CURLOPT_HEADER, FALSE);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $strPost); // add POST fields
    curl_setopt($ch, CURLOPT_POST, 1);
    $result = curl_exec($ch);
    var_dump($result);
    curl_close($ch);


The output of this file will return a json string
 
{
    "status_code" : 200,
    "status_txt" : "OK",
    "data" : {
        "long_url" : "http:\/\/google.com\/",
        "url" : "http:\/\/bit.ly\/14UO8lv",
        "hash" : "14UO8lv",
        "global_hash" : "900913",
        "new_hash" : 1
    }
}



NOTE
The bitly code is to be used for personal accounts cannot be used for Enterprise use if you want to use it for Enterprise you need to request them for the details but this code even works for that Code too.

Google shorten URL - a Simple CURL script


    $url = "https://www.googleapis.com/urlshortener/v1/url";
    $strPost = '{"longUrl": "YOUR LONG URL"}';
 
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    //if your are using proxy url use proxy setting
    // curl_setopt($ch, CURLOPT_PROXY, $proxy);
    curl_setopt($ch, CURLOPT_HEADER, 1);
    curl_setopt($ch, CURLOPT_HTTPGET,1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($ch, CURLOPT_HTTPHEADER, Array("Content-Type: application/json")); //since i am requesting json response
    curl_setopt($ch, CURLOPT_HEADER, FALSE);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $strPost); // add POST fields
    curl_setopt($ch, CURLOPT_POST, 1);
    $result = curl_exec($ch);
    var_dump($result);
    curl_close($ch);
    exit;

If you dont want to use the google url shortner want to create your own


check it here :
http://opendummies.blogspot.in/2014/07/creating-url-shortner.html