-1
def cross(a, b, w):
    f1 = b - w
    f2 = a - w

    if f1 < 0:
        f1 = f1 * (-1)
    elif f2 < 0:
        f2 = f2 * (-1)
    else:
        pass

    return f1, f2
x = cross(10, 7, 5)
print(x)

I tried to make this more pythonic, e.g. shorten it and combine it with shorted one line if-conditions, though I couldn't find any way.. Does anyone know how to shorten it?

I want to make "both" the f1 and f2 positive if any of them is negative.

Vishesh Mangla
  • 664
  • 9
  • 20
velotoy
  • 1
  • 1
  • 1
    Use `return math.abs(b-w), math.abs(a-w)` as a one-liner solution – Yossi Levi Sep 19 '20 at 16:46
  • OP's code has an `elif`. If both `f1` and `f2` are negative, only `f1` is turned positive. ALL the answers so far has different behavior. Test with `cross(1,2,5)`. – Aziz Sep 19 '20 at 16:47
  • @velotoy, what is it you're trying to shorten? You can remove the `else: pass`, but everything else seems ok. Also, is it your intention to only change the sign of `f1` if both `f1` and `f2` are negative? – Aziz Sep 19 '20 at 16:49
  • 1
    @Aziz oh I didn't quite catch that! My intentions are to only get positive values, e.g. if either f1 or f2 are negative, output the positive. I also thought of turning it into a string and stripping the "-" to achieve it.. Any opinions? – velotoy Sep 19 '20 at 16:54
  • The `elif` part in your code results in `f2` remaining negative if `f1` was positive too. But since you want both to be positive, @VisheshMangla's original answer is correct. – Aziz Sep 19 '20 at 16:59
  • if you want you can use this type of comparison in python (false,true)[Decision Check] ((-1,(b-c,c-a))[a – Deepak Tripathi Sep 19 '20 at 19:26
  • Does this answer your question? [Does Python have a ternary conditional operator?](https://stackoverflow.com/questions/394809/does-python-have-a-ternary-conditional-operator) – Vishesh Mangla Sep 27 '20 at 16:09

4 Answers4

0

Nothing pythonic here

def cross(a, b, w):
    f1 = b - w
    f2 = a - w

    f1 = f1 if f1>0 else -f1
    f2 = f2 if f2>0 else -f2
    
    return f1, f2

x = cross(10, 7, 5)

print(x)

But better will be just to return the abs value

def cross(a, b, w):
    return list(map(abs, ( b - w,  a - w)))

But if you were talking about if else like logic, well it can be reduced to one liner but it would be just more compact code but not efficient.

def cross(a, b, w):
    f1 = b - w
    f2 = a - w

 
    
    return abs(f1), abs(f2) if f1!= abs(f1) else f2 

x = cross(10, 7, 5)

print(x)
Vishesh Mangla
  • 664
  • 9
  • 20
0

Here you go, this will handle the tricky if/else:

def cross(a, b, w):
    f1 = w-b if (b-w) < 0 else b-w
    f2 = w-a if ((a-w) < 0 and (b-w) >= 0) else a-w
    return f1, f2
x = cross(10, 7, 5)
print(x)
0
def cross(a, b, w):
    f1 = abs(b - w)
    f2 = abs(a - w)
    return f1, f2
print(f1,f2)

>>>cross(10,7,5)

>>>output = (2, 5)
Vishesh Mangla
  • 664
  • 9
  • 20
Chifrijo
  • 66
  • 7
0

one-liner solution:

def cross(a, b, w):
    return (abs(b-w),a-w) if b-w<0 else (b-w, abs(a-w)) if a-w<0 else (b-w,a-w)

x = cross(1, 2, 5)
print(x)

output:

(3, -4)
Yossi Levi
  • 1,258
  • 1
  • 4
  • 7