3
\$\begingroup\$

Aim

If you are given a sequence such as (A, G, M, _, _, C, D, _, G) your aim is to push all non empty elements to one side to get (A, G, M, C, D, G, _, _, _). You must preserve the order of the non-empty elements.

Rules

  1. You are free to choose the sequence type. (strings, numbers, containers etc). You must also choose a Nil/None/Null character or element that will fit your sequence type. eg; spaces or underscores inside a string.
  2. User should be able to input the push direction (left or right) and the sequence. Input values and format are up to you to choose. eg; Your code handles sequences that are numbers with 7 as the null character. You set R for right, L for left. So user inputs R, 1577462722727777. The result is 7777777715462222
  3. Rearranged sequence should be displayed or written to a file.
  4. Your code should not exceed 128 bytes.

Points

  1. +(10 / bytes) golfing points
  2. +1 for Moses-Mode. Push half the elements to the left, and half to the right. If odd number of items are present, you may choose the direction of the final element. eg; (A, G, M, _, _, _, C, D, G)

You may use use this calculator to round your points to the hundredth decimal place.

\$\endgroup\$
11
  • \$\begingroup\$ For Moses-Mode, does that have to be a choice along with right and left, or can we just implement it by itself? \$\endgroup\$
    – Maltysen
    Jun 6, 2015 at 21:33
  • 7
    \$\begingroup\$ Your scoring basically forces languages to go for the bonus to have a chance. The weird scoring also feels like a gimmick. Given that the bonus requires you to do a modified task, it would be cleaner to have it be part of the challenge instead. \$\endgroup\$
    – xnor
    Jun 6, 2015 at 21:35
  • \$\begingroup\$ @Maltysen Yes, it should be one of the input choices along with right and left. \$\endgroup\$ Jun 6, 2015 at 21:35
  • 7
    \$\begingroup\$ The 128 byte limit is a needless barrier that keeps wordy languages from participating. \$\endgroup\$
    – xnor
    Jun 6, 2015 at 21:37
  • 4
    \$\begingroup\$ Did you really link to a calculator that rounds numbers? I expected something where we put in byte count and an output or something, not "A guide to rounding to 2dp" \$\endgroup\$
    – Alec Teal
    Jun 7, 2015 at 3:04

14 Answers 14

6
\$\begingroup\$

R - 10 / 71 70 66 39 bytes = 0.26 points

Saved 27 bytes thanks to Mickey!

function(d,s)s[order(xor(!is.na(s),d))]

This creates an unnamed function which takes a direction d (logical with TRUE corresponding to right and FALSE to left (legit per OP's comment)) and a vector s (assumed to use NA for empty elements) as input and returns a vector. To call it, give it a name, e.g. f=function(d,s)....

This does not support "Moses Mode."

Ungolfed + explanation:

f <- function(d, s) {
    # Order the elements of s by non-missing exclusive or direction
    s[order(xor(!is.na(s), d))]
}

Examples:

> test <- c(1, 5, NA, NA, 4, 6, 2, NA, 2, 2, NA, 2, NA, NA, NA, NA)
> f(FALSE, test)
[1] NA NA NA NA NA NA NA NA  1  5  4  6  2  2  2  2
> f(TRUE, test)
[1]  1  5  4  6  2  2  2  2 NA NA NA NA NA NA NA NA

And an extra special example just for FryAmTheEggman:

> c(f(TRUE, test), "BATMAN")
[1] "1" "5" "4" "6" "2" "2" "2" "2" NA NA NA NA NA NA NA NA "BATMAN"
\$\endgroup\$
6
  • 5
    \$\begingroup\$ I can't believe I've never seen a post on this site of an R array containing NA NA NA NA NA NA NA NA BATMAN. ;) \$\endgroup\$ Jun 7, 2015 at 0:43
  • 3
    \$\begingroup\$ @FryAmTheEggman: Until now! \$\endgroup\$
    – Alex A.
    Jun 7, 2015 at 1:29
  • \$\begingroup\$ You could save a few using the an order on the is.na rather than reconstructing the vector. function(d,s){O=is.na(s);if(d)s[order(O)]else s[order(!O)]} \$\endgroup\$
    – MickyT
    Jun 8, 2015 at 2:27
  • \$\begingroup\$ Actually taking it a step further and using an xor this is quite a bit shorter function(d,s)s[order(xor(!is.na(s),d))] \$\endgroup\$
    – MickyT
    Jun 8, 2015 at 2:32
  • 1
    \$\begingroup\$ Wish I could see these sort of things in my own answers :-) \$\endgroup\$
    – MickyT
    Jun 8, 2015 at 7:46
