mhasoba / TheMulQuaBio

The interactive, online Multilingual Quantitative Biologist
MIT License
36 stars 81 forks source link

Issue on page /notebooks/05-Python_I.html #124

Open pinguinium99 opened 11 months ago

pinguinium99 commented 11 months ago

in cfexercises1.py the function foo_3 will not order the three values if the smallest value is in the z position in the function.

def foo_3(x, y, z):
    if x > y:
        tmp = y
        y = x
        x = tmp
    if y > z:
        tmp = z
        z = y
        y = tmp
    return [x, y, z]

foo_3(8,6,1) 

returns: (6, 1, 8)

davidorme commented 11 months ago

This may be an intentional bug 😄 I would argue though that using the tmp variable is a missed opportunity to show off Python unpacking:

def foo_3(x, y, z):
    if x > y:
         y, x = x, y
    if y > z:
         y, z = z, y
    return [x, y, z]
icgk523 commented 11 months ago

This does not work for the exceptional case where z is the lowest value. The following code should solve the problem (but sorts it in descending order):

def foo_3(x, y, z):
    if z > y and y > x:
        x, y, z = z, y, x
    if y > x and x > z:
        x, y ,z = y, x, z
    return [x, y, z]
davidorme commented 11 months ago

Mine wasn't intended to fix the bug (which may be an intentional feature), but just to point out that the tmp variable can be avoided using Python features.