Friday, August 7, 2020

recursion x^y ( X power Y) where X and y are input.

 

/*A recursive algorithm is an algorithm that calls itself.

 * A recursive algorithm has Base case:

 * output computed directly on small inputs

 

Recursive algorithms can be used to solve some problems such as Towers of Hanoi (TOH),

Inorder/Preorder/Postorder Tree Traversals, DFS of Graph, easily.

Recursion techniques can be also divided into following types:

1. Linear Recursion

2. Binary Recursion

3. Tail Recursion

4. Mutual Recursion

5. Nested Recursion

*

* Given Problem Statement.

*Write a program to calculate using recursion x^y ( X power y) . X and y are input.

 

Calculate time complexity  of written code.

 

Write a better solution in terms of time complexity.

 

Calculate time complexity again for a better solution

 

*/

 

////////*  Solution *//////

//As the output depends on every element of the input. O(n) or O(n*log(n)) are the best results of time complexities in this case.

//For small number of inputs the O(n log n) < O(n) ? Let n = 10 then 10*log(10) =10*1=10 which is obviously same value.

//Let n = 5 then 5*log5 = 3.49 which is obviously smaller than 5,which is better than O(5).

//Aiming for O(1) which is constant time but for the recursion problems it is difficult to achieve.

 

/*

 *

 * one big concern is the recursion depth (how many times the algorithm calls itself) .

 * If the depth is small, recursive algorithms are often a good solution,and  O(n*log(n)) Time complexity  holds good.

 * If the recursion depth is large, then the better time complexity tends to O(n),when the input is large number.

 * If the recursion depth is huge, then running out of stack memory becomes a real concern,

 * hence Iterative solutions (using loops) can be preferred to recursive algorithms.

 *

 *

 */

////////*  Solution *//////

 

 

 

#include <chrono>

#include <sstream> // for std::stringstream

#include <iostream>

 

using namespace std;

using namespace std::chrono;

 

typedef high_resolution_clock Clock;

typedef Clock::time_point ClockTime;

 

//Iterative Solution Non-Recursive [Time-Complexity O(log(n))]

long pow(int x, int n)

{

  long pow = 1;

  while ( n )

  {

         if ( n & 1 )

         {

           pow = pow * x;

           --n;

         }

         x = x*x;

         n = n/2;

  }

  return pow;

}

 

// Iterative solution to calculate pow(x, n) using binary operators [Time-Complexity O(log(n))]

 

int powB(int x, unsigned n)

{

    // initialize result by 1

    int pow = 1;

 

    // do till n is not zero

    while (n)

    {

        // if n is odd, multiply result by x

        if (n & 1)

            pow *= x;

 

        // divide n by 2

        n = n >> 1;

 

        // multiply x by itself

        x = x * x;

    }

 

    // return result

    return pow;

}

 

 

//Tail Recursion [Time-Complexity O(n)]

 

float power(int x, unsigned n)

{

 

    if (x==0)

    {

        return 0;

 

    }

    else if(n==0)

    {

        return 1;

 

    }

    else if (n>0)

    {

        return( x* power(x,n-1));

    }

    else

    {

        return ((1/x)*power(x,n+1));

    }

}

void printExecutionTime(ClockTime start_time, ClockTime end_time);

 

int main(int argc , char*argv[])

{

 

////////////////////////////////////////////////////////

     //X and y are inputs.//reading x,y input from Commandline.

    cout << "Program name " <<argv[0];

    std::stringstream convertx{ argv[1]};

    std::stringstream convertn{ argv[2]};

 

    int x;

    unsigned int n;

        if (!(convertx >> x) && !(convertn >> n))  {// do the conversion

            x = 0; // if conversion fails, set x,n to a default value

            n = 0;

        }

////////////////////////////////////////////////////////

 

    ClockTime start_time = Clock::now();

 

   // power(x,n);

   // pow(x,n);

    powB(10,10);

////////////////////////////////////////////////////////

    ClockTime end_time = Clock::now();

 

    printExecutionTime(start_time, end_time);

}

 

void printExecutionTime(ClockTime start_time, ClockTime end_time)

