Amazon Online Assessment (OA) - Roll Dice
Given N
dice, each with faces ranging from 1
to 6
, return the minimum number of rotations necessary for all dice to show the same face.
Note that in one rotation, you can rotate a dice to an adjacent face.
For example, you can rotate a dice showing 1
to show 2
, 3
, 4
, or 5
. To make it show 6
, you need two rotations.
Input
The input consists of the following arguments:
N
: a list of integers representing the dice, each with faces ranging from 1 to 6
Output
Return the minimum number of rotations necessary for all dice to show the same face
Examples
Example 1:
Input:
N = [6, 5, 4]
Output: 2
Example 2:
Input:
N = [6, 6, 1]
Output: 2
Example 3:
Input:
N = [6, 1, 5, 4]
Output: 3
Try It Yourself
1from typing import List
2
3def number_of_rotations(dice: List[int]) -> int:
4 return min(
5 sum(0 if d == v else 1 if d + v != 7 else 2 for d in dice)
6 for v in range(1, 7)
7 )
8
9if __name__ == "__main__":
10 dice = [int(x) for x in input().split()]
11 res = number_of_rotations(dice)
12 print(res)
13