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
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)