{

    auto execution_time_ns = duration_cast<nanoseconds>(end_time - start_time).count();

    auto execution_time_ms = duration_cast<microseconds>(end_time - start_time).count();

    auto execution_time_sec = duration_cast<seconds>(end_time - start_time).count();

    auto execution_time_min = duration_cast<minutes>(end_time - start_time).count();

    auto execution_time_hour = duration_cast<hours>(end_time - start_time).count();

 

    cout << "\nExecution Time: ";

    if(execution_time_hour > 0)

    cout << "" << execution_time_hour << " Hours, ";

    if(execution_time_min > 0)

    cout << "" << execution_time_min % 60 << " Minutes, ";

    if(execution_time_sec > 0)

    cout << "" << execution_time_sec % 60 << " Seconds, ";

    if(execution_time_ms > 0)

    cout << "" << execution_time_ms % long(1E+3) << " MicroSeconds, ";

    if(execution_time_ns > 0)

    cout << "" << execution_time_ns % long(1E+6) << " NanoSeconds, ";

}

 

 

//Understanding

The iterative solution  performs much better than the recursive one, the recursive solution  need more memory for function call stacks so slow.

 

Friday, January 24, 2020

Hash Tables (Key Value based Associative data structure )


Hash Tables


              A hash table is a data structure also known as a dictionary or associative array   except you store each value (object) using a key. It is associative type compact (key-value) based data structure.


·      To create an empty hash table.
                     $hash_Key_Val =  @{}

·      Add value one by one
                     $key = 'Samsatya'
                     $value = 36
$hash_Key_Val[$key] = $value

·      Add values in a loop
for($i = 0;$i -lt $Key.Count ; $i++)
                     {
                               $hash_Key_Val[$Key[$i]] = $Val[$i]; 
                     }

·      Check for the match in the value and return corresponding key
                    foreach($value in $List)
                     {
                                            $CorrespKey =  ($hash_Key_Val.GetEnumerator() | ? {$_.Value -eq $value }).Name     
}


Tuesday, February 26, 2019

Umm Kulthum "Enta Omri" "You Are My Life" November 1967.

Your eyes returned me to the days that had gone by
They taught me to regret the past and its wounds
That which I experienced before my eyes saw you
What is the wasted life to me?
You are my life whose morning began with your light
How much of my life before you has passed and gone by
My darling, how much of my life has gone
My heart never experienced one bit of joy before you
And had never tasted in this world anything but the flavor of injury
I've now just begun to love my life
I've now begun to fear for the passing of my lifetime
Every joy that I longed for before you was fantasy
My heart and mind meet find it in the light of your eyes
Oh life of my heart, oh you who is more precious than my life
Why didn't I meet your love, my darling, sooner?
The sweet nights and the desire and the love
For so long my heart carried them for you
Taste love with me
Taste love with love
From the feeling of my heart whose desire extended to your feeling
Give me your eyes they reflect my eyes in their world
Give me your hands their touch calms my hands
Oh my darling come on forget about that which has passed us
Oh you who is more precious than my days
Oh you who is sweeter than my dreams
Take me to your longing take me
Pull me away from the universe
Far, far away you and I
From the love that awakens our days
From the desire than sleeps our nights
I've reconciled time my days with you
I've reconciled time with you
I forgot my pains with you
And I forgot with you my woes
Your eyes called me to the days that have passed
They taught me to regret the past and its wounds
That which I experienced before my eyes saw you
What is the wasted life to me?

Monday, February 19, 2018

INVERSE KINEMATICS OF A ROBOTIC MANIPULATOR

Inverse kinematics has one of the most difficult problems for a robotic manipulator.
Below are the problems of a Robotic Manipulator.

  1. Singularities.
  2. The possibility of no solution.
  3. The existence of multiple solutions.


  • Singularities.
Singularity is a place of  infinite acceleration.

  • The possibility of No Solution.
A point outside the workspace of the robot has no solution.
  • The Existence of Multiple Solutions.
Redundancy of movement caused by redundant links.



Example

1DOF pendulum desired endeffector positionP  given as an (x,y)  location.



Inverse kinematics of 1DOF?considering pendulum.
Question
1.what is  θ1?
ans:

given position is (x,y).

θ1   is obtained by tan^-(y/x)

In  MatLab

atan2(Y,X)

Check this link.

http://web.eecs.umich.edu/~ocj/courses/autorob/autorob_10_ik_closedform.pdf

