Statistics

Problem Statement for "CombinationLock"

Problem Statement

Combination locks are convenient, because of their design. A combination lock consists of a small wheel to input the combination, and a latch to actually lock things together. The wheel contains consecutive numbers equally spaced around its edge, starting from zero and increasing clockwise. The outer casing of the lock has a small notch. To open the lock, you must rotate the wheel and align the numbers on the wheel with the notch in a specific order.

TopLock, a small company manufacturing combination locks, has recently decided to change its lock setup to give more security than the competitors. Many typical combination locks have a set of three numbers which you must input in order. TopLock locks will have a variable combination length which will be no less than three. In addition, they will be changing the number of numbers on the wheel, which will be between 10 and 100, inclusive.

To open a lock, you must follow the algorithm outlined below:

  1. 1. Set the current rotation direction to counterclockwise, and set N to the number of numbers in the combination.
  2. 2. Rotate the wheel N full turns in the current rotation direction, where a full turn is a 360 degree spin that leaves the notch pointing at the same number.
  3. 3. Continue to rotate the wheel in the current rotation direction until the notch is pointing at the next number in the combination, which successfully inputs that number. Note that by rotating the wheel counter-clockwise, the number that the notch points to will increase.
  4. 4. If there are no more numbers to input, open the lock, you're finished! Otherwise, reverse the current rotation direction, decrease N by one, and go back to step 2.

It is guaranteed that no two adjacent numbers in combo will be the same, so as to avoid ambiguity in step 3. Also, the starting position of the lock, start, will not be the same as the first number in combo.

Given a int[] combo, indicating the combination of the lock, an int size, the number of numbers on the wheel, and an int start, indicating the number that the notch starts off pointing at, your method should return a double indicating the total number of degrees turned to open the lock.

For example, if combo = {10,20,30}, size = 40, and start = 6, you would first rotate the wheel three times counterclockwise and then 4 units to the 10, for a total of 3 * 360 + 4 * (360 / 40) = 1116 degrees, and then you would rotate twice clockwise and 30 units to the 20, and then once counterclockwise and 10 units to the 30. Your method would in this case return 1116 + 990 + 450 = 2556.

Definition

Class:
CombinationLock
Method:
degreesTurned
Parameters:
int[], int, int
Returns:
double
Method signature:
double degreesTurned(int[] combo, int size, int start)
(be sure your method is public)

Notes

  • The numbers on the combination wheel start at 0 and go to (size - 1), increasing clockwise.
  • Double return values must be correct to within an absolute or relative difference of 1e-9.

Constraints

  • combo will have between 3 and 50 elements, inclusive.
  • Each element of combo will be between 0 and (size - 1), inclusive, and no two consecutive elements will be the same.
  • size will be between 10 and 100, inclusive.
  • start will be between 0 and (size - 1), inclusive, and will not be the same as the first element in combo (i.e., the one with index 0)

