g≠i˜AKïDW<UZ>VIøεAXY‚Nè:}ïø{©˜¦2ôíÆ1›.œʒíεćsO_*}P}н€g®£εø©θàDYQiA}V®нßDXQiA}Y‚
O infinito é inserido como o alfabeto em minúsculas ( 'abcdefghijklmnopqrstuvwxyz'
).
Experimente online ou verifique todos os casos de teste .
Nota importante: Se houvesse um real Infinity
e -Infinity
, teria sido 43 42 bytes . Tão pouco mais de 50% em torno de 30% é uma solução alternativa para a falta de Infinity
..
{©Dg≠i˜¦2ôíÆ1›.œʒíεćsO_*}P}н€g®£εø©θàV®нßY‚
Experimente on-line (com Infinity
substituído por 9999999999
e -Infinity
substituído por -9999999999
).
Definitivamente pode ser jogado golfe substancialmente. No final, ficou muito, muito feio, cheio de soluções alternativas. Mas, por enquanto, estou feliz que esteja funcionando.
Explicação:
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]]