-
permutation
I didn't have a clue where to start on this help-free challenge. So I examined the solution and then stared blankly at it.
Please explain this:
Code:
#include <iostream>
#include <string>
string switch(string topermute, int x, int y)
{
string newstring = topermute;
newstring[x] = newstring[y];
newstring[y] = topermute[x]; //avoids temp variable
return newstring;
}
void permute(string topermute, int place)
{
if(place == topermute.length - 1)
{
cout<<topermute<<endl;
}
for(int nextchar = place; nextchar <
topermute.length(); nextchar++)
{
permute(switch(topermute, place, nextchar),
place+1);
}
}
int main(int argc, char* argv[])
{
if(argc!=2)
{
cout<<"Proper input is 'permute string'";
}
else
{
permute(argv[1], 0);
}
return 0;
}
-
Sure. Would you like some beer while we explain it to you? :)
-
Nothing to explain as it won't compile:
Code:
string switch(string topermute, int x, int y)
You can't have a function called switch
-
Oh well then tell the webmaster that cause I copy and pasted into the post.
-
Indeed, I read Thantos's post and have fixed the code on the site.