Examples

  1. {10,20,30}

    40

    6

    Returns: 2556.0

    Explained above.

  2. {0,50,99}

    100

    65

    Returns: 2642.4

  3. {18,74,43}

    77

    22

    Returns: 2814.5454545454545

  4. {0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9}

    10

    5

    Returns: 79344.0

  5. {64,93,87,3,22,53,64,53,11,90,11,59,30,6,11,17,72,0,38,55}

    97

    26

    Returns: 79166.59793814432

  6. {64,93,87,3,22,53,64,53,11,90,11,59,30,6,11,17,72,0,38,55}

    97

    65

    Returns: 79381.85567010309

  7. {9, 40, 79, 78, 33, 16, 11, 7, 41, 88, 6, 48, 85, 80, 88, 64, 96, 79, 66, 69, 2, 17, 8, 54}

    99

    23

    Returns: 111949.0909090909

  8. {31, 15, 57, 21, 9, 31, 20, 35, 28}

    60

    25

    Returns: 18198.0

  9. {12, 21, 87, 40, 61, 50, 3, 24, 82, 62, 43, 25, 62, 25}

    90

    36

    Returns: 40380.0

  10. {38, 7, 24, 1, 36, 16, 48, 3, 31, 2, 3, 2, 26, 48, 28, 7, 53, 60, 11, 35, 8, 42, 23, 54, 17, 46, 40, 10, 27, 38, 34, 6, 48, 60, 10, 23, 15, 58, 29, 49, 1, 32, 53}

    61

    24

    Returns: 348432.7868852459

  11. {44, 24, 57, 27, 9, 44, 75, 54, 31, 61, 65, 3, 46, 77}

    83

    70

    Returns: 40311.32530120482

  12. {7, 9, 4, 10, 6, 1, 6, 10, 3}

    11

    9

    Returns: 18065.454545454544

  13. {57, 28, 8, 6, 7, 8, 3, 1, 58, 38, 40, 2, 64, 68, 54, 33, 30, 13, 6, 30, 10, 43, 30, 42, 17, 9, 47, 4, 49, 45, 46, 36, 15, 55, 54, 22, 5, 69, 23}

    70

    42

    Returns: 288036.00000000006

  14. {25, 0, 3, 23, 5, 16, 26, 15, 8, 4, 24, 22, 5, 1, 19, 24, 12, 11, 24, 17, 1, 7}

    27

    13

    Returns: 94559.99999999999

  15. {5, 6, 1, 9, 8, 4, 0, 10, 9, 3, 6, 0}

    11

    10

    Returns: 30436.363636363636

  16. {2, 24, 32, 74, 8, 47, 76}

    85

    35

    Returns: 11181.176470588234

  17. {64, 44, 39, 10, 23, 47, 35, 30, 63, 38, 33, 70, 61}

    71

    59

    Returns: 35112.67605633802

  18. {29, 64, 9, 12, 53, 33, 11, 35, 69, 29, 41, 79, 51, 24, 5, 71, 37, 54, 9, 17, 58, 30, 47, 69, 64, 25, 46, 81}

    89

    41

    Returns: 151681.34831460673

  19. {29, 57, 22, 12, 48, 14, 49, 4, 1, 46, 42, 36, 34, 57, 54, 14, 25, 11, 60, 10, 35, 3, 65, 34, 67, 70, 29, 43, 61, 70, 53, 67, 29, 52, 5, 54, 43, 54, 58, 45, 26, 45, 6, 37, 60, 38, 51, 10}

    75

    33

    Returns: 432911.99999999994

  20. {48, 15, 29, 78, 20, 64, 9, 73, 24, 64, 30, 35, 73, 14, 7, 38, 66, 13, 28, 82, 43, 24, 21, 8, 37, 44, 75, 38, 36, 28, 45, 41, 44, 74, 75, 20, 10, 0, 10, 35, 21, 40, 26, 10, 12, 33, 4}

    84

    50

    Returns: 414180.0

  21. {2, 22, 31, 27, 2, 27, 24, 30, 5, 10, 5, 3, 19, 35, 28, 2, 19, 29, 20, 34, 19, 11, 35, 13, 19, 6, 24, 20, 7, 35, 30, 25, 19}

    38

    32

    Returns: 207918.94736842107

  22. {60, 69, 8, 35, 40, 1, 53, 29, 48}

    74

    38

    Returns: 17591.351351351354

  23. {4, 68, 28, 53, 59, 69, 38, 16, 41, 79}

    84

    59

    Returns: 21420.000000000004

  24. {60, 0, 66, 17, 4, 63, 30, 21, 20, 24, 47, 22, 9, 64}

    69

    43

    Returns: 40690.4347826087

  25. {17, 2, 21, 8, 11, 22, 7, 1}

    35

    30

    Returns: 14214.857142857145

  26. {54, 35, 16, 1, 21, 25, 39, 50, 18, 13, 31, 5, 44, 51, 31, 57, 7, 31, 27, 28, 46, 43, 12, 5, 24, 13, 10, 25, 27, 14, 17, 18, 47, 33, 17}

    63

    57

    Returns: 232965.71428571423

  27. {3, 4, 19, 46, 64, 23, 71, 55, 60, 73, 20, 25}

    77

    15

    Returns: 30389.61038961039

  28. {34, 2, 1, 7, 47, 54, 6, 9, 24, 14, 1, 29, 7, 23, 33, 22, 12, 49}

    56

    20

    Returns: 65140.71428571428

  29. {2, 22, 31, 27, 2, 27, 24, 30, 5, 10, 5, 3, 19, 35, 28, 2, 19, 29, 20, 34, 19, 11, 35, 13, 19, 6, 24, 20, 7, 35, 30, 25, 19}

    38

    32

    Returns: 207918.94736842107

  30. {60, 69, 8, 35, 40, 1, 53, 29, 48}

    74

    38

    Returns: 17591.351351351354

  31. {4, 68, 28, 53, 59, 69, 38, 16, 41, 79}

    84

    59

    Returns: 21420.000000000004

  32. {60, 0, 66, 17, 4, 63, 30, 21, 20, 24, 47, 22, 9, 64}

    69

    43

    Returns: 40690.4347826087

  33. {17, 2, 21, 8, 11, 22, 7, 1}

    35

    30

    Returns: 14214.857142857145

  34. {54, 35, 16, 1, 21, 25, 39, 50, 18, 13, 31, 5, 44, 51, 31, 57, 7, 31, 27, 28, 46, 43, 12, 5, 24, 13, 10, 25, 27, 14, 17, 18, 47, 33, 17}

    63

    57

    Returns: 232965.71428571423

  35. {5, 6, 2, 0, 8, 0, 4, 5, 6, 3, 7, 5, 10, 0, 8, 4, 2, 4, 5, 7, 9, 6, 7, 1, 4, 2, 3, 1, 3, 9, 1, 7, 4, 1, 7, 1, 2, 9, 4, 7, 0}

    11

    0

    Returns: 316505.45454545453

  36. {3, 4, 19, 46, 64, 23, 71, 55, 60, 73, 20, 25}

    77

    15

    Returns: 30389.61038961039

  37. {34, 2, 1, 7, 47, 54, 6, 9, 24, 14, 1, 29, 7, 23, 33, 22, 12, 49}

    56

    20

    Returns: 65140.71428571428

  38. { 64, 93, 87, 3, 22, 53, 64, 53, 11, 90, 11, 59, 30, 6, 11, 17, 72, 0, 38, 55 }

    97

    26

    Returns: 79166.59793814432

  39. { 0, 50, 99 }

    100

    65

    Returns: 2642.4

  40. { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }

    10

    5

    Returns: 79344.0

  41. { 10, 20, 30 }

    40

    6

    Returns: 2556.0

  42. { 64, 93, 87, 3, 22, 53, 64, 53, 11, 90, 11, 59, 30, 6, 11, 17, 72, 0, 38, 55 }

    97

    96

    Returns: 79266.80412371134


This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2024, TopCoder, Inc. All rights reserved.
This problem was used for: