28
\$\begingroup\$

Introduction

For this example, let's take the string Hello, World! and the array [3, 2, 3]. To find the substring chain, we go through the following process:

The first number of the array is 3, so we get the substring [0 - 3], which is Hel. After that, we remove the first 3 characters from the initial string, which leaves us with lo, World!.

The second number of the array is 2, so we get the substring [0 - 2] from our new string, which gives us lo. The leftover string becomes , World!.

The last number is a 3, which gives us , W. The substring chain is all of the substrings combined, which gives us:

['Hel', 'lo', ', W']

For a more visual example:

[3, 2, 3], 'Hello, World!'
3 -> Hel
2 ->    lo
3 ->      , W

The task

Given a non-empty string and a non-empty array only consisting of positive integers (> 0), output the substring chain. You may assume that the sum of all integers in the array does not exceed the length of the string.

You can also assume that the strings will never contain any newlines.

Test cases

Input: abcdefghijk, [2, 1, 3]
Output: ['ab', 'c', 'def']

Input: Code Golf, [4, 1]
Output: ['Code', ' ']

Input: Ayyy, [3]
Output: ['Ayy']

Input: lexicographically, [2, 2, 2, 7, 4]
Output: ['le', 'xi', 'co', 'graphic', 'ally']

This is , so the submission with the smallest number of bytes wins!

\$\endgroup\$
0

33 Answers 33

1
2
1
\$\begingroup\$

Common Lisp, 78 76 bytes

Assuming anonymous function is allowed:

(lambda(s l)(loop for x in l as y = x then(+ y x)collect(subseq s(- y x)y)))

Usage

(funcall #'(lambda(s l)(loop for x in l as y = x then(+ y x)collect(subseq s(- y x)y)))"AbCdefGhijK"'(3 2 3))

Output

("AbC" "de" "fGh")

-2 bytes by using as instead of and and changing y definition to fit parentheses between two variables in (subseq ...)

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

C 474 byte

char**f(char*a,int*b){int  i,j;static char *t[99];for(j=0;j<99;++j)t[j]=0;t[0]=a;for(i=0,j=1;j<99&&a[i]&&b[j-1];++i)if(i>=b[j-1]){t[j++]=a+i;i=0;a=t[j-1];}t[j]=a+i;return j>=99?0:t;}void showResult(char**p, int*b){char *v;int i,j;if(p==0) {printf("NULL"); return;}printf("\'%s\' [", p[0]);for(i=0;b[i];++i)printf("%d%s", b[i], b[i+1]?", ":"");printf("]==[");for(i=0;b[i];++i){printf("\'");for(v=p[i]; v<p[i+1];++v)putchar(*v);printf("\'%s", b[i+1]?", ": "");}printf("]\n");}

main

main()
{char *test[]={"Hello, World", "abcdefghijk", "Code Golf",
               "Ayyy","lexicographically",0}, **p;
 int  v[80];

 v[0]=3;v[1]=2;v[2]=3;v[3]=0;showResult(f(test[0], v), v);
 v[0]=2;v[1]=1;v[2]=3;v[3]=0;showResult(f(test[1], v), v);
 v[0]=4;v[1]=1;v[2]=0;       showResult(f(test[2], v), v);
 v[0]=3;v[1]=0;              showResult(f(test[3], v), v);
 v[0]=2;v[1]=2;v[2]=2;v[3]=7; v[4]=4; v[5]=0;
                         showResult(f(test[4], v), v);
}
/*
 474
 'Hello, World' [3, 2, 3]==['Hel', 'lo', ', W']
 'abcdefghijk' [2, 1, 3]==['ab', 'c', 'def']
 'Code Golf' [4, 1]==['Code', ' ']
 'Ayyy' [3]==['Ayy']
 'lexicographically' [2, 2, 2, 7, 4]==['le', 'xi', 'co', 'graphic', 'ally']
*/
\$\endgroup\$
1
  • \$\begingroup\$ My only post as a question on write one macro function capable of lambda calculus is cancelled, now they do problems on programs that compile and run ok in C compilers where one not find the compiler to programming language they use...saluti, I hope this not will be now cancelled too. \$\endgroup\$
    – user58988
    Sep 26, 2016 at 20:19
0
\$\begingroup\$

Java - 143 bytes

String[]s(String u,int[]a){String[]k=new String[a.length];for(int i=0;i<k.length;i++){k[i]=u.substring(0,a[i]);u=u.substring(a[i]);}return k;}

Ungolfed version:

String[] s (String u, int[] a) {
    String[] k = new String[a.length];
    for (int i = 0; i < k.length; i++) {
        k[i] = u.substring(0,a[i]);
        u = u.substring(a[i]);
    }
    return k;
}
\$\endgroup\$
1
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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