34
\$\begingroup\$

Task

Given a non-empty array of 0 and 1, halve the lengths of the runs of 0.

Input

An array of 0 and 1. Acceptable format:

  • Real array in your language
  • Linefeed-separated string of 0 and 1
  • Contiguous string of 0 and 1
  • Any other reasonable format

For example, the following three inputs are all acceptable:

  • [1, 0, 0, 1]
  • "1\n0\n0\n1" (where \n is a linefeed U+000A)
  • "1001"

You may assume that the runs of 0 will have even length.

Output

An array of 0 and 1, in the acceptable formats above.

Testcases

input ↦ output
[1,0,0,1,0,0,1] ↦ [1,0,1,0,1]
[1,1,0,0,1,1,0,0,1] ↦ [1,1,0,1,1,0,1]
[1,1,0,0,1,1,1,0,0,1,1] ↦ [1,1,0,1,1,1,0,1,1]
[1,1,1] ↦ [1,1,1]
[0,0,1] ↦ [0,1]
[0,0] ↦ [0]
[1,1,1,0,0,0,0,1,1,1,1,0,0,1,0,0,1,1,0,0,1,1,1,1,0,0,1,0,0] ↦ [1,1,1,0,0,1,1,1,1,0,1,0,1,1,0,1,1,1,1,0,1,0]

Scoring

This is . Shortest answer in bytes wins.

Standard loopholes apply.

\$\endgroup\$
10
  • \$\begingroup\$ In the last testcase, don't the runs of zeroes not have even length? \$\endgroup\$
    – AAM111
    Commented May 3, 2017 at 11:26
  • \$\begingroup\$ @OldBunny2800 Read the test case carefully; the 0-runs have lengths 4, 2, 2, 2, 2, and 2. \$\endgroup\$
    – hyper-neutrino
    Commented May 3, 2017 at 12:21
  • \$\begingroup\$ Can we take true and false instead of 1 and 0? \$\endgroup\$
    – Cyoce
    Commented May 3, 2017 at 18:13
  • \$\begingroup\$ @Cyoce which language? \$\endgroup\$
    – Leaky Nun
    Commented May 3, 2017 at 18:15
  • 1
    \$\begingroup\$ @LeakyNun Ruby, which considers 0 to be truthy. \$\endgroup\$
    – Cyoce
    Commented May 3, 2017 at 18:16

68 Answers 68

1 2
3
1
\$\begingroup\$

brainfuck, 23 bytes

,[.++[->+++++<]>+[,>],]

Try it online!

,[         while(c=getchar()) {
  .++[       putchar(c);
    ->+++++< e=5*(c+2)
  ]
  >+[        if(e!=-1) {
    ,>         getchar();
  ]          }
,]         }
\$\endgroup\$
1
\$\begingroup\$

Desmos, 28 bytes

f(L)=L[(L-1)^{[0...9999]}<1]

Takes input as a list of 0s and 1s, and returns a list of 0s and 1s.

This works by filtering for elements that are either 1, or 0 and with odd (0-based) index in the list.

Desmos lists have a maximum length of 10000 elements, so [0...9999] is a valid golf from [0...L.length].

Try it on Desmos!

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

AWK, 8 bytes

$0||NR%2

Try it online!

I found this nice solution following the thinking of this answer.

It's a simple OR gate used as a AWK pattern: it prints the line if the input is true (i.e., 1) or it's an odd line (NR is the number of the line). It will skip only the 0 in even lines.

AWK, 15 bytes (former answer)

gsub("00","0")1

Try it online!

Globally substitutes all "00" strings to "0". The 1 in the end is a True token to force printing the line if there were no substitutions.

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

Julia, 21 bytes

!x=replace(x,"00"=>0)

Attempt This Online!

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

Wolfram Language (Mathematica), 22 bytes

Select[#>=(i=-i)&]
i=1

Try it online!

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

Octave, 25 bytes

@(a)a(~mod(cumsum(~a),2))

Try it online!

Find the indices of even elements in the cumsum of the logical negation of the input.

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

Uiua, 4 bytes

▽\=.

Try it!

Port of emanresu A's Vyxal answer.

▽\=.
   . # duplicate
 \=  # scan by equality
▽    # keep
\$\endgroup\$
0
\$\begingroup\$

Perl 5 50 + 1 (for -p flag) = 51 bytes

@a=split'',$_;splice@a,$i++,!$_ for@a;$_=join'',@a
\$\endgroup\$
1 2
3

Your Answer

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

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