C++(Bcc), 287 bytes
#include<algorithm.h>
f(a,b)char*a,**b;{int i,j,k,v,p[256];if(!a||!b||!*b)return-1;for(v=0;v<256&&b[v];++v)p[v]=v;if(v>=256)return-1;la:for(i=0,j=0;j<v&&a[i];){for(k=0;b[p[j]][k]==a[i]&&a[i];++i,++k);j=b[p[j]][k]?(i-=k),j+1:0;}if(a[i]&&next_permutation(p,p+v)) goto la;return i&&!a[i];}
because i do not wrote or used too much the next_permutation() i don't know if is all ok.
I don't know 100% if it is a solution too possibly this is out of quality...
One list of string is here one array of pointers to char; NULL terminated
The algo is easy, there is one algo that linearity try if all string in the list fit with argument "a" string
there is one other algo that permute the index of the list of string so it try all possible combination.
ungolf it, test code and results here
#include<stdio.h>
g(a,b)char*a,**b;
{int i,j,k,v,p[256];
if(!a||!b||!*b) return -1;
for(v=0;v<256&&b[v];++v) p[v]=v;
if(v>=256) return -1; // one array of len >256 is too much
la:
for(i=0,j=0;j<v&&a[i];)
{for(k=0;b[p[j]][k]==a[i]&&a[i];++i,++k);
j=b[p[j]][k]?(i-=k),j+1:0;
}
if(a[i]&&next_permutation(p,p+v)) goto la;
return i&&!a[i];
}
#define F for
#define P printf
test(char* a, char** b)
{int i;
P("f(\"%s\",[",a);
F(i=0;b[i];++i)
P("\"%s\"%s", b[i], b[i+1]?", ":"");
P("])=%d\n", f(a,b));
}
main()
{char *a1="Hello, world!", *b1[]={"l","He", "o, worl", "d!", 0};//1
char *a2="la lal al ", *b2[]={"la", " l", "al ", 0};//1
char *a3="this is a string", *b3[]={"this should return falsy", 0};//0
char *a4="thi is a string", *b4[]={"this", "i i", " a", " string", 0};//0
char *a5="aaaaa", *b5[]={"aa", 0};//0
char *a6="foo bar foobar", *b6[]={"foo","bar"," ","spam", 0};//1
char *a7="ababab", *b7[]={"a","ba","ab", 0};//1
char *a8="", *b8[]={"This return 0 even if has to return 1", 0};//0
char *a9="ababc", *b9[]={"a","abc", "b", 0};//1
test(a1,b1);test(a2,b2);test(a3,b3);test(a4,b4);test(a5,b5);test(a6,b6);
test(a7,b7);test(a8,b8);test(a9,b9);
}
f("Hello, world!",["l", "He", "o, worl", "d!"])=1
f("la lal al ",["la", " l", "al "])=1
f("this is a string",["this should return falsy"])=0
f("thi is a string",["this", "i i", " a", " string"])=0
f("aaaaa",["aa"])=0
f("foo bar foobar",["foo", "bar", " ", "spam"])=1
f("ababab",["a", "ba", "ab"])=1
f("",["This return 0 even if has to return 1"])=0
f("ababc",["a", "abc", "b"])=1
this would compile in gcc C++ compiler
#include<algorithm>
int f(char*a,char**b){int i,j,k,v,p[256];if(!a||!b||!*b)return -1;for(v=0;v<256&&b[v];++v)p[v]=v;if(v>=256)return -1;la:;for(i=0,j=0;j<v&&a[i];){for(k=0;b[p[j]][k]==a[i]&&a[i];++i,++k);j=b[p[j]][k]?(i-=k),j+1:0;}if(a[i]&&std::next_permutation(p,p+v))goto la;return i&&!a[i];}
Does it matter if the array contains more strings than are needed to construct the main string? – Shaggy – 2017-04-27T23:36:29.250
What should the return value be in those cases? – Shaggy – 2017-04-27T23:38:37.217
@Shaggy Truthy. If there is extra, then the string can be constructed with all the non-extra parts. I will add a test case. – Comrade SparklePony – 2017-04-27T23:39:55.550
3I recommend adding this test case:
"ababab", ["a","ba","ab"]
– math junkie – 2017-04-27T23:51:54.347When you say capitiization does not matter do you mean that
"tEsT", ["t","e","S"]
will return true? If so, you should add that as a test case too – math junkie – 2017-04-27T23:57:02.777@mathjunkie Capitalization does matter, I don't know why I said it doesn't. Edited. – Comrade SparklePony – 2017-04-27T23:58:21.600
3I'd suggest you add a test case containing regex metacharacters. – Joey – 2017-04-28T07:22:12.477
Missing test case: the string to construct is empty. – Peter Taylor – 2017-04-28T10:11:11.097
@Joey Interesting idea, but it does not fit the challenge in my point of view. This is not a regex challenge. Good idea though. – Comrade SparklePony – 2017-04-28T13:10:22.547
@PeterTaylor Good idea, added. – Comrade SparklePony – 2017-04-28T13:11:52.127
@ComradeSparklePony: The point is to weed out solutions that blindly use regex and just pass the examples you posted, while something like
('abc', ['a', '.', 'b'])
would pass for their solution, which shouldn't. Another point: When using unbalanced parentheses or quantifiers without anything to quantify (*
) in the string list, those solutions would simply crash. – Joey – 2017-04-28T13:12:36.983The final test case added after my submission not only breaks it, but all my mathematical intuition says the solution should be the opposite. If you're allowed to not use all the strings, then you should be allowed to use none of them. – Ørjan Johansen – 2017-04-28T18:35:00.453
@ØrjanJohansen Yes. I keep on confusing myself with this, and I think you are right. I will edit. – Comrade SparklePony – 2017-04-28T18:36:32.110
@ComradeSparklePony is it acceptable to return 0 if it can be created and a positive integer representing how far off the substrings are from the word as a falsy value? – Magic Octopus Urn – 2017-04-28T19:37:58.540
@carusocomputing Sorry, but the question asks for a truth/false value as output, and I will have to stand by that. However, feel free to post a solution that does that alongside a correct solution. – Comrade SparklePony – 2017-04-28T19:53:23.613
@ComradeSparklePony it's actually one byte less for my program to do that ;). Nbd, figured as much haha. – Magic Octopus Urn – 2017-04-28T19:56:34.163