Compute the superset
Clash Royale CLAN TAG#URR8PPP
up vote
21
down vote
favorite
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 code-golf, so make your answer as short as possible!
code-golf set-theory
 |Â
show 1 more comment
up vote
21
down vote
favorite
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 code-golf, so make your answer as short as possible!
code-golf set-theory
Related
â Nathan Merrill
Sep 27 at 14:40
1
Can I useInfinity
instead?
â Luis felipe De jesus Munoz
Sep 27 at 14:42
Can we take input as float values, e.g.,[1.0, 3.0]
instead of[1, 3]
?
â AdmBorkBork
Sep 27 at 14:58
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
Sep 27 at 15:10
If your language can't takeInfinity
and-Infinity
as input, is it allowed to take-999999
and999999
(or even larger/smaller) instead?
â Kevin Cruijssen
Sep 28 at 14:30
 |Â
show 1 more comment
up vote
21
down vote
favorite
up vote
21
down vote
favorite
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 code-golf, so make your answer as short as possible!
code-golf set-theory
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 code-golf, so make your answer as short as possible!
code-golf set-theory
code-golf set-theory
edited Sep 27 at 19:07
asked Sep 27 at 14:36
Nathan Merrill
6,94136115
6,94136115
Related
â Nathan Merrill
Sep 27 at 14:40
1
Can I useInfinity
instead?
â Luis felipe De jesus Munoz
Sep 27 at 14:42
Can we take input as float values, e.g.,[1.0, 3.0]
instead of[1, 3]
?
â AdmBorkBork
Sep 27 at 14:58
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
Sep 27 at 15:10
If your language can't takeInfinity
and-Infinity
as input, is it allowed to take-999999
and999999
(or even larger/smaller) instead?
â Kevin Cruijssen
Sep 28 at 14:30
 |Â
show 1 more comment
Related
â Nathan Merrill
Sep 27 at 14:40
1
Can I useInfinity
instead?
â Luis felipe De jesus Munoz
Sep 27 at 14:42
Can we take input as float values, e.g.,[1.0, 3.0]
instead of[1, 3]
?
â AdmBorkBork
Sep 27 at 14:58
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
Sep 27 at 15:10
If your language can't takeInfinity
and-Infinity
as input, is it allowed to take-999999
and999999
(or even larger/smaller) instead?
â Kevin Cruijssen
Sep 28 at 14:30
Related
â Nathan Merrill
Sep 27 at 14:40
Related
â Nathan Merrill
Sep 27 at 14:40
1
1
Can I use
Infinity
instead
?â Luis felipe De jesus Munoz
Sep 27 at 14:42
Can I use
Infinity
instead
?â Luis felipe De jesus Munoz
Sep 27 at 14:42
Can we take input as float values, e.g.,
[1.0, 3.0]
instead of [1, 3]
?â AdmBorkBork
Sep 27 at 14:58
Can we take input as float values, e.g.,
[1.0, 3.0]
instead of [1, 3]
?â AdmBorkBork
Sep 27 at 14:58
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
Sep 27 at 15:10
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
Sep 27 at 15:10
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
Sep 28 at 14:30
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
Sep 28 at 14:30
 |Â
show 1 more comment
9 Answers
9
active
oldest
votes
up vote
7
down vote
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_nle 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
p<=M+1
can bep<M+2
?
â Kevin Cruijssen
Sep 28 at 9:16
@KevinCruijssen I missed that one entirely... Thanks!
â Arnauld
Sep 28 at 9:41
add a comment |Â
up vote
7
down vote
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.
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
Sep 28 at 2:21
1
I 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 havereduce
andReduce
in there.
â ngm
Sep 28 at 13:45
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
Sep 28 at 17:23
add a comment |Â
up vote
4
down vote
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!
(b,c),
saves a byte.
â Mr. Xcoder
Sep 27 at 20:12
Huh, thought I'd tried that already.
â Mnemonic
Sep 27 at 20:16
Doesn'tg
mean your functionf
isn't reusable and therefore is invalid?
â Neil
Sep 27 at 21:31
@Neil Probably, but that was just a holdover from an earlier attempt.
â Mnemonic
Sep 28 at 2:49
1
You could also do thereturn
becomesprint
for another byte.
â Jonathan Frech
Sep 28 at 7:01
 |Â
