{"group":{"id":1,"name":"Community","lockable":false,"created_at":"2012-01-18T18:02:15.000Z","updated_at":"2025-12-14T01:33:56.000Z","description":"Problems submitted by members of the MATLAB Central community.","is_default":true,"created_by":161519,"badge_id":null,"featured":false,"trending":false,"solution_count_in_trending_period":0,"trending_last_calculated":"2025-12-14T00:00:00.000Z","image_id":null,"published":true,"community_created":false,"status_id":2,"is_default_group_for_player":false,"deleted_by":null,"deleted_at":null,"restored_by":null,"restored_at":null,"description_opc":null,"description_html":null,"published_at":null},"problems":[{"id":44545,"title":"\"Percentages may not total 100 due to rounding\"","description":"*Percentages* are commonly *rounded* when presented in tables.  As a result, the sum of the individual numbers does not always add up to 100%.  A warning is therefore sometimes appended to such tables, along the lines: _\"Percentages may not total 100 due to rounding\"_.\r\n\r\nEXAMPLE 1:\r\nA survey of eleven people for their opinion on a new policy found five to be in favour, five opposed, and one undecided/neutral.  Percentage-wise this becomes\r\n\r\n  In favour:          45% (5 of 11)\r\n  Undecided/neutral:   9% (1 of 11)  \r\n  Opposed:            45% (5 of 11)  \r\n\r\nThe total of these is 99%, rather than the expected 100%.  Despite this conflict, in this example *all of the individual numbers have been correctly entered*.  \r\n\r\nEXAMPLE 2:\r\nIn the same report, a survey of a further ten people on a different policy found four to be in favour, four opposed, and two undecided/neutral.  Suppose the data were presented in the following table\r\n\r\n  In favour:          45%\r\n  Undecided/neutral:  20%\r\n  Opposed:            45%\r\n\r\nGiven the background information, we would quickly recognise this as inconsistent, with a spurious total of 110%, rather than the expected 100%.  In fact, we would probably guess that a copy-and-paste mistake had occurred.  However, it is important to realise that even if we looked at this table alone, in isolation, without any background knowledge of the survey size or the true number of responses in any category, just by knowing that there are only three categories we can firmly conclude that in this example *one or more of the individual numbers must have been entered incorrectly*.  \r\n\r\nYOUR JOB:  \r\nGiven a list (vector) of integer percentages, determine whether among the individual values at least one of them _must_ have been incorrectly entered (return |true|), as in Example 2, or whether there might not be any incorrect entries (return |false|), as in Example 1.","description_html":"\u003cp\u003e\u003cb\u003ePercentages\u003c/b\u003e are commonly \u003cb\u003erounded\u003c/b\u003e when presented in tables.  As a result, the sum of the individual numbers does not always add up to 100%.  A warning is therefore sometimes appended to such tables, along the lines: \u003ci\u003e\"Percentages may not total 100 due to rounding\"\u003c/i\u003e.\u003c/p\u003e\u003cp\u003eEXAMPLE 1:\r\nA survey of eleven people for their opinion on a new policy found five to be in favour, five opposed, and one undecided/neutral.  Percentage-wise this becomes\u003c/p\u003e\u003cpre class=\"language-matlab\"\u003eIn favour:          45% (5 of 11)\r\nUndecided/neutral:   9% (1 of 11)  \r\nOpposed:            45% (5 of 11)  \r\n\u003c/pre\u003e\u003cp\u003eThe total of these is 99%, rather than the expected 100%.  Despite this conflict, in this example \u003cb\u003eall of the individual numbers have been correctly entered\u003c/b\u003e.\u003c/p\u003e\u003cp\u003eEXAMPLE 2:\r\nIn the same report, a survey of a further ten people on a different policy found four to be in favour, four opposed, and two undecided/neutral.  Suppose the data were presented in the following table\u003c/p\u003e\u003cpre class=\"language-matlab\"\u003eIn favour:          45%\r\nUndecided/neutral:  20%\r\nOpposed:            45%\r\n\u003c/pre\u003e\u003cp\u003eGiven the background information, we would quickly recognise this as inconsistent, with a spurious total of 110%, rather than the expected 100%.  In fact, we would probably guess that a copy-and-paste mistake had occurred.  However, it is important to realise that even if we looked at this table alone, in isolation, without any background knowledge of the survey size or the true number of responses in any category, just by knowing that there are only three categories we can firmly conclude that in this example \u003cb\u003eone or more of the individual numbers must have been entered incorrectly\u003c/b\u003e.\u003c/p\u003e\u003cp\u003eYOUR JOB:  \r\nGiven a list (vector) of integer percentages, determine whether among the individual values at least one of them \u003ci\u003emust\u003c/i\u003e have been incorrectly entered (return \u003ctt\u003etrue\u003c/tt\u003e), as in Example 2, or whether there might not be any incorrect entries (return \u003ctt\u003efalse\u003c/tt\u003e), as in Example 1.\u003c/p\u003e","function_template":"% \"May not sum to total due to rounding: the probability of rounding errors\"\r\n% Henry Bottomley, 03 June 2008\r\n% http://www.se16.info/hgb/rounding.pdf\r\n\r\n% \"How to make rounded percentages add up to 100%\"\r\n% https://stackoverflow.com/questions/13483430/how-to-make-rounded-percentages-add-up-to-100\r\n% cf. https://stackoverflow.com/questions/5227215/how-to-deal-with-the-sum-of-rounded-percentage-not-being-100\r\nfunction containsMistake = checkForMistake(z)\r\n    containsMistake = true * + false,\r\nend","test_suite":"%% Basics\r\nassessFunctionAbsence({'regexp', 'regexpi'}, 'FileName','checkForMistake.m')\r\n\r\n%% Example 1 (Row vector)\r\nxVec = round( 100*[5 1 5]/11 );\r\ncontainsMistake = false;\r\nassert( isequal(checkForMistake(xVec), containsMistake) )\r\n\r\n%% Example 1 (Column vector)\r\nxVec = round( 100*[5 1 5]/11 )';\r\ncontainsMistake = false;\r\nassert( isequal(checkForMistake(xVec), containsMistake) )\r\n\r\n%% Example 2 (Row vector)\r\nxVec = [42 20 45];\r\ncontainsMistake = true;\r\nassert( isequal(checkForMistake(xVec), containsMistake) )\r\n\r\n%% One percentage, over and under\r\nxVec = [100];\r\ncontainsMistake = false;\r\nassert( isequal(checkForMistake(xVec), containsMistake) , 'Failed test a')\r\nxVec = round([100.5]);\r\ncontainsMistake = true;\r\nassert( isequal(checkForMistake(xVec), containsMistake) , 'Failed test b')\r\nxVec = round([99.49]);\r\ncontainsMistake = true;\r\nassert( isequal(checkForMistake(xVec), containsMistake) , 'Failed test c')\r\n\r\n%% Two percentages, over and under\r\nxVec = [50 50];\r\ncontainsMistake = false;\r\nassert( isequal(checkForMistake(xVec), containsMistake) , 'Failed test a')\r\nxVec = round([49.5 50.5]);\r\ncontainsMistake = false;\r\nassert( isequal(checkForMistake(xVec), containsMistake) , 'Failed test b')\r\nxVec = round([50.5 50.5]);\r\ncontainsMistake = true;\r\nassert( isequal(checkForMistake(xVec), containsMistake) , 'Failed test c')\r\nxVec = round([49.49 50.49]);\r\ncontainsMistake = true;\r\nassert( isequal(checkForMistake(xVec), containsMistake) , 'Failed test d')\r\n\r\n%% Equal percentages\r\nfor j = [2:250 1000:1000:10000]\r\n    xVec = round( repelem(100/j, j) );\r\n    containsMistake = false;\r\n    assert( isequal(checkForMistake(xVec), containsMistake) )\r\nend;\r\n\r\n%% Geometric series (from Bottomley, §5)\r\nfor j = 10:30\r\n    xVec = round( 10 * (9/10).^[0:j] );\r\n    containsMistake = j \u003c 21;\r\n    assert( isequal(checkForMistake(xVec), containsMistake) )\r\nend;\r\n\r\n%% Geometric series (from Bottomley, §5), permuted\r\nfor j = 2:40\r\n    xVec = round( 10 * (9/10).^[0:j] );\r\n    xVec = xVec( randperm(j+1) );\r\n    containsMistake = j \u003c 21;\r\n    assert( isequal(checkForMistake(xVec), containsMistake) )\r\nend;\r\n\r\n%% Other geometric series (permuted) I\r\nfor j = 2:100\r\n    xVec = round( (99/100).^[0:j] );\r\n    xVec = xVec( randperm(j+1) );\r\n    containsMistake = j \u003c 66;\r\n    assert( isequal(checkForMistake(xVec), containsMistake) )\r\nend;\r\n\r\n%% Other geometric series (permuted) II\r\nfor j = 2:30\r\n    xVec = round( 20 * (4/5).^[0:j] );\r\n    xVec = xVec( randperm(j+1) );\r\n    containsMistake = j \u003c 12;\r\n    assert( isequal(checkForMistake(xVec), containsMistake) )\r\nend;\r\n\r\n%% Other geometric series (permuted) III\r\nfor j = 2:20\r\n    xVec = round( 25 * (3/4).^[0:j] );\r\n    xVec = xVec( randperm(j+1) );\r\n    containsMistake = j \u003c 10;\r\n    assert( isequal(checkForMistake(xVec), containsMistake) )\r\nend;\r\n\r\n%% Other geometric series (permuted) IV\r\nfor j = 2:20\r\n    xVec = round( 50 * (1/2).^[0:j] );\r\n    xVec = xVec( randperm(j+1) );\r\n    containsMistake = j \u003c 5;\r\n    assert( isequal(checkForMistake(xVec), containsMistake) )\r\nend;\r\n\r\n%% Systematic overestimation\r\nfor j = 2:100\r\n    num = randi(round(100/j)+1) - 1;\r\n    xRaw = repelem(num+0.5, j);   % cf. https://oletus.github.io/float16-simulator.js/\r\n    sm = sum(xRaw);\r\n    xRaw = [xRaw max(100-sm, 0)];\r\n    if sm \u003e 100.5,                % Not sm\u003e100, because need to account for extra zero added.\r\n        containsMistake = true;\r\n    else\r\n        containsMistake = false;\r\n    end;\r\n    xVec = round( xRaw );\r\n    xVec = xVec( randperm(j+1) );\r\n    assert( isequal(checkForMistake(xVec), containsMistake) , ['Failed with xRaw = ' num2str(xRaw)] )\r\nend;\r\n\r\n%% Systematic underestimation\r\nfor j = 2:100\r\n    num = randi(round(100/j)+1) - 1;\r\n    xRaw = repelem(num+0.499755859375, j);   % cf. https://oletus.github.io/float16-simulator.js/ \u0026 https://au.mathworks.com/help/matlab/matlab_prog/floating-point-numbers.html\r\n    cs = cumsum(xRaw);\r\n    if cs(end) \u003e 100,\r\n        xRaw( cs \u003e 100 ) = [];\r\n        containsMistake = true;\r\n    else\r\n        xRaw = [xRaw (100-cs(end))];\r\n        containsMistake = false;\r\n    end;\r\n    xVec = round( xRaw );\r\n    assert( isequal(checkForMistake(xVec), containsMistake) , ['Failed with xRaw = ' num2str(xRaw)] )\r\nend;","published":true,"deleted":false,"likes_count":0,"comments_count":0,"created_by":64439,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":3,"test_suite_updated_at":null,"rescore_all_solutions":false,"group_id":1,"created_at":"2018-03-23T08:02:20.000Z","updated_at":"2018-03-24T13:53:40.000Z","published_at":"2018-03-24T13:53:40.000Z","restored_at":null,"restored_by":null,"spam":false,"simulink":false,"admin_reviewed":false,"description_opc":"{\"relationships\":[{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/document\",\"targetMode\":\"\",\"relationshipId\":\"rId1\",\"target\":\"/matlab/document.xml\"},{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/output\",\"targetMode\":\"\",\"relationshipId\":\"rId2\",\"target\":\"/matlab/output.xml\"}],\"parts\":[{\"partUri\":\"/matlab/document.xml\",\"relationship\":[],\"contentType\":\"application/vnd.mathworks.matlab.code.document+xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\"?\u003e\\n\u003cw:document xmlns:w=\\\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\\\"\u003e\u003cw:body\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003ePercentages\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e are commonly\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003erounded\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e when presented in tables. As a result, the sum of the individual numbers does not always add up to 100%. A warning is therefore sometimes appended to such tables, along the lines:\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:i/\u003e\u003c/w:rPr\u003e\u003cw:t\u003e\\\"Percentages may not total 100 due to rounding\\\"\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eEXAMPLE 1: A survey of eleven people for their opinion on a new policy found five to be in favour, five opposed, and one undecided/neutral. Percentage-wise this becomes\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[In favour:          45% (5 of 11)\\nUndecided/neutral:   9% (1 of 11)  \\nOpposed:            45% (5 of 11)]]\u003e\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eThe total of these is 99%, rather than the expected 100%. Despite this conflict, in this example\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eall of the individual numbers have been correctly entered\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eEXAMPLE 2: In the same report, a survey of a further ten people on a different policy found four to be in favour, four opposed, and two undecided/neutral. Suppose the data were presented in the following table\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[In favour:          45%\\nUndecided/neutral:  20%\\nOpposed:            45%]]\u003e\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eGiven the background information, we would quickly recognise this as inconsistent, with a spurious total of 110%, rather than the expected 100%. In fact, we would probably guess that a copy-and-paste mistake had occurred. However, it is important to realise that even if we looked at this table alone, in isolation, without any background knowledge of the survey size or the true number of responses in any category, just by knowing that there are only three categories we can firmly conclude that in this example\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eone or more of the individual numbers must have been entered incorrectly\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eYOUR JOB: Given a list (vector) of integer percentages, determine whether among the individual values at least one of them\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:i/\u003e\u003c/w:rPr\u003e\u003cw:t\u003emust\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e have been incorrectly entered (return\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:rFonts w:cs=\\\"monospace\\\"/\u003e\u003c/w:rPr\u003e\u003cw:t\u003etrue\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e), as in Example 2, or whether there might not be any incorrect entries (return\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:rFonts w:cs=\\\"monospace\\\"/\u003e\u003c/w:rPr\u003e\u003cw:t\u003efalse\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e), as in Example 1.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003c/w:body\u003e\u003c/w:document\u003e\"},{\"partUri\":\"/matlab/output.xml\",\"contentType\":\"text/xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\" standalone=\\\"no\\\" ?\u003e\u003cembeddedOutputs\u003e\u003cmetaData\u003e\u003cevaluationState\u003emanual\u003c/evaluationState\u003e\u003clayoutState\u003ecode\u003c/layoutState\u003e\u003coutputStatus\u003eready\u003c/outputStatus\u003e\u003c/metaData\u003e\u003coutputArray type=\\\"array\\\"/\u003e\u003cregionArray type=\\\"array\\\"/\u003e\u003c/embeddedOutputs\u003e\"}]}"}],"problem_search":{"errors":[],"problems":[{"id":44545,"title":"\"Percentages may not total 100 due to rounding\"","description":"*Percentages* are commonly *rounded* when presented in tables.  As a result, the sum of the individual numbers does not always add up to 100%.  A warning is therefore sometimes appended to such tables, along the lines: _\"Percentages may not total 100 due to rounding\"_.\r\n\r\nEXAMPLE 1:\r\nA survey of eleven people for their opinion on a new policy found five to be in favour, five opposed, and one undecided/neutral.  Percentage-wise this becomes\r\n\r\n  In favour:          45% (5 of 11)\r\n  Undecided/neutral:   9% (1 of 11)  \r\n  Opposed:            45% (5 of 11)  \r\n\r\nThe total of these is 99%, rather than the expected 100%.  Despite this conflict, in this example *all of the individual numbers have been correctly entered*.  \r\n\r\nEXAMPLE 2:\r\nIn the same report, a survey of a further ten people on a different policy found four to be in favour, four opposed, and two undecided/neutral.  Suppose the data were presented in the following table\r\n\r\n  In favour:          45%\r\n  Undecided/neutral:  20%\r\n  Opposed:            45%\r\n\r\nGiven the background information, we would quickly recognise this as inconsistent, with a spurious total of 110%, rather than the expected 100%.  In fact, we would probably guess that a copy-and-paste mistake had occurred.  However, it is important to realise that even if we looked at this table alone, in isolation, without any background knowledge of the survey size or the true number of responses in any category, just by knowing that there are only three categories we can firmly conclude that in this example *one or more of the individual numbers must have been entered incorrectly*.  \r\n\r\nYOUR JOB:  \r\nGiven a list (vector) of integer percentages, determine whether among the individual values at least one of them _must_ have been incorrectly entered (return |true|), as in Example 2, or whether there might not be any incorrect entries (return |false|), as in Example 1.","description_html":"\u003cp\u003e\u003cb\u003ePercentages\u003c/b\u003e are commonly \u003cb\u003erounded\u003c/b\u003e when presented in tables.  As a result, the sum of the individual numbers does not always add up to 100%.  A warning is therefore sometimes appended to such tables, along the lines: \u003ci\u003e\"Percentages may not total 100 due to rounding\"\u003c/i\u003e.\u003c/p\u003e\u003cp\u003eEXAMPLE 1:\r\nA survey of eleven people for their opinion on a new policy found five to be in favour, five opposed, and one undecided/neutral.  Percentage-wise this becomes\u003c/p\u003e\u003cpre class=\"language-matlab\"\u003eIn favour:          45% (5 of 11)\r\nUndecided/neutral:   9% (1 of 11)  \r\nOpposed:            45% (5 of 11)  \r\n\u003c/pre\u003e\u003cp\u003eThe total of these is 99%, rather than the expected 100%.  Despite this conflict, in this example \u003cb\u003eall of the individual numbers have been correctly entered\u003c/b\u003e.\u003c/p\u003e\u003cp\u003eEXAMPLE 2:\r\nIn the same report, a survey of a further ten people on a different policy found four to be in favour, four opposed, and two undecided/neutral.  Suppose the data were presented in the following table\u003c/p\u003e\u003cpre class=\"language-matlab\"\u003eIn favour:          45%\r\nUndecided/neutral:  20%\r\nOpposed:            45%\r\n\u003c/pre\u003e\u003cp\u003eGiven the background information, we would quickly recognise this as inconsistent, with a spurious total of 110%, rather than the expected 100%.  In fact, we would probably guess that a copy-and-paste mistake had occurred.  However, it is important to realise that even if we looked at this table alone, in isolation, without any background knowledge of the survey size or the true number of responses in any category, just by knowing that there are only three categories we can firmly conclude that in this example \u003cb\u003eone or more of the individual numbers must have been entered incorrectly\u003c/b\u003e.\u003c/p\u003e\u003cp\u003eYOUR JOB:  \r\nGiven a list (vector) of integer percentages, determine whether among the individual values at least one of them \u003ci\u003emust\u003c/i\u003e have been incorrectly entered (return \u003ctt\u003etrue\u003c/tt\u003e), as in Example 2, or whether there might not be any incorrect entries (return \u003ctt\u003efalse\u003c/tt\u003e), as in Example 1.\u003c/p\u003e","function_template":"% \"May not sum to total due to rounding: the probability of rounding errors\"\r\n% Henry Bottomley, 03 June 2008\r\n% http://www.se16.info/hgb/rounding.pdf\r\n\r\n% \"How to make rounded percentages add up to 100%\"\r\n% https://stackoverflow.com/questions/13483430/how-to-make-rounded-percentages-add-up-to-100\r\n% cf. https://stackoverflow.com/questions/5227215/how-to-deal-with-the-sum-of-rounded-percentage-not-being-100\r\nfunction containsMistake = checkForMistake(z)\r\n    containsMistake = true * + false,\r\nend","test_suite":"%% Basics\r\nassessFunctionAbsence({'regexp', 'regexpi'}, 'FileName','checkForMistake.m')\r\n\r\n%% Example 1 (Row vector)\r\nxVec = round( 100*[5 1 5]/11 );\r\ncontainsMistake = false;\r\nassert( isequal(checkForMistake(xVec), containsMistake) )\r\n\r\n%% Example 1 (Column vector)\r\nxVec = round( 100*[5 1 5]/11 )';\r\ncontainsMistake = false;\r\nassert( isequal(checkForMistake(xVec), containsMistake) )\r\n\r\n%% Example 2 (Row vector)\r\nxVec = [42 20 45];\r\ncontainsMistake = true;\r\nassert( isequal(checkForMistake(xVec), containsMistake) )\r\n\r\n%% One percentage, over and under\r\nxVec = [100];\r\ncontainsMistake = false;\r\nassert( isequal(checkForMistake(xVec), containsMistake) , 'Failed test a')\r\nxVec = round([100.5]);\r\ncontainsMistake = true;\r\nassert( isequal(checkForMistake(xVec), containsMistake) , 'Failed test b')\r\nxVec = round([99.49]);\r\ncontainsMistake = true;\r\nassert( isequal(checkForMistake(xVec), containsMistake) , 'Failed test c')\r\n\r\n%% Two percentages, over and under\r\nxVec = [50 50];\r\ncontainsMistake = false;\r\nassert( isequal(checkForMistake(xVec), containsMistake) , 'Failed test a')\r\nxVec = round([49.5 50.5]);\r\ncontainsMistake = false;\r\nassert( isequal(checkForMistake(xVec), containsMistake) , 'Failed test b')\r\nxVec = round([50.5 50.5]);\r\ncontainsMistake = true;\r\nassert( isequal(checkForMistake(xVec), containsMistake) , 'Failed test c')\r\nxVec = round([49.49 50.49]);\r\ncontainsMistake = true;\r\nassert( isequal(checkForMistake(xVec), containsMistake) , 'Failed test d')\r\n\r\n%% Equal percentages\r\nfor j = [2:250 1000:1000:10000]\r\n    xVec = round( repelem(100/j, j) );\r\n    containsMistake = false;\r\n    assert( isequal(checkForMistake(xVec), containsMistake) )\r\nend;\r\n\r\n%% Geometric series (from Bottomley, §5)\r\nfor j = 10:30\r\n    xVec = round( 10 * (9/10).^[0:j] );\r\n    containsMistake = j \u003c 21;\r\n    assert( isequal(checkForMistake(xVec), containsMistake) )\r\nend;\r\n\r\n%% Geometric series (from Bottomley, §5), permuted\r\nfor j = 2:40\r\n    xVec = round( 10 * (9/10).^[0:j] );\r\n    xVec = xVec( randperm(j+1) );\r\n    containsMistake = j \u003c 21;\r\n    assert( isequal(checkForMistake(xVec), containsMistake) )\r\nend;\r\n\r\n%% Other geometric series (permuted) I\r\nfor j = 2:100\r\n    xVec = round( (99/100).^[0:j] );\r\n    xVec = xVec( randperm(j+1) );\r\n    containsMistake = j \u003c 66;\r\n    assert( isequal(checkForMistake(xVec), containsMistake) )\r\nend;\r\n\r\n%% Other geometric series (permuted) II\r\nfor j = 2:30\r\n    xVec = round( 20 * (4/5).^[0:j] );\r\n    xVec = xVec( randperm(j+1) );\r\n    containsMistake = j \u003c 12;\r\n    assert( isequal(checkForMistake(xVec), containsMistake) )\r\nend;\r\n\r\n%% Other geometric series (permuted) III\r\nfor j = 2:20\r\n    xVec = round( 25 * (3/4).^[0:j] );\r\n    xVec = xVec( randperm(j+1) );\r\n    containsMistake = j \u003c 10;\r\n    assert( isequal(checkForMistake(xVec), containsMistake) )\r\nend;\r\n\r\n%% Other geometric series (permuted) IV\r\nfor j = 2:20\r\n    xVec = round( 50 * (1/2).^[0:j] );\r\n    xVec = xVec( randperm(j+1) );\r\n    containsMistake = j \u003c 5;\r\n    assert( isequal(checkForMistake(xVec), containsMistake) )\r\nend;\r\n\r\n%% Systematic overestimation\r\nfor j = 2:100\r\n    num = randi(round(100/j)+1) - 1;\r\n    xRaw = repelem(num+0.5, j);   % cf. https://oletus.github.io/float16-simulator.js/\r\n    sm = sum(xRaw);\r\n    xRaw = [xRaw max(100-sm, 0)];\r\n    if sm \u003e 100.5,                % Not sm\u003e100, because need to account for extra zero added.\r\n        containsMistake = true;\r\n    else\r\n        containsMistake = false;\r\n    end;\r\n    xVec = round( xRaw );\r\n    xVec = xVec( randperm(j+1) );\r\n    assert( isequal(checkForMistake(xVec), containsMistake) , ['Failed with xRaw = ' num2str(xRaw)] )\r\nend;\r\n\r\n%% Systematic underestimation\r\nfor j = 2:100\r\n    num = randi(round(100/j)+1) - 1;\r\n    xRaw = repelem(num+0.499755859375, j);   % cf. https://oletus.github.io/float16-simulator.js/ \u0026 https://au.mathworks.com/help/matlab/matlab_prog/floating-point-numbers.html\r\n    cs = cumsum(xRaw);\r\n    if cs(end) \u003e 100,\r\n        xRaw( cs \u003e 100 ) = [];\r\n        containsMistake = true;\r\n    else\r\n        xRaw = [xRaw (100-cs(end))];\r\n        containsMistake = false;\r\n    end;\r\n    xVec = round( xRaw );\r\n    assert( isequal(checkForMistake(xVec), containsMistake) , ['Failed with xRaw = ' num2str(xRaw)] )\r\nend;","published":true,"deleted":false,"likes_count":0,"comments_count":0,"created_by":64439,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":3,"test_suite_updated_at":null,"rescore_all_solutions":false,"group_id":1,"created_at":"2018-03-23T08:02:20.000Z","updated_at":"2018-03-24T13:53:40.000Z","published_at":"2018-03-24T13:53:40.000Z","restored_at":null,"restored_by":null,"spam":false,"simulink":false,"admin_reviewed":false,"description_opc":"{\"relationships\":[{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/document\",\"targetMode\":\"\",\"relationshipId\":\"rId1\",\"target\":\"/matlab/document.xml\"},{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/output\",\"targetMode\":\"\",\"relationshipId\":\"rId2\",\"target\":\"/matlab/output.xml\"}],\"parts\":[{\"partUri\":\"/matlab/document.xml\",\"relationship\":[],\"contentType\":\"application/vnd.mathworks.matlab.code.document+xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\"?\u003e\\n\u003cw:document xmlns:w=\\\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\\\"\u003e\u003cw:body\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003ePercentages\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e are commonly\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003erounded\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e when presented in tables. As a result, the sum of the individual numbers does not always add up to 100%. A warning is therefore sometimes appended to such tables, along the lines:\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:i/\u003e\u003c/w:rPr\u003e\u003cw:t\u003e\\\"Percentages may not total 100 due to rounding\\\"\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eEXAMPLE 1: A survey of eleven people for their opinion on a new policy found five to be in favour, five opposed, and one undecided/neutral. Percentage-wise this becomes\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[In favour:          45% (5 of 11)\\nUndecided/neutral:   9% (1 of 11)  \\nOpposed:            45% (5 of 11)]]\u003e\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eThe total of these is 99%, rather than the expected 100%. Despite this conflict, in this example\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eall of the individual numbers have been correctly entered\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eEXAMPLE 2: In the same report, a survey of a further ten people on a different policy found four to be in favour, four opposed, and two undecided/neutral. Suppose the data were presented in the following table\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[In favour:          45%\\nUndecided/neutral:  20%\\nOpposed:            45%]]\u003e\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eGiven the background information, we would quickly recognise this as inconsistent, with a spurious total of 110%, rather than the expected 100%. In fact, we would probably guess that a copy-and-paste mistake had occurred. However, it is important to realise that even if we looked at this table alone, in isolation, without any background knowledge of the survey size or the true number of responses in any category, just by knowing that there are only three categories we can firmly conclude that in this example\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eone or more of the individual numbers must have been entered incorrectly\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eYOUR JOB: Given a list (vector) of integer percentages, determine whether among the individual values at least one of them\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:i/\u003e\u003c/w:rPr\u003e\u003cw:t\u003emust\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e have been incorrectly entered (return\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:rFonts w:cs=\\\"monospace\\\"/\u003e\u003c/w:rPr\u003e\u003cw:t\u003etrue\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e), as in Example 2, or whether there might not be any incorrect entries (return\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:rFonts w:cs=\\\"monospace\\\"/\u003e\u003c/w:rPr\u003e\u003cw:t\u003efalse\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e), as in Example 1.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003c/w:body\u003e\u003c/w:document\u003e\"},{\"partUri\":\"/matlab/output.xml\",\"contentType\":\"text/xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\" standalone=\\\"no\\\" ?\u003e\u003cembeddedOutputs\u003e\u003cmetaData\u003e\u003cevaluationState\u003emanual\u003c/evaluationState\u003e\u003clayoutState\u003ecode\u003c/layoutState\u003e\u003coutputStatus\u003eready\u003c/outputStatus\u003e\u003c/metaData\u003e\u003coutputArray type=\\\"array\\\"/\u003e\u003cregionArray type=\\\"array\\\"/\u003e\u003c/embeddedOutputs\u003e\"}]}"}],"term":"tag:\"quality control\"","current_player_id":null,"fields":[{"name":"page","type":"integer","callback":null,"default":1,"directive":null,"facet":null,"facet_method":"and","operator":null,"param":null,"static":null,"prepend":true},{"name":"per_page","type":"integer","callback":null,"default":50,"directive":null,"facet":null,"facet_method":"and","operator":null,"param":null,"static":null,"prepend":true},{"name":"sort","type":"string","callback":null,"default":null,"directive":null,"facet":null,"facet_method":"and","operator":null,"param":null,"static":null,"prepend":true},{"name":"body","type":"text","callback":null,"default":"*:*","directive":null,"facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":false},{"name":"group","type":"string","callback":null,"default":null,"directive":"group","facet":true,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"difficulty_rating_bin","type":"string","callback":null,"default":null,"directive":"difficulty_rating_bin","facet":true,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"id","type":"integer","callback":null,"default":null,"directive":"id","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"tag","type":"string","callback":null,"default":null,"directive":"tag","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"product","type":"string","callback":null,"default":null,"directive":"product","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"created_at","type":"timeframe","callback":{},"default":null,"directive":"created_at","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"profile_id","type":"integer","callback":null,"default":null,"directive":"author_id","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"created_by","type":"string","callback":null,"default":null,"directive":"author","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"player_id","type":"integer","callback":null,"default":null,"directive":"solver_id","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"player","type":"string","callback":null,"default":null,"directive":"solver","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"solvers_count","type":"integer","callback":null,"default":null,"directive":"solvers_count","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"comments_count","type":"integer","callback":null,"default":null,"directive":"comments_count","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"likes_count","type":"integer","callback":null,"default":null,"directive":"likes_count","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"leader_id","type":"integer","callback":null,"default":null,"directive":"leader_id","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"leading_solution","type":"integer","callback":null,"default":null,"directive":"leading_solution","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true}],"filters":[{"name":"asset_type","type":"string","callback":null,"default":null,"directive":null,"facet":null,"facet_method":"and","operator":null,"param":null,"static":"\"cody:problem\"","prepend":true},{"name":"profile_id","type":"integer","callback":{},"default":null,"directive":null,"facet":null,"facet_method":"and","operator":null,"param":"author_id","static":null,"prepend":true}],"query":{"params":{"per_page":50,"term":"tag:\"quality control\"","current_player":null,"sort":"map(difficulty_value,0,0,999) asc"},"parser":"MathWorks::Search::Solr::QueryParser","directives":{"term":{"directives":{"tag":[["tag:\"quality control\"","","\"","quality control","\""]]}}},"facets":{"#\u003cMathWorks::Search::Field:0x00007f732e52ea60\u003e":null,"#\u003cMathWorks::Search::Field:0x00007f732e52e9c0\u003e":null},"filters":{"#\u003cMathWorks::Search::Field:0x00007f732e52db60\u003e":"\"cody:problem\""},"fields":{"#\u003cMathWorks::Search::Field:0x00007f732e52ece0\u003e":1,"#\u003cMathWorks::Search::Field:0x00007f732e52ec40\u003e":50,"#\u003cMathWorks::Search::Field:0x00007f732e52eba0\u003e":"map(difficulty_value,0,0,999) asc","#\u003cMathWorks::Search::Field:0x00007f732e52eb00\u003e":"tag:\"quality control\""},"user_query":{"#\u003cMathWorks::Search::Field:0x00007f732e52eb00\u003e":"tag:\"quality control\""},"queried_facets":{}},"query_backend":{"connection":{"configuration":{"index_url":"http://index-op-v2/solr/","query_url":"http://search-op-v2/solr/","direct_access_index_urls":["http://index-op-v2/solr/"],"direct_access_query_urls":["http://search-op-v2/solr/"],"timeout":10,"vhost":"search","exchange":"search.topic","heartbeat":30,"pre_index_mode":false,"host":"rabbitmq-eks","port":5672,"username":"search","password":"J3bGPZzQ7asjJcCk","virtual_host":"search","indexer":"amqp","http_logging":"true","core":"cody"},"query_connection":{"uri":"http://search-op-v2/solr/cody/","proxy":null,"connection":{"parallel_manager":null,"headers":{"User-Agent":"Faraday v1.0.1"},"params":{},"options":{"params_encoder":"Faraday::FlatParamsEncoder","proxy":null,"bind":null,"timeout":null,"open_timeout":null,"read_timeout":null,"write_timeout":null,"boundary":null,"oauth":null,"context":null,"on_data":null},"ssl":{"verify":true,"ca_file":null,"ca_path":null,"verify_mode":null,"cert_store":null,"client_cert":null,"client_key":null,"certificate":null,"private_key":null,"verify_depth":null,"version":null,"min_version":null,"max_version":null},"default_parallel_manager":null,"builder":{"adapter":{"name":"Faraday::Adapter::NetHttp","args":[],"block":null},"handlers":[{"name":"Faraday::Response::RaiseError","args":[],"block":null}],"app":{"app":{"ssl_cert_store":{"verify_callback":null,"error":null,"error_string":null,"chain":null,"time":null},"app":{},"connection_options":{},"config_block":null}}},"url_prefix":"http://search-op-v2/solr/cody/","manual_proxy":false,"proxy":null},"update_format":"RSolr::JSON::Generator","update_path":"update","options":{"url":"http://search-op-v2/solr/cody"}}},"query":{"params":{"per_page":50,"term":"tag:\"quality control\"","current_player":null,"sort":"map(difficulty_value,0,0,999) asc"},"parser":"MathWorks::Search::Solr::QueryParser","directives":{"term":{"directives":{"tag":[["tag:\"quality control\"","","\"","quality control","\""]]}}},"facets":{"#\u003cMathWorks::Search::Field:0x00007f732e52ea60\u003e":null,"#\u003cMathWorks::Search::Field:0x00007f732e52e9c0\u003e":null},"filters":{"#\u003cMathWorks::Search::Field:0x00007f732e52db60\u003e":"\"cody:problem\""},"fields":{"#\u003cMathWorks::Search::Field:0x00007f732e52ece0\u003e":1,"#\u003cMathWorks::Search::Field:0x00007f732e52ec40\u003e":50,"#\u003cMathWorks::Search::Field:0x00007f732e52eba0\u003e":"map(difficulty_value,0,0,999) asc","#\u003cMathWorks::Search::Field:0x00007f732e52eb00\u003e":"tag:\"quality control\""},"user_query":{"#\u003cMathWorks::Search::Field:0x00007f732e52eb00\u003e":"tag:\"quality control\""},"queried_facets":{}},"options":{"fields":["id","difficulty_rating"]},"join":" "},"results":[{"id":44545,"difficulty_rating":"unrated"}]}}