Generate the Abacaba sequence

35

2

This challenge is about printing the abacaba sequence of a specific depth.

Here is a diagram of the first 5 sequences (a(N) is the abacaba sequence of depth N, upper/lowercase is just to show the pattern, this is not needed in the output of your program):

a(0) = A
a(1) = aBa
a(2) = abaCaba
a(3) = abacabaDabacaba
a(4) = abacabadabacabaEabacabadabacaba
...
a(25) = abacabadabacabaeabacabadabacabafabacabadabacabaeabacabadabacabagabacabadabacabaeabacabadabacabafabacabadabacabaeabacabadabacabahabacabadabacabaeabacabadabacabafabacabadabacabaeabacabadabacabagabacabadabacabaeabacabadabacabafabacabadabacabaeabacabadabacabaiabacabadabacabaeabacabadabacabafabacabadabacabaeabacabadabacabagabacabadabacabaeabacabadabacabafabacabadabacabaeabacabadabacabahabacabadabacabaeabacabadabacabafabacabadabacabaeabacabadabacabagabacabadabacabaeabacabadabacabafabacabadabacabaeabacabadabacabajabacabadabacabaeabacabadabacabafabacabadabacabaeabacabadabacabagabacabadabacabaeabacabadabacabafabacabadabacabaeabacabadabacabahabacabadabacabaeabacabadabacabafabacabadabacabaeabacabadabacabagabacabadabacabaeabacabadabacabafabacabadabacabaeabacabadabacabaia...

As you can probably tell, the n'th abacaba sequence is the last one with the n'th letter and itself again added to it. (a(n) = a(n - 1) + letter(n) + a(n - 1))

Your task is to make a program or function that takes an integer and prints the abacaba sequence of that depth. The output has to be correct at least for values up to and including 15.

Loovjo

Posted 2016-01-09T12:28:29.827

Reputation: 7 357

3Wouldn't the sequence be undefined after ₂₅? – LegionMammal978 – 2016-01-09T13:04:28.687

@Legion OP's mentioned that "The output has to be correct at least for values up to and including 15", so it doesn't matter. – nicael – 2016-01-09T13:26:09.743

3@nicael I know, I was just wondering how (∞) would be defined. – LegionMammal978 – 2016-01-09T13:29:04.397

Huh. I had been contemplating posting this challenge. – quintopia – 2016-01-09T15:16:13.000

2Also known as the ruler sequence (but with letters instead of numbers), for something more easily Google-able. – user253751 – 2016-01-10T06:00:42.757

Does the input have to be in decimal? – user253751 – 2016-01-10T06:05:55.467

Why is a(0)="a"? Why not ""? – CalculatorFeline – 2016-03-10T20:11:42.303

1@CatsAreFluffy Because this is how the famous sequence is defined. – mbomb007 – 2016-03-10T21:47:02.513

4For what it's worth, any valid solution to this problem is also the solution to the Towers of Hanoi puzzle for N disks. – Jeff Zeitlin – 2019-04-02T11:59:25.013

3Can we use 1-based indexing instead of 0-based indexing? – Esolanging Fruit – 2019-04-03T17:21:46.010

Answers

8

Pyth, 11 bytes

u++GHG<GhQk

Simple reduction.

orlp

Posted 2016-01-09T12:28:29.827

Reputation: 37 067

2@Loovjo Oh. Makes no sense, 0 should be the empty sequence IMO, but I'll conform to the question... – orlp – 2016-01-09T12:52:45.740

4Yeah, simple. goes and bangs head on wall – J Atkin – 2016-02-13T20:07:50.863

@JAtkin Open up Pyth's rev-doc.txt next to this answer, and it should readily show itself to be simple. – orlp – 2016-02-13T20:19:20.203

