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
Venezuelan girlfriend wants to travel the USA to be with me. What is the process?
How do I handle a potential work/personal life conflict as the manager of one of my friends?
Can I run a new neutral wire to repair a broken circuit?
Bullying boss launched a smear campaign and made me unemployable
What reasons are there for a Capitalist to oppose a 100% inheritance tax?
How does having to sign to support someone for elections fit with having a secret ballot?
How to tell a function to use the default argument values?
Examples of smooth manifolds admitting inbetween one and a continuum of complex structures
Non-Commutative Algebra book
What do you call someone who asks many questions?
ssTTsSTtRrriinInnnnNNNIiinngg
Expand and Contract
Question about the derivation of the intensity formula of a diffraction grating
How to compactly explain secondary and tertiary characters without resorting to stereotypes?
I would say: "You are another teacher", but she is a woman and I am a man
How much of data wrangling is a data scientist's job?
How do conventional missiles fly?
How to add frame around section using titlesec?
If human space travel is limited by the G force vulnerability, is there a way to counter G forces?
Assassin's bullet with mercury
Can mass be shunted off into hyperspace, but the matter remains?
What method can I use to design a dungeon difficult enough that the PCs can't make it through without killing them?
Why was the shrinking from 8″ made only to 5.25″ and not smaller (4″ or less)?
Can we compute the area of a quadrilateral with one right angle when we only know the lengths of any three sides?
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
Teleporting Goat is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
$endgroup$
|
show 5 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
Teleporting Goat is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
$endgroup$
$begingroup$
I feel like there was a very similar challenge before...but welcome to PPCG!
$endgroup$
– Giuseppe
3 hours ago
$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
3 hours ago
$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
3 hours ago
$begingroup$
@Giuseppe Well it's too late, with 2 answers SE won't let me delete it. I had hoped for a better start in PPCG...
$endgroup$
– Teleporting Goat
3 hours ago
1
$begingroup$
Could you add a test case with an even number of strings?
$endgroup$
– Sherlock9
2 hours ago
|
show 5 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
Teleporting Goat is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
$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
Teleporting Goat is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Teleporting Goat is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
edited 2 hours ago
Teleporting Goat
New contributor
Teleporting Goat is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
asked 3 hours ago
Teleporting GoatTeleporting Goat
1313
1313
New contributor
Teleporting Goat is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Teleporting Goat is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Teleporting Goat is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
$begingroup$
I feel like there was a very similar challenge before...but welcome to PPCG!
$endgroup$
– Giuseppe
3 hours ago
$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
3 hours ago
$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
3 hours ago
$begingroup$
@Giuseppe Well it's too late, with 2 answers SE won't let me delete it. I had hoped for a better start in PPCG...
$endgroup$
– Teleporting Goat
3 hours ago
1
$begingroup$
Could you add a test case with an even number of strings?
$endgroup$
– Sherlock9
2 hours ago
|
show 5 more comments
$begingroup$
I feel like there was a very similar challenge before...but welcome to PPCG!
$endgroup$
– Giuseppe
3 hours ago
$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
3 hours ago
$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
3 hours ago
$begingroup$
@Giuseppe Well it's too late, with 2 answers SE won't let me delete it. I had hoped for a better start in PPCG...
$endgroup$
– Teleporting Goat
3 hours ago
1
$begingroup$
Could you add a test case with an even number of strings?
$endgroup$
– Sherlock9
2 hours ago
$begingroup$
I feel like there was a very similar challenge before...but welcome to PPCG!
$endgroup$
– Giuseppe
3 hours ago
$begingroup$
I feel like there was a very similar challenge before...but welcome to PPCG!
$endgroup$
– Giuseppe
3 hours ago
$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
3 hours ago
$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
3 hours ago
$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
3 hours ago
$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
3 hours ago
$begingroup$
@Giuseppe Well it's too late, with 2 answers SE won't let me delete it. I had hoped for a better start in PPCG...
$endgroup$
– Teleporting Goat
3 hours ago
$begingroup$
@Giuseppe Well it's too late, with 2 answers SE won't let me delete it. I had hoped for a better start in PPCG...
$endgroup$
– Teleporting Goat
3 hours ago
1
1
$begingroup$
Could you add a test case with an even number of strings?
$endgroup$
– Sherlock9
2 hours ago
$begingroup$
Could you add a test case with an even number of strings?
$endgroup$
– Sherlock9
2 hours ago
|
show 5 more comments
10 Answers
10
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
3 hours ago
$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
3 hours ago
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$
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
1 hour ago
$begingroup$
Or you could go forLÞŒœṚ;¥/
$endgroup$
– Jonathan Allan
1 hour ago
add a comment |
$begingroup$
R, 63 bytes
function(L,b=!seq(L)%%2,o=L[order(nchar(L))])c(rev(o[!b]),o[b])
Try it online!
Sort by string lengths, then put combine the terms at odd indices (reversed) with terms at even indices.
$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$
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
1 hour ago
$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
1 hour ago
$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
46 mins ago
add a comment |
$begingroup$
PHP, 144 bytes
function($a)usort($a,function($b,$c)return strlen($b)-strlen($c););$e=[];foreach($a as$d)[array_unshift,array_push][++$i%2]($e,$d);return$e;
Try it online!
Sadly, PHP just doesn't have a lot of syntactic sugar to make this shorter. Submitting it anyway!
$endgroup$
add a comment |
$begingroup$
Javascript 79 bytes
Takes input as an array of strings, outputs an arrow-sorted array of strings.
f=s=>s.sort((a,b)=>a.length-b.length).reduce((m,x,i)=>i%2?[...m,x]:[x,...m],[])
Explanation
f = 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$
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. sorts on the length, 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$
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
somsom is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
$endgroup$
$begingroup$
-1s.sort()sorts the strings lexicographically, not by string length.
$endgroup$
– asgallant
39 mins ago
$begingroup$
Right, (x,y)=>x.length-y.length, should fix that.
$endgroup$
– somsom
31 mins ago
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
return StackExchange.using("mathjaxEditing", function ()
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
);
);
, "mathjax-editing");
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "200"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Teleporting Goat is a new contributor. Be nice, and check out our Code of Conduct.
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
10 Answers
10
active
oldest
votes
10 Answers
10
active
oldest
votes
active
oldest
votes
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
3 hours ago
$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
3 hours ago
add a comment |
$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
3 hours ago
$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
3 hours ago
add a comment |
$begingroup$
Python 2, 47 bytes
lambda l:l.sort(key=len)or l[1::2][::-1]+l[::2]
Try it online!
$endgroup$
Python 2, 47 bytes
lambda l:l.sort(key=len)or l[1::2][::-1]+l[::2]
Try it online!
answered 3 hours ago
ovsovs
19.4k21160
19.4k21160
$begingroup$
You'll need to rearrange some stuff, but you can use[::-2]directly to save 5 bytes.
$endgroup$
– Sherlock9
3 hours ago
$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
3 hours ago
add a comment |
$begingroup$
You'll need to rearrange some stuff, but you can use[::-2]directly to save 5 bytes.
$endgroup$
– Sherlock9
3 hours ago
$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
3 hours ago
$begingroup$
You'll need to rearrange some stuff, but you can use
[::-2] directly to save 5 bytes.$endgroup$
– Sherlock9
3 hours ago
$begingroup$
You'll need to rearrange some stuff, but you can use
[::-2] directly to save 5 bytes.$endgroup$
– Sherlock9
3 hours ago
$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
3 hours ago
$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
3 hours ago
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$
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$
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$
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!
answered 2 hours ago
Embodiment of IgnoranceEmbodiment of Ignorance
2,338126
2,338126
add a comment |
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
1 hour ago
$begingroup$
Or you could go forLÞŒœṚ;¥/
$endgroup$
– Jonathan Allan
1 hour ago
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
1 hour ago
$begingroup$
Or you could go forLÞŒœṚ;¥/
$endgroup$
– Jonathan Allan
1 hour ago
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$
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.
edited 1 hour ago
answered 2 hours ago
Nick KennedyNick Kennedy
1,28649
1,28649
$begingroup$
Nice! Clever golf:Ṛ€1¦can becomem"-.
$endgroup$
– Erik the Outgolfer
1 hour ago
$begingroup$
Or you could go forLÞŒœṚ;¥/
$endgroup$
– Jonathan Allan
1 hour ago
add a comment |
$begingroup$
Nice! Clever golf:Ṛ€1¦can becomem"-.
$endgroup$
– Erik the Outgolfer
1 hour ago
$begingroup$
Or you could go forLÞŒœṚ;¥/
$endgroup$
– Jonathan Allan
1 hour ago
$begingroup$
Nice! Clever golf:
Ṛ€1¦ can become m"-.$endgroup$
– Erik the Outgolfer
1 hour ago
$begingroup$
Nice! Clever golf:
Ṛ€1¦ can become m"-.$endgroup$
– Erik the Outgolfer
1 hour ago
$begingroup$
Or you could go for
LÞŒœṚ;¥/$endgroup$
– Jonathan Allan
1 hour ago
$begingroup$
Or you could go for
LÞŒœṚ;¥/$endgroup$
– Jonathan Allan
1 hour ago
add a comment |
$begingroup$
R, 63 bytes
function(L,b=!seq(L)%%2,o=L[order(nchar(L))])c(rev(o[!b]),o[b])
Try it online!
Sort by string lengths, then put combine the terms at odd indices (reversed) with terms at even indices.
$endgroup$
add a comment |
$begingroup$
R, 63 bytes
function(L,b=!seq(L)%%2,o=L[order(nchar(L))])c(rev(o[!b]),o[b])
Try it online!
Sort by string lengths, then put combine the terms at odd indices (reversed) with terms at even indices.
$endgroup$
add a comment |
$begingroup$
R, 63 bytes
function(L,b=!seq(L)%%2,o=L[order(nchar(L))])c(rev(o[!b]),o[b])
Try it online!
Sort by string lengths, then put combine the terms at odd indices (reversed) with terms at even indices.
$endgroup$
R, 63 bytes
function(L,b=!seq(L)%%2,o=L[order(nchar(L))])c(rev(o[!b]),o[b])
Try it online!
Sort by string lengths, then put combine the terms at odd indices (reversed) with terms at even indices.
answered 3 hours ago
GiuseppeGiuseppe
17.4k31152
17.4k31152
add a comment |
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$
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$
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$
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
edited 2 hours ago
answered 2 hours ago
streetsterstreetster
2,614515
2,614515
add a comment |
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
1 hour ago
$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
1 hour ago
$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
46 mins ago
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
1 hour ago
$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
1 hour ago
$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
46 mins ago
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$
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
edited 30 mins ago
answered 1 hour ago
GrahamGraham
2,62678
2,62678
$begingroup$
Does APL+ not have Monadic≢"tally" to replace∊⍴?
$endgroup$
– Adám
1 hour ago
$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
1 hour ago
$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
46 mins ago
add a comment |
$begingroup$
Does APL+ not have Monadic≢"tally" to replace∊⍴?
$endgroup$
– Adám
1 hour ago
$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
1 hour ago
$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
46 mins ago
$begingroup$
Does APL+ not have Monadic
≢ "tally" to replace ∊⍴ ?$endgroup$
– Adám
1 hour ago
$begingroup$
Does APL+ not have Monadic
≢ "tally" to replace ∊⍴ ?$endgroup$
– Adám
1 hour ago
$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
1 hour ago
$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
1 hour ago
$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
46 mins ago
$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
46 mins ago
add a comment |
$begingroup$
PHP, 144 bytes
function($a)usort($a,function($b,$c)return strlen($b)-strlen($c););$e=[];foreach($a as$d)[array_unshift,array_push][++$i%2]($e,$d);return$e;
Try it online!
Sadly, PHP just doesn't have a lot of syntactic sugar to make this shorter. Submitting it anyway!
$endgroup$
add a comment |
$begingroup$
PHP, 144 bytes
function($a)usort($a,function($b,$c)return strlen($b)-strlen($c););$e=[];foreach($a as$d)[array_unshift,array_push][++$i%2]($e,$d);return$e;
Try it online!
Sadly, PHP just doesn't have a lot of syntactic sugar to make this shorter. Submitting it anyway!
$endgroup$
add a comment |
$begingroup$
PHP, 144 bytes
function($a)usort($a,function($b,$c)return strlen($b)-strlen($c););$e=[];foreach($a as$d)[array_unshift,array_push][++$i%2]($e,$d);return$e;
Try it online!
Sadly, PHP just doesn't have a lot of syntactic sugar to make this shorter. Submitting it anyway!
$endgroup$
PHP, 144 bytes
function($a)usort($a,function($b,$c)return strlen($b)-strlen($c););$e=[];foreach($a as$d)[array_unshift,array_push][++$i%2]($e,$d);return$e;
Try it online!
Sadly, PHP just doesn't have a lot of syntactic sugar to make this shorter. Submitting it anyway!
answered 50 mins ago
gwaughgwaugh
2,048516
2,048516
add a comment |
add a comment |
$begingroup$
Javascript 79 bytes
Takes input as an array of strings, outputs an arrow-sorted array of strings.
f=s=>s.sort((a,b)=>a.length-b.length).reduce((m,x,i)=>i%2?[...m,x]:[x,...m],[])
Explanation
f = 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$
add a comment |
$begingroup$
Javascript 79 bytes
Takes input as an array of strings, outputs an arrow-sorted array of strings.
f=s=>s.sort((a,b)=>a.length-b.length).reduce((m,x,i)=>i%2?[...m,x]:[x,...m],[])
Explanation
f = 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$
add a comment |
$begingroup$
Javascript 79 bytes
Takes input as an array of strings, outputs an arrow-sorted array of strings.
f=s=>s.sort((a,b)=>a.length-b.length).reduce((m,x,i)=>i%2?[...m,x]:[x,...m],[])
Explanation
f = 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$
Javascript 79 bytes
Takes input as an array of strings, outputs an arrow-sorted array of strings.
f=s=>s.sort((a,b)=>a.length-b.length).reduce((m,x,i)=>i%2?[...m,x]:[x,...m],[])
Explanation
f = 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
)
answered 30 mins ago
asgallantasgallant
1994
1994
add a comment |
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. sorts on the length, 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$
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. sorts on the length, 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$
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. sorts on the length, 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$
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. sorts on the length, 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.
answered 1 min ago
AdmBorkBorkAdmBorkBork
27.6k466237
27.6k466237
add a comment |
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
somsom is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
$endgroup$
$begingroup$
-1s.sort()sorts the strings lexicographically, not by string length.
$endgroup$
– asgallant
39 mins ago
$begingroup$
Right, (x,y)=>x.length-y.length, should fix that.
$endgroup$
– somsom
31 mins ago
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
somsom is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
$endgroup$
$begingroup$
-1s.sort()sorts the strings lexicographically, not by string length.
$endgroup$
– asgallant
39 mins ago
$begingroup$
Right, (x,y)=>x.length-y.length, should fix that.
$endgroup$
– somsom
31 mins ago
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
somsom is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
$endgroup$
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
somsom is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
edited 6 mins ago
Embodiment of Ignorance
2,338126
2,338126
New contributor
somsom is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
answered 50 mins ago
somsomsomsom
11
11
New contributor
somsom is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
somsom is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
somsom is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
$begingroup$
-1s.sort()sorts the strings lexicographically, not by string length.
$endgroup$
– asgallant
39 mins ago
$begingroup$
Right, (x,y)=>x.length-y.length, should fix that.
$endgroup$
– somsom
31 mins ago
add a comment |
$begingroup$
-1s.sort()sorts the strings lexicographically, not by string length.
$endgroup$
– asgallant
39 mins ago
$begingroup$
Right, (x,y)=>x.length-y.length, should fix that.
$endgroup$
– somsom
31 mins ago
$begingroup$
-1
s.sort() sorts the strings lexicographically, not by string length.$endgroup$
– asgallant
39 mins ago
$begingroup$
-1
s.sort() sorts the strings lexicographically, not by string length.$endgroup$
– asgallant
39 mins ago
$begingroup$
Right, (x,y)=>x.length-y.length, should fix that.
$endgroup$
– somsom
31 mins ago
$begingroup$
Right, (x,y)=>x.length-y.length, should fix that.
$endgroup$
– somsom
31 mins ago
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
3 hours ago
$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
3 hours ago
$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
3 hours ago
$begingroup$
@Giuseppe Well it's too late, with 2 answers SE won't let me delete it. I had hoped for a better start in PPCG...
$endgroup$
– Teleporting Goat
3 hours ago
1
$begingroup$
Could you add a test case with an even number of strings?
$endgroup$
– Sherlock9
2 hours ago