id/email
password
forgot password | create account
about | help | code help+videos | done | prefs
CodingBat code practice

 

jebbert@volusia.k12.fl.us > test2025_05_22_SL_pathRoute
prev  |  next  |  chance

Write a method that returns the "path route" from 'start' to 'target', but only if it is possible to reach the 'target' in ten or fewer steps. In each case, return an array with exactly 10 elements. To determine a "path route" you will need to replace the digits of each number in the numbers in the route with new digits based on the 'rules' array. Each element in the 'rules' array tells you what a digit gets replaced with based on the index of the array and the value in the element at that index. The index indicates what digit is being replaced and the value of that element tells you what to replace it with. For example, if 'rules' is {1,9,4,1,7,2,0,0,3,5} it means that all the 0's get replaced with 1's, 1's get replaced with 9's, 2's get replaced with 4's, etc. Also, since the value at index 8 is 3, it means that all the 8's get replaced with 3's. If 'start' is 578 and 'target' is 799 with 'rules' of {1,9,4,1,7,2,0,0,3,5} you would return {203,411,799,0,0,0,0,0,0,0}. This is because when you apply the rules to 578 you get 203. That is not the target, so you apply the rules again to 203 and this time get 411. When you apply the rules to 411 you get 799 and reach your target. You are only supposed to return the path up to the target, so you can leave the zeros in the rest of the array. Suppose the 'start' was 578 and the 'target' was 681. This time you should return an array of zeros. This is because it is not possible to reach 'target' in 10 or less steps. If you cannot reach 'target' in 10 or less steps you simply return an array containing all zeros. As preconditions you may assume that 'start' is positive and 'target' is non-negative. You may also assume that 'rules' will have exactly 10 elements and that each element will be a single digit. Look at the test data for examples.


test2025_05_22_SL_pathRoute(30, 6, [2, 5, 3, 7, 8, 1, 0, 9, 6, 4]) → [72, 93, 47, 89, 64, 8, 6, 0, 0, 0]
test2025_05_22_SL_pathRoute(30, 64, [2, 5, 3, 7, 8, 1, 0, 9, 6, 4]) → [72, 93, 47, 89, 64, 0, 0, 0, 0, 0]
test2025_05_22_SL_pathRoute(827092, 172997, [9, 8, 7, 6, 5, 4, 3, 2, 1, 9]) → [172997, 0, 0, 0, 0, 0, 0, 0, 0, 0]

...Save, Compile, Run (ctrl-enter)

public int[] test2025_05_22_SL_pathRoute(int start, int target, int[] rules) { }

Editor font size %:
Shorter output


Forget It! -- delete my code for this problem

Progress graphs:
 Your progress graph for this problem
 Random user progress graph for this problem
 Random Epic Progress Graph

Java Help

Misc Code Practice

Difficulty: 280

Copyright Nick Parlante 2017 - privacy