MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn moreOpportunities for recent engineering grads.

Apply TodayFind code that adds one 0 to each sequence of 0 in a string (composed with only 0 or 1).

For example:

'1010' -> '100100' '0110' -> '001100' '1000' -> '10000' '1111' -> '1111'

48 correct solutions
47 incorrect solutions

Last solution submitted on Aug 28, 2014

1 player likes this solution

1 Comment

Khaled Hamed
on 1 Aug 2012

The problem asks to "add" a zero, no mention of append or prepend! Nice general solution though, and a new trick learned ($&0)

3 players like this solution

5 Comments

Show
2 older comments

Ned Gulley
on 26 Jul 2012

I didn't know about $0! Nifty.

Khaled Hamed
on 26 Jul 2012

Neither did I! Matlab has great help.

Richard Zapor
on 31 Jul 2012

The solution is not correct for the generalized case. 0$0 does a pre-pend of 0 not an append. The correct general solutions is either $00, or better to avoid operator ($0) confusion with the test set using 0s is operator ($%) $&0.

Richard Zapor
on 31 Jul 2012

The solution is not correct for the generalized case. 0$0 does a pre-pend of 0 not an append. The correct general solutions is either $00, or better to avoid operator ($0) confusion with the test set using 0s is operator ($&) $&0. (Typo in prior comment)

Khaled Hamed
on 1 Aug 2012

The problem asks to "add" a zero, no mention of append or prepend! Nice general solution though, and a new trick learned ($&0)

1 Comment