views:

59

answers:

2

I should write a function min_in_list(munbers), which takes a list of numbers and returns the smallest one. NOTE: built-in function min is NOT allowed!

def min_in_list(numbers):
    the_smallest = [n for n in numbers if n < n+1]
    return the_smallest

What's wrong?

+1  A: 
def min_of_two(x, y):
    if x >= y: return x
    else: return y

def min_in_list(numbers):
    return reduce(min_of_two, numbers)

You have to produce 1 number from list, not just another list. And this is work for reduce function (of course, you can implement it without reduce, but by analogy with it).

Andrei
Exactly what I need!
Gusto
@Gusto: yes, but how will you explain to your teacher how you reached that solution?
ΤΖΩΤΖΙΟΥ
@ ΤΖΩΤΖΙΟΥ I don't have to explain anything - this is a preparation to the exam, and not a 'homework' as it is.
Gusto
A: 

Here you go. This is almost certainly about as simple as you could make it. You don't even have to give me credit when you turn the assignment in.

import itertools
import functools
import operator

def min(seq, keyfun=operator.gt):
    lt = lambda n: functools.partial(keyfun, n)

    for i in seq:
        lti = lt(i)
        try:
            next(itertools.ifilter(lti, seq))
        except:
            return i
James Cunningham
too long and too complicated
Gusto