Cody

Problem 2954. Best Problem Elections

Solution 575253

Submitted on 6 Feb 2015 by Zikobrelli
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
% read player id and voted problem number lines = textread('vote.m', '%s'); player.id = str2num(regexp(lines{end}, '\d+', 'match', 'once')); player.vote = uint16(vote()); assignin('caller', 'score', -1); % % get web pages data % 1 - voted problem, % 2 - this problem player correct solutions, % 3 - voted problem solutions - problemexists url = 'http://www.mathworks.com/matlabcentral/cody/problems/'; url1 = sprintf('%s%d', url, player.vote); url2 = sprintf( ... '%s%d/solutions?sort=&term=player_id:%d+status:Correct', ... url, 2954, player.id); url3 = sprintf('%s%d/solutions', url, player.vote); [html1, ~] = urlread(url1); [html2, ~] = urlread(url2); [~,status] = urlread(url3); % % get voted problem creator id, name and problem description creator.id = str2num(regexp(html1, ... '(?<=Created by.*?players/)\d*', ... 'match', 'once')); creator.name = regexp(html1, ... '(?<=Created by <.*?>)[^<]*', ... 'match', 'once'); creator.description = regexp(html1, ... '(?<=Problem \d+\. )[^<>]*', ... 'match', 'once'); % % get some player previous votes data player.countVotes = numel(regexpi(html2, 'solution \d+')); player.prevDateStr = regexp(html2, ... '(?<=Submitted on )\d+ ... \d{4}', ... 'match', 'once'); if isempty(player.prevDateStr) player.prevDateNum = 0; else player.prevDateNum = datenum(player.prevDateStr); end player.thisDateNum = floor(now); player.nextDateStr = regexprep(datestr(player.prevDateNum+7, ... 'dd mmmm yyyy'), ... '^0', ''); % % check if vote is valid and manage errors if status if ~isequal(creator.id,player.id) if (player.countVotes < 10 || player.thisDateNum >= ... player.prevDateNum + 7) && isscalar(player.vote) fprintf(['Congratulations! You have voted for ' ... '"Problem %d. %s" created by %s.\n'], ... player.vote, creator.description, creator.name); assignin('caller', 'score', player.vote); else error(['You voted %d times already. \n' ... 'You can submit next vote on %s or later.'], ... player.countVotes, player.nextDateStr); end else error(['You cannot vote for problems created' ... ' by yourself. Sorry.']); end elseif player.vote error('There is no "Problem %d." on Cody yet.', player.vote); else disp('Got what you want, size 0, but one vote is gone.'); assignin('caller', 'score', 0); end % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % %

v = 0 Got what you want, size 0, but one vote is gone.

Suggested Problems

More from this Author41

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!