What does “or” do in a python return statement / Explanation of list subset sum


I was doing coderbyte's Python Array Addition I challenge, but I couldn't get it quite right. I saw another user's correct code, and I'm kind of puzzled by it. Mainly, what does "or" do in a return statement? It's preventing me from fully understanding the solution to this problem. Thanks.

The question is as follows: Have the function ArrayAdditionI(arr) take the array of numbers stored in arr and return the string true if any combination of numbers in the array can be added up to equal the largest number in the array, otherwise return the string false. For example: if arr contains [4, 6, 23, 10, 1, 3] the output should return true because 4 + 6 + 10 + 3 = 23. The array will not be empty, will not contain all the same elements, and may contain negative numbers.

def subsetsum(target, arr): if len(arr) == 0: return target == 0 return subsetsum(target, arr[1:]) or subsetsum(target - arr[0], arr[1:]) def ArrayAdditionI(arr): arr = sorted(arr) target = arr[-1] arr = arr[:-1] return 'true' if subsetsum(target, arr) else 'false' # keep this function call here # to see how to enter arguments in Python scroll down print ArrayAdditionI(raw_input())


Here's how to break this down:

return subsetsum(target, arr[1:]) or subsetsum(target - arr[0], arr[1:])

This has the form return a or b where a = subsetsum(target, arr[1:]) and b = subsetsum(target - arr[0], arr[1:]).

If bool(a) is True, then the expression a or b short circuits and returns whatever the value of a is. If bool(a) is False, then b must be evaluated to determine the value of the expression a or b.

Thus, return a or b is shorthand for the following, with the benefit that b is not evaluated (if it's a function) if bool(a) is True, thanks to short-circuiting logic.

if bool(a): return a else: return b


It's not doing anything special in the context of "return", so that's kind of confusing. Take away return and just look at a <a href="https://docs.python.org/2/reference/expressions.html#booleans" rel="nofollow">boolean expression</a> of

False or True

Will evaluate to True because one of the statements evaluates to True. The example with return is just taking the result of evaluating the boolean expression and returning it. So True if any of the subsetsum() calls return True, otherwise False.

>>> False or True True >>> False or False False


  • What diagnostic tools are available for Node.js applications?
  • How can I spawn a long running process in a Perl CGI script?
  • VBA: Extract Top 'x' Entries from each category
  • How read between delimiters in php DOM of a XML file?
  • Crafting a LINQ based solution to determine if a set of predicates are satisfied for a pair of colle
  • Runtime error in UVA Online Judge [closed]
  • reduce/reduce conflicts using ocamlyacc
  • How to write string.Contains(someText) in expression Tree
  • How to get the index of element in the List in c#
  • Rails AREL .where statement
  • JSON - slashes not escaping
  • Inversing an interpolation of rotation
  • Xmonad multiple submap key combos
  • VBA Excel, loop through variables
  • How does this usort cmp function actually work?
  • Keep this build forever option - Jenkins
  • Converting a WriteableBitmap image ToArray in UWP
  • How to define and use opencv mat of user type
  • Is there any way to access browser form field suggestions from JavaScript?
  • Splitting given String into two variables - php
  • NetLogo BehaviorSpace - Measure runs using reporters
  • Is my CUDA kernel really runs on device or is being mistekenly executed by host in emulation?
  • Why ng-show works with ng-repeat but ng-if doesn't? [duplicate]
  • Cassandra Data Model
  • Hazelcast - OperationTimeoutException
  • Build own AppleScript numerical error handling
  • Rearranging Cells in UITableView Bug & Saving Changes
  • AT Commands to Send SMS not working in Windows 8.1
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • Benchmarking RAM performance - UWP and C#
  • Angular 2 constructor injection vs direct access
  • Getting Messege Twice Using IMvxMessenger
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • apache spark aggregate function using min value
  • sending mail using smtp is too slow
  • Busy indicator not showing up in wpf window [duplicate]
  • Sorting a 2D array using the second column C++
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • How can I use `wmic` in a Windows PE script?
  • Python/Django TangoWithDjango Models and Databases