Tuesday, November 15, 2016

My First Bug fixed(Windows) during my stay in the Big company C...o. After a Big gap of 3 years( To complete MTech)

How can you do this operation?

I was working on Windows a very good laptop HP. Our client Cisco had their privileged code base for many years and i started working by fixing some bugs in the Install Shield Script based Installer Applications.

First bug which i worked on was when System crashes during Installation procedure, it does not stop there rather it shows that particular operation is successful and finally at the end of Installation  shows that operation finished with errors.

So to Reproduce the error I have renamed the .lib files with some error(for Ex MSCRT.lib to MSFFGE.lib) some thing like this, misspelled libs on my machine, when the Install shield script tries to copy the file after it does the validation for the file name, it comes to this part of the code, now all i have to look into was an IF-Else block of code where the non negative value shows success and any thing less than or greater than zero  is a failure and return only zero for the success case.

BugFix  I provided was (If retcode != 0 ) {show error message)else {show success message}

this worked and my task was over,  as a starter to work in cisco code base i took 3 days to complete this task,
1 day to find out the bug and propose the fix.
1 day to know the procedure to check in code to git repo.(Pull request, check the build in the test build, and work on those code review comments, once approved, submit the pull request.

1 day to actually do all the stuff to close the task and mark it as closed in the agile tool.
Scrum works tool that they were working on was scrum works,(Rally was the tool, while i was working in John Deere).


so it was a happy fix that i provided and i was happy, that i could do this, there was one Lady who reviewed my code and she was instrumental for me to know many of client's software terminologies, as she had already been working there for almost 8 years, one more lady was there, and she paired up with me for the next big task,
To Remove Unapproved JRE Certificates for Java store.

I will write about this in my next topic.



compile gcc/g++5.1 on precise1-Ubuntu

uname -a
Linux  3.13.0-32-generic #57~precise1-Ubuntu SMP Tue Jul 15 03:51:20 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux


Run the dependencies package script and download all the packages.
gcc-5.1.0/contrib/download_prerequisites

note:- sh is omitted from download_prerequisites

Now compile all the packages and link them to compile required gcc.

for gmp
 ../configure  --prefix=/opt/mygmp

for isl

  cd isl-0.14/
  ls
  mkdir objdir;cd objdir
 ../configure --with-gmp-include=/opt/mygmp4.3.2/include  --with-gmp-lib=/opt/mygmp4.3.2/lib --prefix=/opt/myisl0.14



for mpfr 
../configure --with-gmp-include=/opt/mygmp4.3.2/include   --with-gmp-lib=/opt/mygmp4.3.2/lib  --prefix=/opt/mympfr2.4.2



for gcc5.1

mkdir gccbuild;cd gccbuild
 ../gcc-5.1.0/configure  --enable-threads=posix --disable-bootstrap --enable-languages=c,c++ --disable-stage1-checking --disable-multilib --with-gmp-include=/opt/mygmp4.3.2/include --with-gmp-lib=/opt/mygmp4.3.2/lib --with-mpfr-include=/opt/mympfr2.4.2/include --with-mpfr-lib=/opt/mympfr2.4.2/lib --with-mpc-include=/opt/mympc0.8.1/include --with-mpc-lib=/opt/mympc0.8.1/lib --with-isl-include=/opt/myisl0.14/include --with-isl-lib=/opt/myisl0.14/lib --prefix=/opt/mygcc5.1



=======

Tuesday, February 16, 2016

Passport Payment Status Pending to Success How to do it

Go to https://portal2.passportindia.gov.in, Login using your credentials.
Click on View Saved/Submitted Applications.

Track Payment Status
PAYMENT ONCE MADE FOR AVAILING PASSPORT SERVICES WILL NOT BE REFUNDED.
IF THE PAYMENT IS SUCCESSFULLY DONE, PLEASE DO NOT PAY AGAIN.
Application Reference NumberXX-XXXXXXXXXX 
Given NameXXXXXX XXXXXXXXX XXXXXX
Service TypeREISSUE
Type of ApplicationNORMAL
Payment StatusSuccess 

If you see Payment Status as pending at "Track Payment Status" you have to log out of the passport india portal,and login again.Now check status again,and Schedule Appointment and proceed accordingly.


I hope this helps.