Hehehe, not what I meant (I don't know pyth, so....) – J Atkin – 2016-02-13T20:22:08.660

7

Python, 44 bytes

f=lambda n:"a"[n:]or f(n-1)+chr(97+n)+f(n-1)

Looks suspiciously might-be-golfable.

Sp3000

Posted 2016-01-09T12:28:29.827

Reputation: 58 729

7

Haskell, 39 37 bytes

a 0="a"
a n=a(n-1)++['a'..]!!n:a(n-1)

Usage example: a 3 -> "abacabadabacaba".

Edit: @Angs found two bytes to save. Thanks!

nimi

Posted 2016-01-09T12:28:29.827

Reputation: 34 639

Wouldn't a n=a(n-1)++[97+n]++a(n-1) work? Can't test right now. – seequ – 2016-01-09T18:36:10.307

@Seeq: no, [97+n] is a list of Integer and a(n-1) is a list of Char (aka String). You cannot concatenate lists with different types. toEnum makes a Char out of the Integer. – nimi – 2016-01-09T18:57:59.443

Ah, I always thought Char was just a specialized integer in Haskell. – seequ – 2016-01-09T23:47:29.397

['a'..]!!n is 2 bytes shorter than toEnum(97+n) – Angs – 2016-10-06T18:11:40.240

@Angs: Good catch! Thanks! – nimi – 2016-10-06T22:29:43.120

6

Pyth, 14 13 bytes

Thanks to Jakube for saving a byte!

VhQ=+k+@GNk;k

A solution with 14 bytes: VhQ=ks[k@GNk;k.

Explanation:

VhQ=+k+@GNk;k

               # Implicit: k = empty string
VhQ            # For N in range input + 1      
   =           # Assign k
      +@GNk    # Position N at alphabet + k
    +k         # k + above
           ;   # End loop
            k  # Print k

Try it here!

Adnan

Posted 2016-01-09T12:28:29.827

Reputation: 41 965

Shouldn't "N in range" be on the V line? hQ is just eval(input) + 1 – Loovjo – 2016-01-09T12:58:04.947

@Loovjo Yeah, that's better and less confusing :) – Adnan – 2016-01-09T13:00:06.620

You can shorten =k to =. Pyth will automatically assign the result to k, since k is the first variable in the expression +k+@GNk. – Jakube – 2016-01-09T14:00:44.283

@Jakube Thank you very much! :) – Adnan – 2016-01-09T14:10:45.560

I have a different answer for this challenge. It won't beat this solution, but it does illustrate a technique for giving the first n characters of the sequence: Vt^2Q=+k@Gx_.BhN`1)k (In this instance, it's set to give the first 2^Q-1 characters as the challenge requires, but you can see how to change that.) – quintopia – 2016-01-09T22:12:06.400

