6
\$\begingroup\$

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

abcdefghijklmnopqrstuvwxyzabcd...

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).

Input

A sequence of digits (list or string).

Output

The text that comes out after applying the above procedure.

Examples

>>> g('99010')
st
>>> g('100')
b
>>> g('2300312')
f
>>> g('390921041912042041010')
mvxyptvab
>>> g('1')
>>> g('1045')
bf
>>> g('33005780')
g
>>> g('100100102')
bcd
>>> g('012')
ab
\$\endgroup\$
  • \$\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
4
\$\begingroup\$

Python 3, 60

i=b=0
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.

\$\endgroup\$
3
\$\begingroup\$

Pip, 20 bytes

Takes a string of digits as command-line argument.

Fda{IyOz@id?i+:dY!y}

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
mvxyptvab
\$\endgroup\$
2
\$\begingroup\$

CJam, 28 bytes

0q{~U{1$'a+o}&_!U^:U;+26%}/;

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.

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

R, 73 bytes

Once I got it sorted in my head:)

(letters[cumsum(a<-head(scan()),-1)%%26+1])[which(cumprod(-(a<1)*2+1)<0)]

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

Tests

> (letters[cumsum(a<-head(scan(),-1))%%26+1])[which(cumprod(-(a<1)*2+1)<0)]
1: 9 9 0 1 0
6: 
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
4: 
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
8: 
Read 7 items
[1] "f"
> (letters[cumsum(a<-head(scan(),-1))%%26+1])[which(cumprod(-(a<1)*2+1)<0)]
1: 1
2: 
Read 1 item
character(0)
> (letters[cumsum(a<-head(scan(),-1))%%26+1])[which(cumprod(-(a<1)*2+1)<0)]
1: 1 0 4 5
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
4: 
Read 3 items
[1] "a" "b"
> 
\$\endgroup\$
1
\$\begingroup\$

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

g=l=>l.replace(/./g,n=>[f?(p%26+10).toString(36):'',-n?p-=-n:f=!f][0],p=f=0)

console.log=x=>O.innerHTML+=x+'\n'

;['99010','100','2300312','390921041912042041010','1','1045','33005780','100100102','012']
.forEach(t=>console.log(t+' ['+g(t)+']'))
<pre id=O></pre>

\$\endgroup\$

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.