Fastest path on a snakes and ladders boardShortest pathway across a snakes and ladders board...
Is it a good security practice to force employees hide their employer to avoid being targeted?
Basic power tool set for Home repair and simple projects
Does anyone recognize these rockets, and their location?
Can an escape pod land on Earth from orbit and not be immediately detected?
Will users know a CardView is clickable
Why is gun control associated with the socially liberal Democratic party?
Leveraging cash for buying car
How can I detect if I'm in a subshell?
Reflecting Telescope Blind Spot?
Do legislators hold the right of legislative initiative?
Cant bend fingertip when finger is straight
Is there a term for someone whose preferred policies are a mix of Left and Right?
Are soroban (Japanese abacus) classes worth doing?
How to address players struggling with simple controls?
Can artificial satellite positions affect tides?
Can I appeal credit ding if ex-wife is responsible for paying mortgage?
Is fission/fusion to iron the most efficient way to convert mass to energy?
Jam with honey & without pectin has a saucy consistency always
How can religions without a hell discourage evil-doing?
The last tree in the Universe
How to make a villain when your PCs are villains?
For Saintsbury, which English novelists constituted the "great quartet of the mid-eighteenth century"?
Nth term of Van Eck Sequence
What should I be aware of in buying second-hand sinks and toilets?
Fastest path on a snakes and ladders board
Shortest pathway across a snakes and ladders board (Update)Optimizing puzzle game involving sequence numbers10-faced dice-rolling gameSuper2048 (Google apac Test Problem)Number of unique sequences of 3 digits, given a length is equal to sumUnicode Chess PvP with Move ValidationGame of Quarto in Python — Barebones editionBattleship homework'Snakes and Ladders' gameCalculate knight movesRazzle Dazzle simulator
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}
$begingroup$
I am not looking for a detailed review of this code as I am aware it is very inefficient. The main reason for posting this is to see if there is a better way to achieve my goal (I am certain there must be)
I have created code to find the fewest number of dice rolls that would permit someone to move from the first to last square on a snakes and ladders board. The player wins if they land on or go beyond the final square on the board. They start off the board at position -1.
My approach uses recursion so is OK for small boards. The moment the board reaches a size of 30+ it takes far too long to generate the solution.
Is there a better way to solve this problem
"""Module works out fastest way to traverse a snakes and ladders board"""
def roll_dice(position, roll_number, number_squares, snakes, ladders, list_moves=[]):
"""Roll the dice and then work out if the player can climb a ladder / has won"""
if position in ladders:
position = ladders[position]
if position >= number_squares - 1:
list_moves.append(roll_number)
return
for i in range(1, 7): #For each position roll the dice 6 times
if position + i in snakes:
continue # Forbid a dice-roll that lands on a snake
roll_dice(position + i, roll_number + 1, number_squares, snakes, ladders)
return list_moves
def minimum_moves(number_squares, snakes={}, ladders={}):
"""Returns the minimum number of moves starting from position 0 for a board of size n
snakes and ladders are both dictionaries containing the starting point as the key
and end point as the value"""
# Initialise board
# The player starts off the board at position -1
list_moves = roll_dice(-1, 0, number_squares, snakes, ladders)
print(f"Board is traversable in {min(list_moves)} moves")
if __name__ == "__main__":
NUMBER_SQUARES = 25
SNAKES = {21:0, 19:9, 14: 2, 18:5}
LADDERS = {2: 21, 4:9, 10:20, 17:23}
minimum_moves(NUMBER_SQUARES, SNAKES, LADDERS)
python algorithm time-limit-exceeded pathfinding
$endgroup$
add a comment |
$begingroup$
I am not looking for a detailed review of this code as I am aware it is very inefficient. The main reason for posting this is to see if there is a better way to achieve my goal (I am certain there must be)
I have created code to find the fewest number of dice rolls that would permit someone to move from the first to last square on a snakes and ladders board. The player wins if they land on or go beyond the final square on the board. They start off the board at position -1.
My approach uses recursion so is OK for small boards. The moment the board reaches a size of 30+ it takes far too long to generate the solution.
Is there a better way to solve this problem
"""Module works out fastest way to traverse a snakes and ladders board"""
def roll_dice(position, roll_number, number_squares, snakes, ladders, list_moves=[]):
"""Roll the dice and then work out if the player can climb a ladder / has won"""
if position in ladders:
position = ladders[position]
if position >= number_squares - 1:
list_moves.append(roll_number)
return
for i in range(1, 7): #For each position roll the dice 6 times
if position + i in snakes:
continue # Forbid a dice-roll that lands on a snake
roll_dice(position + i, roll_number + 1, number_squares, snakes, ladders)
return list_moves
def minimum_moves(number_squares, snakes={}, ladders={}):
"""Returns the minimum number of moves starting from position 0 for a board of size n
snakes and ladders are both dictionaries containing the starting point as the key
and end point as the value"""
# Initialise board
# The player starts off the board at position -1
list_moves = roll_dice(-1, 0, number_squares, snakes, ladders)
print(f"Board is traversable in {min(list_moves)} moves")
if __name__ == "__main__":
NUMBER_SQUARES = 25
SNAKES = {21:0, 19:9, 14: 2, 18:5}
LADDERS = {2: 21, 4:9, 10:20, 17:23}
minimum_moves(NUMBER_SQUARES, SNAKES, LADDERS)
python algorithm time-limit-exceeded pathfinding
$endgroup$
1
$begingroup$
Here is my solution if need be - repl.it/repls/StainedMonthlyOutput. (By the way, nice question!)
$endgroup$
– Justin
9 hours ago
$begingroup$
It's okay. I have provided a link to my first solution above if need be. I'm glad it helped you!
$endgroup$
– Justin
9 hours ago
$begingroup$
Follow-up question
$endgroup$
– 200_success
4 hours ago
add a comment |
$begingroup$
I am not looking for a detailed review of this code as I am aware it is very inefficient. The main reason for posting this is to see if there is a better way to achieve my goal (I am certain there must be)
I have created code to find the fewest number of dice rolls that would permit someone to move from the first to last square on a snakes and ladders board. The player wins if they land on or go beyond the final square on the board. They start off the board at position -1.
My approach uses recursion so is OK for small boards. The moment the board reaches a size of 30+ it takes far too long to generate the solution.
Is there a better way to solve this problem
"""Module works out fastest way to traverse a snakes and ladders board"""
def roll_dice(position, roll_number, number_squares, snakes, ladders, list_moves=[]):
"""Roll the dice and then work out if the player can climb a ladder / has won"""
if position in ladders:
position = ladders[position]
if position >= number_squares - 1:
list_moves.append(roll_number)
return
for i in range(1, 7): #For each position roll the dice 6 times
if position + i in snakes:
continue # Forbid a dice-roll that lands on a snake
roll_dice(position + i, roll_number + 1, number_squares, snakes, ladders)
return list_moves
def minimum_moves(number_squares, snakes={}, ladders={}):
"""Returns the minimum number of moves starting from position 0 for a board of size n
snakes and ladders are both dictionaries containing the starting point as the key
and end point as the value"""
# Initialise board
# The player starts off the board at position -1
list_moves = roll_dice(-1, 0, number_squares, snakes, ladders)
print(f"Board is traversable in {min(list_moves)} moves")
if __name__ == "__main__":
NUMBER_SQUARES = 25
SNAKES = {21:0, 19:9, 14: 2, 18:5}
LADDERS = {2: 21, 4:9, 10:20, 17:23}
minimum_moves(NUMBER_SQUARES, SNAKES, LADDERS)
python algorithm time-limit-exceeded pathfinding
$endgroup$
I am not looking for a detailed review of this code as I am aware it is very inefficient. The main reason for posting this is to see if there is a better way to achieve my goal (I am certain there must be)
I have created code to find the fewest number of dice rolls that would permit someone to move from the first to last square on a snakes and ladders board. The player wins if they land on or go beyond the final square on the board. They start off the board at position -1.
My approach uses recursion so is OK for small boards. The moment the board reaches a size of 30+ it takes far too long to generate the solution.
Is there a better way to solve this problem
"""Module works out fastest way to traverse a snakes and ladders board"""
def roll_dice(position, roll_number, number_squares, snakes, ladders, list_moves=[]):
"""Roll the dice and then work out if the player can climb a ladder / has won"""
if position in ladders:
position = ladders[position]
if position >= number_squares - 1:
list_moves.append(roll_number)
return
for i in range(1, 7): #For each position roll the dice 6 times
if position + i in snakes:
continue # Forbid a dice-roll that lands on a snake
roll_dice(position + i, roll_number + 1, number_squares, snakes, ladders)
return list_moves
def minimum_moves(number_squares, snakes={}, ladders={}):
"""Returns the minimum number of moves starting from position 0 for a board of size n
snakes and ladders are both dictionaries containing the starting point as the key
and end point as the value"""
# Initialise board
# The player starts off the board at position -1
list_moves = roll_dice(-1, 0, number_squares, snakes, ladders)
print(f"Board is traversable in {min(list_moves)} moves")
if __name__ == "__main__":
NUMBER_SQUARES = 25
SNAKES = {21:0, 19:9, 14: 2, 18:5}
LADDERS = {2: 21, 4:9, 10:20, 17:23}
minimum_moves(NUMBER_SQUARES, SNAKES, LADDERS)
python algorithm time-limit-exceeded pathfinding
python algorithm time-limit-exceeded pathfinding
edited 6 hours ago
200_success
133k20166438
133k20166438
asked 11 hours ago
EMLEML
61011
61011
1
$begingroup$
Here is my solution if need be - repl.it/repls/StainedMonthlyOutput. (By the way, nice question!)
$endgroup$
– Justin
9 hours ago
$begingroup$
It's okay. I have provided a link to my first solution above if need be. I'm glad it helped you!
$endgroup$
– Justin
9 hours ago
$begingroup$
Follow-up question
$endgroup$
– 200_success
4 hours ago
add a comment |
1
$begingroup$
Here is my solution if need be - repl.it/repls/StainedMonthlyOutput. (By the way, nice question!)
$endgroup$
– Justin
9 hours ago
$begingroup$
It's okay. I have provided a link to my first solution above if need be. I'm glad it helped you!
$endgroup$
– Justin
9 hours ago
$begingroup$
Follow-up question
$endgroup$
– 200_success
4 hours ago
1
1
$begingroup$
Here is my solution if need be - repl.it/repls/StainedMonthlyOutput. (By the way, nice question!)
$endgroup$
– Justin
9 hours ago
$begingroup$
Here is my solution if need be - repl.it/repls/StainedMonthlyOutput. (By the way, nice question!)
$endgroup$
– Justin
9 hours ago
$begingroup$
It's okay. I have provided a link to my first solution above if need be. I'm glad it helped you!
$endgroup$
– Justin
9 hours ago
$begingroup$
It's okay. I have provided a link to my first solution above if need be. I'm glad it helped you!
$endgroup$
– Justin
9 hours ago
$begingroup$
Follow-up question
$endgroup$
– 200_success
4 hours ago
$begingroup$
Follow-up question
$endgroup$
– 200_success
4 hours ago
add a comment |
2 Answers
2
active
oldest
votes
$begingroup$
Don't use mutable default arguments. If you need to default to a list then default to None
and then change to an empty list.
Take the following example code:
>>> def example_list_builder(value, list_=[]):
list_.append(value)
return list_
>>> example_list_builder(1)
[1]
>>> example_list_builder(2)
[1, 2]
This makes list_
a really poorly defined singleton variable, that can't be accessed globally.
Your code looks like it's something like $O(6^n)$ where $n$ is the size of the board.
You can instead make it $O(l^2)$ where $l$ is LADDERS
and SNAKES
.
To improve your code, you should remove the simulation and instead only work with the LADDERS
and SNAKES
variables.
For each snake and ladder you should traverse it, and then traverse the snake or ladder after this. You should note the distance, there are two ways to do this:
- Least tiles visited.
- Least turns taken.
For the latter, you'll have to check for collisions with other snakes and ladders.
You should take snakes into account, as the fastest path for the following is 4 turns:
NUMBER_SQUARES = 1000
LADDERS = {1: 502, 501: 999}
SNAKES = {503: 500}
$endgroup$
1
$begingroup$
Thanks for the suggestions. I will see if I can come up with a faster solution using these ideas.
$endgroup$
– EML
9 hours ago
$begingroup$
How would you code thelist_moves
then so it isn't mutable but still returns all the values to theminimum_moves
function? Thanks
$endgroup$
– EML
9 hours ago
1
$begingroup$
@EMLdef fn(value=None): if value is None: value = []; ...
ordef fn(value=None): value = value or []; ...
.
$endgroup$
– Peilonrayz
9 hours ago
$begingroup$
After implementing this, I gotValueError: min() arg is an empty sequence
. I pass the list_moves between stacks
$endgroup$
– EML
9 hours ago
add a comment |
$begingroup$
continue # Forbid a dice-roll that lands on a snake
This may prevent finding the shortest path - it's possible to imagine a board with two long ladders, where the first ladder passes the bottom of the second ladder, but a snake descends from after the top of the first ladder to before the bottom of the second ladder. Be careful: if you do follow snakes, you'll need some logic to get you out of loops.
if position >= number_squares - 1:
That could be simplified to
if position > number_squares:
if __name__ == "__main__":
This is great - good work!
What you probably want to do is to convert the board to a directed graph whose edges are straight runs of board and whose nodes are the destinations of the snakes and ladders.
It might be possible to work with a map of the board, with all the squares containing the number of moves until the end:
- Initially, all squares contain
None
. - Now work backwards from the end position, marking how many die throws are necessary to reach the end from there (the first few squares will be
1
, for example). But don't write anything at the top of a snake or bottom of a ladder. - Whenever you reach a square that's already marked, check to see if it's already the same or less than you're about to write to it; if so, then stop exploring that branch.
- If you reach the top of a ladder, you can mark the bottom square with the same number as the top square.
- If you reach the bottom of a snake, the converse applies - mark the top of it with the same number as the bottom.
- When you've completed exploring all branches, the number in the beginning square will be the minimum number of turns needed to reach the top.
It should become obvious that the distinction between snakes and ladders is unimportant here - they just have beginnings and ends, so can be combined into a single list, or just be properties of their beginning and end squares.
$endgroup$
$begingroup$
Thanks. Yes that was one of the reasons I avoided snakes all together. I could imagine an infinite loop developing and had no idea how I would avoid this
$endgroup$
– EML
5 hours ago
$begingroup$
Having said this, I think the above method is very bad. I agree that a directed graph is actually a really smart idea and I have, in fact, been trying to build one as I realised this would be a really smart way to solve the problem. Thanks for suggesting this too as it supports my hypothesis
$endgroup$
– EML
5 hours ago
1
$begingroup$
To be honest, I mentioned the directed graph and then realised that just labelling the squares is going to be easier, given that we need to deal with moves that skip over the starts of shortcuts. I'd go with the latter option.
$endgroup$
– Toby Speight
5 hours ago
$begingroup$
Thanks for this. I had basically finished my directed graph code and as this is actually something I am not used to writing, I think getting feedback on this would be most helpful
$endgroup$
– EML
4 hours ago
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: "196"
};
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: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
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%2fcodereview.stackexchange.com%2fquestions%2f222228%2ffastest-path-on-a-snakes-and-ladders-board%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
$begingroup$
Don't use mutable default arguments. If you need to default to a list then default to None
and then change to an empty list.
Take the following example code:
>>> def example_list_builder(value, list_=[]):
list_.append(value)
return list_
>>> example_list_builder(1)
[1]
>>> example_list_builder(2)
[1, 2]
This makes list_
a really poorly defined singleton variable, that can't be accessed globally.
Your code looks like it's something like $O(6^n)$ where $n$ is the size of the board.
You can instead make it $O(l^2)$ where $l$ is LADDERS
and SNAKES
.
To improve your code, you should remove the simulation and instead only work with the LADDERS
and SNAKES
variables.
For each snake and ladder you should traverse it, and then traverse the snake or ladder after this. You should note the distance, there are two ways to do this:
- Least tiles visited.
- Least turns taken.
For the latter, you'll have to check for collisions with other snakes and ladders.
You should take snakes into account, as the fastest path for the following is 4 turns:
NUMBER_SQUARES = 1000
LADDERS = {1: 502, 501: 999}
SNAKES = {503: 500}
$endgroup$
1
$begingroup$
Thanks for the suggestions. I will see if I can come up with a faster solution using these ideas.
$endgroup$
– EML
9 hours ago
$begingroup$
How would you code thelist_moves
then so it isn't mutable but still returns all the values to theminimum_moves
function? Thanks
$endgroup$
– EML
9 hours ago
1
$begingroup$
@EMLdef fn(value=None): if value is None: value = []; ...
ordef fn(value=None): value = value or []; ...
.
$endgroup$
– Peilonrayz
9 hours ago
$begingroup$
After implementing this, I gotValueError: min() arg is an empty sequence
. I pass the list_moves between stacks
$endgroup$
– EML
9 hours ago
add a comment |
$begingroup$
Don't use mutable default arguments. If you need to default to a list then default to None
and then change to an empty list.
Take the following example code:
>>> def example_list_builder(value, list_=[]):
list_.append(value)
return list_
>>> example_list_builder(1)
[1]
>>> example_list_builder(2)
[1, 2]
This makes list_
a really poorly defined singleton variable, that can't be accessed globally.
Your code looks like it's something like $O(6^n)$ where $n$ is the size of the board.
You can instead make it $O(l^2)$ where $l$ is LADDERS
and SNAKES
.
To improve your code, you should remove the simulation and instead only work with the LADDERS
and SNAKES
variables.
For each snake and ladder you should traverse it, and then traverse the snake or ladder after this. You should note the distance, there are two ways to do this:
- Least tiles visited.
- Least turns taken.
For the latter, you'll have to check for collisions with other snakes and ladders.
You should take snakes into account, as the fastest path for the following is 4 turns:
NUMBER_SQUARES = 1000
LADDERS = {1: 502, 501: 999}
SNAKES = {503: 500}
$endgroup$
1
$begingroup$
Thanks for the suggestions. I will see if I can come up with a faster solution using these ideas.
$endgroup$
– EML
9 hours ago
$begingroup$
How would you code thelist_moves
then so it isn't mutable but still returns all the values to theminimum_moves
function? Thanks
$endgroup$
– EML
9 hours ago
1
$begingroup$
@EMLdef fn(value=None): if value is None: value = []; ...
ordef fn(value=None): value = value or []; ...
.
$endgroup$
– Peilonrayz
9 hours ago
$begingroup$
After implementing this, I gotValueError: min() arg is an empty sequence
. I pass the list_moves between stacks
$endgroup$
– EML
9 hours ago
add a comment |
$begingroup$
Don't use mutable default arguments. If you need to default to a list then default to None
and then change to an empty list.
Take the following example code:
>>> def example_list_builder(value, list_=[]):
list_.append(value)
return list_
>>> example_list_builder(1)
[1]
>>> example_list_builder(2)
[1, 2]
This makes list_
a really poorly defined singleton variable, that can't be accessed globally.
Your code looks like it's something like $O(6^n)$ where $n$ is the size of the board.
You can instead make it $O(l^2)$ where $l$ is LADDERS
and SNAKES
.
To improve your code, you should remove the simulation and instead only work with the LADDERS
and SNAKES
variables.
For each snake and ladder you should traverse it, and then traverse the snake or ladder after this. You should note the distance, there are two ways to do this:
- Least tiles visited.
- Least turns taken.
For the latter, you'll have to check for collisions with other snakes and ladders.
You should take snakes into account, as the fastest path for the following is 4 turns:
NUMBER_SQUARES = 1000
LADDERS = {1: 502, 501: 999}
SNAKES = {503: 500}
$endgroup$
Don't use mutable default arguments. If you need to default to a list then default to None
and then change to an empty list.
Take the following example code:
>>> def example_list_builder(value, list_=[]):
list_.append(value)
return list_
>>> example_list_builder(1)
[1]
>>> example_list_builder(2)
[1, 2]
This makes list_
a really poorly defined singleton variable, that can't be accessed globally.
Your code looks like it's something like $O(6^n)$ where $n$ is the size of the board.
You can instead make it $O(l^2)$ where $l$ is LADDERS
and SNAKES
.
To improve your code, you should remove the simulation and instead only work with the LADDERS
and SNAKES
variables.
For each snake and ladder you should traverse it, and then traverse the snake or ladder after this. You should note the distance, there are two ways to do this:
- Least tiles visited.
- Least turns taken.
For the latter, you'll have to check for collisions with other snakes and ladders.
You should take snakes into account, as the fastest path for the following is 4 turns:
NUMBER_SQUARES = 1000
LADDERS = {1: 502, 501: 999}
SNAKES = {503: 500}
edited 12 mins ago
Chris Happy
1034
1034
answered 10 hours ago
PeilonrayzPeilonrayz
28.9k344118
28.9k344118
1
$begingroup$
Thanks for the suggestions. I will see if I can come up with a faster solution using these ideas.
$endgroup$
– EML
9 hours ago
$begingroup$
How would you code thelist_moves
then so it isn't mutable but still returns all the values to theminimum_moves
function? Thanks
$endgroup$
– EML
9 hours ago
1
$begingroup$
@EMLdef fn(value=None): if value is None: value = []; ...
ordef fn(value=None): value = value or []; ...
.
$endgroup$
– Peilonrayz
9 hours ago
$begingroup$
After implementing this, I gotValueError: min() arg is an empty sequence
. I pass the list_moves between stacks
$endgroup$
– EML
9 hours ago
add a comment |
1
$begingroup$
Thanks for the suggestions. I will see if I can come up with a faster solution using these ideas.
$endgroup$
– EML
9 hours ago
$begingroup$
How would you code thelist_moves
then so it isn't mutable but still returns all the values to theminimum_moves
function? Thanks
$endgroup$
– EML
9 hours ago
1
$begingroup$
@EMLdef fn(value=None): if value is None: value = []; ...
ordef fn(value=None): value = value or []; ...
.
$endgroup$
– Peilonrayz
9 hours ago
$begingroup$
After implementing this, I gotValueError: min() arg is an empty sequence
. I pass the list_moves between stacks
$endgroup$
– EML
9 hours ago
1
1
$begingroup$
Thanks for the suggestions. I will see if I can come up with a faster solution using these ideas.
$endgroup$
– EML
9 hours ago
$begingroup$
Thanks for the suggestions. I will see if I can come up with a faster solution using these ideas.
$endgroup$
– EML
9 hours ago
$begingroup$
How would you code the
list_moves
then so it isn't mutable but still returns all the values to the minimum_moves
function? Thanks$endgroup$
– EML
9 hours ago
$begingroup$
How would you code the
list_moves
then so it isn't mutable but still returns all the values to the minimum_moves
function? Thanks$endgroup$
– EML
9 hours ago
1
1
$begingroup$
@EML
def fn(value=None): if value is None: value = []; ...
or def fn(value=None): value = value or []; ...
.$endgroup$
– Peilonrayz
9 hours ago
$begingroup$
@EML
def fn(value=None): if value is None: value = []; ...
or def fn(value=None): value = value or []; ...
.$endgroup$
– Peilonrayz
9 hours ago
$begingroup$
After implementing this, I got
ValueError: min() arg is an empty sequence
. I pass the list_moves between stacks$endgroup$
– EML
9 hours ago
$begingroup$
After implementing this, I got
ValueError: min() arg is an empty sequence
. I pass the list_moves between stacks$endgroup$
– EML
9 hours ago
add a comment |
$begingroup$
continue # Forbid a dice-roll that lands on a snake
This may prevent finding the shortest path - it's possible to imagine a board with two long ladders, where the first ladder passes the bottom of the second ladder, but a snake descends from after the top of the first ladder to before the bottom of the second ladder. Be careful: if you do follow snakes, you'll need some logic to get you out of loops.
if position >= number_squares - 1:
That could be simplified to
if position > number_squares:
if __name__ == "__main__":
This is great - good work!
What you probably want to do is to convert the board to a directed graph whose edges are straight runs of board and whose nodes are the destinations of the snakes and ladders.
It might be possible to work with a map of the board, with all the squares containing the number of moves until the end:
- Initially, all squares contain
None
. - Now work backwards from the end position, marking how many die throws are necessary to reach the end from there (the first few squares will be
1
, for example). But don't write anything at the top of a snake or bottom of a ladder. - Whenever you reach a square that's already marked, check to see if it's already the same or less than you're about to write to it; if so, then stop exploring that branch.
- If you reach the top of a ladder, you can mark the bottom square with the same number as the top square.
- If you reach the bottom of a snake, the converse applies - mark the top of it with the same number as the bottom.
- When you've completed exploring all branches, the number in the beginning square will be the minimum number of turns needed to reach the top.
It should become obvious that the distinction between snakes and ladders is unimportant here - they just have beginnings and ends, so can be combined into a single list, or just be properties of their beginning and end squares.
$endgroup$
$begingroup$
Thanks. Yes that was one of the reasons I avoided snakes all together. I could imagine an infinite loop developing and had no idea how I would avoid this
$endgroup$
– EML
5 hours ago
$begingroup$
Having said this, I think the above method is very bad. I agree that a directed graph is actually a really smart idea and I have, in fact, been trying to build one as I realised this would be a really smart way to solve the problem. Thanks for suggesting this too as it supports my hypothesis
$endgroup$
– EML
5 hours ago
1
$begingroup$
To be honest, I mentioned the directed graph and then realised that just labelling the squares is going to be easier, given that we need to deal with moves that skip over the starts of shortcuts. I'd go with the latter option.
$endgroup$
– Toby Speight
5 hours ago
$begingroup$
Thanks for this. I had basically finished my directed graph code and as this is actually something I am not used to writing, I think getting feedback on this would be most helpful
$endgroup$
– EML
4 hours ago
add a comment |
$begingroup$
continue # Forbid a dice-roll that lands on a snake
This may prevent finding the shortest path - it's possible to imagine a board with two long ladders, where the first ladder passes the bottom of the second ladder, but a snake descends from after the top of the first ladder to before the bottom of the second ladder. Be careful: if you do follow snakes, you'll need some logic to get you out of loops.
if position >= number_squares - 1:
That could be simplified to
if position > number_squares:
if __name__ == "__main__":
This is great - good work!
What you probably want to do is to convert the board to a directed graph whose edges are straight runs of board and whose nodes are the destinations of the snakes and ladders.
It might be possible to work with a map of the board, with all the squares containing the number of moves until the end:
- Initially, all squares contain
None
. - Now work backwards from the end position, marking how many die throws are necessary to reach the end from there (the first few squares will be
1
, for example). But don't write anything at the top of a snake or bottom of a ladder. - Whenever you reach a square that's already marked, check to see if it's already the same or less than you're about to write to it; if so, then stop exploring that branch.
- If you reach the top of a ladder, you can mark the bottom square with the same number as the top square.
- If you reach the bottom of a snake, the converse applies - mark the top of it with the same number as the bottom.
- When you've completed exploring all branches, the number in the beginning square will be the minimum number of turns needed to reach the top.
It should become obvious that the distinction between snakes and ladders is unimportant here - they just have beginnings and ends, so can be combined into a single list, or just be properties of their beginning and end squares.
$endgroup$
$begingroup$
Thanks. Yes that was one of the reasons I avoided snakes all together. I could imagine an infinite loop developing and had no idea how I would avoid this
$endgroup$
– EML
5 hours ago
$begingroup$
Having said this, I think the above method is very bad. I agree that a directed graph is actually a really smart idea and I have, in fact, been trying to build one as I realised this would be a really smart way to solve the problem. Thanks for suggesting this too as it supports my hypothesis
$endgroup$
– EML
5 hours ago
1
$begingroup$
To be honest, I mentioned the directed graph and then realised that just labelling the squares is going to be easier, given that we need to deal with moves that skip over the starts of shortcuts. I'd go with the latter option.
$endgroup$
– Toby Speight
5 hours ago
$begingroup$
Thanks for this. I had basically finished my directed graph code and as this is actually something I am not used to writing, I think getting feedback on this would be most helpful
$endgroup$
– EML
4 hours ago
add a comment |
$begingroup$
continue # Forbid a dice-roll that lands on a snake
This may prevent finding the shortest path - it's possible to imagine a board with two long ladders, where the first ladder passes the bottom of the second ladder, but a snake descends from after the top of the first ladder to before the bottom of the second ladder. Be careful: if you do follow snakes, you'll need some logic to get you out of loops.
if position >= number_squares - 1:
That could be simplified to
if position > number_squares:
if __name__ == "__main__":
This is great - good work!
What you probably want to do is to convert the board to a directed graph whose edges are straight runs of board and whose nodes are the destinations of the snakes and ladders.
It might be possible to work with a map of the board, with all the squares containing the number of moves until the end:
- Initially, all squares contain
None
. - Now work backwards from the end position, marking how many die throws are necessary to reach the end from there (the first few squares will be
1
, for example). But don't write anything at the top of a snake or bottom of a ladder. - Whenever you reach a square that's already marked, check to see if it's already the same or less than you're about to write to it; if so, then stop exploring that branch.
- If you reach the top of a ladder, you can mark the bottom square with the same number as the top square.
- If you reach the bottom of a snake, the converse applies - mark the top of it with the same number as the bottom.
- When you've completed exploring all branches, the number in the beginning square will be the minimum number of turns needed to reach the top.
It should become obvious that the distinction between snakes and ladders is unimportant here - they just have beginnings and ends, so can be combined into a single list, or just be properties of their beginning and end squares.
$endgroup$
continue # Forbid a dice-roll that lands on a snake
This may prevent finding the shortest path - it's possible to imagine a board with two long ladders, where the first ladder passes the bottom of the second ladder, but a snake descends from after the top of the first ladder to before the bottom of the second ladder. Be careful: if you do follow snakes, you'll need some logic to get you out of loops.
if position >= number_squares - 1:
That could be simplified to
if position > number_squares:
if __name__ == "__main__":
This is great - good work!
What you probably want to do is to convert the board to a directed graph whose edges are straight runs of board and whose nodes are the destinations of the snakes and ladders.
It might be possible to work with a map of the board, with all the squares containing the number of moves until the end:
- Initially, all squares contain
None
. - Now work backwards from the end position, marking how many die throws are necessary to reach the end from there (the first few squares will be
1
, for example). But don't write anything at the top of a snake or bottom of a ladder. - Whenever you reach a square that's already marked, check to see if it's already the same or less than you're about to write to it; if so, then stop exploring that branch.
- If you reach the top of a ladder, you can mark the bottom square with the same number as the top square.
- If you reach the bottom of a snake, the converse applies - mark the top of it with the same number as the bottom.
- When you've completed exploring all branches, the number in the beginning square will be the minimum number of turns needed to reach the top.
It should become obvious that the distinction between snakes and ladders is unimportant here - they just have beginnings and ends, so can be combined into a single list, or just be properties of their beginning and end squares.
edited 5 hours ago
answered 5 hours ago
Toby SpeightToby Speight
29.2k743124
29.2k743124
$begingroup$
Thanks. Yes that was one of the reasons I avoided snakes all together. I could imagine an infinite loop developing and had no idea how I would avoid this
$endgroup$
– EML
5 hours ago
$begingroup$
Having said this, I think the above method is very bad. I agree that a directed graph is actually a really smart idea and I have, in fact, been trying to build one as I realised this would be a really smart way to solve the problem. Thanks for suggesting this too as it supports my hypothesis
$endgroup$
– EML
5 hours ago
1
$begingroup$
To be honest, I mentioned the directed graph and then realised that just labelling the squares is going to be easier, given that we need to deal with moves that skip over the starts of shortcuts. I'd go with the latter option.
$endgroup$
– Toby Speight
5 hours ago
$begingroup$
Thanks for this. I had basically finished my directed graph code and as this is actually something I am not used to writing, I think getting feedback on this would be most helpful
$endgroup$
– EML
4 hours ago
add a comment |
$begingroup$
Thanks. Yes that was one of the reasons I avoided snakes all together. I could imagine an infinite loop developing and had no idea how I would avoid this
$endgroup$
– EML
5 hours ago
$begingroup$
Having said this, I think the above method is very bad. I agree that a directed graph is actually a really smart idea and I have, in fact, been trying to build one as I realised this would be a really smart way to solve the problem. Thanks for suggesting this too as it supports my hypothesis
$endgroup$
– EML
5 hours ago
1
$begingroup$
To be honest, I mentioned the directed graph and then realised that just labelling the squares is going to be easier, given that we need to deal with moves that skip over the starts of shortcuts. I'd go with the latter option.
$endgroup$
– Toby Speight
5 hours ago
$begingroup$
Thanks for this. I had basically finished my directed graph code and as this is actually something I am not used to writing, I think getting feedback on this would be most helpful
$endgroup$
– EML
4 hours ago
$begingroup$
Thanks. Yes that was one of the reasons I avoided snakes all together. I could imagine an infinite loop developing and had no idea how I would avoid this
$endgroup$
– EML
5 hours ago
$begingroup$
Thanks. Yes that was one of the reasons I avoided snakes all together. I could imagine an infinite loop developing and had no idea how I would avoid this
$endgroup$
– EML
5 hours ago
$begingroup$
Having said this, I think the above method is very bad. I agree that a directed graph is actually a really smart idea and I have, in fact, been trying to build one as I realised this would be a really smart way to solve the problem. Thanks for suggesting this too as it supports my hypothesis
$endgroup$
– EML
5 hours ago
$begingroup$
Having said this, I think the above method is very bad. I agree that a directed graph is actually a really smart idea and I have, in fact, been trying to build one as I realised this would be a really smart way to solve the problem. Thanks for suggesting this too as it supports my hypothesis
$endgroup$
– EML
5 hours ago
1
1
$begingroup$
To be honest, I mentioned the directed graph and then realised that just labelling the squares is going to be easier, given that we need to deal with moves that skip over the starts of shortcuts. I'd go with the latter option.
$endgroup$
– Toby Speight
5 hours ago
$begingroup$
To be honest, I mentioned the directed graph and then realised that just labelling the squares is going to be easier, given that we need to deal with moves that skip over the starts of shortcuts. I'd go with the latter option.
$endgroup$
– Toby Speight
5 hours ago
$begingroup$
Thanks for this. I had basically finished my directed graph code and as this is actually something I am not used to writing, I think getting feedback on this would be most helpful
$endgroup$
– EML
4 hours ago
$begingroup$
Thanks for this. I had basically finished my directed graph code and as this is actually something I am not used to writing, I think getting feedback on this would be most helpful
$endgroup$
– EML
4 hours ago
add a comment |
Thanks for contributing an answer to Code Review Stack Exchange!
- 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.
Use MathJax to format equations. MathJax reference.
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%2fcodereview.stackexchange.com%2fquestions%2f222228%2ffastest-path-on-a-snakes-and-ladders-board%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
1
$begingroup$
Here is my solution if need be - repl.it/repls/StainedMonthlyOutput. (By the way, nice question!)
$endgroup$
– Justin
9 hours ago
$begingroup$
It's okay. I have provided a link to my first solution above if need be. I'm glad it helped you!
$endgroup$
– Justin
9 hours ago
$begingroup$
Follow-up question
$endgroup$
– 200_success
4 hours ago