Thursday 25 July 2013

Solution to the puzzle Towering personalities

Here is my solution to the programming puzzle of Towering personalities in python.
This python function takes a list of tuples as its argument.
 Where
    Tuple(n):(height,weight) of nth person

#!/usr/bin/python  
 #This function takes a list of tuples. Tuple(n):(height,weight) of nth person  
 def htower_len(ht_wt):  
   ht_sorted = sorted(ht_wt,reverse=True)  
   wt_sorted = sorted(ht_wt,key=lambda ht_wt:ht_wt[1])  
   max_len = 1   
   len1 = len(ht_sorted)  
   i=0  
   j=0  
   while i < (len1-1):  
     if(ht_sorted[i+1][1] < ht_sorted[0][1]):  
       max_len = max_len+1  
     i=i+1        
   print "maximum tower length :" ,max_len  

I constructed 3 different test cases to test this function.
This above function can be invoked using below test app code, and their results are shown below the function calls:

Test case 1:
htower_len([(5,75),(6.7,83),(4,78),(5.2,90)])
maximum tower length : 3

htower_len([(65, 100),(70, 150),(56, 90),(75, 190),(60, 95),(68, 110)])
maximum tower length : 6

htower_len([(3,2),(5,9),(6,7),(7,8)])  
maximum tower length : 3

Tuesday 23 July 2013

Towering personalities... A Puzzle.

Here is a nice programming puzzle to test - algorithm design , coding skills:
We are designing a tower consisting of people standing atop another person's shoulders. For practical reasons, each person must be both shorter and lighter than the person below him or her.
Given the heights and weights of each person in the circus, write a method to compute the largest possible number of people in such a tower, i.e. effectively the highest possible height of such human tower in number of persons.  
E.g
 Input (height, weight): (65, 100) (70, 150) (56, 90) (75, 190) (60, 95) (68, 110)
Output: The longest tower is of length 6 and includes from top to bottom:
 (56, 90) (60,95) (65,100) (68,110) (70,150) (75,190)

I will post my solution in a while.

Wednesday 17 July 2013

My solution to the puzzle posted here few days back is below.

Implement a function h(n) as below:

h(h(n)) = -n

 static int run = 0  
 int f(n)  
 {  
 run++;  
 return (pow(-1,(run-1))*n);  
 }   

Monday 15 July 2013

Python functions - Default values as function arguments

There is a slightly quirky thing(I think so, maybe other's don't) to note about Python functions if one has to  make one of the arguments as a default argument.
Remember this be heart:
Python default argument to a function are evaluated and stored only when the function is defined which, which happens only once.

So if you have a function as below:

def mygettime(curtime = time.time())
      print curtime

So everytime you call mygettime
>>mygettime()
>>mygettime()
>>mygettime()

expecting it to print different times , you are in for a surprise if you haven't got this cleared.

It prints same value (seconds since epoch time 1 Jan 1970 ) , giving you a notion that you have just stopped the time..
No you haven't.

What is happening then:
Python evaluates curtime = time.time() only once at the time function mygettime is defined 
which returns the time in seconds since the epoch and that value is what is stored and same value is printed in every call which was evaluated and stored only once.

There are different aspects to this behaviour depending upon whether the default argument is a mutable, or immutable python data type.






Sunday 14 July 2013

                        function(function(n))=...

Read this very interesting puzzle recently. It can be viewed from many different perspectives - mathematics, signal processing , computer science program; So whatever stream you are comfortable with, you can try to solve this one from that perspective and using the tools and concepts in that stream.

Implement a function h(n) as below:

h(h(n)) = -n

I was able to wrote a C programming language solution for it. 
Will post it here soon.

                                      C++ Copy constructors   


Copy constructor is a special type of constructor invoked when initializing an object of a class when the object is created. During the creation, if  a implicit type conversion happens, due to arguments  passed to the object of a class during, the copy constructor needs a const reference as its argument in the constructor definition and declaration, else the compiler complains as it is a violation of standard[To paraphrase the relevant clause in C++ Standard: "An rvalue cannot bind to a non-const reference. "

The below post explains the problem in simple terms:-
http://stackoverflow.com/questions/17597870/c-array-of-objects-of-a-class-with-overloaded-constructors