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

 

orion.a.smith@gmail.com apcsa-encryption > apcsaDecryptRotateSort
prev  |  next  |  chance

Given an ArrayList of words extracted from a ciphertext String and an ArrayList of Integers representing operations (a.k.a. the key to this encryption technique), perform a repeated sequence of rotations, partial insertion sorts and substring operations, as follows.


Step 1: All the even-index values of ops (except for the last value) hold how many times to rotate the words ArrayList.  If the value in ops is positive do a number of right rotations, placing the last value at the beginning.  If the value in ops is negative do a number of left rotations equal to the absolute value of the negative number, placing the first value at the end.  So if the element at index 0 of ops is 2, rotate words right twice.  If the element at index 0 of ops is -4, rotate words left 4 times.

Step 2: All the odd-index values of ops hold how many passes of an insertion sort to complete, which (starting at index 1) puts the values at the beginning of words into order with respect to each other.  So if the element at index 1 of ops is 2, the first 3 elements of words will be in order relative to one another (index 0, index 1 and index 2 of words).  Precondition: the number of insertion sort passes will always be less than words.size().

Step 3: Replace all elements of words with a substring of their original values, removing a number of characters at the front equal to the number stored in the ops value.  So if the element at index 2 of ops is 4, all elements of words need to remove their first 4 characters.

If there are at least three more elements of ops, repeat steps 1-3 on them.  So index 3 of ops could hold another number of rotations, element 4 could hold another number of insertion sort passes and element 5 could hold another set of beginning characters to remove.

The last element of ops will hold the number of words to output.  Remove any words that are greater than this value.  So if the last element of ops is 4, remove all elements of words after index 3 (words will thus have 4 remaining elements).
Return the modified version of words.

apcsaDecryptRotateSort(["23contain", "05secret", "18ashier", "29tsktsking", "14congressional", "14pitmans", "09excrements", "36grassy", "05jiffs", "41broiders", "44cesspits", "28arithmetician", "38pitilessness", "09words", "45biophysics"], [3, 7, 2, 2]) → "secret words"
apcsaDecryptRotateSort(["19dryest", "06winders", "27secular", "23gadroons", "30pushpin", "29particular", "40winnings", "13words", "24reals", "20icehouses", "31illume", "37dovetails", "10secret", "37rooty", "15planetary", "05boroughs", "07clothes", "23compass", "26auctioneer", "06anesthetizing"], [14, 7, 2, 2]) → "secret words"
apcsaDecryptRotateSort(["22batty", "24cringes", "24bassetting", "09mudguards", "09actinides", "03toolmaking", "20thrice", "13godfathers", "17corporal", "22conspicuously", "12secular", "31youthen", "10words", "02secret", "22pandemic", "29lashing", "18stupendous", "30imprecations", "32stricture", "17expendable", "15younker", "23parging", "26beclothe", "20cubism"], [13, 8, 2, 2]) → "secret words"

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

public String apcsaDecryptRotateSort(ArrayList<String> words, ArrayList<Integer> ops) { }

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: 330

Copyright Nick Parlante 2017 - privacy