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?
python recursion permutation
add a comment |
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?
python recursion permutation
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 toif not word: return[""]
– Serge
Nov 20 at 15:48
add a comment |
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?
python recursion permutation
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
python recursion permutation
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 toif not word: return[""]
– Serge
Nov 20 at 15:48
add a comment |
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 toif 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
add a comment |
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:].
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
add a comment |
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:].
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
add a comment |
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:].
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
add a comment |
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:].
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:].
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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