Follow this blog by Email

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.
    Tuple(n):(height,weight) of nth person

 #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)  
   while i < (len1-1):  
     if(ht_sorted[i+1][1] < ht_sorted[0][1]):  
       max_len = max_len+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:
maximum tower length : 3

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

maximum tower length : 3

No comments: