Arrow those variables!

29

2

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.

Teleporting Goat

Posted 2019-04-03T16:37:17.747

Reputation: 709

I feel like there was a very similar challenge before...but welcome to PPCG! – Giuseppe – 2019-04-03T16:48:43.143

@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? – Teleporting Goat – 2019-04-03T16:54:41.057

1

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.

– Giuseppe – 2019-04-03T16:55:50.890

@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... – Teleporting Goat – 2019-04-03T17:03:23.980

1It's OK, we all start from the beginning :-) – Giuseppe – 2019-04-03T17:06:06.323

1Could you add a test case with an even number of strings? – Sherlock9 – 2019-04-03T17:22:06.423

Are we guaranteed that all strings will be unique? – Shaggy – 2019-04-03T19:55:32.930

@Shaggy No, that's not a necessity – Teleporting Goat – 2019-04-03T19:56:54.357

May we return a list of strings? – Adám – 2019-04-03T20:02:03.417

Will strings have at least one character each? – Adám – 2019-04-03T20:12:07.453

@Adám, "Any I/O format is allowed." – Shaggy – 2019-04-03T21:23:27.990

Answers

15

Python 2, 47 bytes

lambda l:l.sort(key=len)or l[1::2][::-1]+l[::2]

Try it online!

ovs

Posted 2019-04-03T16:37:17.747

Reputation: 21 408

You'll need to rearrange some stuff, but you can use [::-2] directly to save 5 bytes. – Sherlock9 – 2019-04-03T17:03:47.907

@Sherlock9 I tried that, but then I had to check for the length, as lists with even / uneven lengths have to be handled differently. – ovs – 2019-04-03T17:06:08.077

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" ? – potato – 2019-04-05T13:54:50.400

9

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.

Giuseppe

Posted 2019-04-03T16:37:17.747

Reputation: 21 077

1

o<-L[... The other way to 'arrow variables'.

A less important aside, pryr::f(...) works here for 46. Try it online!

– CriminallyVulgar – 2019-04-04T07:40:38.157

@CriminallyVulgar using additional libraries turns this into a separate language, R + pryr which is why I typically avoid doing it unless there's a good reason to -- like for number theory questions, numbers is indispensable. – Giuseppe – 2019-04-08T15:02:46.617

7

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
  )

asgallant

Posted 2019-04-03T16:37:17.747

Reputation: 309

1

I don't think you have to count f=. 77

– dana – 2019-04-04T11:54:04.387

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. – asgallant – 2019-04-04T16:11:54.753

2

I think it depends whether your function uses recursion. i.e. f=x=>x?f(x-1). If so, you need to include f since you are calling it in your function. However, since you are not using recursion, you shouldn't have to include f. There are several posts in Meta, this one seems to explain it a little better. https://codegolf.meta.stackexchange.com/a/9032/8340

– dana – 2019-04-04T16:34:48.093

That would explain the inconsistencies I've seen. – asgallant – 2019-04-04T16:54:33.230

5

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.

Nick Kennedy

Posted 2019-04-03T16:37:17.747

Reputation: 11 829

Nice! Clever golf: Ṛ€1¦ can become m"-. – Erik the Outgolfer – 2019-04-03T18:38:12.773

Or you could go for LÞŒœṚ;¥/ – Jonathan Allan – 2019-04-03T18:49:17.837

5

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!

Embodiment of Ignorance

Posted 2019-04-03T16:37:17.747

Reputation: 7 014

5

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

streetster

Posted 2019-04-03T16:37:17.747

Reputation: 3 635

5

Ruby, 51 bytes

->l{r=1;l.sort_by!(&:size).map{l[r-=2]||(l*2)[~r]}}

Try it online!

G B

Posted 2019-04-03T16:37:17.747

Reputation: 11 099

5

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.

Jonah

Posted 2019-04-03T16:37:17.747

Reputation: 8 729

Very nice! You have a space at the end though, remove it for 11 bytes :) – Galen Ivanov – 2019-04-04T06:18:20.203

1Thanks Galen. Fixed! – Jonah – 2019-04-04T10:01:46.893

5

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

Emigna

Posted 2019-04-03T16:37:17.747

Reputation: 50 798

You can remove the first R and replace « with i to save a byte, since the third bullet-point rule allows both versions of uninterleaving. – Kevin Cruijssen – 2019-04-04T07:00:55.100

