1

I'm confused on how to make a function that calculate the minimum values from all variable

for example

>>>Myscore1 = 6 
>>>Myscore2 =-3
>>>Myscore3 = 10 

the function will return the score and True if it is minimum value or else it is False . So from the above example the output will be:

>>>[(6,False),(-3,True),(10,False)]
4
  • 3
    Is there any reason your scores aren't in a list? Then you could just do min(scores) Commented May 24, 2013 at 11:18
  • 1
    @BenjaminGruenbaum yeah, it is an assignment and it is written not in a list. Commented May 24, 2013 at 11:20
  • -1 because you did't tried anything so far Commented May 24, 2013 at 11:50
  • I would insist that the scores are in a list. You could for instance first create an empty list, then append the list with new values as they come, i.e.: Myscores = []; Myscores.append(6) etc. Commented May 24, 2013 at 11:52

3 Answers 3

4
scores = [6, -3, 10]
def F(scores):
    min_score = min(scores)
    return [(x, x == min_score) for x in scores]

>>> F(scores)
[(6, False), (-3, True), (10, False)]
Sign up to request clarification or add additional context in comments.

Comments

1

Quite simply :

>>> scores = [Myscore1, Myscore2, Myscore3]
>>> [(x, (x == min(scores))) for x in scores]
[(6, False), (-3, True), (10, False)]

2 Comments

note: Be careful of using functions like min repeatedly when dealing with large data structures, it works fine here but can lead to O(N^2) behaviour
@jamylak : it is true. It would be nice, though, if a modern language were able to see that scores cannot change during this line, and therefore min(score) cannot change either.
1

A one liner using enumerate

scores = [6, -3, 10]

import operator
res = [[scores[i], True] 
   if i == min(enumerate(scores), key = operator.itemgetter(1))[0] 
   else [scores[i], False] 
   for i in range(len(scores))]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.