-3
\$\begingroup\$

Given an arbitrary sequence consisting of 1 and A split this sequence up so that A is at the beginning of each sub sequence. Sequences without a leading A and Sequences without 1 before the next A will be self contained.

Example

Input

111A11A1111111A1A1AA

should result in

111
A11
A1111111
A1
A1
A
A

Above already contains the edge cases * sequence of 1 without prior A * subsequent A's without 1 between

Update Open to all programming languages (functional / non-functional)

\$\endgroup\$

closed as unclear what you're asking by user202729, Dennis Apr 12 '18 at 21:03

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 5
    \$\begingroup\$ Welcome to PPCG. You'd need to add a winning criterion such as code-golf. Also, it might be a dupe -- but I didn't check yet. For future submissions, you may want to use our sandbox before posting to the main site. \$\endgroup\$ – Arnauld Apr 12 '18 at 13:17
  • 3
    \$\begingroup\$ You should add more examples with some edge cases, as currently looks like a simple s/A/\nA/g task. \$\endgroup\$ – manatwork Apr 12 '18 at 13:23
  • 1
    \$\begingroup\$ s/question/challenge/ \$\endgroup\$ – user202729 Apr 12 '18 at 13:55
  • 6
    \$\begingroup\$ I think this challenge is too simple. \$\endgroup\$ – mbomb007 Apr 12 '18 at 15:28
  • 3
    \$\begingroup\$ Please add an example that starts with an A. The behavior of the current answers is very inconsistent for everything except the one example you provide. \$\endgroup\$ – Dennis Apr 12 '18 at 21:02

23 Answers 23

3
\$\begingroup\$

APL (Dyalog Unicode), 12 bytes

Anonymous tacit function. Returns a list of strings.

⊢⊂⍨1⊣@1=∘'A'

Try it online! ( is just to display the list of strings as separate lines.)

=∘'A' Boolean list (1s and 0s) indicating As

1⊣@1 replace with a 1 at the first position

⊢⊂⍨ begin partitions of the argument at each 1 in that

\$\endgroup\$
  • \$\begingroup\$ Hmm, I never programmed in APL, so perhaps it's not possible/shorter, but isn't there a simple replace every A with \nA? \$\endgroup\$ – Kevin Cruijssen Apr 12 '18 at 14:01
  • 2
    \$\begingroup\$ @KevinCruijssen Then it is time to begin! Dyalog APL has PCRE built-in, but it isn't so simple. See the discussion beginning here. \$\endgroup\$ – Adám Apr 12 '18 at 14:15
  • \$\begingroup\$ 10 bytes: '.1*'⎕s'&' \$\endgroup\$ – ngn Apr 12 '18 at 16:46
  • \$\begingroup\$ @ngn There's no way you're telling me that that is a derivative of mine. Go ahead and post it or the equivalent 5-byter QuadS. \$\endgroup\$ – Adám Apr 12 '18 at 19:34
  • \$\begingroup\$ @Adám Of course, it isn't. Unfortunately it's on hold now and I can't post the 5-byter. \$\endgroup\$ – ngn Apr 13 '18 at 2:21
2
\$\begingroup\$

Retina, 6 bytes

\BA
¶A

Try it online!

An alternative to Luis's solution. Finds all A which are preceded by another character and inserts a linefeed in front of them.

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

Retina, 6 5 bytes

1 byte saved thanks to @Neil!

L`.1*

Try it online!

Explanation

L`      Configure the stage as "list", so that it outputs all matchings
.1*     Regex to be matched: any character followed by zero or more "1"
\$\endgroup\$
2
\$\begingroup\$

Haskell, 54 37 bytes

import Data.List
groupBy(\_ x->x<'A')

Try it online!

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

Jelly, 5 bytes

1 The slooooow but shorter method which gets all ways* to partition the list and then finds the maximal as measured by its transpose:

ŒṖZÞṪ

Try it online!

* that is all 2length(input)-1 ways!

2 The speedy method using the "partition at truthy indices" atom (the five byte =”Aœṗ fails for inputs starting with A):

Ḋ=”A0;œṗ

Try the speedy one!

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

05AB1E, 10 8 bytes

'A"
A".:

Try it online!

Can probably be golfed further, but first submission and attempt at this.

Explanation

'A"           Push A character, Push string "(new line) A"
 A"           close string
.:            Replace all. Replace "A" with "(new line) A" in input

If you want code to match output like in original post then 11 bytes

I'A" A".:#» 

-2 bytes thanks to Soaku!

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

Befunge-98 (FBBI), 14 12 bytes

-2 bytes thanks to Jo King

#@~:d%5/ja,,

Try it online!

#@            # skips @
  ~           push a byte of input
   :          duplicate it
    d%        modulo 13, 'A':0, '1':10
      5/      divided by 5, 'A':0, '1':2
        j     jump this number of blocks, skips the next two instructions for '1'
         a,   push 10 to the stack and output as a byte (\n)
           ,  output the byte of input

If there is no input left, ~ reflects the pointer and @ terminates the program

