Compute the superset

18

1

Your task here is simple:

Given a list of integer sets, find the set union. In other words, find the shortest list of integer sets that contain all the elements in the original list of sets (but no other elements). For example:

[1,5] and [3,9] becomes [1,9] as it contains all of the elements in both [1,5] and [3,9]
[1,3] and [5,9] stays as [1,3] and [5,9], because you don't want to include 4

Sets are notated using range notation: [1,4] means the integers 1,2,3,4. Sets can also be unbounded: [3,] means all of the integers >= 3, and [,-1] means all of the integers <= -1. It is guaranteed that the first element of the range will not be greater than the second.

You can choose to take sets in string notation, or you can use 2-element tuples, using a constant non-integer as the "infinite" value. You can use two distinct constants to represent the infinite upper bound and the infinite lower bound. For example, in Javascript, you could use [3,{}] to notate all integers >= 3, as long as you consistently used {} across all test cases.

Test cases:

[1,3]                     => [1,3]
[1,]                      => [1,]
[,9]                      => [,9]
[,]                       => [,]
[1,3],[4,9]               => [1,9]
[1,5],[8,9]               => [1,5],[8,9]
[1,5],[1,5]               => [1,5]
[1,5],[3,7]               => [1,7]
[-10,7],[1,5]             => [-10,7]
[1,1],[2,2],[3,3]         => [1,3]
[3,7],[1,5]               => [1,7]
[1,4],[8,]                => [1,4],[8,]
[1,4],[-1,]               => [-1,]
[1,4],[,5]                => [,5]
[1,4],[,-10]              => [1,4],[,-10]
[1,4],[,]                 => [,]
[1,4],[3,7],[8,9],[11,20] => [1,9],[11,20]

This is , so make your answer as short as possible!

Nathan Merrill

Posted 2018-09-27T14:36:12.150

Reputation: 13 591

Related – Nathan Merrill – 2018-09-27T14:40:48.620

1Can I use Infinity instead {}? – Luis felipe De jesus Munoz – 2018-09-27T14:42:26.297

Can we take input as float values, e.g., [1.0, 3.0] instead of [1, 3]? – AdmBorkBork – 2018-09-27T14:58:33.820

As long as you treat them as integers, yes. In other words [1.0, 3.0], [4.0, 5.0] should still become [1.0, 5.0] – Nathan Merrill – 2018-09-27T15:10:47.697

If your language can't take Infinity and -Infinity as input, is it allowed to take -999999 and 999999 (or even larger/smaller) instead? – Kevin Cruijssen – 2018-09-28T14:30:23.000

No, sorry. They need to be distinctly non-integers. If your language only consists of integers, then the only option you have is to take input via string representation. – Nathan Merrill – 2018-09-28T14:32:37.593

Answers

7

R + intervals, 90 87 81 bytes

function(...)t(reduce(Intervals(rbind(...),type="Z")))+c(1,-1)
library(intervals)

Try it online!

Input is a list of intervals. -Inf and Inf are R built-ins for minus/plus infinity. Output is a matrix of columns of intervals.

Not usually a fan of using non-standard libraries but this one was fun. TIO doesn't have intervals installed. You can try it on your own installation or at https://rdrr.io/snippets/

The intervals package supports real and integer (type = "Z") intervals and the reduce function is a built-in for what the challenge wants, but the output seems to default to open intervals, so close_intervals +c(1,-1) is needed to get the desired result.

Old version had examples in list of lists which might be convenient so I've left the link here.

ngm

Posted 2018-09-27T14:36:12.150

Reputation: 3 974

I think you can save a few bytes :function(...)close_intervals(reduce(Intervals(rbind(...),type="Z"))). Or even better you can check with op if they allow a matrix as input. – JayCe – 2018-09-28T02:21:00.300

1I was literally lying awake in bed last night thinking "there must have been a better way to make a matrix out of the input vectors". I think the challenge is better leaving the input as-is. But it was fun to have reduce and Reduce in there. – ngm – 2018-09-28T13:45:24.243

I love the "double reduce" thing! just not golfy enough;) What about modifying the open intervals like this: f=function(...)t(reduce(Intervals(rbind(...),type="Z")))+c(1,-1) ? – JayCe – 2018-09-28T17:23:30.150

6

JavaScript (ES6), 103 bytes

Saved 1 byte thanks to @Shaggy
Saved 1 byte thanks to @KevinCruijssen

Expects +/-Infinity for infinite values.

a=>(a.sort(([p],[P])=>p-P).map(m=M=([p,q])=>p<M+2?M=q>M?q:M:(b.push([m,M]),m=p,M=q),b=[]),b[0]=[m,M],b)

