Imagine you have an infinite sequence of the alphabet repeated infinitely many times:


You start at index 0, which corresponds to the letter a, and the should_write boolean flag is False.

The input is a list or string of single-digit numbers. For each number n in the input, you should:

  • Print (or add to the return value) the n-th item of the list if the should_write flag is set to True

  • If n is 0, swap the should_write flag.

  • Otherwise move (jump) the index n places forward.

At the end of the input, stop (and return the result if using a function).


A sequence of digits (list or string).


The text that comes out after applying the above procedure.


>>> g('99010')
>>> g('100')
>>> g('2300312')
>>> g('390921041912042041010')
>>> g('1')
>>> g('1045')
>>> g('33005780')
>>> g('100100102')
>>> g('012')
  • \$\begingroup\$ Does the input have to be a string of digits, as in your test cases, or can it be a list of numbers? \$\endgroup\$ – xnor Oct 13 '15 at 20:23
  • 2
    \$\begingroup\$ Are you sure about your test cases? Several of them seem wrong to me. \$\endgroup\$ – Peter Taylor Oct 13 '15 at 20:28
  • \$\begingroup\$ @xnor a list of digits \$\endgroup\$ – Caridorc Oct 13 '15 at 20:32
  • \$\begingroup\$ @Caridorc In '33005780', how is more than 1 char printed? The should_write flag starts off, is turned on for one digit and turned back off immediately, and then only turned on again at the end. \$\endgroup\$ – xnor Oct 13 '15 at 20:39
  • \$\begingroup\$ @xnor found obvious bug in my reference implementation and facepalmed, will correct testcases asap... \$\endgroup\$ – Caridorc Oct 13 '15 at 20:40

Python 3, 60

for c in input():b and print(chr(97+i%26));b^=c<1;i+=c

Takes a list of numbers and prints the chars on separate lines.

Basically just follows the instructions. The index is i and the should_write flag is b. The expression b and short-circuits to print only when b is true.


Pip, 20 bytes

Takes a string of digits as command-line argument.


This code uses a number of Pip's preinitialized variables. i is initially 0, y is initially "" (falsy) and z contains the lowercase alphabet. a gets the first command-line arg.

Fda{               }  For each digit d in a:
    IyOz@i              If y (the flag) is truthy, output letter of z at index i
                        Pip's cyclic indexing takes care of the infinite effect
          d?            If d is truthy (digit is nonzero),
            i+:d          then increment i by d;
                Y!y       otherwise, yank !y into y (thus swapping the flag)

An alternate way of swapping the flag would be !:y, with !: being a compute-and-assign operator just like +:.

C:\Users\dlosc> pip.py -e Fda{IyOz@id?i+:dY!y} 390921041912042041010

CJam, 28 bytes


Test it here.

Nothing really special here. Like xnor, I don't have a string of letters but simply a running total (mod 26) which I add to a when I need to print it. The should_write flag is kept track of in U.


R, 73 bytes

Once I got it sorted in my head:)


This takes a vector of integers from STDIN and returns a vector of letters.

Ungolfed and explanation

a <- head(scan()),-1)    # get integers from stdin and drop last one
a <- cumsum(a) %% 26 + 1 # do the cumulative sum of the vector to use as a vector of letter indexes
l <- letters[a]          # vector of potential letters 
p <- -(a<1)*2+1          # turns 0's into -1 and everything else into 1
p <- cumprod(p)          # the cumlative prod then creates a vector of on and off values
f <- which(p<0)          # index values of the returned characters
l[f]                     # return vector of characters


> (letters[cumsum(a<-head(scan(),-1))%%26+1])[which(cumprod(-(a<1)*2+1)<0)]
1: 9 9 0 1 0
Read 5 items
[1] "s" "t"
> (letters[cumsum(a<-head(scan(),-1))%%26+1])[which(cumprod(-(a<1)*2+1)<0)]
1: 1 0 0
Read 3 items
[1] "b"
> (letters[cumsum(a<-head(scan(),-1))%%26+1])[which(cumprod(-(a<1)*2+1)<0)]
1: 2 3 0 0 3 1 2
Read 7 items
[1] "f"
> (letters[cumsum(a<-head(scan(),-1))%%26+1])[which(cumprod(-(a<1)*2+1)<0)]
1: 1
Read 1 item
> (letters[cumsum(a<-head(scan(),-1))%%26+1])[which(cumprod(-(a<1)*2+1)<0)]
1: 1 0 4 5
Read 4 items
[1] "b" "f"
> (letters[cumsum(a<-head(scan(),-1))%%26+1])[which(cumprod(-(a<1)*2+1)<0)]
1: 0 1 2
Read 3 items
[1] "a" "b"

JavaScript (ES6) 76

(note: the fromCharCode function is so lengthy that for lowercase letters it's better a conversion to base 36)

Test running the snippet below in any EcmaScript 6 compliant browser



.forEach(t=>console.log(t+' ['+g(t)+']'))
<pre id=O></pre>


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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