How does my solution for recursively generating all permutations of a string work?











up vote
0
down vote

favorite












def permutationString(word):
print('word', word)
result =

if len(word) == 0:
result.append('')
return result

for i in range(len(word)):
before = word[0 : i]
after = word[i + 1 :]
print('before',before)
print('after', after)
partials = permutationString(before + after)
print(partials)
for s in partials:
result.append(word[i] + s)

return result


This is my solution for generating a permutation for a given string.



For an input abc, it gives me ['abc', 'acb', 'bac', 'bca', 'cab', 'cba'] which seems to be correct.



My question is, I don't really understand how the magic works. The code itself is pretty intuitive; we just try each character as the first character and then append the permutations.



But I really don't get how the partials is generated and I am not sure how the solution does not work when we do not do result.append('') when the word is empty.



Is there an intuitive explanation of how the magic works?










share|improve this question
























  • a) Ideally we to handle permutations of empyt word too so conditional is necesseary, b) if permutationString('') return empty set, then the inner loop is empty for one letter words, then for two lettern words, and so on. c) if you dislike append shorten the conditional to if not word: return[""]
    – Serge
    Nov 20 at 15:48















up vote
0
down vote

favorite












def permutationString(word):
print('word', word)
result =

if len(word) == 0:
result.append('')
return result

for i in range(len(word)):
before = word[0 : i]
after = word[i + 1 :]
print('before',before)
print('after', after)
partials = permutationString(before + after)
print(partials)
for s in partials:
result.append(word[i] + s)

return result


This is my solution for generating a permutation for a given string.



For an input abc, it gives me ['abc', 'acb', 'bac', 'bca', 'cab', 'cba'] which seems to be correct.



My question is, I don't really understand how the magic works. The code itself is pretty intuitive; we just try each character as the first character and then append the permutations.



But I really don't get how the partials is generated and I am not sure how the solution does not work when we do not do result.append('') when the word is empty.



Is there an intuitive explanation of how the magic works?










share|improve this question
























  • a) Ideally we to handle permutations of empyt word too so conditional is necesseary, b) if permutationString('') return empty set, then the inner loop is empty for one letter words, then for two lettern words, and so on. c) if you dislike append shorten the conditional to if not word: return[""]
    – Serge
    Nov 20 at 15:48













up vote
0
down vote

favorite









up vote
0
down vote

favorite











def permutationString(word):
print('word', word)
result =

if len(word) == 0:
result.append('')
return result

for i in range(len(word)):
before = word[0 : i]
after = word[i + 1 :]
print('before',before)
print('after', after)
partials = permutationString(before + after)
print(partials)
for s in partials:
result.append(word[i] + s)

return result


This is my solution for generating a permutation for a given string.



For an input abc, it gives me ['abc', 'acb', 'bac', 'bca', 'cab', 'cba'] which seems to be correct.



My question is, I don't really understand how the magic works. The code itself is pretty intuitive; we just try each character as the first character and then append the permutations.



But I really don't get how the partials is generated and I am not sure how the solution does not work when we do not do result.append('') when the word is empty.



Is there an intuitive explanation of how the magic works?










share|improve this question















def permutationString(word):
print('word', word)
result =

if len(word) == 0:
result.append('')
return result

for i in range(len(word)):
before = word[0 : i]
after = word[i + 1 :]
print('before',before)
print('after', after)
partials = permutationString(before + after)
print(partials)
for s in partials:
result.append(word[i] + s)

return result


This is my solution for generating a permutation for a given string.



For an input abc, it gives me ['abc', 'acb', 'bac', 'bca', 'cab', 'cba'] which seems to be correct.



My question is, I don't really understand how the magic works. The code itself is pretty intuitive; we just try each character as the first character and then append the permutations.



But I really don't get how the partials is generated and I am not sure how the solution does not work when we do not do result.append('') when the word is empty.



Is there an intuitive explanation of how the magic works?







python recursion permutation






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 at 1:22









smci

14.5k672104




14.5k672104










asked Nov 20 at 1:11









Dawn17

734413




734413












  • a) Ideally we to handle permutations of empyt word too so conditional is necesseary, b) if permutationString('') return empty set, then the inner loop is empty for one letter words, then for two lettern words, and so on. c) if you dislike append shorten the conditional to if not word: return[""]
    – Serge
    Nov 20 at 15:48


















  • a) Ideally we to handle permutations of empyt word too so conditional is necesseary, b) if permutationString('') return empty set, then the inner loop is empty for one letter words, then for two lettern words, and so on. c) if you dislike append shorten the conditional to if not word: return[""]
    – Serge
    Nov 20 at 15:48
