Try it online!

How?

We first sort the intervals by their lower bound, from lowest to highest. Upper bounds are ignored.

We then iterate over the sorted intervals \$[p_n,q_n]\$, while keeping track of the current lower and upper bounds \$m\$ and \$M\$, initialized to \$p_1\$ and \$q_1\$ respectively.

For each interval \$[p_n,q_n]\$:

  • If \$p_n\le M+1\$: this interval can be merged with the previous ones. But we may have a new upper bound, so we update \$M\$ to \$\max(M,q_n)\$.
  • Otherwise: there is a gap between the previous intervals and this one. We create a new interval \$[m,M]\$ and update \$m\$ and \$M\$ to \$p_n\$ and \$q_n\$ respectively.

At the end of the process, we create a last interval with the current bounds \$[m,M]\$.

Commented

a => (                  // a[] = input array
  a.sort(([p], [P]) =>  // sort the intervals by their lower bound; we do not care about
    p - P)              // the upper bounds for now
  .map(m = M =          // initialize m and M to non-numeric values
    ([p, q]) =>         // for each interval [p, q] in a[]:
    p < M + 2 ?         //   if M is a number and p is less than or equal to M + 1:
      M = q > M ? q : M //     update the maximum M to max(M, q)
    : (                 //   else (we've found a gap, or this is the 1st iteration):
      b.push([m, M]),   //     push the interval [m, M] in b[]
      m = p,            //     update the minimum m to p
      M = q             //     update the maximum M to q
    ),                  //
    b = []              //   start with b[] = empty array
  ),                    // end of map()
  b[0] = [m, M], b      // overwrite the 1st entry of b[] with the last interval [m, M]
)                       // and return the final result

Arnauld

Posted 2018-09-27T14:36:12.150

Reputation: 111 334

p<=M+1 can be p<M+2? – Kevin Cruijssen – 2018-09-28T09:16:17.123

@KevinCruijssen I missed that one entirely... Thanks! – Arnauld – 2018-09-28T09:41:36.140

4

Python 2, 118 113 112 111 106 105 104 101 bytes

x=input()
x.sort();a=[];b,c=x[0]
for l,r in x:
 if l>c+1:a+=(b,c),;b,c=l,r
 c=max(c,r)
print[(b,c)]+a

Saved one byte thanks to Mr.Xcoder, one thanks to Jonathan Frech, and three thanks to Dead Possum.
Try it online!

user48543

Posted 2018-09-27T14:36:12.150

Reputation:

(b,c), saves a byte. – Mr. Xcoder – 2018-09-27T20:12:46.570

Huh, thought I'd tried that already. – None – 2018-09-27T20:16:46.653

Doesn't g mean your function f isn't reusable and therefore is invalid? – Neil – 2018-09-27T21:31:10.707

@Neil Probably, but that was just a holdover from an earlier attempt. – None – 2018-09-28T02:49:37.140

1You could also do the return becomes print for another byte. – Jonathan Frech – 2018-09-28T07:01:03.867

@JonathanFrech The ~-l won't work with floats, and I don't think there's an integer infinity in Python. – None – 2018-09-28T15:26:52.900

@Mnemonic Well, yes; sorry. Using your interpretation of integer set which allows for a non-integer to be a bound ... – Jonathan Frech – 2018-09-28T18:34:44.383

@JonathanFrech The problem statement requires handling infinite sets. I can't use integers for that. – None – 2018-09-28T18:39:13.617

@Mnemonic One could possibly interpret the rules as allowing to process (conceptually) infinite sets of integers. – Jonathan Frech – 2018-10-02T21:15:36.213

@JonathanFrech That's the natural way to interpret it, but I don't think there's any nice way to represent that in Python. – None – 2018-10-03T00:11:39.947

@Mnemonic Well, I meant representing the sets using generators and defining a lazy union function which would in theory produce the correct result ... :P – Jonathan Frech – 2018-10-03T00:29:54.433

@JonathanFrech If you can do it for less than the byte it would save here, go for it. – None – 2018-10-03T02:16:49.050

Full program is shorter: 101 bytes

– Dead Possum – 2018-10-03T11:01:42.037

2

Ruby, 89 76 bytes

->a{[*a.sort.reduce{|s,y|s+=y;y[0]-s[-3]<2&&s[-3,3]=s.max;s}.each_slice(2)]}

Try it online!

Sort the array, then flatten by appending all the ranges to the first: if a range overlaps the previous one, discard 2 elements from the last 3 (keeping only the max).

Unflatten everything at the end.

G B

Posted 2018-09-27T14:36:12.150

Reputation: 11 099

