Find the Odd odds

14

1

Given an unordered collection of positive integers by any reasonable input method, return all of the sub-collections that have an odd number of odd elements (i.e. have an odd total).

This is so you should aim to minimize the byte count of your program.

Since some languages only have ordered collections (lists, arrays, vectors, etc.) or don't have an unordered collection that allows duplicates, you may use ordered collections (regardless of your language choice), however you should not output any duplicate collections with different orders (e.g. [2,3] and [3,2]). You may output in whatever order you see fit.

Test cases

[2,3,7,2] -> [[3],[7],[2,3],[2,7],[2,2,3],[2,2,7]]
[2,4,6,8] -> []
[4,9]     -> [[9],[4,9]]

Post Rock Garf Hunter

Posted 2017-05-26T19:13:26.137

Reputation: 55 382

2Are duplicate subcollections allowed? As in, for [2, 2, 3], may we return [[2, 2, 3], [2, 3], [2, 3]]? – HyperNeutrino – 2017-05-26T19:16:10.980

1Hint: the sum of such a set can only be odd. Any other variant of these sets can only have an even sum. – tuskiomi – 2017-05-26T19:17:01.240

@HyperNeutrino No you should only return each one once – Post Rock Garf Hunter – 2017-05-26T19:17:35.067

Okay. Do the subcollections need to be in ascending order or is it fine to list them in the order provided in the original array? – HyperNeutrino – 2017-05-26T19:18:32.223