4
\$\begingroup\$

Haskell 10/24 bytes = 0.42 points

import Data.List
(.sort)

Sadly the sort function is in Data.List, so I have to import it.

The sequence type is boolean values, i.e. True or False. The empty value is False. Pushing non empty values to the right is indicated by id and to the left by reverse.

Example usage:

*Main> (.sort) id [True,False,False,True,False,True]
[False,False,False,True,True,True]
*Main> (.sort) reverse [True,False,False,True,False,True]
[True,True,True,False,False,False]
\$\endgroup\$
4
\$\begingroup\$

Pyth, 10 / 15 14 bytes + 1 = 1.71 points

[email protected]@dzQ

This takes two lines of input. The first one is a string (spaces are the special characters) and the second one is 0 (push all spaces to the right), 1 (Moses-mode) or 4 (push all spaces to the left).

Try it online: Demonstration

Thanks to @isaacg for saving one byte

Explanation:

                 implicit: z = input string, Q = input number
       -zd       remove all spaces from z
     c2          split into 2 equal pieces
    a            append
          @dz    get all spaces in z
  .p             create all permutation of the 3 strings
 @           Q   choose the Qth permutation
s                and join the strings       

Pyth, 10 / 7 bytes = 1.43 points

oqQqNdz

Input format: string on first line, and 0 (push spaces to the left) or 1 (push spaces to the right) on second line.

Try it online: Demonstration

This is worth less points, but it's a quite simple and elegant solution.

Explanation:

          implicit: z = input string, Q = input number
o     z   order the chars N of z by the key:
 qQqNd       Q == (N == " ")

When Q == 0 all spaces get the key 0 and all other chars get the key 1, otherwise it's exactly opposite. Then the string is sorted by their keys. Since sorting is stable in Pyth, it sorts all spaces either to the beginning or the end and keeps all other chars in the same order.

\$\endgroup\$
1
  • \$\begingroup\$ To get all of the spaces in z, use @dz. It's shorter than using J. \$\endgroup\$
    – isaacg
    Jun 7, 2015 at 0:40
4
\$\begingroup\$

I honestly don't mind that the challenge has a byte limit. It is the OP's choice to impose that restriction to make things more interesting. However, just to represent how a "wordy" language would do, here's java:

Java

283 bytes = 0.04points + 1 = 1.04

enum E{;public static void main(String[]r){String s="",t=r[0].substring(1),d="_";char p=r[0].charAt(0);for(;t.indexOf(d)>=0;){r=t.split(d,2);t=r[0]+r[1];s+=d;}int x=(t.length()-1)/2;if(p=='L')t+=s;else if(p=='R')t=s+t;else t=t.substring(0,x)+s+t.substring(x+1);System.out.print(t);}}

Input is from the command line. The first character can be L for left, R for right, anything else for moses mode. Underscore is used for the null character.

If I remove the moses mode code, I get:

207 bytes = 0.05points

enum E{;public static void main(String[]r){String s="",t=r[0].substring(1),d="_";char p=r[0].charAt(0);for(;t.indexOf(d)>=0;){r=t.split(d,2);t=r[0]+r[1];s+=d;}if(p=='L')t+=s;else t=s+t;System.out.print(t);}}

Input is from the command line. L is left, anything else is right. Underscore is null.

I quite enjoyed the challenge of trying to fit Java code inside 128 bytes. Java just isn't a competitive language for codegolfs though.

\$\endgroup\$
2
\$\begingroup\$

k,10/12 = 0.83 points

{x((>:;<:)y)x}

It takes an array of booleans and a 0 or 1 to say to push to the right or the left.

k){x((>:;<:)y)x}[1011110101101b;0]
1111111110000b
k){x((>:;<:)y)x}[1011110101101b;1]
0000111111111b

k, 10/29 = 0.34 points

A more flexible version that should work for all input types and which allows you to choose the null character.

{x@,/$[~z;|:;](&~:;&:)@\:x=y}

.

k){x@,/$[~z;|:;](&~:;&:)@\:x=y}[1 0 3 6 5 6 0 6 0;6;0]
6 6 6 1 0 3 5 0 0
k){x@,/$[~z;|:;](&~:;&:)@\:x=y}[1 0 3 6 5 6 0 6 0;6;1]
1 0 3 5 0 0 6 6 6
k){x@,/$[~z;|:;](&~:;&:)@\:x=y}[1 0 3 6 5 6 0 6 0;0;1]
1 3 6 5 6 6 0 0 0
k){x@,/$[~z;|:;](&~:;&:)@\:x=y}["abc def ghi";" ";1]
"abcdefghi  "
k){x@,/$[~z;|:;](&~:;&:)@\:x=y}["abc def ghi";" ";0]
"  abcdefghi"
\$\endgroup\$
1
\$\begingroup\$