1

Pascal (FPC), 367 362 357 bytes

uses math;type r=record a,b:real end;procedure d(a:array of r);var i,j:word;t:r;begin for i:=0to length(a)-1do for j:=0to i do if a[i].a<a[j].a then begin t:=a[i];a[i]:=a[j];a[j]:=t;end;j:=0;for i:=1to length(a)-1do if a[j].b>=a[i].a-1then begin a[j].a:=min(a[i].a,a[j].a);a[j].b:=max(a[i].b,a[j].b)end else j:=j+1;for i:=0to j do writeln(a[i].a,a[i].b)end;

Try it online!

A procedure that takes a dynamic array of records consisting of 2 range bounds, modifies the array in place and then writes it on standard output, one range per line. (Sorry for that twisted sentence.) Uses 1/0 for ubounded up and -1/0 for unbounded down.

Readable version

It would be nice to just return the array with corrected number of elements, but the dynamic array passed to function/procedure is not dynamic array anymore... First I found this, then there is this excellent, mind-boggling explanation.

This is the best data structure I could found for shortening the code. If you have better options, feel free to make a suggestion.

AlexRacer

Posted 2018-09-27T14:36:12.150

Reputation: 979

1

C (clang), 346 342 bytes

Compiler flags -DP=printf("(%d,%d)\n",-DB=a[i+1], and -DA=a[i]

typedef struct{int a,b;}t;s(t**x,t**y){if((*x)->a>(*y)->a)return 1;else if((*x)->a<(*y)->a)return -1;}i;f(t**a){for(i=0;A;)i++;qsort(a,i,sizeof(t*),s);for(i=0;B;i++){if(B->a<=A->b+1){A->b=B->b;if(B->a<A->a)A->a=B->a;else B->a=A->a;}}for(i=0;A;i++){if(!B)break;if(A->a!=B->a)P,A->a,A->b);}P,A->a,A->b);}

Try it online!

Logern

Posted 2018-09-27T14:36:12.150

Reputation: 845

I think you are relying on i's global value. – Jonathan Frech – 2018-09-28T07:05:03.227

What @JonathanFrech means is that while(A)i++; should be for(i=0;A;)i++; to explicitly set i=0 before using it in the while-loop, instead of using its default 0 value on global level. Not sure anymore why, but it's required according to the meta rules. Mainly because methods should be self-contained / re-usable, without having to reset global values in between method calls, IIRC. – Kevin Cruijssen – 2018-09-28T11:54:26.973

Fixed reliance on the global i value – Logern – 2018-09-28T12:58:01.313

1

@KevinCruijssen See Do function submissions have to be reusable?.

– Jonathan Frech – 2018-09-28T18:32:17.407

246 bytes – ceilingcat – 2018-10-04T08:03:39.607

1

05AB1E (legacy), 88 79 78 bytes

g≠i˜AKïDW<UZ>VIøεAXY‚Nè:}ïø{©˜¦2ôíÆ1›.œʒíεćsO_*}P}н€g®£εø©θàDYQiA}V®нßDXQiA}Y‚

Infinity is input as the lowercase alphabet ('abcdefghijklmnopqrstuvwxyz').

Try it online or verify all test cases.

Important note: If there was an actual Infinity and -Infinity, it would have been 43 42 bytes instead. So little over 50% around 30% is as work-around for the lack of Infinity..

{©Dg≠i˜¦2ôíÆ1›.œʒíεćsO_*}P}н€g®£εø©θàV®нßY‚

Try it online (with Infinity replaced with 9999999999 and -Infinity replaced with -9999999999).

Can definitely be golfed substantially. In the end it turned out very, very ugly full of workarounds. But for now I'm just glad it's working.

Explanation:

Dg≠i          # If the length of the implicit input is NOT 1:
              #   i.e. [[1,3]] → length 1 → 0 (falsey)
              #   i.e. [[1,4],["a..z",-5],[3,7],[38,40],[8,9],[11,20],[25,"a..z"],[15,23]]
              #    → length 8 → 1 (truthy)
    ˜         #  Take the input implicit again, and flatten it
              #   i.e. [[1,4],["a..z",-5],[3,7],[38,40],[8,9],[11,20],[25,"a..z"],[15,23]]
              #    → [1,4,"a..z",-5,3,7,38,40,8,9,11,20,25,"a..z",15,23]
     AK       #  Remove the alphabet
              #   i.e. [1,4,"a..z",-5,3,7,38,40,8,9,11,20,25,"a..z",15,23]
              #    → ['1','4','-5','3','7','38','40','8','9','11','20','25','15','23']
       ï      #  Cast everything to an integer, because `K` turns them into strings..
              #   i.e. ['1','4','-5','3','7','38','40','8','9','11','20','25','15','23']
              #    → [1,4,-5,3,7,38,40,8,9,11,20,25,15,23]
        D     #  Duplicate it
         W<   #  Determine the min - 1
              #   i.e. [1,4,-5,3,7,38,40,8,9,11,20,25,15,23] → -5
           U  #  Pop and store it in variable `X`
         Z>   #  Determine the max + 1
              #   i.e. [1,4,-5,3,7,38,40,8,9,11,20,25,15,23] → 40
           V  #  Pop and store it in variable `Y`
