Cody

# Problem 44356. Code breaker, Part II: Operation Orthos

Solution 1281397

Submitted on 5 Oct 2017 by yurenchu
• Size: 59
• This is the leading solution.
This solution is locked. To view this solution, you need to provide a solution of the same size or smaller.

### Test Suite

Test Status Code Input and Output
1   Pass
% This test suite can be updated if inappropriate 'hacks' % are discovered in any submitted solutions, % so the assessment of your submission may therefore change over time.

2   Pass
x = {'qspdffe x/ pqfsbujpo', 'Psuipt jt HP!'}; s_correct.shift = uint8(1); s_correct.message = {'proceed w/ operation', 'Orthos is GO!'}; s = decode(x); %disp(' *** '); disp(s.message); disp(' *** ') assert( isequal(s.shift, s_correct.shift) ) assert( isequal(s.message{1}, s_correct.message{1}) ) assert( isequal(s.message{2}, s_correct.message{2}) ) assert( isequal(s.message, s_correct.message) ) assert( isequal(s, s_correct) ) assert( isequal(class(s.shift), 'uint8') ) assert( isequal(class(s.message), 'cell') ) assert( isequal(class(s.message{1}), 'char') )

3   Pass
x = {'Hfqq Ons ts 555-7839 FXFU.', 'twymtx nx ts mtqi', 'bfnynsl ktw dtzw fianhj', 'TUJWFYNTS UTXYUTSJI'}; s_correct.shift = uint8(5); s_correct.message = {'Call Jin on 555-7839 ASAP.', 'orthos is on hold', 'waiting for your advice', 'OPERATION POSTPONED'}; s = decode(x); %disp(' *** '); disp(s.message); disp(' *** ') assert( isequal(s.shift, s_correct.shift) ) assert( isequal(s.message, s_correct.message) )

4   Pass
x = {'Ocz kvnnrjmy rvn xcvibzy: "Knpdko" oj "Gjlzgk".', 'Jkzmvodji Kcjzids rvn npxxznnapg. Izso rdgg wz Jmocjn.', 'Edi Uvkxgzout rdgg wz gzvydib Ozvh V'}; s_correct.shift = uint8(21); s_correct.message = {'The password was changed: "Psuipt" to "Loqelp".', 'Operation Phoenix was successful. Next will be Orthos.', 'Jin Zapcletzy will be leading Team A'}; s = decode(x); assert( isequal(s.shift, s_correct.shift) ) assert( isequal(s.message, s_correct.message) )

5   Pass
x = {'bxmzzuzs r/ adftae az fdmow', 'adftae etxp oaybxqfq uz 2 iwe', 'abqdmfuaz pmfq eturfqp nmow 2 p.', 'naawe dqoquhqp. fwe Vuz' }; s_correct.shift = uint8(12); s_correct.message = {'planning f/ orthos on track', 'orthos shld complete in 2 wks', 'operation date shifted back 2 d.', 'books received. tks Jin'}; s = decode(x); assert( isequal(s.shift, s_correct.shift) ) assert( isequal(s.message, s_correct.message) )

6   Pass
x = {'Hdsffafy xgj Ghwjslagf Gjlzgk zsk gxxauasddq ugeewfuwv. Lzak oadd tw dwv tq Lwse S, kmhhgjlwv tq Lwse T sk fwwvwv.'}; s_correct.shift = uint8(18); s_correct.message = {'Planning for Operation Orthos has officially commenced. This will be led by Team A, supported by Team B as needed.'}; s = decode(x); assert( isequal(s.shift, s_correct.shift) ) assert( isequal(s.message, s_correct.message) )

7   Pass
x = {'Mncpyrgml Nfmclgv qryprcb rfpcc bywq yem. Yjj ncpqmllcj ypc rm pckygl ml bsrw slrgj Mncpyrgml Nfmclgv gq amknjcrcb.'}; s_correct.shift = nan; s_correct.message = nan; s = decode(x); assert( isnan(s.shift) ) assert( isnan(s.message) )

8   Pass
x = {'Iutloxskj vgyycuxj oy tuc "Ruwkrv".', 'Vrgt lux uxznuy ruuqy UQ, haz tkkj suxk \$\$\$', 'Ksgor Pot Fgvirkzfe ut xacqxb@sgzncuxqy.ius xk. Zakyjge', 'Zgrqkj zu Zkgs H. Ngvve zu yavvuxz uvkxgzout....', 'Xk. latjy, vry zxgtylkx zu giiuatz "YZOBKDSYX" - ZDY', 'jUT''Z cUXXE'}; s_correct.shift = uint8(6); s_correct.message = {'Confirmed password is now "Loqelp".', 'Plan for orthos looks OK, but need more \$\$\$', 'Email Jin Zapcletzy on ruwkrv@mathworks.com re. Tuesday', 'Talked to Team B. Happy to support operation....', 'Re. funds, pls transfer to account "STIVEXMSR" - TXS', 'dON''T wORRY'}; s = decode(x); assert( isequal(s.shift, s_correct.shift) ) assert( isequal(s.message, s_correct.message) )