a) Ideally we to handle permutations of empyt word too so conditional is necesseary, b) if permutationString('') return empty set, then the inner loop is empty for one letter words, then for two lettern words, and so on. c) if you dislike append shorten the conditional to if not word: return[""]
– Serge
Nov 20 at 15:48




a) Ideally we to handle permutations of empyt word too so conditional is necesseary, b) if permutationString('') return empty set, then the inner loop is empty for one letter words, then for two lettern words, and so on. c) if you dislike append shorten the conditional to if not word: return[""]
– Serge
Nov 20 at 15:48












1 Answer
1






active

oldest

votes

















up vote
1
down vote













I have a full lengthy answer here.



The shorter answer is that there's only one way to write a sequence with no elements. That is the empty sequence. So the list with all the permutations of '' is [''].



Suppose you got that wrong and return instead, that is, no solutions. What would happen then?



Well, in the inductive case, as you said, you try each element as the first, and put it in front of the solutions for permutations without that element. So consider a sequence with just one element 'x'. You are going to put x in front of all the solutions for ''. If permutations of '' returned , that is an empty list with no solutions, then you would have no solutions to put x in front of. It returns [''] though, so you're going to put 'x' in front of that one solution ''.



Bonus



Once you think you got it, you may want to try another similar algorithm for calculating permutations.



Try to build all permutations of word selecting only the first element word[0] at each recursive step, and somehow 'combining' it with the solutions for the permutations of word[1:].






share|improve this answer























  • good answer, but [''] is not a singleton.
    – acushner
    Nov 20 at 14:51






  • 2




    I'm using "singleton" in the mathematical sense of a structure holding a single element.
    – Jorge Adriano
    Nov 20 at 14:54










  • fair enough, but can be confusing because this is a site focused on programming, not mathematics.
    – acushner
    Nov 20 at 17:25












  • fair point, removed that word to avoid any ambiguity
    – Jorge Adriano
    Nov 20 at 18:30










  • either way, thank you! i didn't know that mathematical significance of that word till your post. again, good answer.
    – acushner
    Nov 20 at 22:27











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53384842%2fhow-does-my-solution-for-recursively-generating-all-permutations-of-a-string-wor%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
1
down vote













I have a full lengthy answer here.



The shorter answer is that there's only one way to write a sequence with no elements. That is the empty sequence. So the list with all the permutations of '' is [''].



Suppose you got that wrong and return instead, that is, no solutions. What would happen then?



Well, in the inductive case, as you said, you try each element as the first, and put it in front of the solutions for permutations without that element. So consider a sequence with just one element 'x'. You are going to put x in front of all the solutions for ''. If permutations of '' returned , that is an empty list with no solutions, then you would have no solutions to put x in front of. It returns [''] though, so you're going to put 'x' in front of that one solution ''.



Bonus



Once you think you got it, you may want to try another similar algorithm for calculating permutations.



Try to build all permutations of word selecting only the first element word[0] at each recursive step, and somehow 'combining' it with the solutions for the permutations of word[1:].






share|improve this answer























  • good answer, but [''] is not a singleton.
    – acushner
    Nov 20 at 14:51






  • 2




    I'm using "singleton" in the mathematical sense of a structure holding a single element.
    – Jorge Adriano
    Nov 20 at 14:54










  • fair enough, but can be confusing because this is a site focused on programming, not mathematics.
    – acushner
    Nov 20 at 17:25












  • fair point, removed that word to avoid any ambiguity
    – Jorge Adriano
    Nov 20 at 18:30










  • either way, thank you! i didn't know that mathematical significance of that word till your post. again, good answer.
    – acushner
    Nov 20 at 22:27















up vote
1
down vote













I have a full lengthy answer here.



The shorter answer is that there's only one way to write a sequence with no elements. That is the empty sequence. So the list with all the permutations of '' is [''].



Suppose you got that wrong and return instead, that is, no solutions. What would happen then?



Well, in the inductive case, as you said, you try each element as the first, and put it in front of the solutions for permutations without that element. So consider a sequence with just one element 'x'. You are going to put x in front of all the solutions for ''. If permutations of '' returned , that is an empty list with no solutions, then you would have no solutions to put x in front of. It returns [''] though, so you're going to put 'x' in front of that one solution ''.



Bonus



Once you think you got it, you may want to try another similar algorithm for calculating permutations.



Try to build all permutations of word selecting only the first element word[0] at each recursive step, and somehow 'combining' it with the solutions for the permutations of word[1:].






share|improve this answer























  • good answer, but [''] is not a singleton.
    – acushner
    Nov 20 at 14:51






  • 2




    I'm using "singleton" in the mathematical sense of a structure holding a single element.
    – Jorge Adriano
    Nov 20 at 14:54










  • fair enough, but can be confusing because this is a site focused on programming, not mathematics.
    – acushner
    Nov 20 at 17:25












  • fair point, removed that word to avoid any ambiguity
    – Jorge Adriano
    Nov 20 at 18:30










  • either way, thank you! i didn't know that mathematical significance of that word till your post. again, good answer.
    – acushner
    Nov 20 at 22:27