Actually, this is shorter (for the same purpose as prev comment): sm@Gx_.Bd`1St^2Q – quintopia – 2016-01-09T22:26:07.387

And here's an even better way to find the exponent of 2 (thanks to isaacg): sm@G/Phd2t^2Q. This one actually ties this answer. – quintopia – 2016-01-09T22:50:29.227

5

Brainfuck, 157 bytes

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

Input is given in binary.

The basic idea is to repeatedly duplicate the current sequence (starting with "a") and to increment the last element after each iteration:

  1. a → aa → ab

  2. ab → abab → abac

  3. abac → abacabac → abacabac

  4. ...

When all of this has been done the specified amount of times, the result gets printed excluding the last element.

In-depth explanation

The memory is arranged in the following way:

.---------.-.-----.----.---.-----.----.---.---
|Countdown|0|Value|Copy|End|Value|Copy|End|...
'---------'-'-----'----'---'-----'----'---'---

            |--Element 1---|--Element 2---|

Countdown holds the number of copy cycles that are yet to be executed. The ABACABA sequence is stored in adjecent blocks, each made up of 3 cells. Value holds the character of the element (i.e. "A","B","C"...). The Copy flag indicates whether or not the corresponding element needs to be copied within the current copying cycle (0=copy, 1=don't). The End flag is set to 0 for the last element while it is being copied (it's 1 in all other cases).

Now to the actual (slightly ungolfed) program:

,                       read Countdown from input
+                       add 1 to avoid off-by-one error
>-[>++<-----]>-----     initialize value cell of first element to 97 ("a")
>>+                     set End flag of first element to 1
<<<<                    move to Countdown
[                       loop until Countdown hits 0 (main loop)
    -                   decrement Countdown
    >>                  move to Value cell of first element
    [                   copying loop
        [               duplication sub-loop
            -           decrement Value cell of the element to copy
            >>          move to End flag
            [>>>]       move to End flag of the last element
            <+<+        increment Copy and Value cell of last element (Copy cell is temporarily abused)
            <           move to End flag of second to last element
            [<<<]>>     move back to Copy cell of first element
            [>>>]<      move to Value cell of the first element where the Copy flag is 0
        ]
        >>[>>>]<        move to (abused) Copy flag of the last element
        [               "copy back" sub-loop
            -           decrement Copy flag
            <<          move to End flag of second to last element
            [<<<]>>     move back to Copy cell of first element
            [>>>]<      move to Value cell of the first element where the Copy flag is 0
            +           increment Value cell
            >>[>>>]<    move back to Copy flag of the last element
        ]
        +>+             set Copy and End flag to 1
        <<<             move to End flag of second to last element
        [<<<]>>         move back to Copy cell of first element
        [>>>]<          move to Value cell of the first element where the Copy flag is 0
        >+<             set Copy flag to 1
        >[>>>]<         move to Value cell of the next element to copy
    ]                   loop ends three cells behind the last "valid" Value cell
    <<<+                increment Value cell of last element
    >>                  move to End flag
    [<-<<]              reset all Copy flag
    <                   move to Countdown
]
>>>>                    move to End flag of first element
[>>>]<<<                move to End flag of last element                
<<<                     skip the last element
[<<.<]                  output Value cells (in reverse order, but that doesn't matter)

orthoplex

Posted 2016-01-09T12:28:29.827

Reputation: 339

2Welcome to the site! I'd be interested in a more detailed breakdown! – Post Rock Garf Hunter – 2019-04-03T14:15:29.957

1@SriotchilismO'Zaic Thanks for your reply :) I have now added a detailed explanation. – orthoplex – 2019-04-03T20:46:05.527

5

Haskell, 36 bytes

tail.(iterate((:"a").succ=<<)"_a"!!)

Try it online!

This uses a different recursive method from most of the other answers. To get the next string in the sequence, we don't join two copies in the previous string with a new letter in between, but instead increment every letter and intersperse a's.

aba -> bcb -> abacaba

xnor

Posted 2016-01-09T12:28:29.827

Reputation: 115 687

1Do you mean bcb instead of cbc? – Jo King – 2019-09-30T02:29:28.260

5

Retina, 37 32 bytes

$
aa
(T`_l`l`.$
)`1(a.*)
$1$1
z

The trailing linefeed is significant. Input is taken in unary.

Try it online!

Martin Ender

Posted 2016-01-09T12:28:29.827

Reputation: 184 808

It does not work. – Leaky Nun – 2016-03-31T04:47:57.837

@KennyLau yes, because Retina changed since I posted this answer. If you check out the release that was most recent when this was posted directly from GitHub, it will work with that. – Martin Ender – 2016-03-31T05:25:23.213

4

JavaScript (ES6), 43 42 bytes

a=n=>n?a(--n)+(n+11).toString(36)+a(n):"a"

A byte saved thanks to @Neil!

Yet another simple recursive solution...

user81655

Posted 2016-01-09T12:28:29.827

Reputation: 10 181

(n+11).toString(36) saves you 1 byte, and works for up to a(25)! – Neil – 2016-01-09T18:08:59.610

@Neil Implemented. Thanks! – user81655 – 2016-01-10T02:24:39.370

4

05AB1E, 12 bytes (non-competitive)

Code:

'aIGDN>.bsJl

I'll be damned. I fixed a lot of bugs thanks to this challenge haha.

Explanation:

'aIGDN>.bsJl

'a             # Push the character 'a'
  I            # User input
   G           # For N in range(1, input)
    D          # Duplicate the stack
     N         # Push N
      >        # Increment
       .b      # Convert to alphabetic character (1 = A, 2 = B, etc.)
         s     # Swap the last two elements
          J    # push ''.join(stack)
           l   # Convert to lowercase
               # Implicit: print the last item of the stack

Adnan

Posted 2016-01-09T12:28:29.827

Reputation: 41 965

Why is it non-competitive? – Loovjo – 2016-01-09T13:31:40.830

@Loovjo I fixed the bugs after the challenge was posted, therefore it's non-competitive :( – Adnan – 2016-01-09T13:32:40.950

3

R, 48 bytes

f=function(n)if(n)paste0(a<-f(n-1),letters[n],a)

Try it online!

Simple recursion.

Robin Ryder

Posted 2016-01-09T12:28:29.827

Reputation: 6 625

Uh, what is paste0??? – Xi'an – 2019-04-23T20:34:11.073

@Xi'an paste0 is equivalent to paste with sep="", so you avoid the spaces between letters that paste would add by default. – Robin Ryder – 2019-04-24T10:48:55.860

3

CJam (14 bytes)

'aqi{'b+1$++}/

Online demo

Peter Taylor

Posted 2016-01-09T12:28:29.827

Reputation: 41 901

3

Ruby (1.9 and up), 38 bytes

?a is a golfier way to write "a" but looks weird when mixed with ternary ?:

a=->n{n<1??a:a[n-1]+(97+n).chr+a[n-1]}

Sherlock9

Posted 2016-01-09T12:28:29.827

Reputation: 11 664

2

Python, 62 54 46 45 bytes

I would like to think that this code can still be golfed down somehow.

Edit: Bug fix thanks to Lynn. -1 byte thanks to squid.

a=lambda n:n and a(n-1)+chr(97+n)+a(n-1)or'a'

Try it online!

Sherlock9

Posted 2016-01-09T12:28:29.827

Reputation: 11 664

Output should be in all-lowercase. The uppercase in the question is just for clarity about the repetition. – Loovjo – 2016-01-09T12:56:38.110

Whoops. Thanks for the clarification. – Sherlock9 – 2016-01-09T12:58:35.303

Blargle. Thanks @user81655 – Sherlock9 – 2016-01-09T13:20:14.610

This is invalid (it never terminates — try it). Even in the base case, the recursive part of the expression is evaluated. – Lynn – 2019-04-02T22:21:26.160

Fixed. Thanks @Lynn! – Sherlock9 – 2019-04-03T05:29:00.323

I'm late, but 45 bytes with chained comparison

– Reinstate Monica – 2019-10-01T18:43:19.240

2

C#, 59 bytes

string a(int n){return n<1?"a":a(n-1)+(char)(97+n)+a(n-1);}

Just another C# solution...

LegionMammal978

Posted 2016-01-09T12:28:29.827

Reputation: 15 731

2

Perl, 33 bytes

map$\.=chr(97+$_).$\,0..pop;print

No real need for un-golfing. Builds the string up by iteratively appending the next character in sequence plus the reverse of the string so far, using the ASCII value of 'a' as its starting point. Uses $\ to save a few strokes, but that's about as tricky as it gets.

Works for a(0) through a(25) and even beyond. Although you get into extended ASCII after a(29), you'll run out of memory long before you run out of character codes:

a(25) is ~64MiB. a(29) is ~1GiB.

To store the result of a(255) (untested!), one would need 2^256 - 1 = 1.15x10^77 bytes, or roughly 1.15x10^65 1-terabyte drives.

type_outcast

Posted 2016-01-09T12:28:29.827

Reputation: 511

1We need those atom-shudder yottabyte drives, now! – CalculatorFeline – 2016-03-10T20:10:22.350

2

Mathematica, 36 32 bytes

##<>#&~Fold~Alphabet[][[;;#+1]]&

Have you ever watched TWOW 11B?

CalculatorFeline

Posted 2016-01-09T12:28:29.827

Reputation: 2 608

There is no need for the "", and then you can use infix notation for Fold. – Martin Ender – 2016-09-22T17:24:43.483

#1 causes null <>s, and #2 only works for binary functions. – CalculatorFeline – 2016-10-15T15:36:51.757

Did you post this comment on the answer you intended? Because I have no idea what you mean. :) – Martin Ender – 2016-10-15T15:39:46.373

*#1 causes StringJoin to join nulls, and #2 only works for binary or associative functions. (x~Fold~y~Fold~z=Fold[x,Fold[y,z]] instead of Fold[x,y,z]) – CalculatorFeline – 2016-10-15T15:42:47.137

Oh you mean "suggestion #1". No it doesn't cause Nulls. Why would it? – Martin Ender – 2016-10-15T15:44:58.473

Oh, I thought you meant "", not "",. With that they both work. Implementing. – CalculatorFeline – 2016-10-15T15:52:56.317

2

Java 7, 158 bytes

class B{public static void main(String[]a){a('a',Byte.valueOf(a[0]));}static void a(char a,int c){if(c>=0){a(a,c-1);System.out.print((char)(a+c));a(a,c-1);}}}

I like to lurk around PPCG and I would enjoy being able to vote/comment on other answers.

Input is given as program parameters. This follows the same format as many other answers here in that it's a straight forward recursive implementation. I would have commented on the other answer but I don't have the rep to comment yet. It's also slightly different in that the recursive call is done twice rather than building a string and passing it along.

Poke

Posted 2016-01-09T12:28:29.827

Reputation: 3 075

Welcome to PPCG then! I hope you'll do some more than voting and commenting in the future (but don't feel like you have to). :) – Martin Ender – 2016-04-11T19:47:06.997

1

Gaia, 14 bytes

₵aØ@⟪¤ṇ3ṁ¤+ṫ⟫ₓ

Try it online!

₵a		| Push lowercase alphabet
  Ø		| push lowercase string
   @         ₓ	| push the input and do everything between ⟪⟫ that many times
    ⟪¤		| swap
      ṇ		| take the last (first) character
       3ṁ	| push the 3rd item on the stack
         ¤+	| swap and concatenate
           ṫ⟫	| and palindromize

Giuseppe

Posted 2016-01-09T12:28:29.827

Reputation: 21 077

1

Japt, 8 bytes

;gCåÈ+iY

Try it

;gCåÈ+iY     :Implicit input of integer
 g           :Index into
; C          :  Lowercase alphabet
   å         :  Cumulatively reduce, with an initial value of an empty string
    +        :    Append a copy of the current value
     i       :    Prepended with
      Y      :    The current letter

Shaggy

Posted 2016-01-09T12:28:29.827

Reputation: 24 623

1

PowerShell, 54 bytes

param($n)for(;$n+1){$d+=[char]($i+++97-32*!$n--)+$d}$d

Try it online!

mazzy

Posted 2016-01-09T12:28:29.827

Reputation: 4 832

1

Husk, 12 bytes

!¡S+oṠ:o→▲"a

Try it online!

Uses 1-based indexing, which I hope is OK.

Explanation

!             (                                          !!)
 ¡             iterate(                              )
  S                        <*>
   +                   (++)
    o                         (                     )
     Ṡ                         join$   .
      :                             (:)
       o                                    .
        →                               succ
         ▲                                   maximum
          "a                                          "a"

              (iterate((++)<*>(join$(:).succ.maximum))"a"!!)

Esolanging Fruit

Posted 2016-01-09T12:28:29.827

Reputation: 13 542

1

APL(NARS), 24 chars, 48 bytes

{⍵<0:⍬⋄k,⎕A[⍵+1],k←∇⍵-1}

test:

  f←{⍵<0:⍬⋄k,⎕A[⍵+1],k←∇⍵-1}
  f 0
A
  f 1
ABA
  f 2
ABACABA
  f 3
ABACABADABACABA
  f 4
ABACABADABACABAEABACABADABACABA

RosLuP

Posted 2016-01-09T12:28:29.827

Reputation: 3 036

1Doesn’t APL use it’s own code page with every character one byte, making this 24 bytes? – Loovjo – 2019-06-25T07:40:18.147

@Loovjo for what I know Nars Apl has character set 2 bytes for character – RosLuP – 2019-06-25T07:43:02.223

1

PHP -r, 43 bytes

register_argc_argv must be enabled for this to work.

for($a=$b=a;$argv[1]--;$a.=++$b.$a);echo$a;

Try it online!

PHP, 51 bytes

An anonymous function that prints the output directly.

function($n){for($a=$b=a;$n--;$a.=++$b.$a);echo$a;}

Try it online!

Shieru Asakoto

Posted 2016-01-09T12:28:29.827

Reputation: 4 445

1

Befunge-93, 41 bytes

1-011v $<
v::+&_:#^!_:1+v
>:0\:^,+"`"$%:<