9   Pass
% Note: The Time Trial section does not check accuracy; that is done above. % Acknowledgements % Portions of this timing test code were inspired by: % (1) Problem 937. "Rubik's Mini Cube: Solve Randomized Cube in 11 Moves or Less; Score is by Time (msec)" by Richard Zapor. % (2) Problem 2733. "Evil Number" by Jan Orwat. % (3) Feedback in comments from Peng Liu. % (4) Problem Problem 1237. "It's race time! Write a faster function than the test suite call of unique()." by Jeremy. % INITIALISE x = {'qspdffe x/ pqfsbujpo', 'Psuipt jt HP!'}; qSmall = 50; qBig = 10000; cutoffTimeBig = 10; % Maximum allowable walltime (in seconds) to run function in a loop with qBig iterations. for dummy = 1 : 20, disp(' . '); end; % *** PRELIMINARY TIMING WITH timeit *** fDecode = @() decode(x); dt_timeit = timeit( fDecode ); fprintf('APPROXIMATE time to decode %u message batches ~ %2.2f seconds, according to ''timeit''.\n\r', qBig, dt_timeit * qBig) % *** PRELIMINARY TIMING WITH SHORT LOOP *** % In case the submitted function has a lot of text output, % get an estimate based on just a few iterations % Initialise t0 = clock; % Loop for i = 1 : qSmall solution = decode( x ); end; % Compute and display elapsed time. dt = etime(clock, t0); disp(' -----=====----- ') %fprintf('Your wall time to decode %u message batches = %i seconds.\n\r', qSmall, floor(dt)) fprintf('APPROXIMATE wall time to decode %u message batches ~ %2.2f seconds, by extrapolating from %u batches.\n\r', qBig, dt * qBig / qSmall, qSmall) disp(' -----=====----- ') % *** 'OFFICIAL' TIMING *** % Re-initialise timer t0 = clock; t0_cpu = cputime; % Loop for i = 1 : qBig solution = decode( x ); end; % Compute and display elapsed time. for dummy = 1 : 20, disp(' . '); end; disp(' -----=====|||||=====----- ') dt = etime(clock, t0); fprintf('Your wall time to decode %u message batches = %2.2f seconds.\n\r', qBig, dt) dt_cpu = (cputime - t0_cpu); fprintf(' ( Your CPU time for this = %2.2f seconds. ) \n\r', dt_cpu) % Display (default) Cody size-based score. all_nodes = mtree('decode.m', '-file'); %str_nodes = mtfind(all_nodes, 'Kind', 'STRING'); %eq_nodes = mtfind(all_nodes, 'Kind', 'EQUALS'); %print_nodes = mtfind(all_nodes, 'Kind', 'PRINT'); %expr_nodes = mtfind(all_nodes, 'Kind', 'EXPR'); %size_score = count(all_nodes) + sum(str_nodes.nodesize-1) + 2*(count(expr_nodes) + count(print_nodes) - count(eq_nodes)); size_score = count(all_nodes); fprintf('Your Cody-standard size-based score = %i.\n\r', size_score) % Report revised performance score combinedScore = size_score + round(dt * 10); fprintf('Your combined score = %i.\n\r', combinedScore) disp(' -----=====|||||=====----- ') % Now disallow any candidate solutions that are TOO SLOW! if dt > cutoffTimeBig, fprintf('Sorry, your submission is TOO SLOW. It must be able to finish within %u seconds.\n\r', cutoffTimeBig) end; assert( dt <= cutoffTimeBig )

. . . . . . . . . . . . . . . . . . . . APPROXIMATE time to decode 10000 message batches ~ 6.11 seconds, according to 'timeit'. -----=====----- APPROXIMATE wall time to decode 10000 message batches ~ 7.49 seconds, by extrapolating from 50 batches. -----=====----- . . . . . . . . . . . . . . . . . . . . -----=====|||||=====----- Your wall time to decode 10000 message batches = 6.04 seconds. ( Your CPU time for this = 6.95 seconds. ) Your Cody-standard size-based score = 59. Your combined score = 119. -----=====|||||=====-----