Arrow those variables!I used to solve code golf puzzles like you, but then I took an arrow in the kneeOutput a Dance Dance Revolution arrow chartSorting a list of strings without using any built-in sort methodSolve a reverse arrow mazePost-determined Array SortingTranspile these ES6 arrow functions!The next colourPronunciationSort™Draw some expanding arrowsGreatest common substring
Chess with symmetric move-square
Can an x86 CPU running in real mode be considered to be basically an 8086 CPU?
Why is the design of haulage companies so “special”?
My colleague's body is amazing
Why did the Germans forbid the possession of pet pigeons in Rostov-on-Don in 1941?
Prevent a directory in /tmp from being deleted
Why has Russell's definition of numbers using equivalence classes been finally abandoned? ( If it has actually been abandoned).
The use of multiple foreign keys on same column in SQL Server
Patience, young "Padovan"
I’m planning on buying a laser printer but concerned about the life cycle of toner in the machine
What is the logic behind how bash tests for true/false?
Why does not dark matter gather and form celestial bodies?
How to make payment on the internet without leaving a money trail?
How does one intimidate enemies without having the capacity for violence?
A Journey Through Space and Time
cryptic clue: mammal sounds like relative consumer (8)
Can I interfere when another PC is about to be attacked?
Can I make popcorn with any corn?
Is it possible to make sharp wind that can cut stuff from afar?
Should I join an office cleaning event for free?
How to use Pandas to get the count of every combination inclusive
What is GPS' 19 year rollover and does it present a cybersecurity issue?
How do I create uniquely male characters?
Calculus Optimization - Point on graph closest to given point
Arrow those variables!
I used to solve code golf puzzles like you, but then I took an arrow in the kneeOutput a Dance Dance Revolution arrow chartSorting a list of strings without using any built-in sort methodSolve a reverse arrow mazePost-determined Array SortingTranspile these ES6 arrow functions!The next colourPronunciationSort™Draw some expanding arrowsGreatest common substring
$begingroup$
Challenge
Robin likes having his variables declaration in the shape of an arrow. Here's how he does it:
- Input any number of strings
- Order them by ascending length
Output them ordered by the middle to roughly form a negative arrowhead, like this (whichever order golfs the best):
5 or 4
3 2
1 1
2 3
4 5
Test Cases
Input:
bow
arrows
sheriffOfNottingham
kingRichard
maidMarian
princeJohn
sherwoodForest
Output:
sheriffOfNottingham
kingRichard
maidMarian
bow
arrows
princeJohn
sherwoodForest
Input:
a
bb
cc
Output (both are valid):
bb
a
cc
cc
a
bb
Input:
one
four
seven
fifteen
Possible output (the only other valid output is its vertical mirror):
seven
one
four
fifteen
Notes
The strings are in camelCase and have no numbers or special characters, only lowercase and uppercase letters.
The input can be anything you like: comma-separated as one string, array, ... Any I/O format is allowed.
- Between strings with the same length, any order is accepted.
code-golf string sorting
New contributor
$endgroup$
|
show 6 more comments
$begingroup$
Challenge
Robin likes having his variables declaration in the shape of an arrow. Here's how he does it:
- Input any number of strings
- Order them by ascending length
Output them ordered by the middle to roughly form a negative arrowhead, like this (whichever order golfs the best):
5 or 4
3 2
1 1
2 3
4 5
Test Cases
Input:
bow
arrows
sheriffOfNottingham
kingRichard
maidMarian
princeJohn
sherwoodForest
Output:
sheriffOfNottingham
kingRichard
maidMarian
bow
arrows
princeJohn
sherwoodForest
Input:
a
bb
cc
Output (both are valid):
bb
a
cc
cc
a
bb
Input:
one
four
seven
fifteen
Possible output (the only other valid output is its vertical mirror):
seven
one
four
fifteen
Notes
The strings are in camelCase and have no numbers or special characters, only lowercase and uppercase letters.
The input can be anything you like: comma-separated as one string, array, ... Any I/O format is allowed.
- Between strings with the same length, any order is accepted.
code-golf string sorting
New contributor
$endgroup$
$begingroup$
I feel like there was a very similar challenge before...but welcome to PPCG!
$endgroup$
– Giuseppe
Apr 3 at 16:48
$begingroup$
@Giuseppe Yeah that's what I thought after posting, there's no way it hasn't been done before. Would you be ok with me deleting it now that you've answered it?
$endgroup$
– Teleporting Goat
Apr 3 at 16:54
1
$begingroup$
well I've been searching for a dupe but I'm not very good at the search...we do have a sandbox for posting challenges which can often catch things like that. I'm perfectly OK with you deleting it if you're worried about it being a dupe.
$endgroup$
– Giuseppe
Apr 3 at 16:55
1
$begingroup$
It's OK, we all start from the beginning :-)
$endgroup$
– Giuseppe
Apr 3 at 17:06
1
$begingroup$
Could you add a test case with an even number of strings?
$endgroup$
– Sherlock9
Apr 3 at 17:22
|
show 6 more comments
$begingroup$
Challenge
Robin likes having his variables declaration in the shape of an arrow. Here's how he does it:
- Input any number of strings
- Order them by ascending length
Output them ordered by the middle to roughly form a negative arrowhead, like this (whichever order golfs the best):
5 or 4
3 2
1 1
2 3
4 5
Test Cases
Input:
bow
arrows
sheriffOfNottingham
kingRichard
maidMarian
princeJohn
sherwoodForest
Output:
sheriffOfNottingham
kingRichard
maidMarian
bow
arrows
princeJohn
sherwoodForest
Input:
a
bb
cc
Output (both are valid):
bb
a
cc
cc
a
bb
Input:
one
four
seven
fifteen
Possible output (the only other valid output is its vertical mirror):
seven
one
four
fifteen
Notes
The strings are in camelCase and have no numbers or special characters, only lowercase and uppercase letters.
The input can be anything you like: comma-separated as one string, array, ... Any I/O format is allowed.
- Between strings with the same length, any order is accepted.
code-golf string sorting
New contributor
$endgroup$
Challenge
Robin likes having his variables declaration in the shape of an arrow. Here's how he does it:
- Input any number of strings
- Order them by ascending length
Output them ordered by the middle to roughly form a negative arrowhead, like this (whichever order golfs the best):
5 or 4
3 2
1 1
2 3
4 5
Test Cases
Input:
bow
arrows
sheriffOfNottingham
kingRichard
maidMarian
princeJohn
sherwoodForest
Output:
sheriffOfNottingham
kingRichard
maidMarian
bow
arrows
princeJohn
sherwoodForest
Input:
a
bb
cc
Output (both are valid):
bb
a
cc
cc
a
bb
Input:
one
four
seven
fifteen
Possible output (the only other valid output is its vertical mirror):
seven
one
four
fifteen
Notes
The strings are in camelCase and have no numbers or special characters, only lowercase and uppercase letters.
The input can be anything you like: comma-separated as one string, array, ... Any I/O format is allowed.
- Between strings with the same length, any order is accepted.
code-golf string sorting
code-golf string sorting
New contributor
New contributor
edited Apr 3 at 17:29
Teleporting Goat
New contributor
asked Apr 3 at 16:37
Teleporting GoatTeleporting Goat
23935
23935
New contributor
New contributor
$begingroup$
I feel like there was a very similar challenge before...but welcome to PPCG!
$endgroup$
– Giuseppe
Apr 3 at 16:48
$begingroup$
@Giuseppe Yeah that's what I thought after posting, there's no way it hasn't been done before. Would you be ok with me deleting it now that you've answered it?
$endgroup$
– Teleporting Goat
Apr 3 at 16:54
1
$begingroup$
well I've been searching for a dupe but I'm not very good at the search...we do have a sandbox for posting challenges which can often catch things like that. I'm perfectly OK with you deleting it if you're worried about it being a dupe.
$endgroup$
– Giuseppe
Apr 3 at 16:55
1
$begingroup$
It's OK, we all start from the beginning :-)
$endgroup$
– Giuseppe
Apr 3 at 17:06
1
$begingroup$
Could you add a test case with an even number of strings?
$endgroup$
– Sherlock9
Apr 3 at 17:22
|
show 6 more comments
$begingroup$
I feel like there was a very similar challenge before...but welcome to PPCG!
$endgroup$
– Giuseppe
Apr 3 at 16:48
$begingroup$
@Giuseppe Yeah that's what I thought after posting, there's no way it hasn't been done before. Would you be ok with me deleting it now that you've answered it?
$endgroup$
– Teleporting Goat
Apr 3 at 16:54
1
$begingroup$
well I've been searching for a dupe but I'm not very good at the search...we do have a sandbox for posting challenges which can often catch things like that. I'm perfectly OK with you deleting it if you're worried about it being a dupe.
$endgroup$
– Giuseppe
Apr 3 at 16:55
1
$begingroup$
It's OK, we all start from the beginning :-)
$endgroup$
– Giuseppe
Apr 3 at 17:06
1
$begingroup$
Could you add a test case with an even number of strings?
$endgroup$
– Sherlock9
Apr 3 at 17:22
$begingroup$
I feel like there was a very similar challenge before...but welcome to PPCG!
$endgroup$
– Giuseppe
Apr 3 at 16:48
$begingroup$
I feel like there was a very similar challenge before...but welcome to PPCG!
$endgroup$
– Giuseppe
Apr 3 at 16:48
$begingroup$
@Giuseppe Yeah that's what I thought after posting, there's no way it hasn't been done before. Would you be ok with me deleting it now that you've answered it?
$endgroup$
– Teleporting Goat
Apr 3 at 16:54
$begingroup$
@Giuseppe Yeah that's what I thought after posting, there's no way it hasn't been done before. Would you be ok with me deleting it now that you've answered it?
$endgroup$
– Teleporting Goat
Apr 3 at 16:54
1
1
$begingroup$
well I've been searching for a dupe but I'm not very good at the search...we do have a sandbox for posting challenges which can often catch things like that. I'm perfectly OK with you deleting it if you're worried about it being a dupe.
$endgroup$
– Giuseppe
Apr 3 at 16:55
$begingroup$
well I've been searching for a dupe but I'm not very good at the search...we do have a sandbox for posting challenges which can often catch things like that. I'm perfectly OK with you deleting it if you're worried about it being a dupe.
$endgroup$
– Giuseppe
Apr 3 at 16:55
1
1
$begingroup$
It's OK, we all start from the beginning :-)
$endgroup$
– Giuseppe
Apr 3 at 17:06
$begingroup$
It's OK, we all start from the beginning :-)
$endgroup$
– Giuseppe
Apr 3 at 17:06
1
1
$begingroup$
Could you add a test case with an even number of strings?
$endgroup$
– Sherlock9
Apr 3 at 17:22
$begingroup$
Could you add a test case with an even number of strings?
$endgroup$
– Sherlock9
Apr 3 at 17:22
|
show 6 more comments
25 Answers
25
active
oldest
votes
$begingroup$
Python 2, 47 bytes
lambda l:l.sort(key=len)or l[1::2][::-1]+l[::2]
Try it online!
$endgroup$
$begingroup$
You'll need to rearrange some stuff, but you can use[::-2]
directly to save 5 bytes.
$endgroup$
– Sherlock9
Apr 3 at 17:03
$begingroup$
@Sherlock9 I tried that, but then I had to check for the length, as lists with even / uneven lengths have to be handled differently.
$endgroup$
– ovs
Apr 3 at 17:06
$begingroup$
Also works for Python 3. Would removing "lambda l:" and "or" and make it on 2 lines to save 11 bytes still be acceptable as "Any I/O format is allowed" ?
$endgroup$
– potato
2 days ago
add a comment |
$begingroup$
R, 63 48 bytes
function(L)c(rev(o<-L[order(nchar(L))]),o)[!0:1]
Try it online!
Sort by string lengths, then combine the reversed list with the sorted list, finally, take every 2nd element, starting at 1-based index 1.
$endgroup$
$begingroup$
o<-L[...
The other way to 'arrow variables'. A less important aside,pryr::f(...)
works here for 46. Try it online!
$endgroup$
– CriminallyVulgar
Apr 4 at 7:40
add a comment |
$begingroup$
Javascript 77 bytes
Takes input as an array of strings, outputs an arrow-sorted array of strings.
s=>s.sort((a,b)=>a.length-b.length).reduce((m,x,i)=>i%2?[...m,x]:[x,...m],[])
Explanation
s => // take input as an array of strings s
s.sort((a,b)=>a.length-b.length) // sort input by string length
.reduce( // reduce
(m,x,i)=>i%2?[...m,x]:[x,...m], // if index is even, stick string x at the end of the memo
// array, else at the beginning
[] // memo initialized to empty array
)
$endgroup$
1
$begingroup$
I don't think you have to countf=
. 77
$endgroup$
– dana
Apr 4 at 11:54
$begingroup$
That is inconsistent in the js code golf submissions from what I have seen. I'm happy to exclude it if it doesn't count.
$endgroup$
– asgallant
Apr 4 at 16:11
2
$begingroup$
I think it depends whether your function uses recursion. i.e.f=x=>x?f(x-1)
. If so, you need to includef
since you are calling it in your function. However, since you are not using recursion, you shouldn't have to includef
. There are several posts in Meta, this one seems to explain it a little better. codegolf.meta.stackexchange.com/a/9032/8340
$endgroup$
– dana
Apr 4 at 16:34
$begingroup$
That would explain the inconsistencies I've seen.
$endgroup$
– asgallant
Apr 4 at 16:54
add a comment |
$begingroup$
C# (Visual C# Interactive Compiler), 89 bytes
n=>(n=n.OrderBy(x=>x.Length)).Where((a,b)=>b%2>0).Reverse().Concat(n.Where((a,b)=>b%2<1))
Try it online!
$endgroup$
add a comment |
$begingroup$
K (oK), 24 bytes
Solution:
x(<#:'x)(|&~w),&w:2!!#x:
Try it online!
Explanation:
Generate the 6 4 2 0 1 3 5
sequence, use that to index into the ascending lengths of input, and use that to index into the original array:
x(<#:'x)(|&~w),&w:2!!#x: / the solution
x: / save input as x
# / count (#) of x
! / range 0 to ...
2! / modulo 2
w: / save as w
& / indices where true
, / join with
( ) / do this together
~w / not (~) w
& / indices where true
| / reverse
( ) / do this together
#:'x / count (#:) of each (') x
< / indices to sort ascending
x / index into x
$endgroup$
add a comment |
$begingroup$
Jelly, 9 8 bytes
LÞŒœm"-Ẏ
Try it online!
LÞŒœṚ;¥/
is also 8 bytes.
Thanks to @EriktheOutgolfer and @JonathanAllan for both offering golfs to save a byte.
$endgroup$
$begingroup$
Nice! Clever golf:Ṛ€1¦
can becomem"-
.
$endgroup$
– Erik the Outgolfer
Apr 3 at 18:38
$begingroup$
Or you could go forLÞŒœṚ;¥/
$endgroup$
– Jonathan Allan
Apr 3 at 18:49
add a comment |
$begingroup$
Ruby, 51 bytes
->lr=1;l.sort_by!(&:size).mapl[r-=2]
Try it online!
$endgroup$
add a comment |
$begingroup$
05AB1E, 6 5 bytes
Saved 1 byte thanks to Kevin Cruijssen
I/O is a list of strings.
Link is modified for newline separated I/O for easier testing.
éι`Rì
Try it online!
Explanation
é # sort by length ascending
ι # uninterleave into 2 parts, both sorted ascending
` # push the 2 parts separately to the stack
R # reverse the second part
ì # and append it to the first
$endgroup$
$begingroup$
You can remove the firstR
and replace«
withi
to save a byte, since the third bullet-point rule allows both versions of uninterleaving.
$endgroup$
– Kevin Cruijssen
Apr 4 at 7:00
$begingroup$
@KevinCruijssen: Oh yeah, Thanks!
$endgroup$
– Emigna
Apr 4 at 9:14
add a comment |
$begingroup$
J, 11 bytes
,~`,/@:#&>
Try it online!
We sort it down first.
Then we reduce the list form right to left, but alternating which side we put the new element on. Done.
$endgroup$
$begingroup$
Very nice! You have a space at the end though, remove it for 11 bytes :)
$endgroup$
– Galen Ivanov
Apr 4 at 6:18
1
$begingroup$
Thanks Galen. Fixed!
$endgroup$
– Jonah
Apr 4 at 10:01
add a comment |
$begingroup$
PowerShell, 66 bytes
1..($a=$args|sort l*).count|?$_%2|%$a[-$_];$x=,$a[-++$_]+$x;$x
Try it online!
Takes input via splatting, which manifests on TIO as separate command-line arguments. sort
s on the l
ength, stores that into $a
, and constructs a range from 1
up to the count
of input strings. We then pull out only the odd ones ?$_%2
and feed those into a loop |%...
. Each iteration, we put the "last", then the "third from last", and so on onto the pipeline with $a[-$_]
. Separately, we also accumulate into $x
the "second from last", "fourth from last", etc. Out of the loop and the pipeline is flushed (so those elements are output) and then we output $x
. In both instances, the default output gives us newlines between items automatically.
$endgroup$
add a comment |
$begingroup$
PHP, 144 141 bytes
function($a)usort($a,function($b,$c)return strlen($b)-strlen($c););$e=[];foreach($a as$d)(array_.[unshift,push][++$i%2])($e,$d);return$e;
Try it online!
-3 bytes thanks to @Ismael Miguel!
$endgroup$
$begingroup$
nice one. Where can I read more about[array_unshift,array_push][++$i%2]($e,$d)
?
$endgroup$
– abhig10
Apr 4 at 10:15
2
$begingroup$
@abhig10 sure. It's an array with the two function names['array_push','array_unshift']
with[++$i%2]
as the index of the array alternating between a0
or1
so will evaluate to the other function each time. PHP's "variable functions" let you assign a varible to a function and execute by calling with parenthesis (ex:$f='array_push'; $f($e,$d);
==array_push($e,$d)
) so the($e,$d)
is then calling the evaluated element of the array. Just a shorter way to doif (++$i%2) array_push($e,$d); else array_unshift($e,$e);
. Guess there was some PHP syntactic sugar after all!
$endgroup$
– gwaugh
Apr 4 at 14:33
$begingroup$
Okay, it took me sometime to understand this. Awesome.
$endgroup$
– abhig10
Apr 4 at 15:21
1
$begingroup$
You can save 3 bytes by replacing[array_unshift,array_push][++$i%2]($e,$d)
with(array_.[unshift,push][++$i%2])($e,$d)
. What I did was to remove the repeatedarray_
, concatenated it and then the result is passed to the call.
$endgroup$
– Ismael Miguel
2 days ago
1
$begingroup$
@IsmaelMiguel that's brilliant. Thank you!
$endgroup$
– gwaugh
2 days ago
add a comment |
$begingroup$
MATLAB, 87 bytes
function f(y);[B,I]=sort(cellfun(@(x)length(x),y));yflip(I(1:2:end)),yI(2:2:end)'
Takes input as cell array of strings, outputs column of strings (not sure if that's legal)
> s = 'qweq qwe qw','qweqw','12132132131231231','asdasdasda','qwe','w';
> f(s)
> >>
> ans =
>
> 6×1 cell array
>
> 'qweq qwe qw'
> 'qweqw'
> 'qwe'
> '1234'
> 'asdasdasda'
> '12132132131231231'
PS: Thanks Sanchises for pointing to a bug with odd-length inputs
$endgroup$
$begingroup$
This fails on odd number of input strings, e.g.f('loooooooong','medium','short')
$endgroup$
– Sanchises
Apr 4 at 9:38
$begingroup$
Also some general golfing tips: theend
is optional for afunction
. Usingfunction x=f(y);x=...'
is shorter thanfunction f(y);disp(...')
.
$endgroup$
– Sanchises
Apr 4 at 9:43
$begingroup$
If you're stuck, here's how I would do it.
$endgroup$
– Sanchises
Apr 4 at 9:54
$begingroup$
@Sanchises thanks for pointing bug out. I did fix it exactly like you did. My issue withdisp
is i am not sure what output rules are. Should it be pure text or not? ordisp(...)
is okay or even justx=...
as you suggest
$endgroup$
– aaaaaa
Apr 4 at 16:36
$begingroup$
That question can be quite tricky to answer. In general however, input/output is as flexible as possible so you can focus on the main challenge (there's a list of allowed I/O by default on meta) and in this case, no extra restrictions are specified by the OP. In my experience, justy=input('');doSomething()
(without trailing semicolon to output the result) is shortest in MATLAB
$endgroup$
– Sanchises
Apr 4 at 17:03
|
show 6 more comments
$begingroup$
APL (Dyalog Unicode), 18 16 bytesSBCS
⊢⌷⍨∘⊂∘⍋≢¨×¯1*⍳∘⍴
Try it online!
-2 bytes thanks to @dzaima
Explanation of this giant composition of function:
(all the ∘
do is compose these functions together)
⊢⌷⍨∘⊂∘⍋≢¨×¯1*⍳∘⍴ ⍝ A giant train of functions. Argument is ⊢
⍴ ⍝ The shape of the argument (number of strings)
⍳ ⍝ Range 1..shape
¯1* ⍝ Calculate -1 to the power of each element in the range
⍝ gives ¯1 1 ¯1
× ⍝ Multiply each with...
≢¨ ⍝ The length of the corresponding string
⍋ ⍝ Grade: gets the indices for the sorted values
⌷ ⍝ Access with these indices into
⊢ ⍝ The argument
⊂ ⍝ (enclosed, otherwise the access would be dimension-wise)
¹
$endgroup$
$begingroup$
≢¨×¯1*⍳∘⍴
->(⊢∘-≢¨)
and it gets even shorter if you turn it into a dfn
$endgroup$
– ngn
yesterday
$begingroup$
however, i'm not sure this algorithm is correct. we should negate the length of every other string in their sorted order, not in the order they come from the input
$endgroup$
– ngn
yesterday
add a comment |
$begingroup$
APL+WIN, 31 38 bytes
See Adams comment
⊃n[(⍳⍴n)~a],⌽n[a←2×⍳⌊.5×⍴n←n[⍒∊⍴¨n←⎕]]
Try it online Courtesy of Dyalog Classic!
Prompts for a nested vector of strings
$endgroup$
$begingroup$
Does APL+ not have Monadic≢
"tally" to replace∊⍴
?
$endgroup$
– Adám
Apr 3 at 19:02
1
$begingroup$
Fails on'12' '1234' '1234' '1234' '1234' '12345678' '12345678' '12345678' '12345678'
. Clearly, the result should have been'12345678' '12345678' '1234' '1234' '12' '1234' '1234' '12345678' '12345678'
$endgroup$
– Adám
Apr 3 at 19:06
$begingroup$
@Adám My ancient version of APL+ does not have ≢. Agreed on your second comment I will take a look at it tomorrow.
$endgroup$
– Graham
Apr 3 at 19:26
add a comment |
$begingroup$
Retina, 26 bytes
N$`
$.&
*,2,^A`.+
,2,G`.+
Try it online! Explanation:
N$`
$.&
Sort the lines in ascending order of length ($.&
returns the length of the line).
*,2,^A`.+
Temporarily delete alternate lines and output the remaining lines in reverse order.
,2,G`.+
Keep the only lines that were temporarily deleted and output them.
$endgroup$
add a comment |
$begingroup$
Gaia, 10 bytes
el∫v:v+2%ụ
Try it online!
e | eval as Gaia code (list of strings)
l∫ | ∫ort by lengths (ascending)
v:v | reverse, dup, reverse
+ | concatenate lists
2% | take every other element
ụ | join by newlines and output
$endgroup$
4
$begingroup$
i like that your comments in unwrapped code form an arrow of strings
$endgroup$
– aaaaaa
Apr 3 at 23:47
add a comment |
$begingroup$
Japt, 8 bytes
ñÊó g0_w
-3 bytes thanks to Shaggy!
Try it
$endgroup$
$begingroup$
10 bytes with output as a 2D-array, which would seem to be allowed.
$endgroup$
– Shaggy
Apr 3 at 21:28
$begingroup$
Or, maybe, 8 bytes? On my phone so haven't tested it properly.
$endgroup$
– Shaggy
Apr 3 at 21:30
$begingroup$
@Shaggy I was looking for a function to find every nth element, but I couldn't find it. Thanks!
$endgroup$
– Embodiment of Ignorance
Apr 3 at 21:37
$begingroup$
there's alsoA.ë()
but I don't know if that'll lead to a shorter solution.
$endgroup$
– Shaggy
Apr 3 at 21:45
add a comment |
$begingroup$
PowerShell, 49 bytes
$args|sort l*|sort$_.Length*($global:x=-$x*2+1)
Try it online!
The double distillation.
$endgroup$
add a comment |
$begingroup$
T-SQL, 84 bytes
Input is a table variable
SELECT a FROM(SELECT*,row_number()over(order by len(a))r
FROM @)x order by(r%2-.5)*r
Try it online
$endgroup$
add a comment |
$begingroup$
Perl 6, 31 bytes
*.sort(&chars).sort:$++%2*$--
Try it online!
Sort by string length, then by static sequence 0, -1, 0, -3, 0, -5, ...
$endgroup$
add a comment |
$begingroup$
Javascript 95 Bytes
s=>s.sort((x,y)=>x.length-y.length).reduce((a,e,i)=>i%2?a.push(e):a.unshift(e);return a;,[]);
New contributor
$endgroup$
$begingroup$
-1s.sort()
sorts the strings lexicographically, not by string length.
$endgroup$
– asgallant
Apr 3 at 19:34
$begingroup$
Right, (x,y)=>x.length-y.length, should fix that.
$endgroup$
– somsom
Apr 3 at 19:41
add a comment |
$begingroup$
Red, 116 101 bytes
func[b][sort/compare b func[x y][(length? x)> length? y]collect[forall b[keep take b]keep reverse b]]
Try it online!
$endgroup$
add a comment |
$begingroup$
perl 5 (-p0777F/n/ -M5.01
), 59 bytes
for$x(sort$b=~y///c-length$a@F)--$}improve this answer
$endgroup$
perl 5 (-p0777F/n/ -M5.01
), 59 bytes
for$x(sort$b=~y///c-length$a@F)--${
TIO
edited Apr 4 at 9:18
answered Apr 4 at 8:17
Nahuel FouilleulNahuel Fouilleul
3,015211
3,015211
add a comment |
add a comment |
$begingroup$
C (gcc), 136 bytes
S(a,b)char**a,**b;a=strlen(*b)-strlen(*a);f(l,s,o,t,i,b,e)char**s,**o,*t;qsort(s,l,8,S);e=l-1;for(i=b=0;i-l;++i)o[i%2?b++:e--]=s[i];
Try it online!
The function f
is the solution. It takes the number of strings, the strings themselves, and the output buffer as arguments (plus four more used internally).
$endgroup$
$begingroup$
Why is./.bin.tio
in the output?
$endgroup$
– Teleporting Goat
Apr 3 at 22:27
$begingroup$
@TeleportingGoat Probably because their footer is using all ofargv
, which includes the filename
$endgroup$
– Jo King
Apr 4 at 1:46
$begingroup$
Exactly, it was just a quick test. One can construct any data that takes appropriate format. I'll update the TIO link later.
$endgroup$
– LambdaBeta
Apr 4 at 13:42
add a comment |
$begingroup$
C (gcc), 136 bytes
S(a,b)char**a,**b;a=strlen(*b)-strlen(*a);f(l,s,o,t,i,b,e)char**s,**o,*t;qsort(s,l,8,S);e=l-1;for(i=b=0;i-l;++i)o[i%2?b++:e--]=s[i];
Try it online!
The function f
is the solution. It takes the number of strings, the strings themselves, and the output buffer as arguments (plus four more used internally).
$endgroup$
$begingroup$
Why is./.bin.tio
in the output?
$endgroup$
– Teleporting Goat
Apr 3 at 22:27
$begingroup$
@TeleportingGoat Probably because their footer is using all ofargv
, which includes the filename
$endgroup$
– Jo King
Apr 4 at 1:46
$begingroup$
Exactly, it was just a quick test. One can construct any data that takes appropriate format. I'll update the TIO link later.
$endgroup$
– LambdaBeta
Apr 4 at 13:42
add a comment |
$begingroup$
C (gcc), 136 bytes
S(a,b)char**a,**b;a=strlen(*b)-strlen(*a);f(l,s,o,t,i,b,e)char**s,**o,*t;qsort(s,l,8,S);e=l-1;for(i=b=0;i-l;++i)o[i%2?b++:e--]=s[i];
Try it online!
The function f
is the solution. It takes the number of strings, the strings themselves, and the output buffer as arguments (plus four more used internally).
$endgroup$
C (gcc), 136 bytes
S(a,b)char**a,**b;a=strlen(*b)-strlen(*a);f(l,s,o,t,i,b,e)char**s,**o,*t;qsort(s,l,8,S);e=l-1;for(i=b=0;i-l;++i)o[i%2?b++:e--]=s[i];
Try it online!
The function f
is the solution. It takes the number of strings, the strings themselves, and the output buffer as arguments (plus four more used internally).
edited Apr 4 at 13:44
answered Apr 3 at 22:19
LambdaBetaLambdaBeta
2,149418
2,149418
$begingroup$
Why is./.bin.tio
in the output?
$endgroup$
– Teleporting Goat
Apr 3 at 22:27
$begingroup$
@TeleportingGoat Probably because their footer is using all ofargv
, which includes the filename
$endgroup$
– Jo King
Apr 4 at 1:46
$begingroup$
Exactly, it was just a quick test. One can construct any data that takes appropriate format. I'll update the TIO link later.
$endgroup$
– LambdaBeta
Apr 4 at 13:42
add a comment |
$begingroup$
Why is./.bin.tio
in the output?
$endgroup$
– Teleporting Goat
Apr 3 at 22:27
$begingroup$
@TeleportingGoat Probably because their footer is using all ofargv
, which includes the filename
$endgroup$
– Jo King
Apr 4 at 1:46
$begingroup$
Exactly, it was just a quick test. One can construct any data that takes appropriate format. I'll update the TIO link later.
$endgroup$
– LambdaBeta
Apr 4 at 13:42
$begingroup$
Why is
./.bin.tio
in the output?$endgroup$
– Teleporting Goat
Apr 3 at 22:27
$begingroup$
Why is
./.bin.tio
in the output?$endgroup$
– Teleporting Goat
Apr 3 at 22:27
$begingroup$
@TeleportingGoat Probably because their footer is using all of
argv
, which includes the filename$endgroup$
– Jo King
Apr 4 at 1:46
$begingroup$
@TeleportingGoat Probably because their footer is using all of
argv
, which includes the filename$endgroup$
– Jo King
Apr 4 at 1:46
$begingroup$
Exactly, it was just a quick test. One can construct any data that takes appropriate format. I'll update the TIO link later.
$endgroup$
– LambdaBeta
Apr 4 at 13:42
$begingroup$
Exactly, it was just a quick test. One can construct any data that takes appropriate format. I'll update the TIO link later.
$endgroup$
– LambdaBeta
Apr 4 at 13:42
add a comment |
$begingroup$
Japt, 8 bytes
Input as an array of lines, output as an array of 2 arrays of lines, one for each half of the list.
ñÊó
hUÎÔ
Try it (Additional code to allow for I/O as newline separated string)
ñÊó :Implicit input of array U
ñ :Sort by
Ê : Length
ó :Uninterleave
hUÎÔ :Newline reassigns to U
h :Set the first element in U to
UÎ : The first element in U
Ô : Reversed
$endgroup$
add a comment |
$begingroup$
Japt, 8 bytes
Input as an array of lines, output as an array of 2 arrays of lines, one for each half of the list.
ñÊó
hUÎÔ
Try it (Additional code to allow for I/O as newline separated string)
ñÊó :Implicit input of array U
ñ :Sort by
Ê : Length
ó :Uninterleave
hUÎÔ :Newline reassigns to U
h :Set the first element in U to
UÎ : The first element in U
Ô : Reversed
$endgroup$
add a comment |
$begingroup$
Japt, 8 bytes
Input as an array of lines, output as an array of 2 arrays of lines, one for each half of the list.
ñÊó
hUÎÔ
Try it (Additional code to allow for I/O as newline separated string)
ñÊó :Implicit input of array U
ñ :Sort by
Ê : Length
ó :Uninterleave
hUÎÔ :Newline reassigns to U
h :Set the first element in U to
UÎ : The first element in U
Ô : Reversed
$endgroup$
Japt, 8 bytes
Input as an array of lines, output as an array of 2 arrays of lines, one for each half of the list.
ñÊó
hUÎÔ
Try it (Additional code to allow for I/O as newline separated string)
ñÊó :Implicit input of array U
ñ :Sort by
Ê : Length
ó :Uninterleave
hUÎÔ :Newline reassigns to U
h :Set the first element in U to
UÎ : The first element in U
Ô : Reversed
answered Apr 4 at 10:27
ShaggyShaggy
18.9k21768
18.9k21768
add a comment |
add a comment |
Teleporting Goat is a new contributor. Be nice, and check out our Code of Conduct.
Teleporting Goat is a new contributor. Be nice, and check out our Code of Conduct.
Teleporting Goat is a new contributor. Be nice, and check out our Code of Conduct.
Teleporting Goat is a new contributor. Be nice, and check out our Code of Conduct.
If this is an answer to a challenge…
…Be sure to follow the challenge specification. However, please refrain from exploiting obvious loopholes. Answers abusing any of the standard loopholes are considered invalid. If you think a specification is unclear or underspecified, comment on the question instead.
…Try to optimize your score. For instance, answers to code-golf challenges should attempt to be as short as possible. You can always include a readable version of the code in addition to the competitive one.
Explanations of your answer make it more interesting to read and are very much encouraged.…Include a short header which indicates the language(s) of your code and its score, as defined by the challenge.
More generally…
…Please make sure to answer the question and provide sufficient detail.
…Avoid asking for help, clarification or responding to other answers (use comments instead).
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
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodegolf.stackexchange.com%2fquestions%2f182615%2farrow-those-variables%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
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
Required, but never shown
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
Required, but never shown
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
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
$begingroup$
I feel like there was a very similar challenge before...but welcome to PPCG!
$endgroup$
– Giuseppe
Apr 3 at 16:48
$begingroup$
@Giuseppe Yeah that's what I thought after posting, there's no way it hasn't been done before. Would you be ok with me deleting it now that you've answered it?
$endgroup$
– Teleporting Goat
Apr 3 at 16:54
1
$begingroup$
well I've been searching for a dupe but I'm not very good at the search...we do have a sandbox for posting challenges which can often catch things like that. I'm perfectly OK with you deleting it if you're worried about it being a dupe.
$endgroup$
– Giuseppe
Apr 3 at 16:55
1
$begingroup$
It's OK, we all start from the beginning :-)
$endgroup$
– Giuseppe
Apr 3 at 17:06
1
$begingroup$
Could you add a test case with an even number of strings?
$endgroup$
– Sherlock9
Apr 3 at 17:22