Try it online!

Probably some room for improvement here.

Explanation

The sequence is generated using its recursive definition. First, we initialize the stack with the 4 values (1, input+2), (0, -1). Each pair (a, b) on the stack is an "instruction" used in generating the sequence, which, when executed, gets popped and does the following:

  • if a == 0:
    • if b == -1, terminate
    • if b == 0, do nothing
    • if b != -1 and b != 0, print b+96 as an ASCII character
  • if a != 0, push (b-1, b-1), (0, b-1), (b-1, b-1)

The top two items on the stack are continuously run as a sequence-building instruction until the program terminates.

Worth noting that &+ is used the first time around to get the input, as well as every subsequent time as a decrement (because when there is no more input, & returns -1)

negative seven

Posted 2016-01-09T12:28:29.827

Reputation: 1 931

1

Lua, 69 bytes

f=function(n)return n<1 and"a"or f(n-1)..string.char(97+n)..f(n-1)end

Try it online!



C++ (gcc), 58 53 bytes

string f(int n){return n--?f(n)+char(98+n)+f(n):"a";}

- 5 bytes saved by ceilingcat

Try it online!

Mahmoud Sagr

Posted 2016-01-09T12:28:29.827

Reputation: 11

1

k4, 23 bytes

{.Q.a x{x,(1+|/x),x}/0}

      x{           }/0  / do {func} x times, passing output of last iteration as input (first input is 0)
          (1+|\x)       / 1 + max x
        x,       ,x     / join x either side
 .Q.a                   / index into alphabet