up vote
1
down vote










up vote
1
down vote









I have a full lengthy answer here.



The shorter answer is that there's only one way to write a sequence with no elements. That is the empty sequence. So the list with all the permutations of '' is [''].



Suppose you got that wrong and return instead, that is, no solutions. What would happen then?



Well, in the inductive case, as you said, you try each element as the first, and put it in front of the solutions for permutations without that element. So consider a sequence with just one element 'x'. You are going to put x in front of all the solutions for ''. If permutations of '' returned , that is an empty list with no solutions, then you would have no solutions to put x in front of. It returns [''] though, so you're going to put 'x' in front of that one solution ''.



Bonus



Once you think you got it, you may want to try another similar algorithm for calculating permutations.



Try to build all permutations of word selecting only the first element word[0] at each recursive step, and somehow 'combining' it with the solutions for the permutations of word[1:].






share|improve this answer














I have a full lengthy answer here.



The shorter answer is that there's only one way to write a sequence with no elements. That is the empty sequence. So the list with all the permutations of '' is [''].



Suppose you got that wrong and return instead, that is, no solutions. What would happen then?



Well, in the inductive case, as you said, you try each element as the first, and put it in front of the solutions for permutations without that element. So consider a sequence with just one element 'x'. You are going to put x in front of all the solutions for ''. If permutations of '' returned , that is an empty list with no solutions, then you would have no solutions to put x in front of. It returns [''] though, so you're going to put 'x' in front of that one solution ''.



Bonus



Once you think you got it, you may want to try another similar algorithm for calculating permutations.



Try to build all permutations of word selecting only the first element word[0] at each recursive step, and somehow 'combining' it with the solutions for the permutations of word[1:].







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 20 at 18:30

























answered Nov 20 at 14:45









Jorge Adriano

1,405814




1,405814












  • good answer, but [''] is not a singleton.
    – acushner
    Nov 20 at 14:51






  • 2




    I'm using "singleton" in the mathematical sense of a structure holding a single element.
    – Jorge Adriano
    Nov 20 at 14:54










  • fair enough, but can be confusing because this is a site focused on programming, not mathematics.
    – acushner
    Nov 20 at 17:25












  • fair point, removed that word to avoid any ambiguity
    – Jorge Adriano
    Nov 20 at 18:30










  • either way, thank you! i didn't know that mathematical significance of that word till your post. again, good answer.
    – acushner
    Nov 20 at 22:27


















  • good answer, but [''] is not a singleton.
    – acushner
    Nov 20 at 14:51






  • 2




    I'm using "singleton" in the mathematical sense of a structure holding a single element.
    – Jorge Adriano
    Nov 20 at 14:54










  • fair enough, but can be confusing because this is a site focused on programming, not mathematics.
    – acushner
    Nov 20 at 17:25












  • fair point, removed that word to avoid any ambiguity
    – Jorge Adriano
    Nov 20 at 18:30










  • either way, thank you! i didn't know that mathematical significance of that word till your post. again, good answer.
    – acushner
    Nov 20 at 22:27
















good answer, but [''] is not a singleton.
– acushner
Nov 20 at 14:51




good answer, but [''] is not a singleton.
– acushner
Nov 20 at 14:51




2




2




I'm using "singleton" in the mathematical sense of a structure holding a single element.
– Jorge Adriano
Nov 20 at 14:54




I'm using "singleton" in the mathematical sense of a structure holding a single element.
– Jorge Adriano
Nov 20 at 14:54












fair enough, but can be confusing because this is a site focused on programming, not mathematics.
– acushner
Nov 20 at 17:25






fair enough, but can be confusing because this is a site focused on programming, not mathematics.
– acushner
Nov 20 at 17:25














fair point, removed that word to avoid any ambiguity
– Jorge Adriano
Nov 20 at 18:30




fair point, removed that word to avoid any ambiguity
– Jorge Adriano
Nov 20 at 18:30












either way, thank you! i didn't know that mathematical significance of that word till your post. again, good answer.
– acushner
Nov 20 at 22:27




either way, thank you! i didn't know that mathematical significance of that word till your post. again, good answer.
– acushner
Nov 20 at 22:27


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53384842%2fhow-does-my-solution-for-recursively-generating-all-permutations-of-a-string-wor%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Tonle Sap (See)

I get strange results when I access the Sqlitedatabase with Unity C# via XAMPP

Guatemaltekische Davis-Cup-Mannschaft