Cody

Solution 1633120

Submitted on 25 Sep 2018 by William
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. % The assessment of your submission may therefore change over time. global bncWordlist bncWordlist = {'the', 'of', 'and', 'a', 'in', 'to', 'it', 'is', 'to', 'was', ... 'I', 'for', 'that', 'you', 'he', 'be', 'with', 'on', 'by', 'at', ... 'have', 'are', 'not', 'this', '''s', 'but', 'had', 'they', 'his', ... 'from', 'she', 'that', 'which', 'or', 'we', '''s', 'an', 'n''t', 'were', ... 'as', 'do', 'been', 'their', 'has', 'would', 'there', 'what', 'will', 'all', ... 'if', 'can', 'her', 'said', 'who', 'one', 'so', 'up', 'as', 'them', 'some', ... 'when', 'could', 'him', 'into', 'its', 'then', 'two', 'out', 'time', ... 'my', 'about', 'did', 'your', 'now', 'me', 'no', 'other', 'only', 'just', ... 'more', 'these', 'also', 'people', 'know', 'any', 'first', 'see', 'very', 'new', ... 'may', 'well', 'should', 'her', 'like', 'than', 'how', 'get', 'way', 'one', 'our'};

2   Pass
% EDIT (2019-07-02). Anti-hacking provision % Ensure only builtin functions will be called. ! rm -v fileread.m ! rm -v assert.m % END EDIT (2019-07-02) % EDIT (2018-06-18). Anti-hacking provision RE = regexp(fileread('decode.m'), '\w+', 'match'); tabooWords = {'ans', 'assert', 'freepass', 'tic'}; testResult = cellfun( @(z) ismember(z, tabooWords), RE ); msg = ['Please do not do that in your code!' char([10 13]) ... 'Found: ' strjoin(RE(testResult)) '.' char([10 13]) ... 'Banned word.' char([10 13])]; assert(~any( cellfun( @(z) ismember(z, tabooWords), RE ) ), msg) for j = 1 : randi(10) decode({'Pizza for dinner.'}, {'mozzarella', 'mushrooms'}); end; % END EDIT (2018-06-18)

rm: cannot remove 'fileread.m': No such file or directory rm: cannot remove 'assert.m': No such file or directory

3   Pass
global bncWordlist x = {'Vomftt qvstvfe, pqfo op eppst.', 'Ffmt dbo ljmm, opu pomz xpvoe.'}; s_correct.shift = uint8(1); s_correct.message = {'Unless pursued, open no doors.', 'Eels can kill, not only wound.'}; s_correct.certitude = uint8(79); s = decode(x, bncWordlist); %disp(' *** '); disp(s.message); disp(' *** ') assert( isequal(s.shift, s_correct.shift), 'Wrong shift.' ) assert( isequal(s.message{1}, s_correct.message{1}), 'Wrong message{1}.' ) assert( isequal(s.message{2}, s_correct.message{2}), 'Wrong message{2}.' ) assert( isequal(s.message, s_correct.message), 'Wrong message.' ) assert( isequal(s.certitude, s_correct.certitude), 'Wrong certitude.' ) assert( isequal(s, s_correct), 'Wrong s.' ) assert( isequal(class(s.shift), 'uint8'), 'Wrong class.' ) assert( isequal(class(s.message), 'cell'), 'Wrong class.' ) assert( isequal(class(s.message{1}), 'char'), 'Wrong class.' ) assert( isequal(class(s.certitude), 'uint8'), 'Wrong class.' )

4   Pass
global bncWordlist x = {'Doo zduiduh lv edvhg rq ghfhswlrq.', ... 'Khqfh, zkhq deoh wr dwwdfn, zh pxvw vhhp xqdeoh; zkhq xvlqj rxu irufhv, zh pxvw vhhp lqdfwlyh; zkhq zh duh qhdu, zh pxvw pdnh wkh hqhpb eholhyh zh duh idu dzdb; zkhq idu dzdb, zh pxvw pdnh klp eholhyh zh duh qhdu.'}; s_correct.shift = uint8(3); s_correct.message = {'All warfare is based on deception.', ... 'Hence, when able to attack, we must seem unable; when using our forces, we must seem inactive; when we are near, we must make the enemy believe we are far away; when far away, we must make him believe we are near.'}; s_correct.certitude = uint8(95); s = decode(x, bncWordlist); %disp(' *** '); disp(s.message); disp(' *** ') assert( isequal(s.shift, s_correct.shift), 'Wrong shift.' ) assert( isequal(s.message, s_correct.message), 'Wrong message.' ) assert( isequal(s.certitude, s_correct.certitude), 'Wrong certitude.' )

5   Pass
global bncWordlist x = {'Elia lrq yxfqp ql bkqfzb qeb bkbjv. Cbfdk afploabo, xka zorpe efj.', ... 'Fc eb fp pbzrob xq xii mlfkqp, yb mobmxoba clo efj. Fc eb fp fk prmboflo pqobkdqe, bsxab efj.'}; s_correct.shift = uint8(23); s_correct.message = {'Hold out baits to entice the enemy. Feign disorder, and crush him.', ... 'If he is secure at all points, be prepared for him. If he is in superior strength, evade him.'}; s_correct.certitude = uint8(100); s = decode(x, bncWordlist); assert( isequal(s.shift, s_correct.shift), 'Wrong shift.' ) assert( isequal(s.message, s_correct.message), 'Wrong message.' ) assert( isequal(s.certitude, s_correct.certitude), 'Wrong certitude.' )

6   Pass
global bncWordlist x = {'Ax qgmj ghhgfwfl ak gx uzgdwjau lwehwj, kwwc lg ajjalslw zae. Hjwlwfv lg tw owsc, lzsl zw esq yjgo sjjgysfl.', ... 'Ax zw ak lscafy zak wskw, yanw zae fg jwkl.', ... 'Ax zak xgjuwk sjw mfalwv, kwhsjslw lzwe.', ... 'Sllsuc zae ozwjw zw ak mfhjwhsjwv, shhwsj ozwjw qgm sjw fgl wphwulwv.'}; s_correct.shift = uint8(18); s_correct.message = {'If your opponent is of choleric temper, seek to irritate him. Pretend to be weak, that he may grow arrogant.', ... 'If he is taking his ease, give him no rest.', ... 'If his forces are united, separate them.', ... 'Attack him where he is unprepared, appear where you are not expected.'}; s_correct.certitude = uint8(100); s = decode(x, bncWordlist); assert( isequal(s.shift, s_correct.shift), 'Wrong shift.' ) assert( isequal(s.message, s_correct.message), 'Wrong message.' ) assert( isequal(s.certitude, s_correct.certitude), 'Wrong certitude.' )

7   Pass
global bncWordlist x = {'Aes cèwo vo foxd bémyvdo vo dowzy', ... 'Yx wo dbksdo no dbkîdbo aekxn to dbksdo no vk népksdo ne csvoxmo / Vo csvoxmo ocd n''yb, wksc t''ks mryscs vk mknoxmo', ... 'Mrkaeo wyd, mrkaeo zrbkco nsdc kfom owzrkco / Pksd no Mvkeno WM, vo mywwkxny no vk zrbkco', ... 'Mkb t''cesc ex WM n''kddkaeo, ckxc dsmc, kedroxdsaeo zkc ox dym / Zbêd à pbkzzob ceb vo lokd zyeb vo wyefowoxd rsz-ryz', ... 'Ne bkz n''kddkaeo aes pbkzzo, ézkdo, wkdbkaeo od zkdkdbkaeo / Zvec no ckxq de mvkaeoc, t''cesc WM noc Mkbzkdoc', ... 'Vo zénkqyqeo ox fyqeo ke xyw no myno Cyvkkb ye Mvkeno WM / Do zbyzyco n''émyedob moms / Ae''yx ézovvo voc fyiovvoc, nèc ae''yx cyxxo voc myxcyxxoc'}; s_correct.shift = uint8([0 21]'); % TIP: The original message was not English! s_correct.message(1, :) = {'Aes cèwo vo foxd bémyvdo vo dowzy', ... 'Yx wo dbksdo no dbkîdbo aekxn to dbksdo no vk népksdo ne csvoxmo / Vo csvoxmo ocd n''yb, wksc t''ks mryscs vk mknoxmo', ... 'Mrkaeo wyd, mrkaeo zrbkco nsdc kfom owzrkco / Pksd no Mvkeno WM, vo mywwkxny no vk zrbkco', ... 'Mkb t''cesc ex WM n''kddkaeo, ckxc dsmc, kedroxdsaeo zkc ox dym / Zbêd à pbkzzob ceb vo lokd zyeb vo wyefowoxd rsz-ryz', ... 'Ne bkz n''kddkaeo aes pbkzzo, ézkdo, wkdbkaeo od zkdkdbkaeo / Zvec no ckxq de mvkaeoc, t''cesc WM noc Mkbzkdoc', ... 'Vo zénkqyqeo ox fyqeo ke xyw no myno Cyvkkb ye Mvkeno WM / Do zbyzyco n''émyedob moms / Ae''yx ézovvo voc fyiovvoc, nèc ae''yx cyxxo voc myxcyxxoc'} s_correct.message(2, :) = {'Fjx hèbt at ktci gérdait at itbed', ... 'Dc bt igpxit st igpîigt fjpcs yt igpxit st ap séupxit sj hxatcrt / At hxatcrt thi s''dg, bpxh y''px rwdxhx ap rpstcrt', ... 'Rwpfjt bdi, rwpfjt ewgpht sxih pktr tbewpht / Upxi st Rapjst BR, at rdbbpcsd st ap ewgpht', ... 'Rpg y''hjxh jc BR s''piipfjt, hpch ixrh, pjiwtcixfjt eph tc idr / Egêi à ugpeetg hjg at qtpi edjg at bdjktbtci wxe-wde', ... 'Sj gpe s''piipfjt fjx ugpeet, éepit, bpigpfjt ti epipigpfjt / Eajh st hpcv ij rapfjth, y''hjxh BR sth Rpgepith', ... 'At eéspvdvjt tc kdvjt pj cdb st rdst Hdappg dj Rapjst BR / It egdedht s''érdjitg rtrx / Fj''dc éetaat ath kdntaath, sèh fj''dc hdcct ath rdchdccth'} s_correct.certitude = uint8(11); s = decode(x, bncWordlist); assert( isequal(s.shift, s_correct.shift), 'Wrong shift.' ) assert( isequal(s.message, s_correct.message), 'Wrong message.' ) assert( isequal(s.certitude, s_correct.certitude), 'Wrong certitude.' )

s_correct = struct with fields: shift: [2×1 uint8] message: {1×6 cell} s_correct = struct with fields: shift: [2×1 uint8] message: {2×6 cell}

8   Pass
global bncWordlist x = {'Brvdujcnm mrbxamna yxbcdujcnb ynaonlc mrblryurwn; brvdujcnm onja yxbcdujcnb lxdajpn; brvdujcnm fnjtwnbb yxbcdujcnb bcanwpcq.' ... 'X mrerwn jac xo bdkcunch jwm bnlanlh!'}; s_correct.shift = uint8(9); s_correct.message = {'Simulated disorder postulates perfect discipline; simulated fear postulates courage; simulated weakness postulates strength.' ... 'O divine art of subtlety and secrecy!'}; s_correct.certitude = uint8(12); s = decode(x, bncWordlist); assert( isequal(s.shift, s_correct.shift), 'Wrong shift.' ) assert( isequal(s.message, s_correct.message), 'Wrong message.' ) assert( isequal(s.certitude, s_correct.certitude), 'Wrong certitude.' )

9   Pass
global bncWordlist x = {'Nv treefk vekvi zekf rcczretv nzky evzxysfizex gizetvj lekzc nv riv rthlrzekvu nzky kyvzi uvjzxej.', ... 'Nv riv efk wzk kf cvru re ridp fe kyv drity lecvjj nv riv wrdzczri nzky kyv wrtv fw kyv tflekip — zkj dflekrzej reu wfivjkj, zkj gzkwrccj reu givtzgztvj, zkj drijyvj reu jnrdgj.', ... 'Nv jyrcc sv lerscv kf klie erklirc rumrekrxvj kf rttflek lecvjj nv drbv ljv fw cftrc xlzuvj.'}; s_correct.shift = uint8(17); s_correct.message = {'We cannot enter into alliance with neighboring princes until we are acquainted with their designs.', ... 'We are not fit to lead an army on the march unless we are familiar with the face of the country — its mountains and forests, its pitfalls and precipices, its marshes and swamps.', ... 'We shall be unable to turn natural advantages to account unless we make use of local guides.'}; s_correct.certitude = uint8(97); s = decode(x, bncWordlist); assert( isequal(s.shift, s_correct.shift), 'Wrong shift.' ) assert( isequal(s.message, s_correct.message), 'Wrong message.' ) assert( isequal(s.certitude, s_correct.certitude), 'Wrong certitude.' )

10   Pass
global bncWordlist x = {'Oa oqvvq''u cnycau dggp: "Ctu itcvkc ctvku".'}; s_correct.shift = uint8(2); s_correct.message = {'My motto''s always been: "Ars gratia artis".'}; s_correct.certitude = uint8(73); s = decode(x, bncWordlist); assert( isequal(s.shift, s_correct.shift), 'Wrong shift.' ) assert( isequal(s.message, s_correct.message), 'Wrong message.' ) assert( isequal(s.certitude, s_correct.certitude), 'Wrong certitude.' )

11   Pass
global bncWordlist x = {'Bestow rewards without regard 2 rule; issue orders without regard 2 previous arrangements', ... '"Let advance bee richly rewarded & retreat bee heavily punished."', ... ' Qwertyuiop''asdfghjkl. Zxcvbnm-0123456789 = pass. ', ... 'αβγδ — persimon–apricot hybrid.', ... 'aIanasatbebydoheifinisitmemynoofonor''ssotoupweallandanyarebutcandidforgethadhasherhimhishowitsmaynewnotnown''toneouroutseeshethetwowaswaywhoyoualsobeenfromhaveintojustknowlikemoreonlysaidsomethanthatthemthentheythistimeverywellwerewhatwhenwillwithyouraboutcouldfirstothertheirtherethesewhichwouldpeopleshould'}; s_correct.shift = uint8(0); s_correct.message = x; s_correct.certitude = uint8(0); s = decode(x, bncWordlist); assert( isequal(s.shift, s_correct.shift), 'Wrong shift.' ) assert( isequal(s.message, s_correct.message), 'Wrong message.' ) assert( isequal(s.certitude, s_correct.certitude), 'Wrong certitude.' )

12   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 global bncWordlist x = {'Vomftt qvstvfe, pqfo op eppst.', 'Ffmt dbo ljmm, opu pomz xpvoe.'}; qSmall = 50; qBig = 1000; %cutoffTimeBig = 10; % Maximum allowable walltime (in seconds) to run function in a loop with qBig iterations. % EDIT (2018-06-17). Reduced time [slightly] to pose reasonable challenge. % Accounted for improving Cody server speed per Problem 44655. cutoffTimeBig = 8; % Maximum allowable walltime (in seconds) to run function in a loop with qBig iterations. tRef = datetime('2018-06-17', 'InputFormat','yyyy-MM-dd'); tNow = datetime('now'); yearsElapsed = (datenum(tNow) - datenum(tRef)) / 365.24; disp(' . '); fprintf('\r\n\r\n\r\nSubmission evaluated for speed on %s.\r\n', datestr(tNow, 'dd mmmm yyyy')) rInf = 0.2; delta = cutoffTimeBig - rInf; tau = 3.6; % Data from Problem 44655, based on Problem 963. qBig = ceil( qBig * (cutoffTimeBig - rInf) * exp(yearsElapsed/tau) / delta ); fprintf('\r\n\r\n\r\nTo account for computational power increases over time, number of iterations increased to %u.\r\n', qBig) % END EDIT (2018-06-17) % *** PRELIMINARY TIMING WITH timeit *** fDecode = @() decode(x, bncWordlist); dt_timeit = timeit( fDecode ); for dummy = 1 : 10, disp(' . '); end; 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, bncWordlist); end; % Compute and display elapsed time. dt = etime(clock, t0); for dummy = 1 : 10, disp(' . '); end; %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) % *** 'OFFICIAL' TIMING *** % Re-initialise timer t0 = clock; t0_cpu = cputime; % Loop for i = 1 : qBig % EDIT (2018-06-17). Ensure each case is unique. characters = [' , .' char(randi([97,122], [1,23]))]; x{2} = characters( randperm(30) ); % END EDIT (2018-06-17) solution = decode(x, bncWordlist); end; % Compute and display elapsed time. dt = etime(clock, t0); for dummy = 1 : 10, disp(' . '); end; 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'); 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, 'Exceeded time limit specified in Test Suite.' )

. Submission evaluated for speed on 03 July 2019. To account for computational power increases over time, number of iterations increased to 1337. . . . . . . . . . . APPROXIMATE time to decode 1337 message batches ~ 7.64 seconds, according to 'timeit'. . . . . . . . . . . APPROXIMATE wall time to decode 1337 message batches ~ 7.65 seconds, by extrapolating from 50 batches. . . . . . . . . . . Your wall time to decode 1337 message batches = 7.97 seconds. ( Your CPU time for this = 9.27 seconds. ) Your Cody-standard size-based score = 360. Your combined score = 440. -----=====|||||=====-----