Iø            #  Take the input again, and transpose/zip it (swapping rows and columns)
              #   i.e. [[1,4],["a..z",-5],[3,7],[38,40],[8,9],[11,20],[25,"a..z"],[15,23]]
              #    → [[1,'a..z',3,38,8,11,25,15],[4,-5,7,40,9,20,'a..z',23]]
  ε       }   #  Map both to:
   A          #   Push the lowercase alphabet
    XY‚       #   Push variables `X` and `Y`, and pair them into a list
       Nè     #   Index into this list, based on the index of the mapping
         :    #   Replace every alphabet with this min-1 or max+1
              #   i.e. [[1,'a..z',3,38,8,11,25,15],[4,-5,7,40,9,20,'a..z',23]]
              #    → [['1','-6','3','38','8','11','25','15'],['4','-5','7','40','9','20','41','23']]
ï             #  Cast everything to integers again, because `:` turns them into strings..
              #   i.e. [['1','-6','3','38','8','11','25','15'],['4','-5','7','40','9','20','41','23']]
              #    → [[1,-6,3,38,8,11,25,15],[4,-5,7,40,9,20,41,23]]
 ø            #  Now zip/transpose back again
              #   i.e. [[1,-6,3,38,8,11,25,15],[4,-5,7,40,9,20,41,23]]
              #    → [[1,4],[-6,-5],[3,7],[38,40],[8,9],[11,20],[25,41],[15,23]]
  {           #  Sort the pairs based on their lower range (the first number)
              #   i.e. [[1,4],[-6,-5],[3,7],[38,40],[8,9],[11,20],[25,41],[15,23]]
              #    → [[-6,-5],[1,4],[3,7],[8,9],[11,20],[15,23],[25,41],[38,40]]
   ©          #  Store it in the register (without popping)
˜             #  Flatten the list
              #   i.e. [[-6,-5],[1,4],[3,7],[8,9],[11,20],[15,23],[25,41],[38,40]]
              #    → [-6,-5,1,4,3,7,8,9,11,20,15,23,25,41,38,40]
 ¦            #  And remove the first item
              #   i.e. [-6,-5,1,4,3,7,8,9,11,20,15,23,25,41,38,40]
              #    → [-5,1,4,3,7,8,9,11,20,15,23,25,41,38,40]
  2ô          #  Then pair every two elements together
              #   i.e. [-5,1,4,3,7,8,9,11,20,15,23,25,41,38,40]
              #    → [[-5,1],[4,3],[7,8],[9,11],[20,15],[23,25],[41,38],[40]]
    í         #  Reverse each pair
              #   i.e. [[-5,1],[4,3],[7,8],[9,11],[20,15],[23,25],[41,38],[40]]
              #    → [[1,-5],[3,4],[8,7],[11,9],[15,20],[25,23],[38,41],[40]]
     Æ        #  Take the difference of each pair (by subtracting)
              #   i.e. [[1,-5],[3,4],[8,7],[11,9],[15,20],[25,23],[38,41],[40]]
              #    → [6,-1,1,2,-5,2,-3,40]
      1›      #  Determine for each if they're larger than 1
              #   i.e. [6,-1,1,2,-5,2,-3,40] → [1,0,0,1,0,1,0,1]
.œ            #  Create every possible partition of these values
              #   i.e. [1,0,0,1,0,1,0,1] → [[[1],[0],[0],[1],[0],[1],[0],[1]],
              #                             [[1],[0],[0],[1],[0],[1],[0,1]],
              #                             ...,
              #                             [[1,0,0,1,0,1,0],[1]],
              #                             [[1,0,0,1,0,1,0,1]]]
  ʒ         } #  Filter the partitions by:
   í          #   Reverse each inner partition
              #    i.e. [[1],[0,0,1],[0,1],[0,1]] → [[1],[1,0,0],[1,0],[1,0]]
    ε     }   #   Map each partition to:
     ć        #    Head extracted
              #     i.e. [1,0,0] → [0,0] and 1
              #     i.e. [1] → [] and 1
              #     i.e. [1,0,1] → [1,0] and 1
      s       #    Swap so the rest of the list is at the top of the stack again
       O      #    Take its sum
              #     i.e. [0,0] → 0
              #     i.e. [] → 0
              #     i.e. [1,0] → 1
        _     #    And check if it's exactly 0
              #     i.e. 0 → 1 (truthy)
              #     i.e. 1 → 0 (falsey)
         *    #    And multiply it with the extracted head
              #    (is only 1 when the partition has a single trailing 1 and everything else a 0)
              #     i.e. 1 and 1 → 1 (truthy)
              #     i.e. 1 and 0 → 0 (falsey)
           P  #   And check if all mapped partitions are 1
