Quantcast
Channel: Compact way of writing (a + b == c or a + c == b or b + c == a) - Stack Overflow
Browsing all 17 articles
Browse latest View live

Answer by 303 for Compact way of writing (a + b == c or a + c == b or b + c...

There is little to gain with such a small expression but using a function just to not having to repeat the summation and comparison could be an option. It makes it a bit more maintainable when wanting...

View Article



Answer by shA.t for Compact way of writing (a + b == c or a + c == b or b + c...

As an old habit of my programming, I think placing complex expression at right in a clause can make it more readable like this:a == b+c or b == a+c or c == a+bPlus ():((a == b+c) or (b == a+c) or (c ==...

View Article

Answer by Jack for Compact way of writing (a + b == c or a + c == b or b + c...

Don't try and simplify it. Instead, name what you're doing with a function:def any_two_sum_to_third(a, b, c): return a + b == c or a + c == b or b + c == aif any_two_sum_to_third(foo, bar, baz):...

View Article

Answer by Vitalii Fedorenko for Compact way of writing (a + b == c or a + c...

Python 3:(a+b+c)/2 in (a,b,c)(a+b+c+d)/2 in (a,b,c,d)...It scales to any number of variables:arr = [a,b,c,d,...]sum(arr)/2 in arrHowever, in general I agree that unless you have more than three...

View Article

Answer by Igor Levicki for Compact way of writing (a + b == c or a + c == b...

How about just:a == b + c or abs(a) == abs(b - c)Note that this won't work if variables are unsigned.From the viewpoint of code optimization (at least on x86 platform) this seems to be the most...

View Article


Answer by Paul J Abernathy for Compact way of writing (a + b == c or a + c ==...

The solution provided by Alex Varga "a in (b+c, b-c, c-b)" is compact and mathematically beautiful, but I wouldn't actually write code that way because the next developer coming along would not...

View Article

Answer by Arcanum for Compact way of writing (a + b == c or a + c == b or b +...

The following code can be used to iteratively compare each element with the sum of the others, which is computed from sum of the whole list, excluding that element. l = [a,b,c] any(sum(l)-e == e for e...

View Article

Answer by Barry for Compact way of writing (a + b == c or a + c == b or b + c...

If we look at the Zen of Python, emphasis mine:The Zen of Python, by Tim PetersBeautiful is better than ugly. Explicit is better than implicit.Simple is better than complex. Complex is better than...

View Article


Answer by mbeckish for Compact way of writing (a + b == c or a + c == b or b...

(a+b-c)*(a+c-b)*(b+c-a) == 0If the sum of any two terms is equal to the third term, then one of the factors will be zero, making the entire product zero.

View Article


Answer by Hammerite for Compact way of writing (a + b == c or a + c == b or b...

def any_sum_of_others (*nums): num_elements = len(nums) for i in range(num_elements): discriminating_map = map(lambda j: -1 if j == i else 1, range(num_elements)) if sum(n * u for n, u in zip(nums,...

View Article

Answer by Padmabushan for Compact way of writing (a + b == c or a + c == b or...

In a generic way,m = a+b-c;if (m == 0 || m == 2*a || m == 2*b) do_stuff ();if, manipulating an input variable is OK for you,c = a+b-c;if (c==0 || c == 2*a || c == 2*b) do_stuff ();if you want to...

View Article

Answer by Alex Varga for Compact way of writing (a + b == c or a + c == b or...

Solving the three equalities for a:a in (b+c, b-c, c-b)

View Article

Answer by Mark Ransom for Compact way of writing (a + b == c or a + c == b or...

Python has an any function that does an or on all the elements of a sequence. Here I've converted your statement into a 3-element tuple.any((a + b == c, a + c == b, b + c == a))Note that or is short...

View Article


Answer by wwii for Compact way of writing (a + b == c or a + c == b or b + c...

Request is for more compact OR more pythonic - I tried my hand at more compact.givenimport functools, itertoolsf = functools.partial(itertools.permutations, r = 3)def g(x,y,z): return x + y == zThis is...

View Article

Answer by ThatGuyRussell for Compact way of writing (a + b == c or a + c == b...

If you will only be using three variables then your initial method:a + b == c or a + c == b or b + c == aIs already very pythonic.If you plan on using more variables then your method of reasoning...

View Article


Answer by Cyphase for Compact way of writing (a + b == c or a + c == b or b +...

If you know you're only dealing with positive numbers, this will work, and is pretty clean:a, b, c = sorted((a, b, c))if a + b == c: do_stuff()As I said, this only works for positive numbers; but if...

View Article

Compact way of writing (a + b == c or a + c == b or b + c == a)

Is there a more compact or pythonic way to write the boolean expressiona + b == c or a + c == b or b + c == aI came up with a + b + c in (2*a, 2*b, 2*c)but that is a little strange.

View Article

Browsing all 17 articles
Browse latest View live




Latest Images