Question
Neo needs to break into the server room, but he has only partial information about the code that opens the door. Parts of the code
Neo needs to break into the server room, but he has only partial information about the code that opens the door. Parts of the code are known, and other parts have been narrowed down to several options, which are listed inside parentheses. He also knows the sum of all of the digits in the code. Write a recursive method that will find the correct code. If the given pattern was 1(2,34)5(3,2), then the code must start with 1 and have a 5 in the middle. The numbers in parentheses are different choices for those parts of the code. The possible codes in this case would be 1253, 1252, 13453, and 13452. If the target sum was 11, then the answer would be 1253. Some possible inputs would not lead to a unique code. In that case, return any code that adds up to the correct total. Hints: 1. Assume that the input is well-formed. You dont need to worry about patterns where the parentheses dont match up, for example. 2. Since the characters for 0-9 are in order in the ASCII standard, you can easily get the value of a character as an int by doing this: achar - 0 3. To approach this problem recursively, your code needs to make a decision, and then attempt to solve the rest of the problem. There are two key questions: How can you make the problem smaller? How can you backtrack to change your decision if it turns out to be wrong? Examples: breakCode("1", 1) "1" breakCode("1", 2) null breakCode("1(2,3)", 3) "12" breakCode("1(2,3)", 4) "13" breakCode("1(21,34)", 8) "134" breakCode("1(2,3,4)", 5) "14" breakCode("1(2,3)2(4,5)", 9) "1224" breakCode("1(2,3)2(4,5)", 11) "1325" public static String breakCode(String pattern, int target) { // insert your code here }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started