н             #  Take the head (there should only be one valid partition left)
              #   i.e. [[[1],[0,0,1],[0,1],[0,1]]] → [[1],[0,0,1],[0,1],[0,1]]
 €g           #  Take the length of each inner list
              #   i.e. [[1],[0,0,1],[0,1],[0,1]] → [1,3,2,2]
   ®          #  Push the sorted pairs we've saved in the register earlier
    £         #  Split the pairs into sizes equal to the partition-lengths
              #   i.e. [1,3,2,2] and [[-6,-5],[1,4],[3,7],[8,9],[11,20],[15,23],[25,41],[38,40]]
              #    → [[[-6,-5]],[[1,4],[3,7],[8,9]],[[11,20],[15,23]],[[25,41],[38,40]]]
ε             #  Map each list of pairs to:
 ø            #   Zip/transpose (swapping rows and columns)
              #    i.e. [[1,4],[3,7],[8,9]] → [[1,3,8],[4,7,9]]
              #    i.e. [[25,41],[38,40]] → [[25,38],[41,40]]
  ©           #   Store it in the register
   θ          #   Take the last list (the ending ranges)
              #    i.e. [[25,38],[41,40]] → [41,40]
    à         #   And determine the max
              #    i.e. [41,40] → 41
     DYQi }   #   If this max is equal to variable `Y`
              #     i.e. 41 (`Y` = 41) → 1 (truthy)
         A    #    Replace it back to the lowercase alphabet
           V  #   Store this max in variable `Y`
  ®           #   Take the zipped list from the register again
   н          #   This time take the first list (the starting ranges)
              #    i.e. [[25,38],[41,40]] → [25,38]
    ß         #   And determine the min
              #    i.e. [25,38] → 25
     DXQi }   #   If this min is equal to variable `X`
              #     i.e. 25 (`X` = -6) → 0 (falsey)
         A    #    Replace it back to the lowercase alphabet
           Y‚ #   And pair it up with variable `Y` (the max) to complete the mapping
              #    i.e. 25 and 'a..z' → [25,'a..z']
              #  Implicitly close the mapping (and output the result)
              #   i.e. [[[-6,-5]],[[1,4],[3,7],[8,9]],[[11,20],[15,23]],[[25,41],[38,40]]]
              #    → [['a..z',-5],[1,9],[11,23],[25,'a..z']]
              # Implicit else (only one pair in the input):
              #  Output the (implicit) input as is
              #   i.e. [[1,3]]

Kevin Cruijssen

Posted 2018-09-27T14:36:12.150

Reputation: 67 575

1

Wolfram Language (Mathematica), 57 bytes

List@@(#-{0,1}&/@IntervalUnion@@(Interval[#+{0,1}]&/@#))&

Try it online!

Takes input as a list of lists {a,b} representing the interval [a,b], where a can be -Infinity and b can be Infinity.

Uses the built-in IntervalUnion, but of course we have to massage the intervals into shape first. To pretend that the intervals are integers, we add 1 to the upper bound (making sure that the union of [1,3] and [4,9] is [1,9]). At the end, we undo this operation, and turn the result back into a list of lists.

There's also a completely different approach, which clocks in at 73 bytes:

NumericalSort@#//.{x___,{a_,b_},{c_,d_},y___}/;b+1>=c:>{x,{a,b~Max~d},y}&

Here, after sorting the intervals, we just replace two consecutive intervals by their union whenever that would be a single interval, and repeat until there is no such operation left to be done.

Misha Lavrov

Posted 2018-09-27T14:36:12.150

Reputation: 4 846

1

Stax, 46 39 bytes

ÿδ7│ⁿ╚╪║»ÿ1Wç♥├óπ◙+╣╝[á╬p£ß₧ΓÑ°♥ºië«4T╗

Run and debug it

This program takes input in the originally specified string notation.

recursive

Posted 2018-09-27T14:36:12.150

Reputation: 8 616