@HyperNeutrino They may be in any order, (ideally they would be an unordered collection, but many languages don't have such a construct so ordered collections are fine as long as the order is not important) – Post Rock Garf Hunter – 2017-05-26T19:21:08.990

If we consider an ordered list, [2, 3] isn't necessarily a duplicate of [3, 2] since 2 may occur twice in the original list (like in your example). Should it still output this only once? I would find that strange. – sigvaldm – 2017-05-26T19:45:02.730

@sigvaldm The challenge is about unordered collections, the two 2s present are not distinguishable. If you choose to use an ordered collection for this challenge that is fine, but you should treat the input as if it is unordered, in which case those two are duplicates. – Post Rock Garf Hunter – 2017-05-26T19:47:44.437

Your test case contains only primes. That's a coincidence, yes? – Dennis – 2017-05-26T20:24:45.970

I wouldn't call it a problem, but one might draw conclusions after looking at the test case. A test case with empty output would also be useful. – Dennis – 2017-05-26T20:28:07.947

Can languages with unordered collections use ordered collections? – CalculatorFeline – 2017-05-26T20:41:16.913

@CalculatorFeline Yes. – Post Rock Garf Hunter – 2017-05-26T20:41:57.007

Can we assume that the input is given in order? (Several golfing languages use sorted ordered collections as a substitute for unordered collections, and operations which normally work correctly on both ordered and unordered collections will assume the collection is ordered if the inputs are out of order.) – None – 2017-05-27T02:13:29.800

@ais523 No you may not. – Post Rock Garf Hunter – 2017-05-27T02:19:33.587

Answers

5

05AB1E, 6 bytes

{æÙʒOÉ

Try it online!

{æÙʒOÉ
{      Sort
 æ     Powerset
  Ù    Uniqufy
   ʒ   Keep elements where
    O                      the sum
     É                             is uneven

-2 bytes thanks to @EriktheOutgolfer

HyperNeutrino

Posted 2017-05-26T19:13:26.137

Reputation: 26 575

@WheatWizard Yes (comment reply to Jonathan). Thanks for reminding me. – HyperNeutrino – 2017-05-26T19:34:01.437

2% can be golfed to É and } can be removed. But your answer seems to have the issue. – Erik the Outgolfer – 2017-05-26T19:57:49.967

4

Python 3, 93 bytes

f=lambda x,r=[[]]:x and f(x[1:],r+[y+x[:1]for y in r])or{(*sorted(y),)for y in r if sum(y)&1}

Returns a set of tuples. Most likely way too long.

Try it online!

Dennis

Posted 2017-05-26T19:13:26.137

Reputation: 196 637

Despite its appearance this is actually pretty good. Here's my amateurish attempt for reference

– Post Rock Garf Hunter – 2017-05-26T21:30:39.507

3

Pyth, 10 9 8 bytes

{f%sT2yS

Try it online!

         # implicit input
       S # sort input, this way the subsets will already be sorted
      y  # all subsets
 f       # filter elements when ..
   sT    # the sum ..
  %  2   # is odd
{        # remove all duplicated elements
         # implicit output

Rod

Posted 2017-05-26T19:13:26.137

Reputation: 17 588

1{SMf%sT2y saves a byte it seems. – Erik the Outgolfer – 2017-05-26T19:56:52.833

3

Python 2, 91 bytes

r=[[]]
for n in input():r+=map([n].__add__,r)
print{tuple(sorted(y))for y in r if sum(y)&1}

Prints a set of tuples. If a set of strings is allowed, tuple(sorted(y)) can be replaced with `sorted(y)` for 86 bytes.

Try it online!

Dennis

Posted 2017-05-26T19:13:26.137

Reputation: 196 637

2

Jelly, 9 bytes

ṢŒPSḂ$ÐfQ

Try it online!

Bug fixed thanks to Jonathan Allan.

ṢŒPSḂ$ÐfQ  Main Link
Ṣ          Sort
 ŒP        Powerset
      Ðf   Filter to keep elements where                         is truthy
    Ḃ                                    the last bit of
   S                                                     the sum
        Q  Only keep unique elements

HyperNeutrino

Posted 2017-05-26T19:13:26.137

Reputation: 26 575

2

Mathematica 31 44 38 bytes

Among all subsets of the input set, it returns those for which the sum, Tr, is odd.

6 bytes saved thanks to alephalpha.

Select[Union@Subsets@Sort@#,OddQ@*Tr]&

 Select[Union@Subsets@Sort@#,OddQ@*Tr]&[{2,3,7,2}]

{{3}, {7}, {2, 3}, {2, 7}, {2, 2, 3}, {2, 2, 7}}

DavidC

Posted 2017-05-26T19:13:26.137

Reputation: 24 524

What's with the space? – CalculatorFeline – 2017-05-26T20:41:45.417

1Unfortunately, this doesn't meet the spec, as {2,3} and {3,2} should not both be returned (same with {2,7} and {7,2}). – Greg Martin – 2017-05-27T00:05:45.433

Select[Union@Subsets@Sort@#,OddQ@*Tr]& – alephalpha – 2017-05-27T15:42:40.953

2

Perl 6, 50 bytes

{.combinations.grep(*.sum!%%2).unique(:as(*.Bag))}

To filter out the same-up-to-ordering combinations I filter out duplicates by converting each to a Bag (unordered collection) before comparing. Unfortunately I couldn't find a way to accept a Bag as input that was as concise.

Sean

Posted 2017-05-26T19:13:26.137

Reputation: 4 136

2

Brachylog, 11 bytes

o⊇ᵘ{+ḃt1&}ˢ

Try it online!

I hoped to find a shorter solution, but here's the best I could do.

Explanation

o⊇ᵘ{+ḃt1&}ˢ    
o                                        the input, sorted
 ⊇ᵘ           Find all unique subsets of

   {    &}ˢ   Then select only those results where
    +                                          the sum
     ḃ                           the base 2 of
      t        The last digit of
       1                                               is 1.

Yeah, I could've used modulo 2 to check for oddness, but that's not an odd approach ;)

Leo

Posted 2017-05-26T19:13:26.137

Reputation: 8 482

1

PHP, 126 bytes

for(;++$i>>$argc<1;sort($t),$s&1?$r[join(_,$t)]=$t:0)for ($t=[],$j=$s=0;++$j<$argc;)$i>>$j&1?$s+=$t[]=$argv[$j]:0;print_r($r);

takes input from command line arguments; run with -nr or try it online.

breakdown

for(;++$i>>$argc<1;             # loop through subsets
    sort($t),                       # 2. sort subset
    $s&1?$r[join(_,$t)]=$t:0        # 3. if sum is odd, add subset to results
    )                               # 1. create subset:
    for ($t=[],$j=$s=0;++$j<$argc;)     # loop through elements
        $i>>$j&1?                       # if bit $j is set in $i
        $s+=$t[]=$argv[$j]:0;           # then add element to subset
print_r($r);                    # print results

Titus

Posted 2017-05-26T19:13:26.137

Reputation: 13 814