Finding numbers in a list of permutations based on the position of one of the numbers in the list
I've been coding a program that solves a maths problem with specific conditions.
In this maths problem, there are seven slots. At the beginning, the first three slots are occupied by the numbers 1, 2 and 3. There is an empty slot in the middle. The next three are numbers 4, 5 and 6. Seven slots altogether.
The goal of the problem is for the 1 2 and 3 to switch places with the 4 5 and 6. Only one number can be moved at a time. It can move over another number into an empty slot (the zero) or move sideways switching with an empty slot.
Below is a visualisation of the start:
1 2 3 0 4 5 6
The desired outcome is shown below:
4 5 6 0 1 2 3
Keep in mind that it does not have to be in this order, as long as 4 5 6 is on one side and 1 2 3 is on the other with 0 in the middle.
The program that we are creating uses itertools for a list of permutations. Then, based on the position of the zero, finds a permutation that suits the next move.
What I need is for specific combinations to be extracted (output) from this list, based on the position of zeroes within the combinations. Below is the code so far.
import time
import itertools
nonStop = True
answerList = [1, 2, 3, 0, 4, 5, 6]
combinations = itertools.permutations([1, 2, 3, 0, 4, 5, 6])
while nonStop == True:
for value in combinations:
i = 0
print(value)
i += 1
time.sleep(2)
Thanks in advance. Any help would be greatly appreciated!
python list math permutation itertools
add a comment |
I've been coding a program that solves a maths problem with specific conditions.
In this maths problem, there are seven slots. At the beginning, the first three slots are occupied by the numbers 1, 2 and 3. There is an empty slot in the middle. The next three are numbers 4, 5 and 6. Seven slots altogether.
The goal of the problem is for the 1 2 and 3 to switch places with the 4 5 and 6. Only one number can be moved at a time. It can move over another number into an empty slot (the zero) or move sideways switching with an empty slot.
Below is a visualisation of the start:
1 2 3 0 4 5 6
The desired outcome is shown below:
4 5 6 0 1 2 3
Keep in mind that it does not have to be in this order, as long as 4 5 6 is on one side and 1 2 3 is on the other with 0 in the middle.
The program that we are creating uses itertools for a list of permutations. Then, based on the position of the zero, finds a permutation that suits the next move.
What I need is for specific combinations to be extracted (output) from this list, based on the position of zeroes within the combinations. Below is the code so far.
import time
import itertools
nonStop = True
answerList = [1, 2, 3, 0, 4, 5, 6]
combinations = itertools.permutations([1, 2, 3, 0, 4, 5, 6])
while nonStop == True:
for value in combinations:
i = 0
print(value)
i += 1
time.sleep(2)
Thanks in advance. Any help would be greatly appreciated!
python list math permutation itertools
Seems like you haven't attempted anything to extracted (output) from this list, based on the position of zeroes within the combinations. Please read through the Help Center, in particular How do I ask a good question? If you run into a specific problem, research it thoroughly, search thoroughly here, and if you're still stuck post your code and a description of the problem. Also, remember to include Minimum, Complete, Verifiable Example. People will be glad to help
– Andreas
Nov 21 at 2:27
1
You're calling your permutationscombinations
o_O
– slider
Nov 21 at 2:30
What are thecombinations
supposed to do here that solves this problem explicitly? Also, it doesn't look like your editing anything inanswerList
, nor is there anybreak
condition in yourwhile
loop. One thing that can be helpful is talk yourself through the steps required to move one number, and try to execute that in code. That way you aren't trying to solve the whole problem in one go
– C.Nivs
Nov 21 at 2:33
Also how is your answer represented? A list of states that lead you to your final goal? Why did you choose to compute all possible permutations of your starting list?
– slider
Nov 21 at 2:38
My understanding now since Rory's help is that the combinations are stored in order to be read by the program to make the next move. This is only the beginning of the code but the answer list would be the dictionary essentially. Thanks for the advice on the break in the while loop. All of the positions from the start to end would be printed. Yes, one number moves one or two slots into an empty slot, but they can only move in one direction from the start, not backwards. Imagine like this >>>_<<<
– Ian Heng
Nov 21 at 23:51
add a comment |
I've been coding a program that solves a maths problem with specific conditions.
In this maths problem, there are seven slots. At the beginning, the first three slots are occupied by the numbers 1, 2 and 3. There is an empty slot in the middle. The next three are numbers 4, 5 and 6. Seven slots altogether.
The goal of the problem is for the 1 2 and 3 to switch places with the 4 5 and 6. Only one number can be moved at a time. It can move over another number into an empty slot (the zero) or move sideways switching with an empty slot.
Below is a visualisation of the start:
1 2 3 0 4 5 6
The desired outcome is shown below:
4 5 6 0 1 2 3
Keep in mind that it does not have to be in this order, as long as 4 5 6 is on one side and 1 2 3 is on the other with 0 in the middle.
The program that we are creating uses itertools for a list of permutations. Then, based on the position of the zero, finds a permutation that suits the next move.
What I need is for specific combinations to be extracted (output) from this list, based on the position of zeroes within the combinations. Below is the code so far.
import time
import itertools
nonStop = True
answerList = [1, 2, 3, 0, 4, 5, 6]
combinations = itertools.permutations([1, 2, 3, 0, 4, 5, 6])
while nonStop == True:
for value in combinations:
i = 0
print(value)
i += 1
time.sleep(2)
Thanks in advance. Any help would be greatly appreciated!
python list math permutation itertools
I've been coding a program that solves a maths problem with specific conditions.
In this maths problem, there are seven slots. At the beginning, the first three slots are occupied by the numbers 1, 2 and 3. There is an empty slot in the middle. The next three are numbers 4, 5 and 6. Seven slots altogether.
The goal of the problem is for the 1 2 and 3 to switch places with the 4 5 and 6. Only one number can be moved at a time. It can move over another number into an empty slot (the zero) or move sideways switching with an empty slot.
Below is a visualisation of the start:
1 2 3 0 4 5 6
The desired outcome is shown below:
4 5 6 0 1 2 3
Keep in mind that it does not have to be in this order, as long as 4 5 6 is on one side and 1 2 3 is on the other with 0 in the middle.
The program that we are creating uses itertools for a list of permutations. Then, based on the position of the zero, finds a permutation that suits the next move.
What I need is for specific combinations to be extracted (output) from this list, based on the position of zeroes within the combinations. Below is the code so far.
import time
import itertools
nonStop = True
answerList = [1, 2, 3, 0, 4, 5, 6]
combinations = itertools.permutations([1, 2, 3, 0, 4, 5, 6])
while nonStop == True:
for value in combinations:
i = 0
print(value)
i += 1
time.sleep(2)
Thanks in advance. Any help would be greatly appreciated!
python list math permutation itertools
python list math permutation itertools
edited Nov 21 at 2:29
slider
8,0451129
8,0451129
asked Nov 21 at 2:23
Ian Heng
1
1
Seems like you haven't attempted anything to extracted (output) from this list, based on the position of zeroes within the combinations. Please read through the Help Center, in particular How do I ask a good question? If you run into a specific problem, research it thoroughly, search thoroughly here, and if you're still stuck post your code and a description of the problem. Also, remember to include Minimum, Complete, Verifiable Example. People will be glad to help
– Andreas
Nov 21 at 2:27
1
You're calling your permutationscombinations
o_O
– slider
Nov 21 at 2:30
What are thecombinations
supposed to do here that solves this problem explicitly? Also, it doesn't look like your editing anything inanswerList
, nor is there anybreak
condition in yourwhile
loop. One thing that can be helpful is talk yourself through the steps required to move one number, and try to execute that in code. That way you aren't trying to solve the whole problem in one go
– C.Nivs
Nov 21 at 2:33
Also how is your answer represented? A list of states that lead you to your final goal? Why did you choose to compute all possible permutations of your starting list?
– slider
Nov 21 at 2:38
My understanding now since Rory's help is that the combinations are stored in order to be read by the program to make the next move. This is only the beginning of the code but the answer list would be the dictionary essentially. Thanks for the advice on the break in the while loop. All of the positions from the start to end would be printed. Yes, one number moves one or two slots into an empty slot, but they can only move in one direction from the start, not backwards. Imagine like this >>>_<<<
– Ian Heng
Nov 21 at 23:51
add a comment |
Seems like you haven't attempted anything to extracted (output) from this list, based on the position of zeroes within the combinations. Please read through the Help Center, in particular How do I ask a good question? If you run into a specific problem, research it thoroughly, search thoroughly here, and if you're still stuck post your code and a description of the problem. Also, remember to include Minimum, Complete, Verifiable Example. People will be glad to help
– Andreas
Nov 21 at 2:27
1
You're calling your permutationscombinations
o_O
– slider
Nov 21 at 2:30
What are thecombinations
supposed to do here that solves this problem explicitly? Also, it doesn't look like your editing anything inanswerList
, nor is there anybreak
condition in yourwhile
loop. One thing that can be helpful is talk yourself through the steps required to move one number, and try to execute that in code. That way you aren't trying to solve the whole problem in one go
– C.Nivs
Nov 21 at 2:33
Also how is your answer represented? A list of states that lead you to your final goal? Why did you choose to compute all possible permutations of your starting list?
– slider
Nov 21 at 2:38
My understanding now since Rory's help is that the combinations are stored in order to be read by the program to make the next move. This is only the beginning of the code but the answer list would be the dictionary essentially. Thanks for the advice on the break in the while loop. All of the positions from the start to end would be printed. Yes, one number moves one or two slots into an empty slot, but they can only move in one direction from the start, not backwards. Imagine like this >>>_<<<
– Ian Heng
Nov 21 at 23:51
Seems like you haven't attempted anything to extracted (output) from this list, based on the position of zeroes within the combinations. Please read through the Help Center, in particular How do I ask a good question? If you run into a specific problem, research it thoroughly, search thoroughly here, and if you're still stuck post your code and a description of the problem. Also, remember to include Minimum, Complete, Verifiable Example. People will be glad to help
– Andreas
Nov 21 at 2:27
Seems like you haven't attempted anything to extracted (output) from this list, based on the position of zeroes within the combinations. Please read through the Help Center, in particular How do I ask a good question? If you run into a specific problem, research it thoroughly, search thoroughly here, and if you're still stuck post your code and a description of the problem. Also, remember to include Minimum, Complete, Verifiable Example. People will be glad to help
– Andreas
Nov 21 at 2:27
1
1
You're calling your permutations
combinations
o_O– slider
Nov 21 at 2:30
You're calling your permutations
combinations
o_O– slider
Nov 21 at 2:30
What are the
combinations
supposed to do here that solves this problem explicitly? Also, it doesn't look like your editing anything in answerList
, nor is there any break
condition in your while
loop. One thing that can be helpful is talk yourself through the steps required to move one number, and try to execute that in code. That way you aren't trying to solve the whole problem in one go– C.Nivs
Nov 21 at 2:33
What are the
combinations
supposed to do here that solves this problem explicitly? Also, it doesn't look like your editing anything in answerList
, nor is there any break
condition in your while
loop. One thing that can be helpful is talk yourself through the steps required to move one number, and try to execute that in code. That way you aren't trying to solve the whole problem in one go– C.Nivs
Nov 21 at 2:33
Also how is your answer represented? A list of states that lead you to your final goal? Why did you choose to compute all possible permutations of your starting list?
– slider
Nov 21 at 2:38
Also how is your answer represented? A list of states that lead you to your final goal? Why did you choose to compute all possible permutations of your starting list?
– slider
Nov 21 at 2:38
My understanding now since Rory's help is that the combinations are stored in order to be read by the program to make the next move. This is only the beginning of the code but the answer list would be the dictionary essentially. Thanks for the advice on the break in the while loop. All of the positions from the start to end would be printed. Yes, one number moves one or two slots into an empty slot, but they can only move in one direction from the start, not backwards. Imagine like this >>>_<<<
– Ian Heng
Nov 21 at 23:51
My understanding now since Rory's help is that the combinations are stored in order to be read by the program to make the next move. This is only the beginning of the code but the answer list would be the dictionary essentially. Thanks for the advice on the break in the while loop. All of the positions from the start to end would be printed. Yes, one number moves one or two slots into an empty slot, but they can only move in one direction from the start, not backwards. Imagine like this >>>_<<<
– Ian Heng
Nov 21 at 23:51
add a comment |
1 Answer
1
active
oldest
votes
Here is some high-level advice on your program. I'll assume that on each move the zero will swap places with a number that is one or two slots distance from the zero. I'll also assume that you want to find the smallest number of moves that takes you from the initial list to a "desired outcome" and that the printout is to show all the positions from the starting position to the desired outcome.
If you know graph theory (also called network theory) you could easily solve your problem by performing a breadth-first search from your starting position to any desired position. The nodes in your graph are the 5,040 possible permutations, and two nodes have an edge between them if you can go from one position to the other in one move.
If you don't know graph theory, you can use this following approach. You can use two overall data structures: a queue (such as collections.deque) and a dictionary. Put the initial position into the queue. Also put it as a key in the dictionary with the value None
.
Then run a loop. In each run through the loop, remove one position from the queue. There will be at most four possible moves from this position: swap the zero with the item 2 left, 1 left, 1 right, or 2 right of the zero. (If the zero is at or near an end, the possible moves will be fewer.) For each of those moves, if the resulting position is not already in the dictionary, add it to the queue and the dictionary. The dictionary entry's value is the position you just took from the queue. If the resulting position is already in the dictionary, do nothing.
Now check if the resulting position is a "desired result". If not, continue the loop. If it is, use the dictionary to save all the moves from your desired result back to the initial position. Then print those positions in the desired order, and you are done--break the loop.
Note three things about this approach. First, if any sequence of moves reaches a desired result, this approach will print one of the shortest sequences. Second, not all permutations of the original position are generated. Permutations are generated one at a time until a desired result is reached--there is no need for more. Third, no printing is done until all the moves are done and the good moves have been picked out. This is because most moves will not prove to be useful, so we wait until we know which the useful ones are.
If you want more information, show some more work of your own. But before that, tell me if I understood the rules of your problem correctly.
Hi Rory, that was very informative. Thanks a lot! You understood it exactly, and by dictionary do you mean a record of the moves made? So essentially, the only thing I need to figure out is how the computer actually makes the moves. If the initial position is in the queue, how are the next moves made with Python? Would something like an array work with preset conditions so it can only make certain moves? Could you also please expand on the edges? Thanks
– Ian Heng
Nov 21 at 23:47
@IanHeng: By "dictionary" I mean a Python dictionary, where each key is a position whose value is the position that led to the given position in one move. This links the positions together--these links are the "edges" in the the theoretical graph. You may need to study Python dictionaries and graphs/networks further--you should know these concepts before tackling a problem of this difficulty. I'll not explain calculating the moves until you show more of your own programming attempts. You need to show more effort in code.
– Rory Daulton
Nov 22 at 15:49
@IanHeng: Why are you so reluctant to show more of your work? I have already written a Python program that solves your problem, and the program uses the approach I gave you. It examines3483
positions, not all of them, before reaching and printing the answer.
– Rory Daulton
Nov 23 at 1:10
add a comment |
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',
autoActivateHeartbeat: false,
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
});
}
});
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%2f53404465%2ffinding-numbers-in-a-list-of-permutations-based-on-the-position-of-one-of-the-nu%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
Here is some high-level advice on your program. I'll assume that on each move the zero will swap places with a number that is one or two slots distance from the zero. I'll also assume that you want to find the smallest number of moves that takes you from the initial list to a "desired outcome" and that the printout is to show all the positions from the starting position to the desired outcome.
If you know graph theory (also called network theory) you could easily solve your problem by performing a breadth-first search from your starting position to any desired position. The nodes in your graph are the 5,040 possible permutations, and two nodes have an edge between them if you can go from one position to the other in one move.
If you don't know graph theory, you can use this following approach. You can use two overall data structures: a queue (such as collections.deque) and a dictionary. Put the initial position into the queue. Also put it as a key in the dictionary with the value None
.
Then run a loop. In each run through the loop, remove one position from the queue. There will be at most four possible moves from this position: swap the zero with the item 2 left, 1 left, 1 right, or 2 right of the zero. (If the zero is at or near an end, the possible moves will be fewer.) For each of those moves, if the resulting position is not already in the dictionary, add it to the queue and the dictionary. The dictionary entry's value is the position you just took from the queue. If the resulting position is already in the dictionary, do nothing.
Now check if the resulting position is a "desired result". If not, continue the loop. If it is, use the dictionary to save all the moves from your desired result back to the initial position. Then print those positions in the desired order, and you are done--break the loop.
Note three things about this approach. First, if any sequence of moves reaches a desired result, this approach will print one of the shortest sequences. Second, not all permutations of the original position are generated. Permutations are generated one at a time until a desired result is reached--there is no need for more. Third, no printing is done until all the moves are done and the good moves have been picked out. This is because most moves will not prove to be useful, so we wait until we know which the useful ones are.
If you want more information, show some more work of your own. But before that, tell me if I understood the rules of your problem correctly.
Hi Rory, that was very informative. Thanks a lot! You understood it exactly, and by dictionary do you mean a record of the moves made? So essentially, the only thing I need to figure out is how the computer actually makes the moves. If the initial position is in the queue, how are the next moves made with Python? Would something like an array work with preset conditions so it can only make certain moves? Could you also please expand on the edges? Thanks
– Ian Heng
Nov 21 at 23:47
@IanHeng: By "dictionary" I mean a Python dictionary, where each key is a position whose value is the position that led to the given position in one move. This links the positions together--these links are the "edges" in the the theoretical graph. You may need to study Python dictionaries and graphs/networks further--you should know these concepts before tackling a problem of this difficulty. I'll not explain calculating the moves until you show more of your own programming attempts. You need to show more effort in code.
– Rory Daulton
Nov 22 at 15:49
@IanHeng: Why are you so reluctant to show more of your work? I have already written a Python program that solves your problem, and the program uses the approach I gave you. It examines3483
positions, not all of them, before reaching and printing the answer.
– Rory Daulton
Nov 23 at 1:10
add a comment |
Here is some high-level advice on your program. I'll assume that on each move the zero will swap places with a number that is one or two slots distance from the zero. I'll also assume that you want to find the smallest number of moves that takes you from the initial list to a "desired outcome" and that the printout is to show all the positions from the starting position to the desired outcome.
If you know graph theory (also called network theory) you could easily solve your problem by performing a breadth-first search from your starting position to any desired position. The nodes in your graph are the 5,040 possible permutations, and two nodes have an edge between them if you can go from one position to the other in one move.
If you don't know graph theory, you can use this following approach. You can use two overall data structures: a queue (such as collections.deque) and a dictionary. Put the initial position into the queue. Also put it as a key in the dictionary with the value None
.
Then run a loop. In each run through the loop, remove one position from the queue. There will be at most four possible moves from this position: swap the zero with the item 2 left, 1 left, 1 right, or 2 right of the zero. (If the zero is at or near an end, the possible moves will be fewer.) For each of those moves, if the resulting position is not already in the dictionary, add it to the queue and the dictionary. The dictionary entry's value is the position you just took from the queue. If the resulting position is already in the dictionary, do nothing.
Now check if the resulting position is a "desired result". If not, continue the loop. If it is, use the dictionary to save all the moves from your desired result back to the initial position. Then print those positions in the desired order, and you are done--break the loop.
Note three things about this approach. First, if any sequence of moves reaches a desired result, this approach will print one of the shortest sequences. Second, not all permutations of the original position are generated. Permutations are generated one at a time until a desired result is reached--there is no need for more. Third, no printing is done until all the moves are done and the good moves have been picked out. This is because most moves will not prove to be useful, so we wait until we know which the useful ones are.
If you want more information, show some more work of your own. But before that, tell me if I understood the rules of your problem correctly.
Hi Rory, that was very informative. Thanks a lot! You understood it exactly, and by dictionary do you mean a record of the moves made? So essentially, the only thing I need to figure out is how the computer actually makes the moves. If the initial position is in the queue, how are the next moves made with Python? Would something like an array work with preset conditions so it can only make certain moves? Could you also please expand on the edges? Thanks
– Ian Heng
Nov 21 at 23:47
@IanHeng: By "dictionary" I mean a Python dictionary, where each key is a position whose value is the position that led to the given position in one move. This links the positions together--these links are the "edges" in the the theoretical graph. You may need to study Python dictionaries and graphs/networks further--you should know these concepts before tackling a problem of this difficulty. I'll not explain calculating the moves until you show more of your own programming attempts. You need to show more effort in code.
– Rory Daulton
Nov 22 at 15:49
@IanHeng: Why are you so reluctant to show more of your work? I have already written a Python program that solves your problem, and the program uses the approach I gave you. It examines3483
positions, not all of them, before reaching and printing the answer.
– Rory Daulton
Nov 23 at 1:10
add a comment |
Here is some high-level advice on your program. I'll assume that on each move the zero will swap places with a number that is one or two slots distance from the zero. I'll also assume that you want to find the smallest number of moves that takes you from the initial list to a "desired outcome" and that the printout is to show all the positions from the starting position to the desired outcome.
If you know graph theory (also called network theory) you could easily solve your problem by performing a breadth-first search from your starting position to any desired position. The nodes in your graph are the 5,040 possible permutations, and two nodes have an edge between them if you can go from one position to the other in one move.
If you don't know graph theory, you can use this following approach. You can use two overall data structures: a queue (such as collections.deque) and a dictionary. Put the initial position into the queue. Also put it as a key in the dictionary with the value None
.
Then run a loop. In each run through the loop, remove one position from the queue. There will be at most four possible moves from this position: swap the zero with the item 2 left, 1 left, 1 right, or 2 right of the zero. (If the zero is at or near an end, the possible moves will be fewer.) For each of those moves, if the resulting position is not already in the dictionary, add it to the queue and the dictionary. The dictionary entry's value is the position you just took from the queue. If the resulting position is already in the dictionary, do nothing.
Now check if the resulting position is a "desired result". If not, continue the loop. If it is, use the dictionary to save all the moves from your desired result back to the initial position. Then print those positions in the desired order, and you are done--break the loop.
Note three things about this approach. First, if any sequence of moves reaches a desired result, this approach will print one of the shortest sequences. Second, not all permutations of the original position are generated. Permutations are generated one at a time until a desired result is reached--there is no need for more. Third, no printing is done until all the moves are done and the good moves have been picked out. This is because most moves will not prove to be useful, so we wait until we know which the useful ones are.
If you want more information, show some more work of your own. But before that, tell me if I understood the rules of your problem correctly.
Here is some high-level advice on your program. I'll assume that on each move the zero will swap places with a number that is one or two slots distance from the zero. I'll also assume that you want to find the smallest number of moves that takes you from the initial list to a "desired outcome" and that the printout is to show all the positions from the starting position to the desired outcome.
If you know graph theory (also called network theory) you could easily solve your problem by performing a breadth-first search from your starting position to any desired position. The nodes in your graph are the 5,040 possible permutations, and two nodes have an edge between them if you can go from one position to the other in one move.
If you don't know graph theory, you can use this following approach. You can use two overall data structures: a queue (such as collections.deque) and a dictionary. Put the initial position into the queue. Also put it as a key in the dictionary with the value None
.
Then run a loop. In each run through the loop, remove one position from the queue. There will be at most four possible moves from this position: swap the zero with the item 2 left, 1 left, 1 right, or 2 right of the zero. (If the zero is at or near an end, the possible moves will be fewer.) For each of those moves, if the resulting position is not already in the dictionary, add it to the queue and the dictionary. The dictionary entry's value is the position you just took from the queue. If the resulting position is already in the dictionary, do nothing.
Now check if the resulting position is a "desired result". If not, continue the loop. If it is, use the dictionary to save all the moves from your desired result back to the initial position. Then print those positions in the desired order, and you are done--break the loop.
Note three things about this approach. First, if any sequence of moves reaches a desired result, this approach will print one of the shortest sequences. Second, not all permutations of the original position are generated. Permutations are generated one at a time until a desired result is reached--there is no need for more. Third, no printing is done until all the moves are done and the good moves have been picked out. This is because most moves will not prove to be useful, so we wait until we know which the useful ones are.
If you want more information, show some more work of your own. But before that, tell me if I understood the rules of your problem correctly.
edited Nov 21 at 19:12
answered Nov 21 at 18:51
Rory Daulton
12.7k41930
12.7k41930
Hi Rory, that was very informative. Thanks a lot! You understood it exactly, and by dictionary do you mean a record of the moves made? So essentially, the only thing I need to figure out is how the computer actually makes the moves. If the initial position is in the queue, how are the next moves made with Python? Would something like an array work with preset conditions so it can only make certain moves? Could you also please expand on the edges? Thanks
– Ian Heng
Nov 21 at 23:47
@IanHeng: By "dictionary" I mean a Python dictionary, where each key is a position whose value is the position that led to the given position in one move. This links the positions together--these links are the "edges" in the the theoretical graph. You may need to study Python dictionaries and graphs/networks further--you should know these concepts before tackling a problem of this difficulty. I'll not explain calculating the moves until you show more of your own programming attempts. You need to show more effort in code.
– Rory Daulton
Nov 22 at 15:49
@IanHeng: Why are you so reluctant to show more of your work? I have already written a Python program that solves your problem, and the program uses the approach I gave you. It examines3483
positions, not all of them, before reaching and printing the answer.
– Rory Daulton
Nov 23 at 1:10
add a comment |
Hi Rory, that was very informative. Thanks a lot! You understood it exactly, and by dictionary do you mean a record of the moves made? So essentially, the only thing I need to figure out is how the computer actually makes the moves. If the initial position is in the queue, how are the next moves made with Python? Would something like an array work with preset conditions so it can only make certain moves? Could you also please expand on the edges? Thanks
– Ian Heng
Nov 21 at 23:47
@IanHeng: By "dictionary" I mean a Python dictionary, where each key is a position whose value is the position that led to the given position in one move. This links the positions together--these links are the "edges" in the the theoretical graph. You may need to study Python dictionaries and graphs/networks further--you should know these concepts before tackling a problem of this difficulty. I'll not explain calculating the moves until you show more of your own programming attempts. You need to show more effort in code.
– Rory Daulton
Nov 22 at 15:49
@IanHeng: Why are you so reluctant to show more of your work? I have already written a Python program that solves your problem, and the program uses the approach I gave you. It examines3483
positions, not all of them, before reaching and printing the answer.
– Rory Daulton
Nov 23 at 1:10
Hi Rory, that was very informative. Thanks a lot! You understood it exactly, and by dictionary do you mean a record of the moves made? So essentially, the only thing I need to figure out is how the computer actually makes the moves. If the initial position is in the queue, how are the next moves made with Python? Would something like an array work with preset conditions so it can only make certain moves? Could you also please expand on the edges? Thanks
– Ian Heng
Nov 21 at 23:47
Hi Rory, that was very informative. Thanks a lot! You understood it exactly, and by dictionary do you mean a record of the moves made? So essentially, the only thing I need to figure out is how the computer actually makes the moves. If the initial position is in the queue, how are the next moves made with Python? Would something like an array work with preset conditions so it can only make certain moves? Could you also please expand on the edges? Thanks
– Ian Heng
Nov 21 at 23:47
@IanHeng: By "dictionary" I mean a Python dictionary, where each key is a position whose value is the position that led to the given position in one move. This links the positions together--these links are the "edges" in the the theoretical graph. You may need to study Python dictionaries and graphs/networks further--you should know these concepts before tackling a problem of this difficulty. I'll not explain calculating the moves until you show more of your own programming attempts. You need to show more effort in code.
– Rory Daulton
Nov 22 at 15:49
@IanHeng: By "dictionary" I mean a Python dictionary, where each key is a position whose value is the position that led to the given position in one move. This links the positions together--these links are the "edges" in the the theoretical graph. You may need to study Python dictionaries and graphs/networks further--you should know these concepts before tackling a problem of this difficulty. I'll not explain calculating the moves until you show more of your own programming attempts. You need to show more effort in code.
– Rory Daulton
Nov 22 at 15:49
@IanHeng: Why are you so reluctant to show more of your work? I have already written a Python program that solves your problem, and the program uses the approach I gave you. It examines
3483
positions, not all of them, before reaching and printing the answer.– Rory Daulton
Nov 23 at 1:10
@IanHeng: Why are you so reluctant to show more of your work? I have already written a Python program that solves your problem, and the program uses the approach I gave you. It examines
3483
positions, not all of them, before reaching and printing the answer.– Rory Daulton
Nov 23 at 1:10
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%2f53404465%2ffinding-numbers-in-a-list-of-permutations-based-on-the-position-of-one-of-the-nu%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
Seems like you haven't attempted anything to extracted (output) from this list, based on the position of zeroes within the combinations. Please read through the Help Center, in particular How do I ask a good question? If you run into a specific problem, research it thoroughly, search thoroughly here, and if you're still stuck post your code and a description of the problem. Also, remember to include Minimum, Complete, Verifiable Example. People will be glad to help
– Andreas
Nov 21 at 2:27
1
You're calling your permutations
combinations
o_O– slider
Nov 21 at 2:30
What are the
combinations
supposed to do here that solves this problem explicitly? Also, it doesn't look like your editing anything inanswerList
, nor is there anybreak
condition in yourwhile
loop. One thing that can be helpful is talk yourself through the steps required to move one number, and try to execute that in code. That way you aren't trying to solve the whole problem in one go– C.Nivs
Nov 21 at 2:33
Also how is your answer represented? A list of states that lead you to your final goal? Why did you choose to compute all possible permutations of your starting list?
– slider
Nov 21 at 2:38
My understanding now since Rory's help is that the combinations are stored in order to be read by the program to make the next move. This is only the beginning of the code but the answer list would be the dictionary essentially. Thanks for the advice on the break in the while loop. All of the positions from the start to end would be printed. Yes, one number moves one or two slots into an empty slot, but they can only move in one direction from the start, not backwards. Imagine like this >>>_<<<
– Ian Heng
Nov 21 at 23:51