Pyth - 10 / 1 byte + 1 = 11

Really cheaty, but it seems allowed, basically puts all the code in the options. Doing bonus now.

It takes an array of ints, with 0 as falsey.

The left value is: Y.append(eval(input())) or [i for i in Y[0] if i] + [0]*Y[0].count(0)

The right value is: Y.append(eval(input())) or [0]*Y[0].count(0) + [i for i in Y[0] if i]

The Moses value is: Y.append(eval(input())) or Y.append(chop(2, [i for i in Y[0] if i])) or Y[1][0] + [0]*Y[0].count(0) + Y[1][1]

So here is the code:

Q

Because of security reasons it only works locally, not online.

Old, non-cheaty one - 0.83:

Not going for the bonus, takes array of ints, 0 is falsey, and 1 for right, -1 for left.

Ms%H,fTGf!TG

Explanation coming soon.

M           Two var lambda
 s          Join the two parts of the array
  %         String slice step, reverses if -1, unchanged if 1
   H        Second arg
   ,        Two element list
    fTG     Filter arg 1 for truthiness
    f!TG    Filter arg 1 for falsiness

Try it here online.

\$\endgroup\$
3
  • 1
    \$\begingroup\$ @AlexA. derp :) \$\endgroup\$
    – Maltysen
    Jun 6, 2015 at 22:11
  • 1
    \$\begingroup\$ This is why my "use any format you want" challenges specify "your format can contain delimiters but not operators" \$\endgroup\$
    – Sparr
    Jun 8, 2015 at 3:47
  • \$\begingroup\$ @Sparr or more generally, any "sane" input format. \$\endgroup\$
    – Maltysen
    Jun 8, 2015 at 18:54
1
\$\begingroup\$

R 10 / 69 + 1 = 1.14

Not very pretty. An unnamed function where d is a either 0 (left), 1 (right) or .5 (Moses). s is a vector of integers where 0 as the null.

function(d,s)c(rep(0,(l=length(s[!s]))*(1-d)),s[!!s],rep(0,(l*d)+.5))

Test example

> g=function(d,s)c(rep(0,(l=length(s[!s]))*(1-d)),s[!!s],rep(0,(l*d)+.5))
> test=c(17,0,2,42,2,4,35,0,0,45,0,2,0,7,9)
> g(0,test)
 [1]  0  0  0  0  0 17  2 42  2  4 35 45  2  7  9
> g(1,test)
 [1] 17  2 42  2  4 35 45  2  7  9  0  0  0  0  0
> g(.5,test)
 [1]  0  0 17  2 42  2  4 35 45  2  7  9  0  0  0
> test=c(17,0,2,42,0,2,4,35,0,0,45,0,2,0,7,9)
> g(0,test)
 [1]  0  0  0  0  0  0 17  2 42  2  4 35 45  2  7  9
> g(1,test)
 [1] 17  2 42  2  4 35 45  2  7  9  0  0  0  0  0  0
> g(.5,test)
 [1]  0  0  0 17  2 42  2  4 35 45  2  7  9  0  0  0
> 
\$\endgroup\$
1
\$\begingroup\$

JavaScript (ES6) - 168 bytes, 1.06 points

x=(d,a)=>{c='concat',l='length',o=a.filter(l=>l),z=new Array(a[l]-o[l]).fill(0);if(!d)Array.prototype.splice.apply(o,[a[l]/4,0][c](z));return d?(d>1?z[c](o):o[c](z)):o}

Input:

  • 0 (or any falsy value) for Moses mode, 1 for push left, 2 for push right
  • Array of integers in second parameter, 0 considered empty

Unminified code

x = (mode, data) => {
    var concat = 'concat',
        length = 'length',
        stripped = data.filter(el => {
            return !!el;
        }),
        zeroes = new Array(data[length] - stripped[length]).fill(0);

    if (!mode) {
        Array.prototype.splice.apply(stripped, [data[length] / 4, 0][concat](zeroes));
    }

    return mode ? (mode > 1 ? zeroes[concat](stripped) : stripped[concat](zeroes)) : stripped;
};
\$\endgroup\$
1
\$\begingroup\$

CoffeeScript, 172 bytes = 1.06 points

x=(m,d)->c='concat';l='length';s=d.filter((el)->el);z=new Array(d[l]-s[l]).fill 0;(Array.prototype.splice.apply(s,[d[l]/4,0][c] z);return s)if!m--;return z[c] s if!m;s[c] z

