Expected Dice

Expected Dice


For this task, you'll be playing some classic tabletop games. Or rather, you're going to be, but first you'd like to know more about the probabilities involved in dice rolling.

Many board games fall under the category of "roll and move" games. In these games, you roll some dice and move that many spaces forward. You might then move forward or backwards a number of spaces depending on which space you land on.

Given a description of the board and the dice used, you want to work out what the expected number of moves would be to reach a target square. For this task, all the boards under consideration are circular and when moving off one end you simply continue from the other. A board is represented as a list of integers. Each cell number means how many cells to move (positive for forward, negative for backwards) after landing on that cell. You always start from the first cell (index 0) and the starting and target squares will not require any further movement after landing on them.

For example: If we have one 4-sided die, the simple board [0,0,0,0] and any target square, then you have a 1/4 chance of reaching the target on every roll, so on average it will take 4 rolls. The result should be given with one digit precision as ±0.1.

Input: A number of dice to roll as an integer, the number of sides on each die as an integer, a target space as an integer and a board as a list of integers.

Output: The expected number of moves needed to reach the target space starting from space 0, correct to 1 digit after the decimal place. If it takes 33 1/3 moves, you should return 33.3. All test cases have a finite expected number of moves.


expected(1, 4, 3, [0, 0, 0, 0]) == 4.0   # On these first three, you have a 1/4 chance of reaching the target
expected(1, 4, 1, [0, 0, 0, 0]) == 4.0   # on every roll, so on average it will take 4 rolls.
expected(1, 4, 3, [0, -1, -2, 0]) == 4.0
expected(1, 4, 3, [0, 2, 1, 0]) == 1.3   # You have a 3/4 chance of reaching the exit and 1/4 chance of ending up where you started
expected(1, 6, 1, [0] * 10) == 8.6
expected(2, 6, 1, [0] * 10) == 10.2

How it is used: Some of the methods for solving this task can be used to tackle a wide variety of other problems.

1 ≤ dice_number ≤ 3
2 ≤ sides ≤ 10
0 < target < len( board )
4 ≤ len( board ) ≤ 30
board [0] == 0
board [ target ] == 0
all( board [(i + board [i]) % len( board )] == 0 for i in range(len( board )))

Invalid hot key. Each hot key should be unique and valid
Hot keys:
CheckiO Extensions

CheckiO Extensions allow you to use local files to solve missions. More info in a blog post.

In order to install CheckiO client you'll need installed Python (version at least 3.8)

Install CheckiO Client first:

pip3 install checkio_client

Configure your tool

checkio --domain=py config --key=

Sync solutions into your local folder

checkio sync

(in beta testing) Launch local server so your browser can use it and sync solution between local file end extension on the fly. (doesn't work for safari)

checkio serv -d

Alternatevly, you can install Chrome extension or FF addon

checkio install-plugin
checkio install-plugin --ff
checkio install-plugin --chromium

Read more here about other functionality that the checkio client provides. Feel free to submit an issue in case of any difficulties.

Pair Programming (Beta-version)

Welcome to Pair Programming! Engage in real-time collaboration on coding projects by starting a session and sharing the provided unique URL with friends or colleagues. This feature is perfect for joint project development, debugging, or learning new skills together. Simply click 'Start Session' to begin your collaborative coding journey!

Waiting for Pair Programming to start...

You are trying to join a pair programming session that has not started yet.

Please wait for the session creator to join.

Waiting for Pair Programming to reconnect...

It looks like the creator of the pair programming session closed the editor window.

It might happen accidentally, so that you can wait for reconnection.

<< <
> >>
exec show

Whats Next?

Free accounts will see Best CheckiO solutions with some delay.
Best Solutions will be opened in a moment
Become Awesome and Don't wait
The next stage is ""
Will be activated in
View More Solutions Random Review Solutions Go to the next mission