Question

In: Computer Science

Imagine you have a rotary combination lock with many dials. Each dial has the digits 0...

Imagine you have a rotary combination lock with many dials. Each dial has the digits 0 - 9. At any point in time, one digit from each dial is visible.

Each dial can be rotated up or down. For some dial, if a 4 is currently visible then rotating the dial up would make 5 visible; rotating the dial down would make 3 visible. When 0 is the visible digit then rotating down would make 9 visible. Similarly, when 9 is visible then rotating up would make 0 visible.

We have devised a robotic finger to manipulate such combination systems. The robotic finger takes as input a String that indicates the operations to be performed. An L moves the finger one dial to the left, an R moves the finger one dial to the right, and a + rotates the dial that the finger is at up and a - rotates the dial that the finger is at down.

The robotic finger always starts at the leftmost dial.

Task 1: Basic Operations

Valid Sequence

Given a String that consists of (only) the characters L, R, + and -, determine if this String represents a valid sequence of operations.

A valid sequence of operations would not move the finger to the left of the leftmost dial or to the right of the rightmost dial.

public boolean validOperation(String ops, int numDials) {

Rotations

Given a sequence of operations in String form, as well as an initial arrangement of the dials, determine the digits that will be visible after the operations have been performed.

public int[] rotate(int[] initialState, String ops) {

Task 2: Finding Rotation Sequences

Given an initial state of the dials and a final state, return a String that represents a sequence of operations of shortest length that transforms the initial state to the final state.

public String getRotationSequence(int[] initialState, int[] newState) {

TEST CASE

@Test
public void testValidOp1() {
    RoboticFinger rf = new RoboticFinger();
    assertTrue(rf.validOperation("R++L++-RRR", 4));
}

@Test
public void testValidOp2() {
    RoboticFinger rf = new RoboticFinger();
    assertTrue(!rf.validOperation("R++L++-RRR", 3));
}

@Test
public void testValidOp3() {
    RoboticFinger rf = new RoboticFinger();
    assertTrue(!rf.validOperation("L-+R++RR", 10));
}

@Test
public void testValidOp4() {
    RoboticFinger rf = new RoboticFinger();
    assertTrue(!rf.validOperation("RRR+-R+R+LL", 4));
}

@Test
public void testValidOp5() {
    RoboticFinger rf = new RoboticFinger();
    assertTrue(rf.validOperation("RRR+-R+R+LL", 6));
}

@Test
public void testRotate1() {
    RoboticFinger rf = new RoboticFinger();
    int[] initialState = {1, 2, 4, 4, 5};
    String ops = "R++R-";
    int[] expectedState = {1, 4, 3, 4, 5};
    assertArrayEquals(expectedState, rf.rotate(initialState, ops));
}

@Test
public void testRotate2() {
    RoboticFinger rf = new RoboticFinger();
    int[] initialState = {1, 2, 4, 4, 5};
    String ops = "R++R-R-L++";
    int[] expectedState = {1, 4, 5, 3, 5};
    assertArrayEquals(expectedState, rf.rotate(initialState, ops));
}

@Test
public void testRotate3() {
    RoboticFinger rf = new RoboticFinger();
    int[] initialState = {7, 0, 2, 9, 1, 5, 6};
    String ops = "R-R++R+++R+RR--";
    int[] expectedState = {7, 9, 4, 2, 2, 5, 4};
    assertArrayEquals(expectedState, rf.rotate(initialState, ops));
}
@Test
public void testGetRotationSequence1() {
    RoboticFinger rf = new RoboticFinger();
    int[] initialState = {1, 2, 4, 4, 4, 7};
    int[] finalState = {2, 7, 3, 4, 8, 9};
    int expectedLength = "+R+++++R-RR++++R++".length();
    String ops = rf.getRotationSequence(initialState, finalState);
    assertArrayEquals(finalState, rf.rotate(initialState, ops));
    assertEquals(expectedLength, ops.length());
}

@Test
public void testGetRotationSequence2() {
    RoboticFinger rf = new RoboticFinger();
    int[] initialState = {9, 9, 9, 9, 9, 9, 1};
    int[] finalState = {2, 7, 3, 4, 8, 9, 2};
    int expectedLength = 22;
    String ops = rf.getRotationSequence(initialState, finalState);
    assertArrayEquals(finalState, rf.rotate(initialState, ops));
    assertEquals(expectedLength, ops.length());
}

@Test
public void testGetRotationSequence3() {
    RoboticFinger rf = new RoboticFinger();
    int[] initialState = {5};
    int[] finalState = {7};
    int expectedLength = 2;
    String ops = rf.getRotationSequence(initialState, finalState);
    assertArrayEquals(finalState, rf.rotate(initialState, ops));
    assertEquals(expectedLength, ops.length());
}

Solutions

Expert Solution

Main.java:

import java.util.Arrays;

public class Main {

    public static void main(String[] args) {

        // object to run tests on
        Robot robot = new Robot();

        // task 1
        {
            int[] initialState = {7, 0, 2, 9, 1, 5, 6};
            String ops = "R-R++R+++R+RR--";
            int[] expectedState = {7, 9, 4, 2, 2, 5, 4};
            int[] finalState = robot.rotate(initialState, ops);
            assert Arrays.equals(expectedState, finalState) : "task 1 failed";
        }

        // task 2
        {
            int[] initialState = {1, 2, 4, 4, 4, 7};
            int[] finalState = {2, 7, 3, 4, 8, 9};
            int expectedLength = "+R+++++R-RR++++R++".length();
            String rotationSequence = robot.getRotationSequence(initialState, finalState);
            assert expectedLength == rotationSequence.length() : "task 2 failed";
        }
    }
}
Robot.java:

public class Robot {
    /**
     * Determine the digits that will be visible after the operations have been performed
     *
     * @param initialState represents the state of the combination dials and {@code 0 <= initialState[i] <= 9} for all {@code 0 <= i < initialState.length}.
     * @param ops          represents the operations to perform and only contains the characters 'L', 'R', '+' and '-' or {@code ops} is empty
     * @return the new state
     */
    public int[] rotate(int[] initialState, String ops) {

        int[] finalState = initialState.clone(); // copy of initialState that will be modified and returned

        int stateID = 0; // position of robotic finger

        for(int opsID = 0; opsID < ops.length(); ++opsID) {

            char character = ops.charAt(opsID);

            switch(character) {
                case 'L':
                    --stateID; // moving left
                    break;
                case 'R':
                    ++stateID; // moving right
                    break;
                case '+':
                    finalState[stateID] = (finalState[stateID] + 1) % 10; // dialing up
                    break;
                case '-':
                    finalState[stateID] = (finalState[stateID] + 9) % 10; // dialing down
            }
        }

        return finalState;
    }

    /**
     * Transform the initial state to a given new state using the shortest sequence of operations
     *
     * @param initialState represents the initial state of the dials, and {@code 0 < initialState.length <= 10}, and {@code 0 <= initialState[i] <= 9} for all {@code 0 <= i < initialState.length}.
     * @param newState     represents the new (desired) state of the dials, and {@code 0 < initialState.length <= 10}, and {@code 0 <= initialState[i] <= 9} for all {@code 0 <= i < initialState.length}. Also {@code newState.length() == initialState.length()}.
     * @return a representation of a shortest operation sequence that ensures that the initial state is transformed to the new state.
     */
    public String getRotationSequence(int[] initialState, int[] newState) {

        StringBuilder rotationSequence = new StringBuilder(); // to store the rotation sequence

        for(int stateID = 0; stateID < initialState.length; ++stateID) {

            int initialValue = initialState[stateID];
            int newValue = newState[stateID];

            int dialUpDistance = (newValue < initialValue ? newValue + 10 : newValue) - initialValue; // number of dial ups needed at this position
            int dialDownDistance = initialValue - (newValue > initialValue ? newValue - 10 : newValue); // number of dial downs needed at this position

            // appending the moves with the minimum length
            if(dialUpDistance <= dialDownDistance) {
                rotationSequence.append(new String(new char[dialUpDistance]).replace("\0", "+"));
            } else {
                rotationSequence.append(new String(new char[dialDownDistance]).replace("\0", "-"));
            }

            // moves right
            rotationSequence.append('R');
        }

        // removing "R"s from the end if any
        while(rotationSequence.length() > 0 && rotationSequence.charAt(rotationSequence.length() - 1) == 'R') {
            rotationSequence.deleteCharAt(rotationSequence.length() - 1);
        }

        return rotationSequence.toString();
    }
}

THUMBS UP IF YOU LIKE IT !


Related Solutions

A lock consists of 2 dials, where each dial has 6 letters. What is the probability...
A lock consists of 2 dials, where each dial has 6 letters. What is the probability of guessing the right combination in one try?
A combination lock has a 1.3-cm-diameter knob that is part of the dial you turn to...
A combination lock has a 1.3-cm-diameter knob that is part of the dial you turn to unlock the lock. To turn that knob, you grip it between your thumb and forefinger with a force of 4.0 N as you twist your wrist. Suppose the coefficient of static friction between the knob and your fingers is  0.75. A)What is the most torque you can exert on the knob without having it slip between your fingers?
**java** A bicycle combination lock has four rings with numbers 0 through 9. Given the actual...
**java** A bicycle combination lock has four rings with numbers 0 through 9. Given the actual numbers and the combination to unlock, print instructions to unlock the lock using the minimum number of twists. A “twist up” increases the number value of a ring, and a “twist down” decreases it. For example, if the actual number shown is 1729 and the desired combination is 5714, write your instructions like this: Ring 1: Twist up 4 times Ring 3: Twist down...
How is a combination lock with two numbers (0 and 1) similar to bit encryption, given...
How is a combination lock with two numbers (0 and 1) similar to bit encryption, given we had the ability to add as many wheels as we wanted?
(a) A bank’s safe has 5 dials each of which can be set to a number...
(a) A bank’s safe has 5 dials each of which can be set to a number from 0 to 36 (so each dial has 37 possible settings). i. How many different ways can the 5 dials be set? ii. How many settings are there if no two dials may be set to the same number? iii. How many possible settings are there if all the numbers must be even (count 0 as even), and duplicate numbers are permitted? iv. Re-do...
1a. An ATM requires a four-digit PIN, using the digits 0-9. How many PINs have no...
1a. An ATM requires a four-digit PIN, using the digits 0-9. How many PINs have no repeated digits? 1b. How many ways can president and vice president be determined in a club with twelve members? 1c. A security team visits 12 offices each night. How many different ways can the team order its visits? 1d. In a certain lottery you select seven distinct numbers from 1 through 39, where order makes no difference. How many different ways can you make...
how many significant digits does 0.300 have?
how many significant digits does 0.300 have?
How many valid 3 digit numbers can you make using the digits 0, 1, 2 and...
How many valid 3 digit numbers can you make using the digits 0, 1, 2 and 3 without repeating the digits? How about with repeating?
Imagine that you have been working in the plant community on the Scottish moor. After many...
Imagine that you have been working in the plant community on the Scottish moor. After many field observations, you have demonstrated that plots with more diverse plant communities tend to be more highly productive. You want to test a niche complementarity model, to see if the niche complementarity model describes the reason for the positive relationship between plant diversity and production. To test this model, you plant several plots of each of the following treatments: Treatment 1: 10 plant species...
How many base 10 numbers have five digits? How many five digit numbers have no two...
How many base 10 numbers have five digits? How many five digit numbers have no two consecutive digits equal? How many have at least one pair of consecutive digits equal?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT