i have been working on this course work i got my hands on to learn python, and i have just been working on an exercise, i was wondering if i perhaps cheated though? The exercise says the following;

write a function that takes a list of real numbers and returns both the maximum and the minimum values. Put the function in a module file with some simple test code. Make it work for positive and negative floating point numbers.

i wrote the following as a solution;

def MinMax(numbers):
    ''' This is a function to sort a list of real numbers,
        and return the minimum and maximum values. Input  a string of numbers.
    '''
    a = numbers   
    a.sort()
    minimum=a[0]
    maximum=a[-1]
    return minimum, maximum

is it cheating to use sort() ? it does make it really easy.

sorry if this wasn't appropriate for this forum or something.
Thank you for any answers

Recommended Answers

All 4 Replies

I think it is cheating, you can cheat even more

def MinMax(numbers):
    ''' This function finds the minimum and maximum values.
    Input  a list of real numbers.
    '''
    return min(numbers), max(numbers)

Try to write a function without sort(), min(), max(). You can use a loop and the comparison operators <, >, etc.

It's not cheating if the instructions don't forbid it.
You would do the same for a real-world program.
For practice, however, you should know how to do it with and without the sort.

I think it is cheating, you can cheat even more

def MinMax(numbers):
    ''' This function finds the minimum and maximum values.
    Input  a list of real numbers.
    '''
    return min(numbers), max(numbers)

Try to write a function without sort(), min(), max(). You can use a loop and the comparison operators <, >, etc.

haha, thanks , i will keep those two in mind. I think i will finish the exercise the way im going then redo it without cheating as well.

It's not cheating if the instructions don't forbid it.
You would do the same for a real-world program.
For practice, however, you should know how to do it with and without the sort.

thanks for your answer, i will finish it and redo it without any cheating, should be fun.
and i thought so too, it didn't specify not to do it and it did make it easier!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.