DonationCoder.com forum
 
Read-only archive. This is a static copy of the DonationCoder.com forum. Reading and searching work as before; logging in, posting and private messages do not.
Archive of 2026-10-21 · About this archive · Main site

Author Topic: Functional programming test in Python. Finding the smallest item  (Read 6,898 times)

kartal

  • Supporting Member
  • Joined in 2008
  • **
  • Posts: 1,529
I have been using Python for a while but never delved into functional programming( http://docs.python.org/dev/howto/functional.html ). Here is my first try.  I am using a recursive function to slice the array into 2 pieces, smaller and bigger than average in given pass until the lenght achieves 1. In the past for practice I used loops for this stuff but functional+recursive makes more sense to me now.

This will find the smallest item in the array

Code: (python)
import random, time
print random.seed(time.time())
myarray=[random.randrange(1,100) for a in range(0,10)]  #this creates a random list array


print myarray

def findSmallest(array):
 
    if len(array)<2: return

    array_sum=reduce(lambda x,y:x+y,array) #Calculate the sum of all the elements inside the array by using the "reduce" function
    array_avg=array_sum/len(array)

    smallers=filter(lambda x:x<array_avg,array) #This line filters the array items based on their size compared to the average of the array

#----------------------------------------->Debug   
    #print " sum: ",array_sum,
    #print " average: ",array_avg
    #print "\n"
    print smallers
#----------------------------------------->Debug   
 
    findSmallest(smallers)


if __name__=="__main__":
#    pass
    findSmallest(myarray)
« Last Edit: February 22, 2010, 09:10 PM by kartal »

CWuestefeld

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 1,009
    • Participated in NANY Challenge Once
Re: Functional programming test in Python. Finding the smallest item
« Reply #1 on: February 23, 2010, 05:53 PM »
That's similar to the QuickSort algorithm. Recursively break the set into pairs, with one pair having everything < a given value, the other having those >=.

One difference is that QS assumes that the first item in a given (sub)set is a good representative of the median value. Thus, QS is O(N log(N)) on average (typical with random data), but if you're data is already sorted it falls to O(N^2) like a Bubble Sort.

Anyway, you're still stuck with O(N log(N)) complexity on something that's theoretically possible in O(N) for any input dataset.

But I'm still learning functional programming myself, and don't know how to do any better than your proposal.


kartal

  • Supporting Member
  • Joined in 2008
  • **
  • Posts: 1,529
Re: Functional programming test in Python. Finding the smallest item
« Reply #2 on: February 23, 2010, 06:14 PM »
You are right but I was not trying to come up with a fast method, rather I just wanted to entertain the basic functional programming methods in Python. The method would work faster if I can make the summing faster I think since LogN is not that terrible. Python has a built in array sum mehtod probably faster than using reduce to sumup the array, but again that would only effect the speed of the calculation not the number of steps :)

One another way I can think of without going through the recursion way is using "map", "filters" and "lamda" in the same line. I need to see if that is doable

tinjaw

  • I'm so glad breakbeat techno isn't an illegal drug
  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 1,927
    • Participated in NANY Challenge Twice
    • Coding Snacks
    • Military Service Badge - US Army
    • Linux User
Re: Functional programming test in Python. Finding the smallest item
« Reply #3 on: February 23, 2010, 08:09 PM »
There is also some good introductory material on functional programming in python in chapter one of Text Processing in Python.

Original address: https://www.donationcoder.com/forum/index.php?topic=21845.0