\$\endgroup\$
  • \$\begingroup\$ Alternatively, #@~:9%j a,, and #@~:5%ja, ,. The last one could be 10 bytes, but it outputs an extra null byte \$\endgroup\$ – Jo King Apr 13 '18 at 10:03
1
\$\begingroup\$

Standard ML (MLton), 40 bytes

String.translate(fn#"A"=>"\nA"|x=>str x)

Try it online!

Unfortunate that String.translate is so long...

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

Japt, 6 bytes

d'A_iR

Try it online!

Explanation:

d'A_iR
d       // Replace:
 'A     //   "A" with
   _iR  //   newline + "A"

Alternative 6-byte solution

r'A"
A

Try it online!

These would be 5-byte solutions if we could take a instead of A.

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

Perl 5 -p, 8 bytes

s/A/
A/g

Try it online!

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

JavaScript (Node.js), 27 bytes

z=>z.split(/(?=A)/).join`
`

Try it online!


Explanation :

z =>                   // lambda function taking z as input
    z.split(           // convert z to an array by splitting at
            /(?=A)/    // all `A's`
    ).                 // end split
        join`\n`       // join all via new line

Alternate :

If printing an array is okay, then :

JavaScript (Node.js), 19 bytes

z=>z.split(/(?=A)/)

Try it online!

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

JavaScript (Node.js), 22 bytes

s=>s.match(/A1*|^1+/g)

Try it online!

\$\endgroup\$
  • \$\begingroup\$ Not sure if this would pass a different test case, but the regex /[A1]1*/g is reduces this by one byte (and passes the provided test case). \$\endgroup\$ – Pandacoder Apr 12 '18 at 15:05
  • 1
    \$\begingroup\$ Why not .1*?? \$\endgroup\$ – Neil Apr 12 '18 at 15:57
0
\$\begingroup\$

Scala, 20 bytes

_.replace("A","\nA")

Try it online!

(Not sure if putting the types in the header is allowed, I'll change it if not.)

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

JavaScript, 26 23 bytes

-3 bytes thanks to caird coinheringaahing

s=>s.replace(/A/g,`
A`)

Try it online!

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

Pyth, 8 7 bytes

:Q\A"
A

Try it here

Explanation

:Q\A"\nA
:Q         Replace in the input...
  \A       ... every instance of "A"...
    "\nA   ... with a newline followed by "A".
\$\endgroup\$
0
\$\begingroup\$

JavaScript (Node.js), 20 19 bytes

s=>s.split(/(?=A)/)

Try it online!

Thanks Pandacoder for 1 byte

\$\endgroup\$
  • \$\begingroup\$ It doesn't say outputting a list of lines is acceptable, so this answer is currently invalid. \$\endgroup\$ – caird coinheringaahing Apr 12 '18 at 15:06
  • \$\begingroup\$ @cairdcoinheringaahing Requirement says "split this sequence up", not "split with newline" \$\endgroup\$ – l4m2 Apr 12 '18 at 15:08
0
\$\begingroup\$

Python 2, 29 bytes

lambda I:I.replace('A','\nA')

Try it online!

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

Batch, 44 bytes

@set/pa=
@for %%a in (%a:A= A%)do @echo %%a

Takes input on STDIN.

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

Java (JDK 10), 19 bytes

s->s.split("(?=A)")

Try it online!

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

Pepe, 59 bytes

rEEeREeEeeeeeEREeeeeeeeeREEreeEREEeeREEreEeREEeEReEREEeeRee

Try it online!

That was hard... Outputs with leading newline.

rEEe           # r - Get input               Take the string

REeEeeeeeE     # R - Push A                  A - splitter
REeeeeeeee     # R - Push \0                 \0 - end
REE            # R - Create label A          label A: Next + newline

 reeE          # Output newline              Output newline

 REEee         # R - Forward                 
 REE           # R - Create label \0         label \0: Next without newline

 reEe          # r - Output                  Output the current letter and pop it

 REEeE         # R - Rewind                  
 ReE           # r pointer == A, goto A      Output newline, next letter

 REEee         # R - Forward                 Else
 Ree           # r pointer != \0, goto \0    If stack isn't empty, next letter
\$\endgroup\$
0
\$\begingroup\$

Haskell, 29 bytes

f s=do c<-s;['\n'|c>'1']++[c]

Try it online!

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

R, 26 bytes

Not a whole lot to explain with this one.

cat(gsub("A","\nA",scan()))
\$\endgroup\$
0
\$\begingroup\$

sed, 9 bytes

s/A/\nA/g

Try it online!

\$\endgroup\$
  • 1
    \$\begingroup\$ sed is a language on its own, so you only have to count the bytes in the actual sed command. \$\endgroup\$ – nwellnhof Apr 12 '18 at 20:25
  • \$\begingroup\$ As nothing else is matched beside group 1, you may skip the capturing: s/1*/&\n/g. \$\endgroup\$ – manatwork Apr 12 '18 at 20:29
  • \$\begingroup\$ With Awk you could benefit of defaults: by default gsub() operates on $0; the default action is print; by default print also operates on $0: gsub("A","\nA")1. \$\endgroup\$ – manatwork Apr 12 '18 at 20:37

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