executed with inputs [0,4) we get:

  {.Q.a x{x,(1+|/x),x}/0}'[0 1 2 3]
("a";"aba";"abacaba";"abacabadabacaba")

scrawl

Posted 2016-01-09T12:28:29.827

Reputation: 1 079

1

JavaScript, 65 571 bytes

n=>eval('s="a";for(i=0;i<n;i++)s+=(i+11).toString(36)+s')

Demo:

function a(n){
  return eval('s="a";for(i=0;i<n;i++)s+=(i+11).toString(36)+s')
}
alert(a(3))

1 - thanks Neil for saving 8 bytes

nicael

Posted 2016-01-09T12:28:29.827

Reputation: 4 585

(i+11).toString(36) saves you 6 bytes. – Neil – 2016-01-09T18:14:56.487

@Neil Haha, that's a clever hack – nicael – 2016-01-09T18:16:03.343

Oh, and if you move the assignment s="a"; to before the for then it becomes the default return value and you can drop the trailing ;s for another 2 byte saving. – Neil – 2016-01-09T18:18:08.620

@Neil Nice, didn't know about that. – nicael – 2016-01-09T18:19:52.510

I think you can save a byte by incrementing i inline and dropping the increment in the for loop. So... for(i=0;i<n;)s+=(i+++11)... – Not that Charles – 2016-03-11T04:46:15.320