@KevinCruijssen: Oh yeah, Thanks! – Emigna – 2019-04-04T09:14:51.680

4

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!

640KB

Posted 2019-04-03T16:37:17.747

Reputation: 7 149

nice one. Where can I read more about [array_unshift,array_push][++$i%2]($e,$d)? – abhig10 – 2019-04-04T10:15:28.700

2@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 a 0 or 1 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 do if (++$i%2) array_push($e,$d); else array_unshift($e,$e);. Guess there was some PHP syntactic sugar after all! – 640KB – 2019-04-04T14:33:06.467

Okay, it took me sometime to understand this. Awesome. – abhig10 – 2019-04-04T15:21:36.817

1You 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 repeated array_, concatenated it and then the result is passed to the call. – Ismael Miguel – 2019-04-05T15:07:46.867

1@IsmaelMiguel that's brilliant. Thank you! – 640KB – 2019-04-05T15:13:19.693

4

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.

AdmBorkBork

Posted 2019-04-03T16:37:17.747

Reputation: 41 581

4

MATLAB, 87 bytes

function f(y);[B,I]=sort(cellfun(@(x)length(x),y));{y{flip(I(1:2:end))},y{I(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

aaaaa says reinstate Monica

Posted 2019-04-03T16:37:17.747

Reputation: 381

This fails on odd number of input strings, e.g. f({'loooooooong','medium','short'}) – Sanchises – 2019-04-04T09:38:32.087

Also some general golfing tips: the end is optional for a function. Using function x=f(y);x={...}' is shorter than function f(y);disp({...}'). – Sanchises – 2019-04-04T09:43:00.173

If you're stuck, here's how I would do it.

– Sanchises – 2019-04-04T09:54:04.973

@Sanchises thanks for pointing bug out. I did fix it exactly like you did. My issue with disp is i am not sure what output rules are. Should it be pure text or not? or disp({...}) is okay or even just x={...} as you suggest – aaaaa says reinstate Monica – 2019-04-04T16:36:36.603

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, just y=input('');doSomething() (without trailing semicolon to output the result) is shortest in MATLAB – Sanchises – 2019-04-04T17:03:21.347

(and one final hint: note that my solution was actually three bytes shorter still; it's really worth looking at what all the different braces actually do wrt cell arrays) – Sanchises – 2019-04-05T13:49:42.107

@Sanchises my MATLAB doesn't allow removing end from function definition :-( – aaaaa says reinstate Monica – 2019-04-05T16:22:33.660

Interesting, what version do you use? – Sanchises – 2019-04-05T16:38:18.650

@Sanchises oh my bad. You cannot omit end in in-script define functions. But saving function in a file allows it! – aaaaa says reinstate Monica – 2019-04-05T16:55:44.420

what version of MATLAB is this? I'm trying to golf this down but can't get it to work on my local version (R2016a) – Giuseppe – 2019-04-05T17:29:11.043

1

This can be 58 bytes in Octave.

– Giuseppe – 2019-04-05T17:34:54.847

3

APL (Dyalog Unicode), 18 bytesSBCS

{⍵[⍋-@(2∘|)⍋⍋≢¨⍵]}

Try it online!

Fixed the bug thanks to @ngn.

Explanation:

{⍵[⍋-@(2∘|)⍋⍋≢¨⍵]}
{                } ⍝ Function. Takes a single argument: ⍵, list of strings
             ≢¨⍵   ⍝ The length of each element in the list
           ⍋⍋      ⍝ Sort the lengths
    -@(2∘|)        ⍝ At (@) elements divisible by 2 (|), negate (-)
                   ⍝     gives -1 2 -3 4 -5
   ⍋               ⍝ Sort this list again, gives the indices of that list ^ sorted
 ⍵[             ]  ⍝ Use these indices to index into the argument

¹

Ven

Posted 2019-04-03T16:37:17.747

Reputation: 3 382

1≢¨×¯1*⍳∘⍴ -> (⊢∘-\≢¨) and it gets even shorter if you turn it into a dfn – ngn – 2019-04-07T04:58:14.690

1however, 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 – ngn – 2019-04-07T05:14:09.813

2

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

Graham

Posted 2019-04-03T16:37:17.747

Reputation: 3 184

Does APL+ not have Monadic "tally" to replace ∊⍴ ? – Adám – 2019-04-03T19:02:04.273

1

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'

– Adám – 2019-04-03T19:06:23.237

@Adám My ancient version of APL+ does not have ≢. Agreed on your second comment I will take a look at it tomorrow. – Graham – 2019-04-03T19:26:25.930

2

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.

Neil

Posted 2019-04-03T16:37:17.747

Reputation: 95 035

2

Japt, 8 bytes

ñÊó g0_w

-3 bytes thanks to Shaggy!

Try it

Embodiment of Ignorance

Posted 2019-04-03T16:37:17.747

Reputation: 7 014

10 bytes with output as a 2D-array, which would seem to be allowed. – Shaggy – 2019-04-03T21:28:38.020

Or, maybe, 8 bytes? On my phone so haven't tested it properly.

– Shaggy – 2019-04-03T21:30:26.147

@Shaggy I was looking for a function to find every nth element, but I couldn't find it. Thanks! – Embodiment of Ignorance – 2019-04-03T21:37:50.590

there's also A.ë() but I don't know if that'll lead to a shorter solution. – Shaggy – 2019-04-03T21:45:01.680

2

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

Giuseppe

Posted 2019-04-03T16:37:17.747

Reputation: 21 077

4i like that your comments in unwrapped code form an arrow of strings – aaaaa says reinstate Monica – 2019-04-03T23:47:44.577

2

PowerShell, 49 bytes

$args|sort l*|sort{$_.Length*($global:x=-$x*2+1)}

Try it online!

The double distillation.

mazzy

Posted 2019-04-03T16:37:17.747

Reputation: 4 832

2

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

t-clausen.dk

Posted 2019-04-03T16:37:17.747

Reputation: 2 874

2

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, ...

nwellnhof

Posted 2019-04-03T16:37:17.747

Reputation: 10 037

1

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;},[]);

somsom

Posted 2019-04-03T16:37:17.747

Reputation: 11

-1 s.sort() sorts the strings lexicographically, not by string length. – asgallant – 2019-04-03T19:34:10.260

Right, (x,y)=>x.length-y.length, should fix that. – somsom – 2019-04-03T19:41:22.177

1

C (gcc), 136 128 bytes

S(a,b)int**a,**b;{a=strlen(*b)-strlen(*a);}f(l,s,o,i,b,e)int**s,**o;{qsort(s,l,8,S);e=l-1;for(i=b=0;i-l;)o[i++%2?b++:e--]=s[i];}

Try it online!

-8 bytes thanks to ceilingcat.

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).

LambdaBeta

Posted 2019-04-03T16:37:17.747

Reputation: 2 499

Why is ./.bin.tio in the output?

– Teleporting Goat – 2019-04-03T22:27:21.940

@TeleportingGoat Probably because their footer is using all of argv, which includes the filename – Jo King – 2019-04-04T01:46:52.790

Exactly, it was just a quick test. One can construct any data that takes appropriate format. I'll update the TIO link later. – LambdaBeta – 2019-04-04T13:42:43.993

haha, the problem with these short variable names: you forget what you hat t for in the first place and keep it around even when you don't need it! – LambdaBeta – 2019-04-10T20:44:31.903

122 bytes – ceilingcat – 2019-08-13T19:51:10.883

1

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!

Galen Ivanov

Posted 2019-04-03T16:37:17.747

Reputation: 13 815

1

perl 5 (-p0777F/\n/ -M5.01), 59 bytes

for$x(sort{$b=~y///c-length$a}@F){--$|?$\="$x
".$\:say$x}}{

TIO

Nahuel Fouilleul

Posted 2019-04-03T16:37:17.747

Reputation: 5 582

0

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

Shaggy

Posted 2019-04-03T16:37:17.747

Reputation: 24 623

0

Haskell, 104 96 bytes

import Data.List
f s=splitAt((length s+1)`div`2)s
g=sortOn length
h(x,y)=reverse(g x)++g y
z=h.f

Try it online!

bugs

Posted 2019-04-03T16:37:17.747

Reputation: 211

Invalid, you have to sort before cutting list in half – ASCII-only – 2019-04-14T02:05:14.930

90 – ASCII-only – 2019-04-14T02:17:58.330