# Halve the falses

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.

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

# APL (Dyalog), 9 bytes

'00'⎕R'0'


Try it online!

• It says DOMAIN ERROR: I-Beam function 7160 has been withdrawn Aug 14, 2020 at 14:50

## Batch, 24 bytes

@set/ps=
@echo %s:00=0%


Takes input on STDIN. Somewhat competitive for once.

# Common Lisp, SBCL, 48 32 bytes

-16 bytes thanks to Julian Wolf

(format t"~{~[0~*~;1~]~}"(read))


input:

(1 0 0 0 0 1 1 1 0 0)


output:

1001110


### Explanation

We read input list. List is used in format function. We loop through it outputting 1 if element is 1 and outputting 0 and skipping next element of list for 0.

• Using ~[ rather than ~:[ lets you index with 0 and 1 directly, which should save you a bunch of bytes May 3, 2017 at 16:07
• @JulianWolf Thank you!
– user65167
May 3, 2017 at 16:14

# Jelly, 10 bytes

Œg¹m2$S?€F  Try it online! Explanation Œg¹m2$S?€F
Œg          - Group runs of equal elements
€   - To each run...
S?    - If sum is truthy,
¹         -   return the run as it is

## CJam, 20 18 bytes

1l~]e2/z~2ff/]z:~


Expects input as a list of bits, separated by spaces.

Explanation:

1   e# Push 1:              | 1
l   e# Read line:           | 1 "1 1 0 0 1 1 1 0 0 1 1"
~   e# Evaluate:            | 1 1 1 0 0 1 1 1 0 0 1 1
]   e# Wrap stack in array: | [1 1 0 0 1 1 1 0 0 1 1]
e  e# Run-length encode:   | [[3 1] [2 0] [3 1] [2 0] [2 1]]
2/  e# Group by 2:          | [[[3 1] [2 0]] [[3 1] [2 0]] [[2 1]]]
z   e# Transpose:           | [[[3 1] [3 1] [2 1]] [[2 0] [2 0]]]
~   e# Unpack array:        | [[3 1] [3 1] [2 1]] [[2 0] [2 0]]
2ff/ e# Divide each by 2:   | [[[3 1] [3 1] [2 1]] [[1 0] [1 0]]]
]   e# Wrap stack in array: | [[[[3 1] [3 1] [2 1]] [[1 0] [1 0]]]]
z   e# Transpose:           | [[[3 1] [1 0]] [[3 1] [1 0]] [[2 1]]]
:~  e# Unpack each:         | [[3 1] [1 0] [3 1] [1 0] [2 1]]
e~  e# RLE decode:          | [1 1 1 0 1 1 1 0 1 1]
(;  e# Delete last element: | [1 1 0 1 1 1 0 1 1]
e# Implicit output: "110111011"


### Alternative (9 bytes):

l~[0_]/0*


Expects input as a CJam list literal.

Explanation:

l~   e# Read line and eval: | [1 1 0 0 1 1 1 0 0 1 1]
[0_] e# Push [0 0]:         | [1 1 0 0 1 1 1 0 0 1 1] [0 0]
/    e# Split array:        | [[1 1] [1 1 1] [1 1]]
0*   e# Join with 0:        | [1 1 0 1 1 1 0 1 1]


# ES7, 23 bytes

_=>_.replace(/00/g,0)


Input is string without whitespace + it allows odd lengths of runs of 0, it just keeps it as is.

# Zsh, 13 bytes

<<<${1//00/0}  Try it online! # x86-16 machine code, 15 bytes 00000000: b401 aca8 0175 04f6 dc78 01aa e2f4 c3 .....u...x.....  Listing: B4 01 MOV AH, 1 ; set zero run counter flag set to positive CLOOP: AC LODSB ; load next byte A8 01 TEST AL, 1 ; is a 1 or 0? 75 04 JNZ IS_ONE ; if 1, store to output and loop F6 DC NEG AH ; flip sign of zero run counter flag 78 01 JS ODD_ZERO ; if negative, don't output IS_ONE: AA STOSB ; copy char to output ODD_ZERO: E2 F4 LOOP CLOOP ; loop until end of input C3 RET ; return to caller  Callable function, input array at [SI], len in CX. Output to array at [DI]. Test program output: Try it online! • 11 – l4m2 Oct 15, 2023 at 3:51 # Brainfuck, 51 bytes +[,[>+>+<<-]>>[.>+++++++[<------->-]<[,>]]<[<+>-]<]  Try It Online! This program takes a string of 0's and 1's as input and prints a string of 0's and 1's as output. # Husk, 6 bytes σD;0;0  Try it online! ### Explanation σD;0;0 σ replace all with D 2 copies of ;0 [0] ;0 [0]  • Jan 14, 2022 at 13:22 # Pari/GP, 21 bytes a->[i|i<-q=a,i||q=!q]  Try it online! # APOL, 33 bytes j(ƒ(i ¿(!(I(∋)) ¿(≐(∈) 0 '') 1))) Explanation: j( Join list to string ƒ( List-builder for i Get input ¿( Returning if !( Not I( As number ∋ Loop item ) ) If true return ¿( Returning if ≐(∈) Loop counter is even 0 If true return 0 '' Else return an empty string ) 1 Else return 1 ) ) )  # convey, 32 bytes {">>>v "+."@} 1#,0=] ^>@<< ^<$1


Try it online!

How it works with input [1,0,0,0,0,1]:

# Lexurgy, 10 bytes

Input is a string of as and bs, where a is 1 and b is 0.

Finds two bs and replaces them with a b and an empty string.

a:
b b=>b *


Variant that does the same thing with actual 0 and 1 digits (12 bytes):

a:
\0\0=>\0 *
`