...and also, why use eval? – Not that Charles – 2016-03-11T04:47:29.323

That looks slightly syntactically incorrect. How about ...(++i+10)...? – CalculatorFeline – 2016-10-15T15:45:46.270

1

Mathematica, 46 bytes

If[#<1,"a",(a=#0[#-1])<>Alphabet[][[#+1]]<>a]&

Simple recursive function. Another solution:

a@0="a";a@n_:=(b=a[n-1])<>Alphabet[][[n+1]]<>b

LegionMammal978

Posted 2016-01-09T12:28:29.827

Reputation: 15 731

1

K5, 18 bytes

"A"{x,y,x}/`c$66+!

Repeatedly apply a function to a carried value ("A") and each element of a sequence. The sequence is the alphabetic characters from B up to some number N (`c$66+!). The function joins the left argument on either side of the right argument ({x,y,x}).

In action:

 ("A"{x,y,x}/`c$66+!)'!6
("A"
 "ABA"
 "ABACABA"
 "ABACABADABACABA"
 "ABACABADABACABAEABACABADABACABA"
 "ABACABADABACABAEABACABADABACABAFABACABADABACABAEABACABADABACABA")

JohnE

Posted 2016-01-09T12:28:29.827

Reputation: 4 632

I think the sequence should be lowercase, but that costs no bytes. – user48538 – 2016-01-09T18:05:52.457

1

Japt, 20 17 bytes

97oU+98 r@X+Yd +X

Test it online!

How it works

         // Implicit: U = input integer
65oU+66  // Generate a range from 65 to U+66.
r@       // Reduce each item Y and previous value X in this range with this function:
X+Yd     // return X, plus the character with char code Y,
+X       // plus X.

         // Implicit: output last expression

Non-competing version, 14 bytes

97ôU r@X+Yd +X

The ô function is like o, but creates the range [X..X+Y] instead of [X..Y). Test it online!

I much prefer changing the 97 to 94, in which case the output for 5 looks like so:

^_^`^_^a^_^`^_^b^_^`^_^a^_^`^_^c^_^`^_^a^_^`^_^b^_^`^_^a^_^`^_^

ETHproductions

Posted 2016-01-09T12:28:29.827

Reputation: 47 880

1

MATL, 14 bytes

0i:"t@whh]97+c

This uses version 8.0.0 of the language/compiler, which is earlier than the challenge.

Example

>> matl
 > 0i:"t@whh]97+c
 >
> 3
abacabadabacaba

Explanation

The secuence is created first with numbers 0, 1, 2, ... These are converted to letters 'a', 'b', 'c' at the end.

0         % initiallize: a(0)
i:        % input "N" and create vector [1, 2, ... N]
"         % for each element of that vector
  t       % duplicate current sequence
  @       % push new value of the sequence
  whh     % build new sequence from two copies of old sequence and new value
]         % end for
97+c      % convert 0, 1, 2, ... to 'a', 'b', 'c'. Implicitly print

Edit

Try it online!

Luis Mendo

Posted 2016-01-09T12:28:29.827

Reputation: 87 464

1

Java, 219 bytes

My first code golf attempt. Probably can be golf'd further, but I'm hungry and going out to lunch.

public class a{public static void main(String[]a){String b=j("a",Integer.parseInt(a[0]),1);System.out.println(b);}public static String j(String c,int d,int e){if(d>=e){c+=(char)(97+e)+c;int f=e+1;c=j(c,d,f);}return c;}}

Ungolfed:

public class a {
    public static void main(String[] a) {
        String string = addLetter("a", Integer.parseInt(a[0]), 1);
        System.out.println(string);
    }

    public static String addLetter(String string, int count, int counter) {
        if (count >= counter) {
            string += (char) (97 + counter) + string;
            int f = counter + 1;
            string = addLetter(string, count, f);
        }
        return string;
    }
}

Pretty straightforward brute force recursive algorithm, uses char manipulation.

JamesENL

Posted 2016-01-09T12:28:29.827

Reputation: 299

You can omit the public keyword from a and addLetter/j. – user8397947 – 2016-06-09T21:51:32.107

1

Powershell, 53,46,44,41 Bytes

1..$args[0]|%{}{$d+=[char]($_+96)+$d}{$d}

Pasting into console will generate erronous output on the second run since $d is not re-initialized.

Save 2 bytes by using += Save 3 bytes thanks to @TimmyD

Jonathan Leech-Pepin

Posted 2016-01-09T12:28:29.827

Reputation: 273

@TimmyD Actually gets it down to 41 since I won't need the (,). – Jonathan Leech-Pepin – 2016-04-06T16:31:30.867

No, that was my fault, I actually forgot to update it even though I said I did. – Jonathan Leech-Pepin – 2016-04-06T19:42:34.677

the script does not wirk with 0 and does not generate a uppercase letter – mazzy – 2019-04-02T08:02:20.483

0

Canvas, 8 bytes

ø⁸╵zm{+│

Try it here!

7 bytes taking the index starting with 1, or hacky 6 bytes by taking input "with a trailing newline" (which together can sum up to 5 bytes)

dzaima

Posted 2016-01-09T12:28:29.827

Reputation: 19 048

0

Perl 6, 35 bytes

{('a',{$_~chr(++$+97)~$_}...*)[$_]}

Try it online!

Jo King

Posted 2016-01-09T12:28:29.827

Reputation: 38 234

0

Jelly, 9 bytes

‘R;ṭ¥/ịØa

Try it online!

It would be 8 were it not for the requirement for the sequence to start with zero = a.

Nick Kennedy

Posted 2016-01-09T12:28:29.827

Reputation: 11 829

0

JavaScript, 169 bytes

x=function(n){var r="";for(var i=1;i<=Math.pow(2,n)-1;i++){for(var j=i;j>=0;j--){if(!(i%Math.pow(2,j))){r+="ABCDEFGHIJKLMNOPQRSTUVWXYZ".slice(0,n)[j];break;}}}return r;}

Try it online!

Live demo:

x=function(n){var r="";for(var i=1;i<=Math.pow(2,n)-1;i++){for(var j=i;j>=0;j--){if(!(i%Math.pow(2,j))){r+="ABCDEFGHIJKLMNOPQRSTUVWXYZ".slice(0,n)[j];break;}}}return r;}

console.log(x(prompt("Input depth of ABACABA Pattern")))

Explanation:

x = function(n){ // created function, and yes, I omitted var
  var r = ""; // declaring variable to be returned
  for (var i = 1; i <= Math.pow(2, n) - 1; i++){ // this loop creates each letter in the pattern; "Math.pow(2, n) - 1" is the length of the pattern at depth n
    for (var j = i; j >= 0; j--){ // this loop finds the greatest power of 2 that i is divisible by, since that can be used to find the correct character for that index
      if (! (i % Math.pow(2, j) ) ){ // the modulo part checks the divisibility of 2^j; the "!" replaces "=== 0" 
        r += "ABCDEFGHIJKLMNOPQRSTUVWXYZ".slice(0,n)[j]; // appending character to r
        break; // exits loop once greatest power of 2 i is divisible by is found
      }
    }
  }
  return r;
}

user87487

Posted 2016-01-09T12:28:29.827

Reputation:

The x= can be omitted if you do not refer to it anywhere else, and also I think "ABCDEFGHIJKLMNOPQRSTUVWXYZ".slice(0,n)[j] can be (j+10).toString(36) because (1) .slice(0,n) is not necessary here, and (2) (j+10).toString(36) returns j+10 in base 36, which should be equivalent to your code. – Shieru Asakoto – 2019-06-29T04:36:27.933

0

APL (Dyalog Classic), 20 19 17 bytes

⎕a[¯1↓⊥¨⍨,⍳2,⎕⍴2]

Try it online!

yet another apl answer

ngn

Posted 2016-01-09T12:28:29.827

Reputation: 11 449

0

Clojure, 63 bytes

#(loop[r""i 0](if(= i %)r(recur(str r(char(+ i 97))r)(inc i))))

NikoNyrh

Posted 2016-01-09T12:28:29.827

Reputation: 2 361

0

Mumps (InterSystems), 46 bytes

S Q="",A=65 R N F I=0:1:N{S Q=Q_$C(A+I)_Q} W Q

InterSystems' version of Mumps (some say M, InterSystems likes to call it "Cache Object Script") allows delineating loops with braces which helps keep things on a single line; GT.M would be a few characters longer.

zmerch

Posted 2016-01-09T12:28:29.827

Reputation: 541

0

05AB1E (legacy), 9 bytes

AćsI£vDyý

Try it online!

Also runs with the new 05AB1E version.

code:

A       push the lowercase alphabet
ć       extract first character of that ["bcdefg....", "a"]
s       swap both strings ["a", "bcdefg...."]
I£      drop all letters of the alphabet after the index defined by input
v       for each character 
  D     duplicate last stack entry
  yý    join by current letter
        implicitly close for-loop and print top of stack

Dorian

Posted 2016-01-09T12:28:29.827

Reputation: 1 521

0

Stax, 7 bytes

ö▬⌐ç╗à╢

Run and debug it

This uses 1-based indexing.

0-based indexing costs another byte

recursive

Posted 2016-01-09T12:28:29.827

Reputation: 8 616

0

Matlab, 54 bytes

Just a straight forward recursive implementation.

function s=f(n);s='';if n>-1;k=f(n-1);s=[k,n+97,k];end

I first thought I could do better by using anonymous functions, but it turned out to be way longer, but I thougt I still share it:

i=@(b,c)c{2-b}();
a=@(n,f)i(n<0,{@()'',@()[f(n-1,f)),n+97,f(n-1,f)]});
ba=@(n)a(n,a);

flawr

Posted 2016-01-09T12:28:29.827

Reputation: 40 560

0

J, 30 28 bytes (Try it online!)

I just found out that a(0) = "a", which chopped of 2 bytes.

u:97+(0:`($:@<:,],$:@<:)@.*)