First parameter is mode: 0 (or false) for Moses mode, 1 for push right (prepend), 2 for push left (append)

Second parameter is an array of integers where 0 is considered the empty token (though all falsy values are considered empty, but will be replaced with 0).

Version without Moses mode: 91 bytes = 0.11 points

x=(d,a)->o=a.filter((l)->''!=l);o[if d then'push'else'unshift'] ''while a.length-o.length;o

First parameter is a Boolean indicating whether to push right (true or any truthy value) or push left (false or any falsy value).

Second parameter is the array. Empty tokens include empty strings.

\$\endgroup\$
1
\$\begingroup\$

PHP - 176 bytes, 1.06 points

function x($m,$d){$c=count;$y=array_merge;$s=array_filter($d);$z=array_fill(0,$c($d)-$c($s),0);if(!$m)array_splice($s,$c($s)/4+1,0,$z);return!$m?$s:(--$m?$y($z,$s):$y($s,$z));}

Where $m is:

  • 0: Moses mode
  • 1: Append (push left)
  • 2: Prepend (push right)

And $d is the an array of numeric where 0 or 0.0 is considered empty.

There are notices for undefined constants but they still work.

\$\endgroup\$
1
\$\begingroup\$

rs, 41 bytes

+(.)(\d+)_(\d+)/\1_\2\3
l(_+)(.+)/\2\1
r/

Takes input as a sequence of numbers, with l for left, r for right, and the underscore as the null character, e.g. r_3_12.

Live demo here.

\$\endgroup\$
1
\$\begingroup\$

k2, 28 bytes

{,/ :[y;|:](x@&~x=0;x@&x=0)}

I already have another answer, and there's another K answer (for k3), but I believe this one uses a vastly different approach, IMO. 0 is used as the null value. Two unfortunate issues:

  1. k2 has no binary vectors.
  2. #1 makes this one really long.

Usage:

{,/ :[y;|:](x@&~x=0;x@&x=0)}[1 1 0 1 2 3 0 1;1] / outputs 0 0 1 1 1 2 3 1
{,/ :[y;|:](x@&~x=0;x@&x=0)}[1 1 0 1 2 3 0 1;0] / outputs 1 1 1 2 3 1 0 0

Now, if I am allowed to use just 1 and 0 as my input, I can get much shorter, but then it ends up being almost the same thing as the other K answer. So...yeah. This sucks. :(

\$\endgroup\$
0
\$\begingroup\$

Some have mentioned above that wordy languages can't participate because of the restriction in byte count. I doubt it. Here's a python solution, and I think python is a wordy language, somewhat golfed.

Python 2.7

68 bytes, 0.15 points

62 bytes, 0.16 points (thanks to @Sp3000)

d,s=input();e=filter(None,s),[0]*s.count(0)
print e[d]+e[d+1]

input/ouput

0 for right, -1 for left, list of numbers as sequence. 0 is the null character
>>>0, [1, 0, 2, 3, 6, 0, 0, 0, 5]
[1, 2, 3, 6, 5, 0, 0, 0, 0]
>>>-1, [1, 0, 2, 3, 6, 0, 0, 0, 5]
[0, 0, 0, 0, 1, 2, 3, 6, 5]
\$\endgroup\$
3
  • 1
    \$\begingroup\$ You can save a few by not putting the surrounding parens for the tuple e, and also by not saving n as a separate variable. i.e. e=filter(None,s),[0]*s.count(0) \$\endgroup\$
    – Sp3000
    Jun 7, 2015 at 15:36
  • \$\begingroup\$ @Sp3000 Thank you. Made the edit. \$\endgroup\$ Jun 7, 2015 at 16:28
  • 3
    \$\begingroup\$ python is not a "wordy language" imo. something like java would be. \$\endgroup\$ Jun 7, 2015 at 22:28
0
\$\begingroup\$

Perl - 103 bytes, 1.09 points

$_=<>;s/\n//;$b='_'x s/_//g;$x=length($_)/2;print(($b.$_,$_.$b,(substr($_,0,$x).$b.substr$_,$x))[<>+0])

The sequence is a string. The null element is an underscore character. It reads the string as a line from the standard input, then reads the direction as another line: 0 for right, 1 for left, 2 for Moses mode.

Version without Moses Mode - 49 bytes, 0.20 points

$_=<>;s/\n//;$b='_'x s/_//g;print<>+0?$_.$b:$b.$_

Same as the above except shorter and without Moses mode implemented.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.