show 8 more comments
up vote
2
down vote
Ruby, 89 76 bytes
->a[*a.sort.reduce.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.
add a comment |Â
up vote
1
down vote
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.
add a comment |Â
up vote
1
down vote
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.
add a comment |Â
up vote
1
down vote
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]]
add a comment |Â
up vote
1
down vote
C (clang), 346 342 bytes
Compiler flags -DP=printf("(%d,%d)n"
,-DB=a[i+1]
, and -DA=a[i]
typedef structint 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!
I think you are relying oni
's global value.
â Jonathan Frech
Sep 28 at 7:05
What @JonathanFrech means is thatwhile(A)i++;
should befor(i=0;A;)i++;
to explicitly seti=0
before using it in the while-loop, instead of using its default0
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
Sep 28 at 11:54
Fixed reliance on the globali
value
â Logern
Sep 28 at 12:58
1
@KevinCruijssen See Do function submissions have to be reusable?.
â Jonathan Frech
Sep 28 at 18:32
246 bytes
â ceilingcat
yesterday
add a comment |Â
up vote
1
down vote
Stax, 46 39 bytes
ÿô7âÂÂâ¿âÂÂâªâÂÂûÿ1WçâÂ¥âÂÂóÃÂâÂÂ+â£âÂÂ[áâ¬pãÃÂâ§ÃÂÃÂðâÂ¥úiëë4TâÂÂ
Run and debug it
This program takes input in the originally specified string notation.
add a comment |Â
9 Answers
9
active
oldest
votes
9 Answers
9
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
7
down vote
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_nle 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
p<=M+1
can bep<M+2
?
â Kevin Cruijssen
Sep 28 at 9:16
@KevinCruijssen I missed that one entirely... Thanks!
â Arnauld
Sep 28 at 9:41
add a comment |Â
up vote
7
down vote
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_nle 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
p<=M+1
can bep<M+2
?
â Kevin Cruijssen
Sep 28 at 9:16
@KevinCruijssen I missed that one entirely... Thanks!
â Arnauld
Sep 28 at 9:41
add a comment |Â
up vote
7
down vote
up vote
7
down vote
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_nle 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
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_nle 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
edited Sep 28 at 9:41
answered Sep 27 at 15:35
Arnauld
65.9k583278
65.9k583278
p<=M+1
can bep<M+2
?
â Kevin Cruijssen
Sep 28 at 9:16
@KevinCruijssen I missed that one entirely... Thanks!
â Arnauld
Sep 28 at 9:41
add a comment |Â
p<=M+1
can bep<M+2
?
â Kevin Cruijssen
Sep 28 at 9:16
@KevinCruijssen I missed that one entirely... Thanks!
â Arnauld
Sep 28 at 9:41
p<=M+1
can be p<M+2
?â Kevin Cruijssen
Sep 28 at 9:16
p<=M+1
can be p<M+2
?â Kevin Cruijssen
Sep 28 at 9:16
@KevinCruijssen I missed that one entirely... Thanks!
â Arnauld
Sep 28 at 9:41
@KevinCruijssen I missed that one entirely... Thanks!
â Arnauld
Sep 28 at 9:41
add a comment |Â
up vote
7
down vote
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.
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
Sep 28 at 2:21
1
I 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 havereduce
andReduce
in there.
â ngm
Sep 28 at 13:45
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
Sep 28 at 17:23
add a comment |Â
up vote
7
down vote
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.
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
Sep 28 at 2:21
1
I 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 havereduce
andReduce
in there.
â ngm
Sep 28 at 13:45
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
Sep 28 at 17:23
add a comment |Â
up vote
7
down vote
up vote
7
down vote
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.
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.
edited Sep 28 at 17:46
answered Sep 27 at 21:00
ngm
2,63922
2,63922
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
Sep 28 at 2:21
1
I 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 havereduce
andReduce
in there.
â ngm
Sep 28 at 13:45
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
Sep 28 at 17:23
add a comment |Â
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
Sep 28 at 2:21
1
I 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 havereduce
andReduce
in there.
â ngm
Sep 28 at 13:45
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
Sep 28 at 17:23
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
Sep 28 at 2:21
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
Sep 28 at 2:21
1
1
I 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
Sep 28 at 13:45
I 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
Sep 28 at 13:45
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
Sep 28 at 17:23
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
Sep 28 at 17:23
add a comment |Â
up vote
4
down vote
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!
(b,c),
saves a byte.
â Mr. Xcoder
Sep 27 at 20:12
Huh, thought I'd tried that already.
â Mnemonic
Sep 27 at 20:16
Doesn'tg
mean your functionf
isn't reusable and therefore is invalid?
â Neil
Sep 27 at 21:31
@Neil Probably, but that was just a holdover from an earlier attempt.
â Mnemonic
Sep 28 at 2:49
1
You could also do thereturn
becomesprint
for another byte.
â Jonathan Frech
Sep 28 at 7:01
 |Â
show 8 more comments
up vote
4
down vote
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!
(b,c),
saves a byte.
â Mr. Xcoder
Sep 27 at 20:12
Huh, thought I'd tried that already.
â Mnemonic
Sep 27 at 20:16
Doesn'tg
mean your functionf
isn't reusable and therefore is invalid?
â Neil
Sep 27 at 21:31
@Neil Probably, but that was just a holdover from an earlier attempt.
â Mnemonic
Sep 28 at 2:49
1
You could also do thereturn
becomesprint
for another byte.
â Jonathan Frech
Sep 28 at 7:01
 |Â
show 8 more comments
up vote
4
down vote
up vote
4
down vote
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!
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!
edited 2 days ago
answered Sep 27 at 16:02
Mnemonic
4,4421629
4,4421629
(b,c),
saves a byte.
â Mr. Xcoder
Sep 27 at 20:12
Huh, thought I'd tried that already.
â Mnemonic
Sep 27 at 20:16
Doesn'tg
mean your functionf
isn't reusable and therefore is invalid?
â Neil
Sep 27 at 21:31
@Neil Probably, but that was just a holdover from an earlier attempt.
â Mnemonic
Sep 28 at 2:49
1
You could also do thereturn
becomesprint
for another byte.
â Jonathan Frech
Sep 28 at 7:01
 |Â
show 8 more comments
(b,c),
saves a byte.
â Mr. Xcoder
Sep 27 at 20:12
Huh, thought I'd tried that already.
â Mnemonic
Sep 27 at 20:16
Doesn'tg
mean your functionf
isn't reusable and therefore is invalid?
â Neil
Sep 27 at 21:31
@Neil Probably, but that was just a holdover from an earlier attempt.
â Mnemonic
Sep 28 at 2:49
1
You could also do thereturn
becomesprint
for another byte.
â Jonathan Frech
Sep 28 at 7:01
(b,c),
saves a byte.â Mr. Xcoder
Sep 27 at 20:12
(b,c),
saves a byte.â Mr. Xcoder
Sep 27 at 20:12
Huh, thought I'd tried that already.
â Mnemonic
Sep 27 at 20:16
Huh, thought I'd tried that already.
â Mnemonic
Sep 27 at 20:16
Doesn't
g
mean your function f
isn't reusable and therefore is invalid?â Neil
Sep 27 at 21:31
Doesn't
g
mean your function f
isn't reusable and therefore is invalid?â Neil
Sep 27 at 21:31
@Neil Probably, but that was just a holdover from an earlier attempt.
â Mnemonic
Sep 28 at 2:49
@Neil Probably, but that was just a holdover from an earlier attempt.
â Mnemonic
Sep 28 at 2:49
1
1
You could also do the
return
becomes print
for another byte.â Jonathan Frech
Sep 28 at 7:01
You could also do the
return
becomes print
for another byte.â Jonathan Frech
Sep 28 at 7:01
 |Â
show 8 more comments
up vote
2
down vote
Ruby, 89 76 bytes
->a[*a.sort.reduce.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.
add a comment |Â
up vote
2
down vote
Ruby, 89 76 bytes
->a[*a.sort.reduce.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.
add a comment |Â
up vote
2
down vote
up vote
2
down vote
Ruby, 89 76 bytes
->a[*a.sort.reduce.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.
Ruby, 89 76 bytes
->a[*a.sort.reduce.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.
edited Oct 2 at 21:27
answered Sep 28 at 11:26
G B
6,8271324
6,8271324
add a comment |Â
add a comment |Â
up vote
1
down vote
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.
add a comment |Â
up vote
1
down vote
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.
add a comment |Â
up vote
1
down vote
up vote
1
down vote
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.
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.
edited Sep 28 at 12:11
answered Sep 27 at 23:33
AlexRacer
869210
869210
add a comment |Â
add a comment |Â
up vote
1
down vote
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.
add a comment |Â
up vote
1
down vote
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.
add a comment |Â
up vote
1
down vote
up vote
1
down vote
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.
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.
answered Sep 28 at 16:47
Misha Lavrov
3,387320
3,387320
add a comment |Â
add a comment |Â
up vote
1
down vote
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]]
add a comment |Â
up vote
1
down vote
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]]
add a comment |Â
up vote
1
down vote
up vote
1
down vote
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]]
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]]
edited Sep 28 at 16:57
answered Sep 28 at 16:46
Kevin Cruijssen
31.1k553168
31.1k553168
add a comment |Â
add a comment |Â
up vote
1
down vote
C (clang), 346 342 bytes
Compiler flags -DP=printf("(%d,%d)n"
,-DB=a[i+1]
, and -DA=a[i]
typedef structint 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!
I think you are relying oni
's global value.
â Jonathan Frech
Sep 28 at 7:05
What @JonathanFrech means is thatwhile(A)i++;
should befor(i=0;A;)i++;
to explicitly seti=0
before using it in the while-loop, instead of using its default0
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
Sep 28 at 11:54
Fixed reliance on the globali
value
â Logern
Sep 28 at 12:58
1
@KevinCruijssen See Do function submissions have to be reusable?.
â Jonathan Frech
Sep 28 at 18:32
246 bytes
â ceilingcat
yesterday
add a comment |Â
up vote
1
down vote
C (clang), 346 342 bytes
Compiler flags -DP=printf("(%d,%d)n"
,-DB=a[i+1]
, and -DA=a[i]
typedef structint 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!
I think you are relying oni
's global value.
â Jonathan Frech
Sep 28 at 7:05
What @JonathanFrech means is thatwhile(A)i++;
should befor(i=0;A;)i++;
to explicitly seti=0
before using it in the while-loop, instead of using its default0
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
Sep 28 at 11:54
Fixed reliance on the globali
value
â Logern
Sep 28 at 12:58
1
@KevinCruijssen See Do function submissions have to be reusable?.
â Jonathan Frech
Sep 28 at 18:32
246 bytes
â ceilingcat
yesterday
add a comment |Â
up vote
1
down vote
up vote
1
down vote
C (clang), 346 342 bytes
Compiler flags -DP=printf("(%d,%d)n"
,-DB=a[i+1]
, and -DA=a[i]
typedef structint 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!
C (clang), 346 342 bytes
Compiler flags -DP=printf("(%d,%d)n"
,-DB=a[i+1]
, and -DA=a[i]
typedef structint 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!
edited Sep 28 at 17:01
answered Sep 28 at 0:12
Logern
30115
30115
I think you are relying oni
's global value.
â Jonathan Frech
Sep 28 at 7:05
What @JonathanFrech means is thatwhile(A)i++;
should befor(i=0;A;)i++;
to explicitly seti=0
before using it in the while-loop, instead of using its default0
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
Sep 28 at 11:54
Fixed reliance on the globali
value
â Logern
Sep 28 at 12:58
1
@KevinCruijssen See Do function submissions have to be reusable?.
â Jonathan Frech
Sep 28 at 18:32
246 bytes
â ceilingcat
yesterday
add a comment |Â
I think you are relying oni
's global value.
â Jonathan Frech
Sep 28 at 7:05
What @JonathanFrech means is thatwhile(A)i++;
should befor(i=0;A;)i++;
to explicitly seti=0
before using it in the while-loop, instead of using its default0
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
Sep 28 at 11:54
Fixed reliance on the globali
value
â Logern
Sep 28 at 12:58
1
@KevinCruijssen See Do function submissions have to be reusable?.
â Jonathan Frech
Sep 28 at 18:32
246 bytes
â ceilingcat
yesterday
I think you are relying on
i
's global value.â Jonathan Frech
Sep 28 at 7:05
I think you are relying on
i
's global value.â Jonathan Frech
Sep 28 at 7:05
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
Sep 28 at 11:54
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
Sep 28 at 11:54
Fixed reliance on the global
i
valueâ Logern
Sep 28 at 12:58
Fixed reliance on the global
i
valueâ Logern
Sep 28 at 12:58
1
1
@KevinCruijssen See Do function submissions have to be reusable?.
â Jonathan Frech
Sep 28 at 18:32
@KevinCruijssen See Do function submissions have to be reusable?.
â Jonathan Frech
Sep 28 at 18:32
246 bytes
â ceilingcat
yesterday
246 bytes
â ceilingcat
yesterday
add a comment |Â
up vote
1
down vote
Stax, 46 39 bytes
ÿô7âÂÂâ¿âÂÂâªâÂÂûÿ1WçâÂ¥âÂÂóÃÂâÂÂ+â£âÂÂ[áâ¬pãÃÂâ§ÃÂÃÂðâÂ¥úiëë4TâÂÂ
Run and debug it
This program takes input in the originally specified string notation.
add a comment |Â
up vote
1
down vote
Stax, 46 39 bytes
ÿô7âÂÂâ¿âÂÂâªâÂÂûÿ1WçâÂ¥âÂÂóÃÂâÂÂ+â£âÂÂ[áâ¬pãÃÂâ§ÃÂÃÂðâÂ¥úiëë4TâÂÂ
Run and debug it
This program takes input in the originally specified string notation.
add a comment |Â
up vote
1
down vote
up vote
1
down vote
Stax, 46 39 bytes
ÿô7âÂÂâ¿âÂÂâªâÂÂûÿ1WçâÂ¥âÂÂóÃÂâÂÂ+â£âÂÂ[áâ¬pãÃÂâ§ÃÂÃÂðâÂ¥úiëë4TâÂÂ
Run and debug it
This program takes input in the originally specified string notation.
Stax, 46 39 bytes
ÿô7âÂÂâ¿âÂÂâªâÂÂûÿ1WçâÂ¥âÂÂóÃÂâÂÂ+â£âÂÂ[áâ¬pãÃÂâ§ÃÂÃÂðâÂ¥úiëë4TâÂÂ
Run and debug it
This program takes input in the originally specified string notation.
edited Oct 2 at 21:03
answered Oct 2 at 15:18
recursive
4,4961220
4,4961220
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodegolf.stackexchange.com%2fquestions%2f172922%2fcompute-the-superset%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Related
â Nathan Merrill
Sep 27 at 14:40
1
Can I use
Infinity
instead?
â Luis felipe De jesus Munoz
Sep 27 at 14:42
Can we take input as float values, e.g.,
[1.0, 3.0]
instead of[1, 3]
?â AdmBorkBork
Sep 27 at 14:58
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
Sep 27 at 15:10
If your language can't take
Infinity
and-Infinity
as input, is it allowed to take-999999
and999999
(or even larger/smaller) instead?â Kevin Cruijssen
Sep 28 at 14:30