Usage:

   u:97+(0:`($:@<:,],$:@<:)@.*) 2
abacaba

How it works:

u:97+(0:`($:@<:,],$:@<:)@.*) n NB. pseudocode:
                               NB. input as n
                               NB. function as generate_tree
u:                             NB.   convert_to_ASCII(
  97+                          NB.     add_to_all(97,
     (xx`yyyyyyyyyyyyyyy@.z)   NB.       if z then y or x:
                          *    NB.         z: n>0?
      0:                       NB.         x: 0
         ($:@<:,],$:@<:)       NB.         y:
          ppppp,q,rrrrr        NB.           concat(p,q,r):
          $:@<:                NB.             p:
          $:@                  NB.               generate_tree(
             <:                NB.                 n-1)
                ]              NB.             q: n
                  $:@<:        NB.             r: generate_tree(n-1)

Leaky Nun

Posted 2016-01-09T12:28:29.827

Reputation: 45 011

0

Pyke, 11 bytes (noncompeting)

G@QVk?tQG@:

Try it here!

G@          - s = alphabet[input]
  QV        - repeat input times:
     ?tQ    -   input -= 1
        G@  -  alphabet[^]
    k     : - s = s.replace("", ^)

Blue

Posted 2016-01-09T12:28:29.827

Reputation: 26 661