{"group":{"id":1,"name":"Community","lockable":false,"created_at":"2012-01-18T18:02:15.000Z","updated_at":"2026-04-16T00:12:35.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":"2026-04-16T00: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":56588,"title":"Exact Cover","description":"An efficient solution to the exact cover problem can be useful in many situations. In this problem, you are welcome to use Knuth's Algorithm X (utilizing the Dancing Links technique), or any other solution.\r\nThe exact cover problem is specified here by a row vector, u, representing the universe, and a cell array, s, containing the sets (row vectors) with which to try to exactly cover the respective universe. Given u and s, return x, a cell array containing one or more sets from s that exactly cover u. If no solution exists, return an empty cell array.\r\nFor simplicity, you may assume that all universes consist of integers and that all sets in s are subsets of the respective universes. Solutions may not be unique.\r\nExample:\r\nu = 1:7;\r\ns = {[1 4 7] [1 4] [4 5 7] [3 5 6] [2 3 6 7] [2 7]};\r\nx = {[1 4] [3 5 6] [2 7]}","description_html":"\u003cdiv style = \"text-align: start; line-height: 20.44px; min-height: 0px; white-space: normal; color: rgb(0, 0, 0); font-family: Menlo, Monaco, Consolas, monospace; font-style: normal; font-size: 14px; font-weight: 400; text-decoration: none solid rgb(0, 0, 0); white-space: normal; \"\u003e\u003cdiv style=\"block-size: 266.312px; display: block; min-width: 0px; padding-block-start: 0px; padding-top: 0px; perspective-origin: 407px 133.156px; transform-origin: 407px 133.156px; vertical-align: baseline; \"\u003e\u003cdiv style=\"block-size: 42px; font-family: Helvetica, Arial, sans-serif; line-height: 21px; margin-block-end: 9px; margin-block-start: 2px; margin-bottom: 9px; margin-inline-end: 10px; margin-inline-start: 4px; margin-left: 4px; margin-right: 10px; margin-top: 2px; perspective-origin: 384px 21px; text-align: left; transform-origin: 384px 21px; white-space: pre-wrap; margin-left: 4px; margin-top: 2px; margin-bottom: 9px; margin-right: 10px; \"\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003eAn efficient solution to the \u003c/span\u003e\u003c/span\u003e\u003ca target='_blank' href = \"https://en.wikipedia.org/wiki/Exact_cover\"\u003e\u003cspan style=\"\"\u003e\u003cspan style=\"\"\u003eexact cover problem\u003c/span\u003e\u003c/span\u003e\u003c/a\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003e can be useful in many situations. In this problem, you are welcome to use \u003c/span\u003e\u003c/span\u003e\u003ca target='_blank' href = \"https://en.wikipedia.org/wiki/Knuth%27s_Algorithm_X\"\u003e\u003cspan style=\"\"\u003e\u003cspan style=\"\"\u003eKnuth's Algorithm X\u003c/span\u003e\u003c/span\u003e\u003c/a\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003e (utilizing the \u003c/span\u003e\u003c/span\u003e\u003ca target='_blank' href = \"https://en.wikipedia.org/wiki/Dancing_Links\"\u003e\u003cspan style=\"\"\u003e\u003cspan style=\"\"\u003eDancing Links\u003c/span\u003e\u003c/span\u003e\u003c/a\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003e technique), or any other solution.\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"block-size: 63px; font-family: Helvetica, Arial, sans-serif; line-height: 21px; margin-block-end: 9px; margin-block-start: 2px; margin-bottom: 9px; margin-inline-end: 10px; margin-inline-start: 4px; margin-left: 4px; margin-right: 10px; margin-top: 2px; perspective-origin: 384px 31.5px; text-align: left; transform-origin: 384px 31.5px; white-space: pre-wrap; margin-left: 4px; margin-top: 2px; margin-bottom: 9px; margin-right: 10px; \"\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003eThe exact cover problem is specified here by a row vector, u, representing the universe, and a cell array, s, containing the sets (row vectors) with which to try to exactly cover the respective universe. Given u and s, return x, a cell array containing one or more sets from s that exactly cover u. If no solution exists, return an empty cell array.\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"block-size: 42px; font-family: Helvetica, Arial, sans-serif; line-height: 21px; margin-block-end: 9px; margin-block-start: 2px; margin-bottom: 9px; margin-inline-end: 10px; margin-inline-start: 4px; margin-left: 4px; margin-right: 10px; margin-top: 2px; perspective-origin: 384px 21px; text-align: left; transform-origin: 384px 21px; white-space: pre-wrap; margin-left: 4px; margin-top: 2px; margin-bottom: 9px; margin-right: 10px; \"\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003eFor simplicity, you may assume that all universes consist of integers and that all sets in s are subsets of the respective universes. Solutions may not be unique.\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"block-size: 21px; font-family: Helvetica, Arial, sans-serif; line-height: 21px; margin-block-end: 9px; margin-block-start: 2px; margin-bottom: 9px; margin-inline-end: 10px; margin-inline-start: 4px; margin-left: 4px; margin-right: 10px; margin-top: 2px; perspective-origin: 384px 10.5px; text-align: left; transform-origin: 384px 10.5px; white-space: pre-wrap; margin-left: 4px; margin-top: 2px; margin-bottom: 9px; margin-right: 10px; \"\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003eExample:\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"background-color: rgb(247, 247, 247); block-size: 61.3125px; border-bottom-left-radius: 4px; border-bottom-right-radius: 4px; border-end-end-radius: 4px; border-end-start-radius: 4px; border-start-end-radius: 4px; border-start-start-radius: 4px; border-top-left-radius: 4px; border-top-right-radius: 4px; margin-block-end: 10px; margin-block-start: 10px; margin-bottom: 10px; margin-inline-end: 3px; margin-inline-start: 3px; margin-left: 3px; margin-right: 3px; margin-top: 10px; perspective-origin: 404px 30.6562px; transform-origin: 404px 30.6562px; margin-left: 3px; margin-top: 10px; margin-bottom: 10px; margin-right: 3px; \"\u003e\u003cdiv style=\"background-color: rgba(0, 0, 0, 0); block-size: 20.4375px; border-bottom-left-radius: 0px; border-bottom-right-radius: 0px; border-end-end-radius: 0px; border-end-start-radius: 0px; border-inline-end-color: rgb(233, 233, 233); border-inline-end-style: solid; border-inline-end-width: 1px; border-inline-start-color: rgb(233, 233, 233); border-inline-start-style: solid; border-inline-start-width: 1px; border-left-color: rgb(233, 233, 233); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(233, 233, 233); border-right-style: solid; border-right-width: 1px; border-start-end-radius: 0px; border-start-start-radius: 0px; border-top-left-radius: 0px; border-top-right-radius: 0px; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; min-block-size: 18px; min-height: 18px; padding-inline-start: 4px; padding-left: 4px; perspective-origin: 404px 10.2188px; transform-origin: 404px 10.2188px; white-space: nowrap; \"\u003e\u003cspan style=\"block-size: auto; border-inline-end-color: rgb(0, 0, 0); border-inline-end-style: none; border-inline-end-width: 0px; border-inline-start-color: rgb(0, 0, 0); border-inline-start-style: none; border-inline-start-width: 0px; border-left-color: rgb(0, 0, 0); border-left-style: none; border-left-width: 0px; border-right-color: rgb(0, 0, 0); border-right-style: none; border-right-width: 0px; display: inline; margin-inline-end: 45px; margin-right: 45px; min-block-size: 0px; min-height: 0px; padding-inline-start: 0px; padding-left: 0px; perspective-origin: 0px 0px; tab-size: 4; transform-origin: 0px 0px; white-space: pre; margin-right: 45px; \"\u003e\u003cspan style=\"margin-inline-end: 0px; margin-right: 0px; \"\u003eu = 1:7;\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"background-color: rgba(0, 0, 0, 0); block-size: 20.4375px; border-bottom-left-radius: 0px; border-bottom-right-radius: 0px; border-end-end-radius: 0px; border-end-start-radius: 0px; border-inline-end-color: rgb(233, 233, 233); border-inline-end-style: solid; border-inline-end-width: 1px; border-inline-start-color: rgb(233, 233, 233); border-inline-start-style: solid; border-inline-start-width: 1px; border-left-color: rgb(233, 233, 233); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(233, 233, 233); border-right-style: solid; border-right-width: 1px; border-start-end-radius: 0px; border-start-start-radius: 0px; border-top-left-radius: 0px; border-top-right-radius: 0px; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; min-block-size: 18px; min-height: 18px; padding-inline-start: 4px; padding-left: 4px; perspective-origin: 404px 10.2188px; transform-origin: 404px 10.2188px; white-space: nowrap; \"\u003e\u003cspan style=\"block-size: auto; border-inline-end-color: rgb(0, 0, 0); border-inline-end-style: none; border-inline-end-width: 0px; border-inline-start-color: rgb(0, 0, 0); border-inline-start-style: none; border-inline-start-width: 0px; border-left-color: rgb(0, 0, 0); border-left-style: none; border-left-width: 0px; border-right-color: rgb(0, 0, 0); border-right-style: none; border-right-width: 0px; display: inline; margin-inline-end: 45px; margin-right: 45px; min-block-size: 0px; min-height: 0px; padding-inline-start: 0px; padding-left: 0px; perspective-origin: 0px 0px; tab-size: 4; transform-origin: 0px 0px; white-space: pre; margin-right: 45px; \"\u003e\u003cspan style=\"margin-inline-end: 0px; margin-right: 0px; \"\u003es = {[1 4 7] [1 4] [4 5 7] [3 5 6] [2 3 6 7] [2 7]};\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"background-color: rgba(0, 0, 0, 0); block-size: 20.4375px; border-bottom-left-radius: 0px; border-bottom-right-radius: 0px; border-end-end-radius: 0px; border-end-start-radius: 0px; border-inline-end-color: rgb(233, 233, 233); border-inline-end-style: solid; border-inline-end-width: 1px; border-inline-start-color: rgb(233, 233, 233); border-inline-start-style: solid; border-inline-start-width: 1px; border-left-color: rgb(233, 233, 233); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(233, 233, 233); border-right-style: solid; border-right-width: 1px; border-start-end-radius: 0px; border-start-start-radius: 0px; border-top-left-radius: 0px; border-top-right-radius: 0px; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; min-block-size: 18px; min-height: 18px; padding-inline-start: 4px; padding-left: 4px; perspective-origin: 404px 10.2188px; transform-origin: 404px 10.2188px; white-space: nowrap; \"\u003e\u003cspan style=\"block-size: auto; border-inline-end-color: rgb(0, 0, 0); border-inline-end-style: none; border-inline-end-width: 0px; border-inline-start-color: rgb(0, 0, 0); border-inline-start-style: none; border-inline-start-width: 0px; border-left-color: rgb(0, 0, 0); border-left-style: none; border-left-width: 0px; border-right-color: rgb(0, 0, 0); border-right-style: none; border-right-width: 0px; display: inline; margin-inline-end: 45px; margin-right: 45px; min-block-size: 0px; min-height: 0px; padding-inline-start: 0px; padding-left: 0px; perspective-origin: 0px 0px; tab-size: 4; transform-origin: 0px 0px; white-space: pre; margin-right: 45px; \"\u003e\u003cspan style=\"margin-inline-end: 0px; margin-right: 0px; \"\u003ex = {[1 4] [3 5 6] [2 7]}\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003c/div\u003e\u003c/div\u003e\u003c/div\u003e","function_template":"function x = AlgorithmX(u,s)\r\n    x = s;\r\nend","test_suite":"%%\r\nu = 1:7;\r\ns = {[1 4 7] [1 4] [4 5 7] [3 5 6] [2 3 6 7] [2 7]};\r\nx = AlgorithmX(u,s);\r\nassert(all(ismember(cellfun(@(i)prod(i),x),cellfun(@(i)prod(i),s))))\r\nassert(isequal(sort(u),sort(cell2mat(x))))\r\n\r\n%%\r\nu = primes(25);\r\ns = {[23 13 11] [2] [2 5 17] [5 23 7] [7 3 17] [13 2 19] [3 13 23] [19 5 23] [3 11 13 17 19 23] [19 17 13] [17 11 23] [5 7] [7 17 5] [2 13 11]};\r\nx = AlgorithmX(u,s);\r\nassert(all(ismember(cellfun(@(i)prod(i),x),cellfun(@(i)prod(i),s))))\r\nassert(isequal(sort(u),sort(cell2mat(x))))\r\n\r\n%%\r\nu = primes(25);\r\ns = {[23 13 11] [2 5 17] [5 23 7] [7 3 17] [13 2 19] [3 13 23] [19 5 23] [19 17 13] [17 11 23] [7 17 5] [2 13 11]};\r\nx = AlgorithmX(u,s);\r\nassert(all(ismember(cellfun(@(i)prod(i),x),cellfun(@(i)prod(i),s))))\r\nassert(isequal(sort(u),sort(cell2mat(x))))\r\n\r\n%%\r\nu = primes(25);\r\ns = {[23 13 11] [2 5 17] [5 23 7] [13 2 19] [3 13 23] [19 5 23] [19 17 13] [17 11 23] [7 17 5] [2 13 11]};\r\nx = AlgorithmX(u,s);\r\nassert(isempty(x))\r\n\r\n%%\r\nu = primes(25);\r\ns = {};\r\nx = AlgorithmX(u,s);\r\nassert(isempty(x))\r\n\r\n%%\r\nu = primes(100);\r\ns = {[7 67 23 47 3 79 13 89 11 31 29 71 83 37 59 53 2 41 97 43 17 19 5 73] [73] [31] [83] [89 11] [67] [79] [59] [13] [5 61] [29] [43] [71] [53] [47] [19] [7] [97] [37] [17] [41 3] [23] [2]}\r\nx = AlgorithmX(u,s);\r\nassert(all(ismember(cellfun(@(i)prod(i),x),cellfun(@(i)prod(i),s))))\r\nassert(isequal(sort(u),sort(cell2mat(x))))\r\n\r\n%%\r\nfiletext = fileread('AlgorithmX.m');\r\nassert(isempty(strfind(filetext,'regexp')))\r\nassert(isempty(strfind(filetext,'assign')))\r\nassert(isempty(strfind(filetext,'eval')))\r\nassert(isempty(strfind(filetext,'echo')))\r\nassert(isempty(strfind(filetext,'!')))","published":true,"deleted":false,"likes_count":3,"comments_count":1,"created_by":15521,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":8,"test_suite_updated_at":null,"rescore_all_solutions":false,"group_id":1,"created_at":"2022-11-12T15:28:28.000Z","updated_at":"2025-08-22T03:22:24.000Z","published_at":"2022-11-12T15:28:28.000Z","restored_at":null,"restored_by":null,"spam":null,"simulink":false,"admin_reviewed":false,"description_opc":"{\"parts\":[{\"partUri\":\"/matlab/document.xml\",\"contentType\":\"application/vnd.mathworks.matlab.code.document+xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\"?\u003e\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\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eAn efficient solution to the \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:hyperlink w:docLocation=\\\"https://en.wikipedia.org/wiki/Exact_cover\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eexact cover problem\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e can be useful in many situations. In this problem, you are welcome to use \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:hyperlink w:docLocation=\\\"https://en.wikipedia.org/wiki/Knuth%27s_Algorithm_X\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eKnuth's Algorithm X\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e (utilizing the \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:hyperlink w:docLocation=\\\"https://en.wikipedia.org/wiki/Dancing_Links\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eDancing Links\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e technique), or any other solution.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eThe exact cover problem is specified here by a row vector, u, representing the universe, and a cell array, s, containing the sets (row vectors) with which to try to exactly cover the respective universe. Given u and s, return x, a cell array containing one or more sets from s that exactly cover u. If no solution exists, return an empty cell array.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eFor simplicity, you may assume that all universes consist of integers and that all sets in s are subsets of the respective universes. Solutions may not be unique.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eExample:\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[u = 1:7;\\ns = {[1 4 7] [1 4] [4 5 7] [3 5 6] [2 3 6 7] [2 7]};\\nx = {[1 4] [3 5 6] [2 7]}]]\u003e\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003c/w:body\u003e\u003c/w:document\u003e\",\"relationship\":null}],\"relationships\":[{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/document\",\"target\":\"/matlab/document.xml\",\"relationshipId\":\"rId1\"}]}"},{"id":2085,"title":"Sudoku Solver - Standard 9x9","description":"Solve a Standard 9x9 \u003chttp://en.wikipedia.org/wiki/Sudoku Sudoku\u003e. Values 1 thru 9 occur in each row, column, and the nine non-overlapping 3x3 matrices starting at the top left corner. \u003chttp://www.free-sudoku.com/sudoku.php?dchoix=evil Sudoku practice site\u003e.\r\n\r\n*Input:* m, a 9x9 matrix of values 0 thru 9. Unknowns are 0s.\r\n\r\n*Output:* mout, a 9x9 matrix of values 1 thru 9 that satisfy Sudoku rules.\r\n\r\n*Scoring:* Time (msec) to solve the Hard Sudoku\r\n\r\n*Example:*\r\n\r\n  m         mout\r\n  390701506 398721546\r\n  042890701 542896731\r\n  106540890 176543892\r\n  820600150 829674153\r\n  400138009 457138269\r\n  031002087 631952487\r\n  065087304 965287314\r\n  703065920 713465928\r\n  204309075 284319675\r\n\r\n*Sudoku Variations:*\r\n\r\nFuture challenges will involve the Sudoku variations Diagonal, Arrow(Sums), Inequality, Irregular, and others as they present themselves.\r\n\r\n*Algorithm Spoiler:*\r\nSudoku's can be readily solved using minimal choice recursion with consistency check. A key step is an index map of all indices that have mutual value exclusion (row,col,3x3), idxmap[81,27]. Another key step is to have an Evolve function that implements all single option values determined by the idxmap. A critical performance enhancement is a Sudoku Consistency Checker that checks for illegal replications of values. Illegal placements by Evolve occur when an incorrect value is asserted into the matrix during recursion trials. The recursive solver finds an idx with minimum options based on idxmap. The values for the idx location are asserted, Evolved, Consistency Checked, and then recursion call if Consistent. When all is Consistent and no unknowns remain the Sudoku is solved. Solution times are in the milli-seconds even for Evil, minimum 17 value, Sudokus.","description_html":"\u003cp\u003eSolve a Standard 9x9 \u003ca href = \"http://en.wikipedia.org/wiki/Sudoku\"\u003eSudoku\u003c/a\u003e. Values 1 thru 9 occur in each row, column, and the nine non-overlapping 3x3 matrices starting at the top left corner. \u003ca href = \"http://www.free-sudoku.com/sudoku.php?dchoix=evil\"\u003eSudoku practice site\u003c/a\u003e.\u003c/p\u003e\u003cp\u003e\u003cb\u003eInput:\u003c/b\u003e m, a 9x9 matrix of values 0 thru 9. Unknowns are 0s.\u003c/p\u003e\u003cp\u003e\u003cb\u003eOutput:\u003c/b\u003e mout, a 9x9 matrix of values 1 thru 9 that satisfy Sudoku rules.\u003c/p\u003e\u003cp\u003e\u003cb\u003eScoring:\u003c/b\u003e Time (msec) to solve the Hard Sudoku\u003c/p\u003e\u003cp\u003e\u003cb\u003eExample:\u003c/b\u003e\u003c/p\u003e\u003cpre class=\"language-matlab\"\u003em         mout\r\n390701506 398721546\r\n042890701 542896731\r\n106540890 176543892\r\n820600150 829674153\r\n400138009 457138269\r\n031002087 631952487\r\n065087304 965287314\r\n703065920 713465928\r\n204309075 284319675\r\n\u003c/pre\u003e\u003cp\u003e\u003cb\u003eSudoku Variations:\u003c/b\u003e\u003c/p\u003e\u003cp\u003eFuture challenges will involve the Sudoku variations Diagonal, Arrow(Sums), Inequality, Irregular, and others as they present themselves.\u003c/p\u003e\u003cp\u003e\u003cb\u003eAlgorithm Spoiler:\u003c/b\u003e\r\nSudoku's can be readily solved using minimal choice recursion with consistency check. A key step is an index map of all indices that have mutual value exclusion (row,col,3x3), idxmap[81,27]. Another key step is to have an Evolve function that implements all single option values determined by the idxmap. A critical performance enhancement is a Sudoku Consistency Checker that checks for illegal replications of values. Illegal placements by Evolve occur when an incorrect value is asserted into the matrix during recursion trials. The recursive solver finds an idx with minimum options based on idxmap. The values for the idx location are asserted, Evolved, Consistency Checked, and then recursion call if Consistent. When all is Consistent and no unknowns remain the Sudoku is solved. Solution times are in the milli-seconds even for Evil, minimum 17 value, Sudokus.\u003c/p\u003e","function_template":"function mout=sudoku_solver(m)\r\n% m is a 9x9 Sudoku array with 0 for unknown values\r\n% create mout a consistent sudoku array\r\n  mout=m;\r\nend","test_suite":"assignin('caller','score',500);\r\n%%\r\n% Test 1\r\nmstr=['012300007'; '040600010'; '078900020'; '000000040'; '100000002'; '060000000'; '080001230'; '090004060'; '300007890']; \r\n% convert string to array\r\nm=zeros(9);\r\nfor i=1:9\r\n m(i,:)=mstr(i,:)-'0'  ; \r\nend\r\n\r\ntic\r\nmout=sudoku_solver(m)\r\ntoc\r\n\r\nvalid=all(arrayfun(@(i) all(sum([mout mout']==i)),1:length(mout)));\r\nassert(valid==1)\r\n\r\nptr=find(m\u003e0);\r\nvalid2=isequal(m(ptr),mout(ptr));\r\nassert(valid2==1)\r\n\r\n%%\r\n% Test 2\r\nmstr=['000004500'; '000003600'; '432008700'; '867000000'; '000000000'; '000000417'; '001900854'; '006400000'; '003700000']; \r\n% convert string to array\r\nm=zeros(9);\r\nfor i=1:9\r\n m(i,:)=mstr(i,:)-'0'  ; \r\nend\r\n\r\ntic\r\nmout=sudoku_solver(m)\r\ntoc\r\n\r\nvalid=all(arrayfun(@(i) all(sum([mout mout']==i)),1:length(mout)));\r\nassert(valid==1)\r\n\r\nptr=find(m\u003e0);\r\nvalid2=isequal(m(ptr),mout(ptr));\r\nassert(valid2==1)\r\n\r\n%%\r\n% Test 3\r\nmstr=['120034000'; '000000056'; '000200000'; '007800002'; '600000001'; '500006300'; '000008000'; '340000000'; '000560078']; \r\n% convert string to array\r\nm=zeros(9);\r\nfor i=1:9\r\n m(i,:)=mstr(i,:)-'0'  ; \r\nend\r\n\r\ntic\r\nmout=sudoku_solver(m)\r\ntoc\r\n\r\nvalid=all(arrayfun(@(i) all(sum([mout mout']==i)),1:length(mout)));\r\nassert(valid==1)\r\n\r\nptr=find(m\u003e0);\r\nvalid2=isequal(m(ptr),mout(ptr));\r\nassert(valid2==1)\r\n\r\n\r\n%%\r\n% Timed Test on a Hard Sudoku\r\n% Non-Valid answer creates a Max score but not a fail\r\n% Hard Sudoku\r\nmstr=['005700009'; '030090010'; '100005300'; '600004700'; '040010050'; '002500001'; '004600002'; '080020040'; '200008600']; \r\n% convert string to array\r\nm=zeros(9);\r\nfor i=1:9\r\n m(i,:)=mstr(i,:)-'0'  ; \r\nend\r\n\r\ntime0=cputime;\r\n mout=sudoku_solver(m)\r\netime=(cputime-time0)*1000 % msec\r\nvalid=all(arrayfun(@(i) all(sum([mout mout']==i)),1:length(mout)));\r\nptr=find(m\u003e0);\r\nvalid2=isequal(m(ptr),mout(ptr));\r\n% Not Asserting for Valid answer\r\nif ~valid,etime=500;end\r\nif ~valid2,etime=500;end\r\nassignin('caller','score',min(500,floor(etime)));","published":true,"deleted":false,"likes_count":8,"comments_count":2,"created_by":3097,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":52,"test_suite_updated_at":null,"rescore_all_solutions":false,"group_id":1,"created_at":"2014-01-02T00:31:17.000Z","updated_at":"2026-04-08T09:57:58.000Z","published_at":"2014-01-02T01:30:54.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:t\u003eSolve a Standard 9x9\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:hyperlink w:docLocation=\\\"http://en.wikipedia.org/wiki/Sudoku\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eSudoku\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e. Values 1 thru 9 occur in each row, column, and the nine non-overlapping 3x3 matrices starting at the top left corner.\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:hyperlink w:docLocation=\\\"http://www.free-sudoku.com/sudoku.php?dchoix=evil\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eSudoku practice site\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\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:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eInput:\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e m, a 9x9 matrix of values 0 thru 9. Unknowns are 0s.\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:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eOutput:\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e mout, a 9x9 matrix of values 1 thru 9 that satisfy Sudoku rules.\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:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eScoring:\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e Time (msec) to solve the Hard Sudoku\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:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eExample:\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[m         mout\\n390701506 398721546\\n042890701 542896731\\n106540890 176543892\\n820600150 829674153\\n400138009 457138269\\n031002087 631952487\\n065087304 965287314\\n703065920 713465928\\n204309075 284319675]]\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:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eSudoku Variations:\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\u003eFuture challenges will involve the Sudoku variations Diagonal, Arrow(Sums), Inequality, Irregular, and others as they present themselves.\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:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eAlgorithm Spoiler:\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e Sudoku's can be readily solved using minimal choice recursion with consistency check. A key step is an index map of all indices that have mutual value exclusion (row,col,3x3), idxmap[81,27]. Another key step is to have an Evolve function that implements all single option values determined by the idxmap. A critical performance enhancement is a Sudoku Consistency Checker that checks for illegal replications of values. Illegal placements by Evolve occur when an incorrect value is asserted into the matrix during recursion trials. The recursive solver finds an idx with minimum options based on idxmap. The values for the idx location are asserted, Evolved, Consistency Checked, and then recursion call if Consistent. When all is Consistent and no unknowns remain the Sudoku is solved. Solution times are in the milli-seconds even for Evil, minimum 17 value, Sudokus.\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\"}]}"},{"id":60751,"title":"List the partitions of an integer","description":"Several Cody problems deal with partitions of an integer, or the ways an integer  can be written as the sum of positive numbers—either all integers up to and including  or a subset of those numbers. \r\nMost of the problems involve counting partitions. Cody Problem 1873 counts the partitions of an integer, while Cody Problem 42918 counts the partitions with a given number of parts. Cody Problem 45429 restricts the parts to primes; Cody Problem 1240 restricts them to U.S. coin denominations; and Cody Problem 54740 restricts them to American football scores.  \r\nA couple of problems involves listing partitions. Cody Problem 590 asks us to list one partition of a number, without repeating parts, and Cody Problem 59586 involves listing ways to make a sum in Killer Sudoku. \r\nThis problem involves listing all partitions of an integer. For example, 5 = 4+1 = 3+2 = 3+1+1 = 2+2+1 = 2+1+1+1 = 1+1+1+1+1. Notice that rearrangements of a partition are not included. \r\nWrite a function to list partitions of an integer. Return the partitions in a cell array and sort them as in the example—i.e., from larger to smaller numbers. For , the required output is\r\n{[5] [4 1] [3 2] [3 1 1] [2 2 1] [2 1 1 1] [1 1 1 1 1]}","description_html":"\u003cdiv style = \"text-align: start; line-height: 20.44px; min-height: 0px; white-space: normal; color: rgb(0, 0, 0); font-family: Menlo, Monaco, Consolas, monospace; font-style: normal; font-size: 14px; font-weight: 400; text-decoration: none solid rgb(0, 0, 0); white-space: normal; \"\u003e\u003cdiv style=\"block-size: 318px; display: block; min-width: 0px; padding-block-start: 0px; padding-top: 0px; perspective-origin: 407px 159px; transform-origin: 407px 159px; vertical-align: baseline; \"\u003e\u003cdiv style=\"block-size: 42px; font-family: Helvetica, Arial, sans-serif; line-height: 21px; margin-block-end: 9px; margin-block-start: 2px; margin-bottom: 9px; margin-inline-end: 10px; margin-inline-start: 4px; margin-left: 4px; margin-right: 10px; margin-top: 2px; perspective-origin: 384px 21px; text-align: left; transform-origin: 384px 21px; white-space-collapse: preserve; margin-left: 4px; margin-top: 2px; margin-bottom: 9px; margin-right: 10px; \"\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003eSeveral Cody problems deal with partitions of an integer, or the ways an integer \u003c/span\u003e\u003c/span\u003e\u003cspan style=\"font-family: STIXGeneral, STIXGeneral-webfont, serif; font-style: italic; font-weight: 400; color: rgb(0, 0, 0);\"\u003en\u003c/span\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003e can be written as the sum of positive numbers—either all integers up to and including \u003c/span\u003e\u003c/span\u003e\u003cspan style=\"font-family: STIXGeneral, STIXGeneral-webfont, serif; font-style: italic; font-weight: 400; color: rgb(0, 0, 0);\"\u003en\u003c/span\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003e or a subset of those numbers. \u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"block-size: 84px; font-family: Helvetica, Arial, sans-serif; line-height: 21px; margin-block-end: 9px; margin-block-start: 2px; margin-bottom: 9px; margin-inline-end: 10px; margin-inline-start: 4px; margin-left: 4px; margin-right: 10px; margin-top: 2px; perspective-origin: 384px 42px; text-align: left; transform-origin: 384px 42px; white-space-collapse: preserve; margin-left: 4px; margin-top: 2px; margin-bottom: 9px; margin-right: 10px; \"\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003eMost of the problems involve counting partitions. \u003c/span\u003e\u003c/span\u003e\u003ca target='_blank' href = \"https://www.mathworks.com/matlabcentral/cody/problems/1873\"\u003e\u003cspan style=\"\"\u003e\u003cspan style=\"text-decoration: underline; text-decoration-line: underline; \"\u003eCody Problem 1873\u003c/span\u003e\u003c/span\u003e\u003c/a\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003e counts the partitions of an integer, while \u003c/span\u003e\u003c/span\u003e\u003ca target='_blank' href = \"https://www.mathworks.com/matlabcentral/cody/problems/42918\"\u003e\u003cspan style=\"\"\u003e\u003cspan style=\"text-decoration: underline; text-decoration-line: underline; \"\u003eCody Problem 42918\u003c/span\u003e\u003c/span\u003e\u003c/a\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003e counts the partitions with a given number of parts. \u003c/span\u003e\u003c/span\u003e\u003ca target='_blank' href = \"https://www.mathworks.com/matlabcentral/cody/problems/45429\"\u003e\u003cspan style=\"\"\u003e\u003cspan style=\"text-decoration: underline; text-decoration-line: underline; \"\u003eCody Problem 45429\u003c/span\u003e\u003c/span\u003e\u003c/a\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003e restricts the parts to primes; \u003c/span\u003e\u003c/span\u003e\u003ca target='_blank' href = \"https://www.mathworks.com/matlabcentral/cody/problems/1240\"\u003e\u003cspan style=\"\"\u003e\u003cspan style=\"text-decoration: underline; text-decoration-line: underline; \"\u003eCody Problem 1240\u003c/span\u003e\u003c/span\u003e\u003c/a\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003e restricts them to U.S. coin denominations; and \u003c/span\u003e\u003c/span\u003e\u003ca target='_blank' href = \"https://www.mathworks.com/matlabcentral/cody/problems/54740\"\u003e\u003cspan style=\"\"\u003e\u003cspan style=\"text-decoration: underline; text-decoration-line: underline; \"\u003eCody Problem 54740\u003c/span\u003e\u003c/span\u003e\u003c/a\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003e restricts them to American football scores. \u003c/span\u003e\u003c/span\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003e \u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"block-size: 42px; font-family: Helvetica, Arial, sans-serif; line-height: 21px; margin-block-end: 9px; margin-block-start: 2px; margin-bottom: 9px; margin-inline-end: 10px; margin-inline-start: 4px; margin-left: 4px; margin-right: 10px; margin-top: 2px; perspective-origin: 384px 21px; text-align: left; transform-origin: 384px 21px; white-space-collapse: preserve; margin-left: 4px; margin-top: 2px; margin-bottom: 9px; margin-right: 10px; \"\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003eA couple of problems involves listing partitions. \u003c/span\u003e\u003c/span\u003e\u003ca target='_blank' href = \"https://www.mathworks.com/matlabcentral/cody/problems/590\"\u003e\u003cspan style=\"\"\u003e\u003cspan style=\"text-decoration: underline; text-decoration-line: underline; \"\u003eCody Problem 590\u003c/span\u003e\u003c/span\u003e\u003c/a\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003e asks us to list one partition of a number, without repeating parts, and \u003c/span\u003e\u003c/span\u003e\u003ca target='_blank' href = \"https://www.mathworks.com/matlabcentral/cody/problems/59586\"\u003e\u003cspan style=\"\"\u003e\u003cspan style=\"text-decoration: underline; text-decoration-line: underline; \"\u003eCody Problem 59586\u003c/span\u003e\u003c/span\u003e\u003c/a\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003e involves listing ways to make a sum in Killer Sudoku. \u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"block-size: 42px; font-family: Helvetica, Arial, sans-serif; line-height: 21px; margin-block-end: 9px; margin-block-start: 2px; margin-bottom: 9px; margin-inline-end: 10px; margin-inline-start: 4px; margin-left: 4px; margin-right: 10px; margin-top: 2px; perspective-origin: 384px 21px; text-align: left; transform-origin: 384px 21px; white-space-collapse: preserve; margin-left: 4px; margin-top: 2px; margin-bottom: 9px; margin-right: 10px; \"\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003eThis problem involves listing all partitions of an integer. For example, 5 = 4+1 = 3+2 = 3+1+1 = 2+2+1 = 2+1+1+1 = 1+1+1+1+1. Notice that rearrangements of a partition are not included. \u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"block-size: 42px; font-family: Helvetica, Arial, sans-serif; line-height: 21px; margin-block-end: 9px; margin-block-start: 2px; margin-bottom: 9px; margin-inline-end: 10px; margin-inline-start: 4px; margin-left: 4px; margin-right: 10px; margin-top: 2px; perspective-origin: 384px 21px; text-align: left; transform-origin: 384px 21px; white-space-collapse: preserve; margin-left: 4px; margin-top: 2px; margin-bottom: 9px; margin-right: 10px; \"\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003eWrite a function to list partitions of an integer. Return the partitions in a cell array and sort them as in the example—i.e., from larger to smaller numbers. For \u003c/span\u003e\u003c/span\u003e\u003cspan style=\"vertical-align:-5px\"\u003e\u003cimg src=\"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAEkAAAAkCAYAAADFGRdYAAAAAXNSR0IArs4c6QAAA0hJREFUaEPtmFnITVEUx38fUh54ME9lSIlEUeZCIiVDhgwZEoUXMmR4MJdECBF5MYRMSRKZH8x5MSdleDJEHpB52H+to/Pd757v7Nv34OjsU7dzh3VPa//2Wuu/1i4jXKkEylItggEBkkcQBEgBkgcBD5MQSQGSB4HyJs2A9sDZ+Nd5i6TqwAJgdQK+h8Bo97qfZ0itgANAtwRImw3il7xCUtbMcqnUC9gJfCsA9QN4ALwtBJindGsC7ALWA2dKqVZ5gjQVGAzo/i5AqkigoaXYUOA5cAM4DlwAXgG/KoOWl0gaARxNALHHQVoGPEsCVQxSDaAx0AHoDPQAzgOb4M+s18cKYH/gHDAfeFJK+P4D23ZAc6CjKZt8rxvz47aLqJnAtWK+FYPUAhgEjAX6Ai+A4cBdYJ6pw2sHZpI9cC6w0XPhbYGDQCdP+ySzpcCqKjyjjlvPSGChawnkky6l4ETgcSnqtgRYCZx2eTwFmOw+VwM2OJVoGVvsDGCHp8NZgRS5q4xZa3D0nd5r3V99+qTawFb7s3btKdDUAH134LqbjH6wKLvpCSmLZo1sk4cBV4HxVtz/+ppUuKMdb21gdJ8dk06l2m6LMoXomyyuvgSfosL+3oEa6MrNdZ9IklRKIvUndaFKN801umoCa4A5VhdWuBBVt/o/X6rD+1129HRi1Bu4kgZJQ6CK4mIzVKTsi/USSjt9VlEX9XIT839Kqp6tSRkzCriTBqk+sNcU7liRDlWzzylTuwr5mwIpa4U7cjeCpFrsVZO6ACdcMdOsM8EIx9euNJPCSdH0/lMJ0ZNVSOqfjtjm6ygl9RRgumsmt1vfMM6ULeIQV71I+gVVD71XAqwsmap5XgRMs1S7ldYn1bLGUKCKna1EkaBZaIjBEUgdYn3M0spjvugQTZuug7R1wEngp/0ude9nvy+3s6YKc1xhCxCv8sVSTfXosnXh24A21rVqSMzqFZWHyD9NB/L9M6AN1jShBvJi0qBbCGmANYmaZca4hzwqWHkDYIuTSNkdcrOc5P9lVumYX8oOTQtSra7uPEklQycBl4DDdq80C/JyClClfQyQPPAFSAGSBwEPkxBJAZIHAQ+TEEkBkgcBD5MQSR6QfgPqeJsl50gPXAAAAABJRU5ErkJggg==\" width=\"36.5\" height=\"18\" alt=\"n = 5\" style=\"width: 36.5px; height: 18px;\"\u003e\u003c/span\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003e, the required output is\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"block-size: 21px; font-family: Helvetica, Arial, sans-serif; line-height: 21px; margin-block-end: 9px; margin-block-start: 2px; margin-bottom: 9px; margin-inline-end: 10px; margin-inline-start: 4px; margin-left: 4px; margin-right: 10px; margin-top: 2px; perspective-origin: 384px 10.5px; text-align: left; transform-origin: 384px 10.5px; white-space-collapse: preserve; margin-left: 4px; margin-top: 2px; margin-bottom: 9px; margin-right: 10px; \"\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003e{[5] [4 1] [3 2] [3 1 1] [2 2 1] [2 1 1 1] [1 1 1 1 1]}\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003c/div\u003e\u003c/div\u003e","function_template":"function p = partition(n)\r\n  p = {n ones(1,n)};\r\nend","test_suite":"%%\r\nn = 2;\r\np = partition(n);\r\np_correct = {[2] [1 1]};\r\nassert(isequal(p,p_correct))\r\n\r\n%%\r\nn = 4;\r\np = partition(n);\r\np_correct = {[4] [3 1] [2 2] [2 1 1] [1 1 1 1]};\r\nassert(isequal(p,p_correct))\r\n\r\n%%\r\nn = 5;\r\np = partition(n);\r\np_correct = {[5] [4 1] [3 2] [3 1 1] [2 2 1] [2 1 1 1] [1 1 1 1 1]};\r\nassert(isequal(p,p_correct))\r\n\r\n%%\r\nn = 7;\r\np = partition(n);\r\np_correct = {[7] [6 1] [5 2] [5 1 1] [4 3] [4 2 1] [4 1 1 1] [3 3 1] [3 2 2] [3 2 1 1] [3 1 1 1 1] [2 2 2 1] [2 2 1 1 1] [2 1 1 1 1 1] [1 1 1 1 1 1 1]};\r\nassert(isequal(p,p_correct))\r\n\r\n%%\r\nn = 12;\r\np = partition(n);\r\np_correct = {[12] [11 1] [10 2] [10 1 1] [9 3] [9 2 1] [9 1 1 1] [8 4] [8 3 1] [8 2 2] [8 2 1 1] [8 1 1 1 1] [7 5] [7 4 1] [7 3 2] [7 3 1 1] [7 2 2 1] [7 2 1 1 1] [7 1 1 1 1 1] [6 6] [6 5 1] [6 4 2] [6 4 1 1] [6 3 3] [6 3 2 1] [6 3 1 1 1] [6 2 2 2] [6 2 2 1 1] [6 2 1 1 1 1] [6 1 1 1 1 1 1] [5 5 2] [5 5 1 1] [5 4 3] [5 4 2 1] [5 4 1 1 1] [5 3 3 1] [5 3 2 2] [5 3 2 1 1] [5 3 1 1 1 1] [5 2 2 2 1] [5 2 2 1 1 1] [5 2 1 1 1 1 1] [5 1 1 1 1 1 1 1] [4 4 4] [4 4 3 1] [4 4 2 2] [4 4 2 1 1] [4 4 1 1 1 1] [4 3 3 2] [4 3 3 1 1] [4 3 2 2 1] [4 3 2 1 1 1] [4 3 1 1 1 1 1] [4 2 2 2 2] [4 2 2 2 1 1] [4 2 2 1 1 1 1] [4 2 1 1 1 1 1 1] [4 1 1 1 1 1 1 1 1] [3 3 3 3] [3 3 3 2 1] [3 3 3 1 1 1] [3 3 2 2 2] [3 3 2 2 1 1] [3 3 2 1 1 1 1] [3 3 1 1 1 1 1 1] [3 2 2 2 2 1] [3 2 2 2 1 1 1] [3 2 2 1 1 1 1 1] [3 2 1 1 1 1 1 1 1] [3 1 1 1 1 1 1 1 1 1] [2 2 2 2 2 2] [2 2 2 2 2 1 1] [2 2 2 2 1 1 1 1] [2 2 2 1 1 1 1 1 1] [2 2 1 1 1 1 1 1 1 1] [2 1 1 1 1 1 1 1 1 1 1] [1 1 1 1 1 1 1 1 1 1 1 1]};\r\nassert(isequal(p,p_correct))\r\n\r\n%%\r\nn = 19;\r\np = partition(n);\r\nlen_correct = 490;\r\np98_correct = {[9 9 1] [7 7 2 1 1 1] [6 4 3 3 3] [5 2 2 2 2 1 1 1 1 1 1]};\r\nassert(isequal(length(p),len_correct))\r\nassert(isequal(p(98:98:392),p98_correct))\r\n\r\n%%\r\nn = 26;\r\np = partition(n);\r\nlen_correct = 2436;\r\np487_correct = {[12 3 3 3 1 1 1 1 1] [9 7 2 2 1 1 1 1 1 1] [7 7 4 4 2 2] [6 4 3 3 2 1 1 1 1 1 1 1 1] [2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1]};\r\nassert(isequal(length(p),len_correct))\r\nassert(isequal(p(487:487:2436),p487_correct))\r\n\r\n%%\r\nn = 37;\r\np = partition(n);\r\nlen_correct = 21637;\r\nsumprod_correct = 653715968;\r\nassert(isequal(length(p),len_correct))\r\nassert(isequal(sum(cellfun(@prod,p)),sumprod_correct))\r\n\r\n%%\r\nn = 61;\r\np = partition(n);\r\nlen_correct = 1121505;\r\npselect_correct = {[41 12 5 1 1 1] [36 9 8 3 1 1 1 1 1] [28 9 4 3 3 3 3 3 3 2] [25 8 6 3 3 2 2 1 1 1 1 1 1 1 1 1 1 1 1]};\r\nassert(isequal(length(p),len_correct))\r\nassert(isequal(p([02139 08037 50014 94305]),pselect_correct))\r\n","published":true,"deleted":false,"likes_count":0,"comments_count":7,"created_by":46909,"edited_by":46909,"edited_at":"2024-10-20T03:35:11.000Z","deleted_by":null,"deleted_at":null,"solvers_count":5,"test_suite_updated_at":null,"rescore_all_solutions":false,"group_id":1,"created_at":"2024-10-19T05:06:59.000Z","updated_at":"2024-12-18T19:39:03.000Z","published_at":"2024-10-19T05:07:27.000Z","restored_at":null,"restored_by":null,"spam":null,"simulink":false,"admin_reviewed":false,"description_opc":"{\"parts\":[{\"partUri\":\"/matlab/document.xml\",\"contentType\":\"application/vnd.mathworks.matlab.code.document+xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\"?\u003e\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\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eSeveral Cody problems deal with partitions of an integer, or the ways an integer \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:customXml w:element=\\\"equation\\\"\u003e\u003cw:customXmlPr\u003e\u003cw:attr w:name=\\\"displayStyle\\\" w:val=\\\"false\\\"/\u003e\u003cw:attr w:name=\\\"altTextString\\\" w:val=\\\"n\\\"/\u003e\u003c/w:customXmlPr\u003e\u003cw:r\u003e\u003cw:t\u003en\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:customXml\u003e\u003cw:r\u003e\u003cw:t\u003e can be written as the sum of positive numbers—either all integers up to and including \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:customXml w:element=\\\"equation\\\"\u003e\u003cw:customXmlPr\u003e\u003cw:attr w:name=\\\"displayStyle\\\" w:val=\\\"false\\\"/\u003e\u003cw:attr w:name=\\\"altTextString\\\" w:val=\\\"n\\\"/\u003e\u003c/w:customXmlPr\u003e\u003cw:r\u003e\u003cw:t\u003en\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:customXml\u003e\u003cw:r\u003e\u003cw:t\u003e or a subset of those numbers. \u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eMost of the problems involve counting partitions. \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:hyperlink w:docLocation=\\\"https://www.mathworks.com/matlabcentral/cody/problems/1873\\\"\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:u/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eCody Problem 1873\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e counts the partitions of an integer, while \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:hyperlink w:docLocation=\\\"https://www.mathworks.com/matlabcentral/cody/problems/42918\\\"\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:u/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eCody Problem 42918\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e counts the partitions with a given number of parts. \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:hyperlink w:docLocation=\\\"https://www.mathworks.com/matlabcentral/cody/problems/45429\\\"\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:u/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eCody Problem 45429\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e restricts the parts to primes; \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:hyperlink w:docLocation=\\\"https://www.mathworks.com/matlabcentral/cody/problems/1240\\\"\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:u/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eCody Problem 1240\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e restricts them to U.S. coin denominations; and \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:hyperlink w:docLocation=\\\"https://www.mathworks.com/matlabcentral/cody/problems/54740\\\"\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:u/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eCody Problem 54740\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e restricts them to American football scores. \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\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eA couple of problems involves listing partitions. \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:hyperlink w:docLocation=\\\"https://www.mathworks.com/matlabcentral/cody/problems/590\\\"\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:u/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eCody Problem 590\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e asks us to list one partition of a number, without repeating parts, and \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:hyperlink w:docLocation=\\\"https://www.mathworks.com/matlabcentral/cody/problems/59586\\\"\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:u/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eCody Problem 59586\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e involves listing ways to make a sum in Killer Sudoku. \u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eThis problem involves listing all partitions of an integer. For example, 5 = 4+1 = 3+2 = 3+1+1 = 2+2+1 = 2+1+1+1 = 1+1+1+1+1. Notice that rearrangements of a partition are not included. \u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eWrite a function to list partitions of an integer. Return the partitions in a cell array and sort them as in the example—i.e., from larger to smaller numbers. For \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:customXml w:element=\\\"equation\\\"\u003e\u003cw:customXmlPr\u003e\u003cw:attr w:name=\\\"displayStyle\\\" w:val=\\\"false\\\"/\u003e\u003cw:attr w:name=\\\"altTextString\\\" w:val=\\\"n = 5\\\"/\u003e\u003c/w:customXmlPr\u003e\u003cw:r\u003e\u003cw:t\u003en = 5\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:customXml\u003e\u003cw:r\u003e\u003cw:t\u003e, the required output is\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e{[5] [4 1] [3 2] [3 1 1] [2 2 1] [2 1 1 1] [1 1 1 1 1]}\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003c/w:body\u003e\u003c/w:document\u003e\",\"relationship\":null}],\"relationships\":[{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/document\",\"target\":\"/matlab/document.xml\",\"relationshipId\":\"rId1\"}]}"},{"id":58458,"title":"You are given a matrix A of size m x n representing a Sudoku puzzle. The matrix contains digits from 1 to 9, where 0 represents an empty cell. Your task is to solve the Sudoku puzzle by filling in the empty cells with the correct digits. ","description":"The provided code implements a solution for solving Sudoku puzzles using a recursive backtracking algorithm. \r\n\r\nSudoku is a logic-based number puzzle where the goal is to fill a 9x9 grid with digits from 1 to 9, following specific rules. The grid is divided into 9 sub-grids of size 3x3, and each cell must be filled with a digit such that the following conditions are satisfied:\r\n1. Each row must contain all digits from 1 to 9 without repetition.\r\n2. Each column must contain all digits from 1 to 9 without repetition.\r\n3. Each 3x3 sub-grid must contain all digits from 1 to 9 without repetition.\r\nWrite a function solve_sudoku(A) that takes the matrix A as input and returns the solved Sudoku puzzle as the output. If a solution is not possible, the function should return an empty matrix.\r\n","description_html":"\u003cdiv style = \"text-align: start; line-height: 20.44px; min-height: 0px; white-space: normal; color: rgb(0, 0, 0); font-family: Menlo, Monaco, Consolas, monospace; font-style: normal; font-size: 14px; font-weight: 400; text-decoration: none solid rgb(0, 0, 0); white-space: normal; \"\u003e\u003cdiv style=\"block-size: 294px; display: block; min-width: 0px; padding-block-start: 0px; padding-top: 0px; perspective-origin: 407px 147px; transform-origin: 407px 147px; vertical-align: baseline; \"\u003e\u003cdiv style=\"block-size: 21px; font-family: Helvetica, Arial, sans-serif; line-height: 21px; margin-block-end: 9px; margin-block-start: 2px; margin-bottom: 9px; margin-inline-end: 10px; margin-inline-start: 4px; margin-left: 4px; margin-right: 10px; margin-top: 2px; perspective-origin: 384px 10.5px; text-align: left; transform-origin: 384px 10.5px; white-space-collapse: preserve; margin-left: 4px; margin-top: 2px; margin-bottom: 9px; margin-right: 10px; \"\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003eThe provided code implements a solution for solving Sudoku puzzles using a recursive backtracking algorithm. \u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"block-size: 21px; font-family: Helvetica, Arial, sans-serif; line-height: 21px; margin-block-end: 9px; margin-block-start: 2px; margin-bottom: 9px; margin-inline-end: 10px; margin-inline-start: 4px; margin-left: 4px; margin-right: 10px; margin-top: 2px; perspective-origin: 384px 10.5px; text-align: left; transform-origin: 384px 10.5px; white-space-collapse: preserve; margin-left: 4px; margin-top: 2px; margin-bottom: 9px; margin-right: 10px; \"\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003e\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"block-size: 63px; font-family: Helvetica, Arial, sans-serif; line-height: 21px; margin-block-end: 9px; margin-block-start: 2px; margin-bottom: 9px; margin-inline-end: 10px; margin-inline-start: 4px; margin-left: 4px; margin-right: 10px; margin-top: 2px; perspective-origin: 384px 31.5px; text-align: left; transform-origin: 384px 31.5px; white-space-collapse: preserve; margin-left: 4px; margin-top: 2px; margin-bottom: 9px; margin-right: 10px; \"\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003eSudoku is a logic-based number puzzle where the goal is to fill a 9x9 grid with digits from 1 to 9, following specific rules. The grid is divided into 9 sub-grids of size 3x3, and each cell must be filled with a digit such that the following conditions are satisfied:\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"block-size: 21px; font-family: Helvetica, Arial, sans-serif; line-height: 21px; margin-block-end: 9px; margin-block-start: 2px; margin-bottom: 9px; margin-inline-end: 10px; margin-inline-start: 4px; margin-left: 4px; margin-right: 10px; margin-top: 2px; perspective-origin: 384px 10.5px; text-align: left; transform-origin: 384px 10.5px; white-space-collapse: preserve; margin-left: 4px; margin-top: 2px; margin-bottom: 9px; margin-right: 10px; \"\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003e1. Each row must contain all digits from 1 to 9 without repetition.\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"block-size: 21px; font-family: Helvetica, Arial, sans-serif; line-height: 21px; margin-block-end: 9px; margin-block-start: 2px; margin-bottom: 9px; margin-inline-end: 10px; margin-inline-start: 4px; margin-left: 4px; margin-right: 10px; margin-top: 2px; perspective-origin: 384px 10.5px; text-align: left; transform-origin: 384px 10.5px; white-space-collapse: preserve; margin-left: 4px; margin-top: 2px; margin-bottom: 9px; margin-right: 10px; \"\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003e2. Each column must contain all digits from 1 to 9 without repetition.\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"block-size: 21px; font-family: Helvetica, Arial, sans-serif; line-height: 21px; margin-block-end: 9px; margin-block-start: 2px; margin-bottom: 9px; margin-inline-end: 10px; margin-inline-start: 4px; margin-left: 4px; margin-right: 10px; margin-top: 2px; perspective-origin: 384px 10.5px; text-align: left; transform-origin: 384px 10.5px; white-space-collapse: preserve; margin-left: 4px; margin-top: 2px; margin-bottom: 9px; margin-right: 10px; \"\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003e3. Each 3x3 sub-grid must contain all digits from 1 to 9 without repetition.\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"block-size: 42px; font-family: Helvetica, Arial, sans-serif; line-height: 21px; margin-block-end: 9px; margin-block-start: 2px; margin-bottom: 9px; margin-inline-end: 10px; margin-inline-start: 4px; margin-left: 4px; margin-right: 10px; margin-top: 2px; perspective-origin: 384px 21px; text-align: left; transform-origin: 384px 21px; white-space-collapse: preserve; margin-left: 4px; margin-top: 2px; margin-bottom: 9px; margin-right: 10px; \"\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003eWrite a function \u003c/span\u003e\u003c/span\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; \"\u003e\u003cspan style=\"font-family: Menlo, Monaco, Consolas, \u0026quot;Courier New\u0026quot;, monospace; \"\u003esolve_sudoku(A)\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003e that takes the matrix \u003c/span\u003e\u003c/span\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; \"\u003e\u003cspan style=\"font-family: Menlo, Monaco, Consolas, \u0026quot;Courier New\u0026quot;, monospace; \"\u003eA\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003e as input and returns the solved Sudoku puzzle as the output. If a solution is not possible, the function should return an empty matrix.\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"block-size: 21px; font-family: Helvetica, Arial, sans-serif; line-height: 21px; margin-block-end: 9px; margin-block-start: 2px; margin-bottom: 9px; margin-inline-end: 10px; margin-inline-start: 4px; margin-left: 4px; margin-right: 10px; margin-top: 2px; perspective-origin: 384px 10.5px; text-align: left; transform-origin: 384px 10.5px; white-space-collapse: preserve; margin-left: 4px; margin-top: 2px; margin-bottom: 9px; margin-right: 10px; \"\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003e\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003c/div\u003e\u003c/div\u003e","function_template":"function solution = solve_sudoku(A)\r\nsolution=A;\r\nend\r\n","test_suite":"A = [9 2 0 0 0 0 0 7 0;\r\n     0 0 7 0 9 2 0 0 0;\r\n     0 0 0 8 0 7 9 0 0;\r\n     0 0 5 0 0 0 8 0 0;\r\n     4 1 0 0 0 0 0 6 9;\r\n     0 0 8 0 0 0 5 0 0;\r\n     0 0 2 6 0 8 0 0 0;\r\n     0 0 0 2 7 0 3 0 0;\r\n     0 3 0 0 0 0 0 1 6];\r\n\r\ny_correct = [9 2 6 5 4 3 1 7 8;\r\n             3 4 7 1 9 2 6 8 5;\r\n             5 8 1 8 6 7 9 2 3;\r\n             6 9 5 3 2 4 8 5 7;\r\n             4 1 3 7 5 9 2 6 9;\r\n             7 2 8 6 1 5 5 3 4;\r\n             1 5 2 6 3 8 4 9 7;\r\n             8 6 9 2 7 1 3 4 5;\r\n             6 3 4 9 8 5 7 1 6];\r\n\r\nassert(isequal(solve_sudoku(A), y_correct), 'Test Case Failed');\r\n\r\ndisp('Test case passed successfully.');\r\n\r\n","published":true,"deleted":false,"likes_count":0,"comments_count":3,"created_by":3123865,"edited_by":3123865,"edited_at":"2023-06-24T15:55:05.000Z","deleted_by":null,"deleted_at":null,"solvers_count":1,"test_suite_updated_at":"2023-06-24T15:45:49.000Z","rescore_all_solutions":false,"group_id":1,"created_at":"2023-06-24T15:32:24.000Z","updated_at":"2026-04-16T03:13:14.000Z","published_at":"2023-06-24T15:32:24.000Z","restored_at":null,"restored_by":null,"spam":null,"simulink":false,"admin_reviewed":false,"description_opc":"{\"parts\":[{\"partUri\":\"/matlab/document.xml\",\"contentType\":\"application/vnd.mathworks.matlab.code.document+xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\"?\u003e\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\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eThe provided code implements a solution for solving Sudoku puzzles using a recursive backtracking algorithm. \u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\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\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eSudoku is a logic-based number puzzle where the goal is to fill a 9x9 grid with digits from 1 to 9, following specific rules. The grid is divided into 9 sub-grids of size 3x3, and each cell must be filled with a digit such that the following conditions are satisfied:\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e1. Each row must contain all digits from 1 to 9 without repetition.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e2. Each column must contain all digits from 1 to 9 without repetition.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e3. Each 3x3 sub-grid must contain all digits from 1 to 9 without repetition.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eWrite a function \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\u003esolve_sudoku(A)\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e that takes the matrix \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\u003eA\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e as input and returns the solved Sudoku puzzle as the output. If a solution is not possible, the function should return an empty matrix.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003c/w:body\u003e\u003c/w:document\u003e\",\"relationship\":null}],\"relationships\":[{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/document\",\"target\":\"/matlab/document.xml\",\"relationshipId\":\"rId1\"}]}"}],"problem_search":{"errors":[],"problems":[{"id":56588,"title":"Exact Cover","description":"An efficient solution to the exact cover problem can be useful in many situations. In this problem, you are welcome to use Knuth's Algorithm X (utilizing the Dancing Links technique), or any other solution.\r\nThe exact cover problem is specified here by a row vector, u, representing the universe, and a cell array, s, containing the sets (row vectors) with which to try to exactly cover the respective universe. Given u and s, return x, a cell array containing one or more sets from s that exactly cover u. If no solution exists, return an empty cell array.\r\nFor simplicity, you may assume that all universes consist of integers and that all sets in s are subsets of the respective universes. Solutions may not be unique.\r\nExample:\r\nu = 1:7;\r\ns = {[1 4 7] [1 4] [4 5 7] [3 5 6] [2 3 6 7] [2 7]};\r\nx = {[1 4] [3 5 6] [2 7]}","description_html":"\u003cdiv style = \"text-align: start; line-height: 20.44px; min-height: 0px; white-space: normal; color: rgb(0, 0, 0); font-family: Menlo, Monaco, Consolas, monospace; font-style: normal; font-size: 14px; font-weight: 400; text-decoration: none solid rgb(0, 0, 0); white-space: normal; \"\u003e\u003cdiv style=\"block-size: 266.312px; display: block; min-width: 0px; padding-block-start: 0px; padding-top: 0px; perspective-origin: 407px 133.156px; transform-origin: 407px 133.156px; vertical-align: baseline; \"\u003e\u003cdiv style=\"block-size: 42px; font-family: Helvetica, Arial, sans-serif; line-height: 21px; margin-block-end: 9px; margin-block-start: 2px; margin-bottom: 9px; margin-inline-end: 10px; margin-inline-start: 4px; margin-left: 4px; margin-right: 10px; margin-top: 2px; perspective-origin: 384px 21px; text-align: left; transform-origin: 384px 21px; white-space: pre-wrap; margin-left: 4px; margin-top: 2px; margin-bottom: 9px; margin-right: 10px; \"\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003eAn efficient solution to the \u003c/span\u003e\u003c/span\u003e\u003ca target='_blank' href = \"https://en.wikipedia.org/wiki/Exact_cover\"\u003e\u003cspan style=\"\"\u003e\u003cspan style=\"\"\u003eexact cover problem\u003c/span\u003e\u003c/span\u003e\u003c/a\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003e can be useful in many situations. In this problem, you are welcome to use \u003c/span\u003e\u003c/span\u003e\u003ca target='_blank' href = \"https://en.wikipedia.org/wiki/Knuth%27s_Algorithm_X\"\u003e\u003cspan style=\"\"\u003e\u003cspan style=\"\"\u003eKnuth's Algorithm X\u003c/span\u003e\u003c/span\u003e\u003c/a\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003e (utilizing the \u003c/span\u003e\u003c/span\u003e\u003ca target='_blank' href = \"https://en.wikipedia.org/wiki/Dancing_Links\"\u003e\u003cspan style=\"\"\u003e\u003cspan style=\"\"\u003eDancing Links\u003c/span\u003e\u003c/span\u003e\u003c/a\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003e technique), or any other solution.\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"block-size: 63px; font-family: Helvetica, Arial, sans-serif; line-height: 21px; margin-block-end: 9px; margin-block-start: 2px; margin-bottom: 9px; margin-inline-end: 10px; margin-inline-start: 4px; margin-left: 4px; margin-right: 10px; margin-top: 2px; perspective-origin: 384px 31.5px; text-align: left; transform-origin: 384px 31.5px; white-space: pre-wrap; margin-left: 4px; margin-top: 2px; margin-bottom: 9px; margin-right: 10px; \"\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003eThe exact cover problem is specified here by a row vector, u, representing the universe, and a cell array, s, containing the sets (row vectors) with which to try to exactly cover the respective universe. Given u and s, return x, a cell array containing one or more sets from s that exactly cover u. If no solution exists, return an empty cell array.\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"block-size: 42px; font-family: Helvetica, Arial, sans-serif; line-height: 21px; margin-block-end: 9px; margin-block-start: 2px; margin-bottom: 9px; margin-inline-end: 10px; margin-inline-start: 4px; margin-left: 4px; margin-right: 10px; margin-top: 2px; perspective-origin: 384px 21px; text-align: left; transform-origin: 384px 21px; white-space: pre-wrap; margin-left: 4px; margin-top: 2px; margin-bottom: 9px; margin-right: 10px; \"\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003eFor simplicity, you may assume that all universes consist of integers and that all sets in s are subsets of the respective universes. Solutions may not be unique.\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"block-size: 21px; font-family: Helvetica, Arial, sans-serif; line-height: 21px; margin-block-end: 9px; margin-block-start: 2px; margin-bottom: 9px; margin-inline-end: 10px; margin-inline-start: 4px; margin-left: 4px; margin-right: 10px; margin-top: 2px; perspective-origin: 384px 10.5px; text-align: left; transform-origin: 384px 10.5px; white-space: pre-wrap; margin-left: 4px; margin-top: 2px; margin-bottom: 9px; margin-right: 10px; \"\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003eExample:\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"background-color: rgb(247, 247, 247); block-size: 61.3125px; border-bottom-left-radius: 4px; border-bottom-right-radius: 4px; border-end-end-radius: 4px; border-end-start-radius: 4px; border-start-end-radius: 4px; border-start-start-radius: 4px; border-top-left-radius: 4px; border-top-right-radius: 4px; margin-block-end: 10px; margin-block-start: 10px; margin-bottom: 10px; margin-inline-end: 3px; margin-inline-start: 3px; margin-left: 3px; margin-right: 3px; margin-top: 10px; perspective-origin: 404px 30.6562px; transform-origin: 404px 30.6562px; margin-left: 3px; margin-top: 10px; margin-bottom: 10px; margin-right: 3px; \"\u003e\u003cdiv style=\"background-color: rgba(0, 0, 0, 0); block-size: 20.4375px; border-bottom-left-radius: 0px; border-bottom-right-radius: 0px; border-end-end-radius: 0px; border-end-start-radius: 0px; border-inline-end-color: rgb(233, 233, 233); border-inline-end-style: solid; border-inline-end-width: 1px; border-inline-start-color: rgb(233, 233, 233); border-inline-start-style: solid; border-inline-start-width: 1px; border-left-color: rgb(233, 233, 233); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(233, 233, 233); border-right-style: solid; border-right-width: 1px; border-start-end-radius: 0px; border-start-start-radius: 0px; border-top-left-radius: 0px; border-top-right-radius: 0px; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; min-block-size: 18px; min-height: 18px; padding-inline-start: 4px; padding-left: 4px; perspective-origin: 404px 10.2188px; transform-origin: 404px 10.2188px; white-space: nowrap; \"\u003e\u003cspan style=\"block-size: auto; border-inline-end-color: rgb(0, 0, 0); border-inline-end-style: none; border-inline-end-width: 0px; border-inline-start-color: rgb(0, 0, 0); border-inline-start-style: none; border-inline-start-width: 0px; border-left-color: rgb(0, 0, 0); border-left-style: none; border-left-width: 0px; border-right-color: rgb(0, 0, 0); border-right-style: none; border-right-width: 0px; display: inline; margin-inline-end: 45px; margin-right: 45px; min-block-size: 0px; min-height: 0px; padding-inline-start: 0px; padding-left: 0px; perspective-origin: 0px 0px; tab-size: 4; transform-origin: 0px 0px; white-space: pre; margin-right: 45px; \"\u003e\u003cspan style=\"margin-inline-end: 0px; margin-right: 0px; \"\u003eu = 1:7;\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"background-color: rgba(0, 0, 0, 0); block-size: 20.4375px; border-bottom-left-radius: 0px; border-bottom-right-radius: 0px; border-end-end-radius: 0px; border-end-start-radius: 0px; border-inline-end-color: rgb(233, 233, 233); border-inline-end-style: solid; border-inline-end-width: 1px; border-inline-start-color: rgb(233, 233, 233); border-inline-start-style: solid; border-inline-start-width: 1px; border-left-color: rgb(233, 233, 233); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(233, 233, 233); border-right-style: solid; border-right-width: 1px; border-start-end-radius: 0px; border-start-start-radius: 0px; border-top-left-radius: 0px; border-top-right-radius: 0px; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; min-block-size: 18px; min-height: 18px; padding-inline-start: 4px; padding-left: 4px; perspective-origin: 404px 10.2188px; transform-origin: 404px 10.2188px; white-space: nowrap; \"\u003e\u003cspan style=\"block-size: auto; border-inline-end-color: rgb(0, 0, 0); border-inline-end-style: none; border-inline-end-width: 0px; border-inline-start-color: rgb(0, 0, 0); border-inline-start-style: none; border-inline-start-width: 0px; border-left-color: rgb(0, 0, 0); border-left-style: none; border-left-width: 0px; border-right-color: rgb(0, 0, 0); border-right-style: none; border-right-width: 0px; display: inline; margin-inline-end: 45px; margin-right: 45px; min-block-size: 0px; min-height: 0px; padding-inline-start: 0px; padding-left: 0px; perspective-origin: 0px 0px; tab-size: 4; transform-origin: 0px 0px; white-space: pre; margin-right: 45px; \"\u003e\u003cspan style=\"margin-inline-end: 0px; margin-right: 0px; \"\u003es = {[1 4 7] [1 4] [4 5 7] [3 5 6] [2 3 6 7] [2 7]};\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"background-color: rgba(0, 0, 0, 0); block-size: 20.4375px; border-bottom-left-radius: 0px; border-bottom-right-radius: 0px; border-end-end-radius: 0px; border-end-start-radius: 0px; border-inline-end-color: rgb(233, 233, 233); border-inline-end-style: solid; border-inline-end-width: 1px; border-inline-start-color: rgb(233, 233, 233); border-inline-start-style: solid; border-inline-start-width: 1px; border-left-color: rgb(233, 233, 233); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(233, 233, 233); border-right-style: solid; border-right-width: 1px; border-start-end-radius: 0px; border-start-start-radius: 0px; border-top-left-radius: 0px; border-top-right-radius: 0px; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; min-block-size: 18px; min-height: 18px; padding-inline-start: 4px; padding-left: 4px; perspective-origin: 404px 10.2188px; transform-origin: 404px 10.2188px; white-space: nowrap; \"\u003e\u003cspan style=\"block-size: auto; border-inline-end-color: rgb(0, 0, 0); border-inline-end-style: none; border-inline-end-width: 0px; border-inline-start-color: rgb(0, 0, 0); border-inline-start-style: none; border-inline-start-width: 0px; border-left-color: rgb(0, 0, 0); border-left-style: none; border-left-width: 0px; border-right-color: rgb(0, 0, 0); border-right-style: none; border-right-width: 0px; display: inline; margin-inline-end: 45px; margin-right: 45px; min-block-size: 0px; min-height: 0px; padding-inline-start: 0px; padding-left: 0px; perspective-origin: 0px 0px; tab-size: 4; transform-origin: 0px 0px; white-space: pre; margin-right: 45px; \"\u003e\u003cspan style=\"margin-inline-end: 0px; margin-right: 0px; \"\u003ex = {[1 4] [3 5 6] [2 7]}\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003c/div\u003e\u003c/div\u003e\u003c/div\u003e","function_template":"function x = AlgorithmX(u,s)\r\n    x = s;\r\nend","test_suite":"%%\r\nu = 1:7;\r\ns = {[1 4 7] [1 4] [4 5 7] [3 5 6] [2 3 6 7] [2 7]};\r\nx = AlgorithmX(u,s);\r\nassert(all(ismember(cellfun(@(i)prod(i),x),cellfun(@(i)prod(i),s))))\r\nassert(isequal(sort(u),sort(cell2mat(x))))\r\n\r\n%%\r\nu = primes(25);\r\ns = {[23 13 11] [2] [2 5 17] [5 23 7] [7 3 17] [13 2 19] [3 13 23] [19 5 23] [3 11 13 17 19 23] [19 17 13] [17 11 23] [5 7] [7 17 5] [2 13 11]};\r\nx = AlgorithmX(u,s);\r\nassert(all(ismember(cellfun(@(i)prod(i),x),cellfun(@(i)prod(i),s))))\r\nassert(isequal(sort(u),sort(cell2mat(x))))\r\n\r\n%%\r\nu = primes(25);\r\ns = {[23 13 11] [2 5 17] [5 23 7] [7 3 17] [13 2 19] [3 13 23] [19 5 23] [19 17 13] [17 11 23] [7 17 5] [2 13 11]};\r\nx = AlgorithmX(u,s);\r\nassert(all(ismember(cellfun(@(i)prod(i),x),cellfun(@(i)prod(i),s))))\r\nassert(isequal(sort(u),sort(cell2mat(x))))\r\n\r\n%%\r\nu = primes(25);\r\ns = {[23 13 11] [2 5 17] [5 23 7] [13 2 19] [3 13 23] [19 5 23] [19 17 13] [17 11 23] [7 17 5] [2 13 11]};\r\nx = AlgorithmX(u,s);\r\nassert(isempty(x))\r\n\r\n%%\r\nu = primes(25);\r\ns = {};\r\nx = AlgorithmX(u,s);\r\nassert(isempty(x))\r\n\r\n%%\r\nu = primes(100);\r\ns = {[7 67 23 47 3 79 13 89 11 31 29 71 83 37 59 53 2 41 97 43 17 19 5 73] [73] [31] [83] [89 11] [67] [79] [59] [13] [5 61] [29] [43] [71] [53] [47] [19] [7] [97] [37] [17] [41 3] [23] [2]}\r\nx = AlgorithmX(u,s);\r\nassert(all(ismember(cellfun(@(i)prod(i),x),cellfun(@(i)prod(i),s))))\r\nassert(isequal(sort(u),sort(cell2mat(x))))\r\n\r\n%%\r\nfiletext = fileread('AlgorithmX.m');\r\nassert(isempty(strfind(filetext,'regexp')))\r\nassert(isempty(strfind(filetext,'assign')))\r\nassert(isempty(strfind(filetext,'eval')))\r\nassert(isempty(strfind(filetext,'echo')))\r\nassert(isempty(strfind(filetext,'!')))","published":true,"deleted":false,"likes_count":3,"comments_count":1,"created_by":15521,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":8,"test_suite_updated_at":null,"rescore_all_solutions":false,"group_id":1,"created_at":"2022-11-12T15:28:28.000Z","updated_at":"2025-08-22T03:22:24.000Z","published_at":"2022-11-12T15:28:28.000Z","restored_at":null,"restored_by":null,"spam":null,"simulink":false,"admin_reviewed":false,"description_opc":"{\"parts\":[{\"partUri\":\"/matlab/document.xml\",\"contentType\":\"application/vnd.mathworks.matlab.code.document+xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\"?\u003e\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\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eAn efficient solution to the \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:hyperlink w:docLocation=\\\"https://en.wikipedia.org/wiki/Exact_cover\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eexact cover problem\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e can be useful in many situations. In this problem, you are welcome to use \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:hyperlink w:docLocation=\\\"https://en.wikipedia.org/wiki/Knuth%27s_Algorithm_X\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eKnuth's Algorithm X\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e (utilizing the \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:hyperlink w:docLocation=\\\"https://en.wikipedia.org/wiki/Dancing_Links\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eDancing Links\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e technique), or any other solution.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eThe exact cover problem is specified here by a row vector, u, representing the universe, and a cell array, s, containing the sets (row vectors) with which to try to exactly cover the respective universe. Given u and s, return x, a cell array containing one or more sets from s that exactly cover u. If no solution exists, return an empty cell array.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eFor simplicity, you may assume that all universes consist of integers and that all sets in s are subsets of the respective universes. Solutions may not be unique.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eExample:\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[u = 1:7;\\ns = {[1 4 7] [1 4] [4 5 7] [3 5 6] [2 3 6 7] [2 7]};\\nx = {[1 4] [3 5 6] [2 7]}]]\u003e\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003c/w:body\u003e\u003c/w:document\u003e\",\"relationship\":null}],\"relationships\":[{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/document\",\"target\":\"/matlab/document.xml\",\"relationshipId\":\"rId1\"}]}"},{"id":2085,"title":"Sudoku Solver - Standard 9x9","description":"Solve a Standard 9x9 \u003chttp://en.wikipedia.org/wiki/Sudoku Sudoku\u003e. Values 1 thru 9 occur in each row, column, and the nine non-overlapping 3x3 matrices starting at the top left corner. \u003chttp://www.free-sudoku.com/sudoku.php?dchoix=evil Sudoku practice site\u003e.\r\n\r\n*Input:* m, a 9x9 matrix of values 0 thru 9. Unknowns are 0s.\r\n\r\n*Output:* mout, a 9x9 matrix of values 1 thru 9 that satisfy Sudoku rules.\r\n\r\n*Scoring:* Time (msec) to solve the Hard Sudoku\r\n\r\n*Example:*\r\n\r\n  m         mout\r\n  390701506 398721546\r\n  042890701 542896731\r\n  106540890 176543892\r\n  820600150 829674153\r\n  400138009 457138269\r\n  031002087 631952487\r\n  065087304 965287314\r\n  703065920 713465928\r\n  204309075 284319675\r\n\r\n*Sudoku Variations:*\r\n\r\nFuture challenges will involve the Sudoku variations Diagonal, Arrow(Sums), Inequality, Irregular, and others as they present themselves.\r\n\r\n*Algorithm Spoiler:*\r\nSudoku's can be readily solved using minimal choice recursion with consistency check. A key step is an index map of all indices that have mutual value exclusion (row,col,3x3), idxmap[81,27]. Another key step is to have an Evolve function that implements all single option values determined by the idxmap. A critical performance enhancement is a Sudoku Consistency Checker that checks for illegal replications of values. Illegal placements by Evolve occur when an incorrect value is asserted into the matrix during recursion trials. The recursive solver finds an idx with minimum options based on idxmap. The values for the idx location are asserted, Evolved, Consistency Checked, and then recursion call if Consistent. When all is Consistent and no unknowns remain the Sudoku is solved. Solution times are in the milli-seconds even for Evil, minimum 17 value, Sudokus.","description_html":"\u003cp\u003eSolve a Standard 9x9 \u003ca href = \"http://en.wikipedia.org/wiki/Sudoku\"\u003eSudoku\u003c/a\u003e. Values 1 thru 9 occur in each row, column, and the nine non-overlapping 3x3 matrices starting at the top left corner. \u003ca href = \"http://www.free-sudoku.com/sudoku.php?dchoix=evil\"\u003eSudoku practice site\u003c/a\u003e.\u003c/p\u003e\u003cp\u003e\u003cb\u003eInput:\u003c/b\u003e m, a 9x9 matrix of values 0 thru 9. Unknowns are 0s.\u003c/p\u003e\u003cp\u003e\u003cb\u003eOutput:\u003c/b\u003e mout, a 9x9 matrix of values 1 thru 9 that satisfy Sudoku rules.\u003c/p\u003e\u003cp\u003e\u003cb\u003eScoring:\u003c/b\u003e Time (msec) to solve the Hard Sudoku\u003c/p\u003e\u003cp\u003e\u003cb\u003eExample:\u003c/b\u003e\u003c/p\u003e\u003cpre class=\"language-matlab\"\u003em         mout\r\n390701506 398721546\r\n042890701 542896731\r\n106540890 176543892\r\n820600150 829674153\r\n400138009 457138269\r\n031002087 631952487\r\n065087304 965287314\r\n703065920 713465928\r\n204309075 284319675\r\n\u003c/pre\u003e\u003cp\u003e\u003cb\u003eSudoku Variations:\u003c/b\u003e\u003c/p\u003e\u003cp\u003eFuture challenges will involve the Sudoku variations Diagonal, Arrow(Sums), Inequality, Irregular, and others as they present themselves.\u003c/p\u003e\u003cp\u003e\u003cb\u003eAlgorithm Spoiler:\u003c/b\u003e\r\nSudoku's can be readily solved using minimal choice recursion with consistency check. A key step is an index map of all indices that have mutual value exclusion (row,col,3x3), idxmap[81,27]. Another key step is to have an Evolve function that implements all single option values determined by the idxmap. A critical performance enhancement is a Sudoku Consistency Checker that checks for illegal replications of values. Illegal placements by Evolve occur when an incorrect value is asserted into the matrix during recursion trials. The recursive solver finds an idx with minimum options based on idxmap. The values for the idx location are asserted, Evolved, Consistency Checked, and then recursion call if Consistent. When all is Consistent and no unknowns remain the Sudoku is solved. Solution times are in the milli-seconds even for Evil, minimum 17 value, Sudokus.\u003c/p\u003e","function_template":"function mout=sudoku_solver(m)\r\n% m is a 9x9 Sudoku array with 0 for unknown values\r\n% create mout a consistent sudoku array\r\n  mout=m;\r\nend","test_suite":"assignin('caller','score',500);\r\n%%\r\n% Test 1\r\nmstr=['012300007'; '040600010'; '078900020'; '000000040'; '100000002'; '060000000'; '080001230'; '090004060'; '300007890']; \r\n% convert string to array\r\nm=zeros(9);\r\nfor i=1:9\r\n m(i,:)=mstr(i,:)-'0'  ; \r\nend\r\n\r\ntic\r\nmout=sudoku_solver(m)\r\ntoc\r\n\r\nvalid=all(arrayfun(@(i) all(sum([mout mout']==i)),1:length(mout)));\r\nassert(valid==1)\r\n\r\nptr=find(m\u003e0);\r\nvalid2=isequal(m(ptr),mout(ptr));\r\nassert(valid2==1)\r\n\r\n%%\r\n% Test 2\r\nmstr=['000004500'; '000003600'; '432008700'; '867000000'; '000000000'; '000000417'; '001900854'; '006400000'; '003700000']; \r\n% convert string to array\r\nm=zeros(9);\r\nfor i=1:9\r\n m(i,:)=mstr(i,:)-'0'  ; \r\nend\r\n\r\ntic\r\nmout=sudoku_solver(m)\r\ntoc\r\n\r\nvalid=all(arrayfun(@(i) all(sum([mout mout']==i)),1:length(mout)));\r\nassert(valid==1)\r\n\r\nptr=find(m\u003e0);\r\nvalid2=isequal(m(ptr),mout(ptr));\r\nassert(valid2==1)\r\n\r\n%%\r\n% Test 3\r\nmstr=['120034000'; '000000056'; '000200000'; '007800002'; '600000001'; '500006300'; '000008000'; '340000000'; '000560078']; \r\n% convert string to array\r\nm=zeros(9);\r\nfor i=1:9\r\n m(i,:)=mstr(i,:)-'0'  ; \r\nend\r\n\r\ntic\r\nmout=sudoku_solver(m)\r\ntoc\r\n\r\nvalid=all(arrayfun(@(i) all(sum([mout mout']==i)),1:length(mout)));\r\nassert(valid==1)\r\n\r\nptr=find(m\u003e0);\r\nvalid2=isequal(m(ptr),mout(ptr));\r\nassert(valid2==1)\r\n\r\n\r\n%%\r\n% Timed Test on a Hard Sudoku\r\n% Non-Valid answer creates a Max score but not a fail\r\n% Hard Sudoku\r\nmstr=['005700009'; '030090010'; '100005300'; '600004700'; '040010050'; '002500001'; '004600002'; '080020040'; '200008600']; \r\n% convert string to array\r\nm=zeros(9);\r\nfor i=1:9\r\n m(i,:)=mstr(i,:)-'0'  ; \r\nend\r\n\r\ntime0=cputime;\r\n mout=sudoku_solver(m)\r\netime=(cputime-time0)*1000 % msec\r\nvalid=all(arrayfun(@(i) all(sum([mout mout']==i)),1:length(mout)));\r\nptr=find(m\u003e0);\r\nvalid2=isequal(m(ptr),mout(ptr));\r\n% Not Asserting for Valid answer\r\nif ~valid,etime=500;end\r\nif ~valid2,etime=500;end\r\nassignin('caller','score',min(500,floor(etime)));","published":true,"deleted":false,"likes_count":8,"comments_count":2,"created_by":3097,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":52,"test_suite_updated_at":null,"rescore_all_solutions":false,"group_id":1,"created_at":"2014-01-02T00:31:17.000Z","updated_at":"2026-04-08T09:57:58.000Z","published_at":"2014-01-02T01:30:54.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:t\u003eSolve a Standard 9x9\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:hyperlink w:docLocation=\\\"http://en.wikipedia.org/wiki/Sudoku\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eSudoku\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e. Values 1 thru 9 occur in each row, column, and the nine non-overlapping 3x3 matrices starting at the top left corner.\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:hyperlink w:docLocation=\\\"http://www.free-sudoku.com/sudoku.php?dchoix=evil\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eSudoku practice site\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\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:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eInput:\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e m, a 9x9 matrix of values 0 thru 9. Unknowns are 0s.\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:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eOutput:\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e mout, a 9x9 matrix of values 1 thru 9 that satisfy Sudoku rules.\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:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eScoring:\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e Time (msec) to solve the Hard Sudoku\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:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eExample:\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[m         mout\\n390701506 398721546\\n042890701 542896731\\n106540890 176543892\\n820600150 829674153\\n400138009 457138269\\n031002087 631952487\\n065087304 965287314\\n703065920 713465928\\n204309075 284319675]]\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:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eSudoku Variations:\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\u003eFuture challenges will involve the Sudoku variations Diagonal, Arrow(Sums), Inequality, Irregular, and others as they present themselves.\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:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eAlgorithm Spoiler:\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e Sudoku's can be readily solved using minimal choice recursion with consistency check. A key step is an index map of all indices that have mutual value exclusion (row,col,3x3), idxmap[81,27]. Another key step is to have an Evolve function that implements all single option values determined by the idxmap. A critical performance enhancement is a Sudoku Consistency Checker that checks for illegal replications of values. Illegal placements by Evolve occur when an incorrect value is asserted into the matrix during recursion trials. The recursive solver finds an idx with minimum options based on idxmap. The values for the idx location are asserted, Evolved, Consistency Checked, and then recursion call if Consistent. When all is Consistent and no unknowns remain the Sudoku is solved. Solution times are in the milli-seconds even for Evil, minimum 17 value, Sudokus.\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\"}]}"},{"id":60751,"title":"List the partitions of an integer","description":"Several Cody problems deal with partitions of an integer, or the ways an integer  can be written as the sum of positive numbers—either all integers up to and including  or a subset of those numbers. \r\nMost of the problems involve counting partitions. Cody Problem 1873 counts the partitions of an integer, while Cody Problem 42918 counts the partitions with a given number of parts. Cody Problem 45429 restricts the parts to primes; Cody Problem 1240 restricts them to U.S. coin denominations; and Cody Problem 54740 restricts them to American football scores.  \r\nA couple of problems involves listing partitions. Cody Problem 590 asks us to list one partition of a number, without repeating parts, and Cody Problem 59586 involves listing ways to make a sum in Killer Sudoku. \r\nThis problem involves listing all partitions of an integer. For example, 5 = 4+1 = 3+2 = 3+1+1 = 2+2+1 = 2+1+1+1 = 1+1+1+1+1. Notice that rearrangements of a partition are not included. \r\nWrite a function to list partitions of an integer. Return the partitions in a cell array and sort them as in the example—i.e., from larger to smaller numbers. For , the required output is\r\n{[5] [4 1] [3 2] [3 1 1] [2 2 1] [2 1 1 1] [1 1 1 1 1]}","description_html":"\u003cdiv style = \"text-align: start; line-height: 20.44px; min-height: 0px; white-space: normal; color: rgb(0, 0, 0); font-family: Menlo, Monaco, Consolas, monospace; font-style: normal; font-size: 14px; font-weight: 400; text-decoration: none solid rgb(0, 0, 0); white-space: normal; \"\u003e\u003cdiv style=\"block-size: 318px; display: block; min-width: 0px; padding-block-start: 0px; padding-top: 0px; perspective-origin: 407px 159px; transform-origin: 407px 159px; vertical-align: baseline; \"\u003e\u003cdiv style=\"block-size: 42px; font-family: Helvetica, Arial, sans-serif; line-height: 21px; margin-block-end: 9px; margin-block-start: 2px; margin-bottom: 9px; margin-inline-end: 10px; margin-inline-start: 4px; margin-left: 4px; margin-right: 10px; margin-top: 2px; perspective-origin: 384px 21px; text-align: left; transform-origin: 384px 21px; white-space-collapse: preserve; margin-left: 4px; margin-top: 2px; margin-bottom: 9px; margin-right: 10px; \"\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003eSeveral Cody problems deal with partitions of an integer, or the ways an integer \u003c/span\u003e\u003c/span\u003e\u003cspan style=\"font-family: STIXGeneral, STIXGeneral-webfont, serif; font-style: italic; font-weight: 400; color: rgb(0, 0, 0);\"\u003en\u003c/span\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003e can be written as the sum of positive numbers—either all integers up to and including \u003c/span\u003e\u003c/span\u003e\u003cspan style=\"font-family: STIXGeneral, STIXGeneral-webfont, serif; font-style: italic; font-weight: 400; color: rgb(0, 0, 0);\"\u003en\u003c/span\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003e or a subset of those numbers. \u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"block-size: 84px; font-family: Helvetica, Arial, sans-serif; line-height: 21px; margin-block-end: 9px; margin-block-start: 2px; margin-bottom: 9px; margin-inline-end: 10px; margin-inline-start: 4px; margin-left: 4px; margin-right: 10px; margin-top: 2px; perspective-origin: 384px 42px; text-align: left; transform-origin: 384px 42px; white-space-collapse: preserve; margin-left: 4px; margin-top: 2px; margin-bottom: 9px; margin-right: 10px; \"\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003eMost of the problems involve counting partitions. \u003c/span\u003e\u003c/span\u003e\u003ca target='_blank' href = \"https://www.mathworks.com/matlabcentral/cody/problems/1873\"\u003e\u003cspan style=\"\"\u003e\u003cspan style=\"text-decoration: underline; text-decoration-line: underline; \"\u003eCody Problem 1873\u003c/span\u003e\u003c/span\u003e\u003c/a\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003e counts the partitions of an integer, while \u003c/span\u003e\u003c/span\u003e\u003ca target='_blank' href = \"https://www.mathworks.com/matlabcentral/cody/problems/42918\"\u003e\u003cspan style=\"\"\u003e\u003cspan style=\"text-decoration: underline; text-decoration-line: underline; \"\u003eCody Problem 42918\u003c/span\u003e\u003c/span\u003e\u003c/a\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003e counts the partitions with a given number of parts. \u003c/span\u003e\u003c/span\u003e\u003ca target='_blank' href = \"https://www.mathworks.com/matlabcentral/cody/problems/45429\"\u003e\u003cspan style=\"\"\u003e\u003cspan style=\"text-decoration: underline; text-decoration-line: underline; \"\u003eCody Problem 45429\u003c/span\u003e\u003c/span\u003e\u003c/a\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003e restricts the parts to primes; \u003c/span\u003e\u003c/span\u003e\u003ca target='_blank' href = \"https://www.mathworks.com/matlabcentral/cody/problems/1240\"\u003e\u003cspan style=\"\"\u003e\u003cspan style=\"text-decoration: underline; text-decoration-line: underline; \"\u003eCody Problem 1240\u003c/span\u003e\u003c/span\u003e\u003c/a\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003e restricts them to U.S. coin denominations; and \u003c/span\u003e\u003c/span\u003e\u003ca target='_blank' href = \"https://www.mathworks.com/matlabcentral/cody/problems/54740\"\u003e\u003cspan style=\"\"\u003e\u003cspan style=\"text-decoration: underline; text-decoration-line: underline; \"\u003eCody Problem 54740\u003c/span\u003e\u003c/span\u003e\u003c/a\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003e restricts them to American football scores. \u003c/span\u003e\u003c/span\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003e \u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"block-size: 42px; font-family: Helvetica, Arial, sans-serif; line-height: 21px; margin-block-end: 9px; margin-block-start: 2px; margin-bottom: 9px; margin-inline-end: 10px; margin-inline-start: 4px; margin-left: 4px; margin-right: 10px; margin-top: 2px; perspective-origin: 384px 21px; text-align: left; transform-origin: 384px 21px; white-space-collapse: preserve; margin-left: 4px; margin-top: 2px; margin-bottom: 9px; margin-right: 10px; \"\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003eA couple of problems involves listing partitions. \u003c/span\u003e\u003c/span\u003e\u003ca target='_blank' href = \"https://www.mathworks.com/matlabcentral/cody/problems/590\"\u003e\u003cspan style=\"\"\u003e\u003cspan style=\"text-decoration: underline; text-decoration-line: underline; \"\u003eCody Problem 590\u003c/span\u003e\u003c/span\u003e\u003c/a\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003e asks us to list one partition of a number, without repeating parts, and \u003c/span\u003e\u003c/span\u003e\u003ca target='_blank' href = \"https://www.mathworks.com/matlabcentral/cody/problems/59586\"\u003e\u003cspan style=\"\"\u003e\u003cspan style=\"text-decoration: underline; text-decoration-line: underline; \"\u003eCody Problem 59586\u003c/span\u003e\u003c/span\u003e\u003c/a\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003e involves listing ways to make a sum in Killer Sudoku. \u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"block-size: 42px; font-family: Helvetica, Arial, sans-serif; line-height: 21px; margin-block-end: 9px; margin-block-start: 2px; margin-bottom: 9px; margin-inline-end: 10px; margin-inline-start: 4px; margin-left: 4px; margin-right: 10px; margin-top: 2px; perspective-origin: 384px 21px; text-align: left; transform-origin: 384px 21px; white-space-collapse: preserve; margin-left: 4px; margin-top: 2px; margin-bottom: 9px; margin-right: 10px; \"\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003eThis problem involves listing all partitions of an integer. For example, 5 = 4+1 = 3+2 = 3+1+1 = 2+2+1 = 2+1+1+1 = 1+1+1+1+1. Notice that rearrangements of a partition are not included. \u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"block-size: 42px; font-family: Helvetica, Arial, sans-serif; line-height: 21px; margin-block-end: 9px; margin-block-start: 2px; margin-bottom: 9px; margin-inline-end: 10px; margin-inline-start: 4px; margin-left: 4px; margin-right: 10px; margin-top: 2px; perspective-origin: 384px 21px; text-align: left; transform-origin: 384px 21px; white-space-collapse: preserve; margin-left: 4px; margin-top: 2px; margin-bottom: 9px; margin-right: 10px; \"\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003eWrite a function to list partitions of an integer. Return the partitions in a cell array and sort them as in the example—i.e., from larger to smaller numbers. For \u003c/span\u003e\u003c/span\u003e\u003cspan style=\"vertical-align:-5px\"\u003e\u003cimg src=\"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAEkAAAAkCAYAAADFGRdYAAAAAXNSR0IArs4c6QAAA0hJREFUaEPtmFnITVEUx38fUh54ME9lSIlEUeZCIiVDhgwZEoUXMmR4MJdECBF5MYRMSRKZH8x5MSdleDJEHpB52H+to/Pd757v7Nv34OjsU7dzh3VPa//2Wuu/1i4jXKkEylItggEBkkcQBEgBkgcBD5MQSQGSB4HyJs2A9sDZ+Nd5i6TqwAJgdQK+h8Bo97qfZ0itgANAtwRImw3il7xCUtbMcqnUC9gJfCsA9QN4ALwtBJindGsC7ALWA2dKqVZ5gjQVGAzo/i5AqkigoaXYUOA5cAM4DlwAXgG/KoOWl0gaARxNALHHQVoGPEsCVQxSDaAx0AHoDPQAzgOb4M+s18cKYH/gHDAfeFJK+P4D23ZAc6CjKZt8rxvz47aLqJnAtWK+FYPUAhgEjAX6Ai+A4cBdYJ6pw2sHZpI9cC6w0XPhbYGDQCdP+ySzpcCqKjyjjlvPSGChawnkky6l4ETgcSnqtgRYCZx2eTwFmOw+VwM2OJVoGVvsDGCHp8NZgRS5q4xZa3D0nd5r3V99+qTawFb7s3btKdDUAH134LqbjH6wKLvpCSmLZo1sk4cBV4HxVtz/+ppUuKMdb21gdJ8dk06l2m6LMoXomyyuvgSfosL+3oEa6MrNdZ9IklRKIvUndaFKN801umoCa4A5VhdWuBBVt/o/X6rD+1129HRi1Bu4kgZJQ6CK4mIzVKTsi/USSjt9VlEX9XIT839Kqp6tSRkzCriTBqk+sNcU7liRDlWzzylTuwr5mwIpa4U7cjeCpFrsVZO6ACdcMdOsM8EIx9euNJPCSdH0/lMJ0ZNVSOqfjtjm6ygl9RRgumsmt1vfMM6ULeIQV71I+gVVD71XAqwsmap5XgRMs1S7ldYn1bLGUKCKna1EkaBZaIjBEUgdYn3M0spjvugQTZuug7R1wEngp/0ude9nvy+3s6YKc1xhCxCv8sVSTfXosnXh24A21rVqSMzqFZWHyD9NB/L9M6AN1jShBvJi0qBbCGmANYmaZca4hzwqWHkDYIuTSNkdcrOc5P9lVumYX8oOTQtSra7uPEklQycBl4DDdq80C/JyClClfQyQPPAFSAGSBwEPkxBJAZIHAQ+TEEkBkgcBD5MQSR6QfgPqeJsl50gPXAAAAABJRU5ErkJggg==\" width=\"36.5\" height=\"18\" alt=\"n = 5\" style=\"width: 36.5px; height: 18px;\"\u003e\u003c/span\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003e, the required output is\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"block-size: 21px; font-family: Helvetica, Arial, sans-serif; line-height: 21px; margin-block-end: 9px; margin-block-start: 2px; margin-bottom: 9px; margin-inline-end: 10px; margin-inline-start: 4px; margin-left: 4px; margin-right: 10px; margin-top: 2px; perspective-origin: 384px 10.5px; text-align: left; transform-origin: 384px 10.5px; white-space-collapse: preserve; margin-left: 4px; margin-top: 2px; margin-bottom: 9px; margin-right: 10px; \"\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003e{[5] [4 1] [3 2] [3 1 1] [2 2 1] [2 1 1 1] [1 1 1 1 1]}\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003c/div\u003e\u003c/div\u003e","function_template":"function p = partition(n)\r\n  p = {n ones(1,n)};\r\nend","test_suite":"%%\r\nn = 2;\r\np = partition(n);\r\np_correct = {[2] [1 1]};\r\nassert(isequal(p,p_correct))\r\n\r\n%%\r\nn = 4;\r\np = partition(n);\r\np_correct = {[4] [3 1] [2 2] [2 1 1] [1 1 1 1]};\r\nassert(isequal(p,p_correct))\r\n\r\n%%\r\nn = 5;\r\np = partition(n);\r\np_correct = {[5] [4 1] [3 2] [3 1 1] [2 2 1] [2 1 1 1] [1 1 1 1 1]};\r\nassert(isequal(p,p_correct))\r\n\r\n%%\r\nn = 7;\r\np = partition(n);\r\np_correct = {[7] [6 1] [5 2] [5 1 1] [4 3] [4 2 1] [4 1 1 1] [3 3 1] [3 2 2] [3 2 1 1] [3 1 1 1 1] [2 2 2 1] [2 2 1 1 1] [2 1 1 1 1 1] [1 1 1 1 1 1 1]};\r\nassert(isequal(p,p_correct))\r\n\r\n%%\r\nn = 12;\r\np = partition(n);\r\np_correct = {[12] [11 1] [10 2] [10 1 1] [9 3] [9 2 1] [9 1 1 1] [8 4] [8 3 1] [8 2 2] [8 2 1 1] [8 1 1 1 1] [7 5] [7 4 1] [7 3 2] [7 3 1 1] [7 2 2 1] [7 2 1 1 1] [7 1 1 1 1 1] [6 6] [6 5 1] [6 4 2] [6 4 1 1] [6 3 3] [6 3 2 1] [6 3 1 1 1] [6 2 2 2] [6 2 2 1 1] [6 2 1 1 1 1] [6 1 1 1 1 1 1] [5 5 2] [5 5 1 1] [5 4 3] [5 4 2 1] [5 4 1 1 1] [5 3 3 1] [5 3 2 2] [5 3 2 1 1] [5 3 1 1 1 1] [5 2 2 2 1] [5 2 2 1 1 1] [5 2 1 1 1 1 1] [5 1 1 1 1 1 1 1] [4 4 4] [4 4 3 1] [4 4 2 2] [4 4 2 1 1] [4 4 1 1 1 1] [4 3 3 2] [4 3 3 1 1] [4 3 2 2 1] [4 3 2 1 1 1] [4 3 1 1 1 1 1] [4 2 2 2 2] [4 2 2 2 1 1] [4 2 2 1 1 1 1] [4 2 1 1 1 1 1 1] [4 1 1 1 1 1 1 1 1] [3 3 3 3] [3 3 3 2 1] [3 3 3 1 1 1] [3 3 2 2 2] [3 3 2 2 1 1] [3 3 2 1 1 1 1] [3 3 1 1 1 1 1 1] [3 2 2 2 2 1] [3 2 2 2 1 1 1] [3 2 2 1 1 1 1 1] [3 2 1 1 1 1 1 1 1] [3 1 1 1 1 1 1 1 1 1] [2 2 2 2 2 2] [2 2 2 2 2 1 1] [2 2 2 2 1 1 1 1] [2 2 2 1 1 1 1 1 1] [2 2 1 1 1 1 1 1 1 1] [2 1 1 1 1 1 1 1 1 1 1] [1 1 1 1 1 1 1 1 1 1 1 1]};\r\nassert(isequal(p,p_correct))\r\n\r\n%%\r\nn = 19;\r\np = partition(n);\r\nlen_correct = 490;\r\np98_correct = {[9 9 1] [7 7 2 1 1 1] [6 4 3 3 3] [5 2 2 2 2 1 1 1 1 1 1]};\r\nassert(isequal(length(p),len_correct))\r\nassert(isequal(p(98:98:392),p98_correct))\r\n\r\n%%\r\nn = 26;\r\np = partition(n);\r\nlen_correct = 2436;\r\np487_correct = {[12 3 3 3 1 1 1 1 1] [9 7 2 2 1 1 1 1 1 1] [7 7 4 4 2 2] [6 4 3 3 2 1 1 1 1 1 1 1 1] [2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1]};\r\nassert(isequal(length(p),len_correct))\r\nassert(isequal(p(487:487:2436),p487_correct))\r\n\r\n%%\r\nn = 37;\r\np = partition(n);\r\nlen_correct = 21637;\r\nsumprod_correct = 653715968;\r\nassert(isequal(length(p),len_correct))\r\nassert(isequal(sum(cellfun(@prod,p)),sumprod_correct))\r\n\r\n%%\r\nn = 61;\r\np = partition(n);\r\nlen_correct = 1121505;\r\npselect_correct = {[41 12 5 1 1 1] [36 9 8 3 1 1 1 1 1] [28 9 4 3 3 3 3 3 3 2] [25 8 6 3 3 2 2 1 1 1 1 1 1 1 1 1 1 1 1]};\r\nassert(isequal(length(p),len_correct))\r\nassert(isequal(p([02139 08037 50014 94305]),pselect_correct))\r\n","published":true,"deleted":false,"likes_count":0,"comments_count":7,"created_by":46909,"edited_by":46909,"edited_at":"2024-10-20T03:35:11.000Z","deleted_by":null,"deleted_at":null,"solvers_count":5,"test_suite_updated_at":null,"rescore_all_solutions":false,"group_id":1,"created_at":"2024-10-19T05:06:59.000Z","updated_at":"2024-12-18T19:39:03.000Z","published_at":"2024-10-19T05:07:27.000Z","restored_at":null,"restored_by":null,"spam":null,"simulink":false,"admin_reviewed":false,"description_opc":"{\"parts\":[{\"partUri\":\"/matlab/document.xml\",\"contentType\":\"application/vnd.mathworks.matlab.code.document+xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\"?\u003e\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\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eSeveral Cody problems deal with partitions of an integer, or the ways an integer \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:customXml w:element=\\\"equation\\\"\u003e\u003cw:customXmlPr\u003e\u003cw:attr w:name=\\\"displayStyle\\\" w:val=\\\"false\\\"/\u003e\u003cw:attr w:name=\\\"altTextString\\\" w:val=\\\"n\\\"/\u003e\u003c/w:customXmlPr\u003e\u003cw:r\u003e\u003cw:t\u003en\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:customXml\u003e\u003cw:r\u003e\u003cw:t\u003e can be written as the sum of positive numbers—either all integers up to and including \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:customXml w:element=\\\"equation\\\"\u003e\u003cw:customXmlPr\u003e\u003cw:attr w:name=\\\"displayStyle\\\" w:val=\\\"false\\\"/\u003e\u003cw:attr w:name=\\\"altTextString\\\" w:val=\\\"n\\\"/\u003e\u003c/w:customXmlPr\u003e\u003cw:r\u003e\u003cw:t\u003en\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:customXml\u003e\u003cw:r\u003e\u003cw:t\u003e or a subset of those numbers. \u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eMost of the problems involve counting partitions. \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:hyperlink w:docLocation=\\\"https://www.mathworks.com/matlabcentral/cody/problems/1873\\\"\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:u/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eCody Problem 1873\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e counts the partitions of an integer, while \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:hyperlink w:docLocation=\\\"https://www.mathworks.com/matlabcentral/cody/problems/42918\\\"\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:u/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eCody Problem 42918\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e counts the partitions with a given number of parts. \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:hyperlink w:docLocation=\\\"https://www.mathworks.com/matlabcentral/cody/problems/45429\\\"\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:u/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eCody Problem 45429\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e restricts the parts to primes; \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:hyperlink w:docLocation=\\\"https://www.mathworks.com/matlabcentral/cody/problems/1240\\\"\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:u/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eCody Problem 1240\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e restricts them to U.S. coin denominations; and \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:hyperlink w:docLocation=\\\"https://www.mathworks.com/matlabcentral/cody/problems/54740\\\"\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:u/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eCody Problem 54740\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e restricts them to American football scores. \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\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eA couple of problems involves listing partitions. \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:hyperlink w:docLocation=\\\"https://www.mathworks.com/matlabcentral/cody/problems/590\\\"\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:u/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eCody Problem 590\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e asks us to list one partition of a number, without repeating parts, and \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:hyperlink w:docLocation=\\\"https://www.mathworks.com/matlabcentral/cody/problems/59586\\\"\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:u/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eCody Problem 59586\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e involves listing ways to make a sum in Killer Sudoku. \u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eThis problem involves listing all partitions of an integer. For example, 5 = 4+1 = 3+2 = 3+1+1 = 2+2+1 = 2+1+1+1 = 1+1+1+1+1. Notice that rearrangements of a partition are not included. \u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eWrite a function to list partitions of an integer. Return the partitions in a cell array and sort them as in the example—i.e., from larger to smaller numbers. For \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:customXml w:element=\\\"equation\\\"\u003e\u003cw:customXmlPr\u003e\u003cw:attr w:name=\\\"displayStyle\\\" w:val=\\\"false\\\"/\u003e\u003cw:attr w:name=\\\"altTextString\\\" w:val=\\\"n = 5\\\"/\u003e\u003c/w:customXmlPr\u003e\u003cw:r\u003e\u003cw:t\u003en = 5\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:customXml\u003e\u003cw:r\u003e\u003cw:t\u003e, the required output is\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e{[5] [4 1] [3 2] [3 1 1] [2 2 1] [2 1 1 1] [1 1 1 1 1]}\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003c/w:body\u003e\u003c/w:document\u003e\",\"relationship\":null}],\"relationships\":[{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/document\",\"target\":\"/matlab/document.xml\",\"relationshipId\":\"rId1\"}]}"},{"id":58458,"title":"You are given a matrix A of size m x n representing a Sudoku puzzle. The matrix contains digits from 1 to 9, where 0 represents an empty cell. Your task is to solve the Sudoku puzzle by filling in the empty cells with the correct digits. ","description":"The provided code implements a solution for solving Sudoku puzzles using a recursive backtracking algorithm. \r\n\r\nSudoku is a logic-based number puzzle where the goal is to fill a 9x9 grid with digits from 1 to 9, following specific rules. The grid is divided into 9 sub-grids of size 3x3, and each cell must be filled with a digit such that the following conditions are satisfied:\r\n1. Each row must contain all digits from 1 to 9 without repetition.\r\n2. Each column must contain all digits from 1 to 9 without repetition.\r\n3. Each 3x3 sub-grid must contain all digits from 1 to 9 without repetition.\r\nWrite a function solve_sudoku(A) that takes the matrix A as input and returns the solved Sudoku puzzle as the output. If a solution is not possible, the function should return an empty matrix.\r\n","description_html":"\u003cdiv style = \"text-align: start; line-height: 20.44px; min-height: 0px; white-space: normal; color: rgb(0, 0, 0); font-family: Menlo, Monaco, Consolas, monospace; font-style: normal; font-size: 14px; font-weight: 400; text-decoration: none solid rgb(0, 0, 0); white-space: normal; \"\u003e\u003cdiv style=\"block-size: 294px; display: block; min-width: 0px; padding-block-start: 0px; padding-top: 0px; perspective-origin: 407px 147px; transform-origin: 407px 147px; vertical-align: baseline; \"\u003e\u003cdiv style=\"block-size: 21px; font-family: Helvetica, Arial, sans-serif; line-height: 21px; margin-block-end: 9px; margin-block-start: 2px; margin-bottom: 9px; margin-inline-end: 10px; margin-inline-start: 4px; margin-left: 4px; margin-right: 10px; margin-top: 2px; perspective-origin: 384px 10.5px; text-align: left; transform-origin: 384px 10.5px; white-space-collapse: preserve; margin-left: 4px; margin-top: 2px; margin-bottom: 9px; margin-right: 10px; \"\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003eThe provided code implements a solution for solving Sudoku puzzles using a recursive backtracking algorithm. \u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"block-size: 21px; font-family: Helvetica, Arial, sans-serif; line-height: 21px; margin-block-end: 9px; margin-block-start: 2px; margin-bottom: 9px; margin-inline-end: 10px; margin-inline-start: 4px; margin-left: 4px; margin-right: 10px; margin-top: 2px; perspective-origin: 384px 10.5px; text-align: left; transform-origin: 384px 10.5px; white-space-collapse: preserve; margin-left: 4px; margin-top: 2px; margin-bottom: 9px; margin-right: 10px; \"\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003e\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"block-size: 63px; font-family: Helvetica, Arial, sans-serif; line-height: 21px; margin-block-end: 9px; margin-block-start: 2px; margin-bottom: 9px; margin-inline-end: 10px; margin-inline-start: 4px; margin-left: 4px; margin-right: 10px; margin-top: 2px; perspective-origin: 384px 31.5px; text-align: left; transform-origin: 384px 31.5px; white-space-collapse: preserve; margin-left: 4px; margin-top: 2px; margin-bottom: 9px; margin-right: 10px; \"\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003eSudoku is a logic-based number puzzle where the goal is to fill a 9x9 grid with digits from 1 to 9, following specific rules. The grid is divided into 9 sub-grids of size 3x3, and each cell must be filled with a digit such that the following conditions are satisfied:\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"block-size: 21px; font-family: Helvetica, Arial, sans-serif; line-height: 21px; margin-block-end: 9px; margin-block-start: 2px; margin-bottom: 9px; margin-inline-end: 10px; margin-inline-start: 4px; margin-left: 4px; margin-right: 10px; margin-top: 2px; perspective-origin: 384px 10.5px; text-align: left; transform-origin: 384px 10.5px; white-space-collapse: preserve; margin-left: 4px; margin-top: 2px; margin-bottom: 9px; margin-right: 10px; \"\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003e1. Each row must contain all digits from 1 to 9 without repetition.\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"block-size: 21px; font-family: Helvetica, Arial, sans-serif; line-height: 21px; margin-block-end: 9px; margin-block-start: 2px; margin-bottom: 9px; margin-inline-end: 10px; margin-inline-start: 4px; margin-left: 4px; margin-right: 10px; margin-top: 2px; perspective-origin: 384px 10.5px; text-align: left; transform-origin: 384px 10.5px; white-space-collapse: preserve; margin-left: 4px; margin-top: 2px; margin-bottom: 9px; margin-right: 10px; \"\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003e2. Each column must contain all digits from 1 to 9 without repetition.\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"block-size: 21px; font-family: Helvetica, Arial, sans-serif; line-height: 21px; margin-block-end: 9px; margin-block-start: 2px; margin-bottom: 9px; margin-inline-end: 10px; margin-inline-start: 4px; margin-left: 4px; margin-right: 10px; margin-top: 2px; perspective-origin: 384px 10.5px; text-align: left; transform-origin: 384px 10.5px; white-space-collapse: preserve; margin-left: 4px; margin-top: 2px; margin-bottom: 9px; margin-right: 10px; \"\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003e3. Each 3x3 sub-grid must contain all digits from 1 to 9 without repetition.\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"block-size: 42px; font-family: Helvetica, Arial, sans-serif; line-height: 21px; margin-block-end: 9px; margin-block-start: 2px; margin-bottom: 9px; margin-inline-end: 10px; margin-inline-start: 4px; margin-left: 4px; margin-right: 10px; margin-top: 2px; perspective-origin: 384px 21px; text-align: left; transform-origin: 384px 21px; white-space-collapse: preserve; margin-left: 4px; margin-top: 2px; margin-bottom: 9px; margin-right: 10px; \"\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003eWrite a function \u003c/span\u003e\u003c/span\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; \"\u003e\u003cspan style=\"font-family: Menlo, Monaco, Consolas, \u0026quot;Courier New\u0026quot;, monospace; \"\u003esolve_sudoku(A)\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003e that takes the matrix \u003c/span\u003e\u003c/span\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; \"\u003e\u003cspan style=\"font-family: Menlo, Monaco, Consolas, \u0026quot;Courier New\u0026quot;, monospace; \"\u003eA\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003e as input and returns the solved Sudoku puzzle as the output. If a solution is not possible, the function should return an empty matrix.\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"block-size: 21px; font-family: Helvetica, Arial, sans-serif; line-height: 21px; margin-block-end: 9px; margin-block-start: 2px; margin-bottom: 9px; margin-inline-end: 10px; margin-inline-start: 4px; margin-left: 4px; margin-right: 10px; margin-top: 2px; perspective-origin: 384px 10.5px; text-align: left; transform-origin: 384px 10.5px; white-space-collapse: preserve; margin-left: 4px; margin-top: 2px; margin-bottom: 9px; margin-right: 10px; \"\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003e\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003c/div\u003e\u003c/div\u003e","function_template":"function solution = solve_sudoku(A)\r\nsolution=A;\r\nend\r\n","test_suite":"A = [9 2 0 0 0 0 0 7 0;\r\n     0 0 7 0 9 2 0 0 0;\r\n     0 0 0 8 0 7 9 0 0;\r\n     0 0 5 0 0 0 8 0 0;\r\n     4 1 0 0 0 0 0 6 9;\r\n     0 0 8 0 0 0 5 0 0;\r\n     0 0 2 6 0 8 0 0 0;\r\n     0 0 0 2 7 0 3 0 0;\r\n     0 3 0 0 0 0 0 1 6];\r\n\r\ny_correct = [9 2 6 5 4 3 1 7 8;\r\n             3 4 7 1 9 2 6 8 5;\r\n             5 8 1 8 6 7 9 2 3;\r\n             6 9 5 3 2 4 8 5 7;\r\n             4 1 3 7 5 9 2 6 9;\r\n             7 2 8 6 1 5 5 3 4;\r\n             1 5 2 6 3 8 4 9 7;\r\n             8 6 9 2 7 1 3 4 5;\r\n             6 3 4 9 8 5 7 1 6];\r\n\r\nassert(isequal(solve_sudoku(A), y_correct), 'Test Case Failed');\r\n\r\ndisp('Test case passed successfully.');\r\n\r\n","published":true,"deleted":false,"likes_count":0,"comments_count":3,"created_by":3123865,"edited_by":3123865,"edited_at":"2023-06-24T15:55:05.000Z","deleted_by":null,"deleted_at":null,"solvers_count":1,"test_suite_updated_at":"2023-06-24T15:45:49.000Z","rescore_all_solutions":false,"group_id":1,"created_at":"2023-06-24T15:32:24.000Z","updated_at":"2026-04-16T03:13:14.000Z","published_at":"2023-06-24T15:32:24.000Z","restored_at":null,"restored_by":null,"spam":null,"simulink":false,"admin_reviewed":false,"description_opc":"{\"parts\":[{\"partUri\":\"/matlab/document.xml\",\"contentType\":\"application/vnd.mathworks.matlab.code.document+xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\"?\u003e\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\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eThe provided code implements a solution for solving Sudoku puzzles using a recursive backtracking algorithm. \u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\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\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eSudoku is a logic-based number puzzle where the goal is to fill a 9x9 grid with digits from 1 to 9, following specific rules. The grid is divided into 9 sub-grids of size 3x3, and each cell must be filled with a digit such that the following conditions are satisfied:\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e1. Each row must contain all digits from 1 to 9 without repetition.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e2. Each column must contain all digits from 1 to 9 without repetition.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e3. Each 3x3 sub-grid must contain all digits from 1 to 9 without repetition.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eWrite a function \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\u003esolve_sudoku(A)\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e that takes the matrix \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\u003eA\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e as input and returns the solved Sudoku puzzle as the output. If a solution is not possible, the function should return an empty matrix.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003c/w:body\u003e\u003c/w:document\u003e\",\"relationship\":null}],\"relationships\":[{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/document\",\"target\":\"/matlab/document.xml\",\"relationshipId\":\"rId1\"}]}"}],"term":"sudoku difficulty_rating_bin:hard","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":"sudoku difficulty_rating_bin:hard","current_player":null,"sort":"map(difficulty_value,0,0,999) asc"},"parser":"MathWorks::Search::Solr::QueryParser","directives":{"term":{"directives":{"difficulty_rating_bin":[["difficulty_rating_bin:hard","","","hard",""]]}}},"facets":{"#\u003cMathWorks::Search::Field:0x00007faf33cb9b18\u003e":null,"#\u003cMathWorks::Search::Field:0x00007faf33cb9a78\u003e":["hard"]},"filters":{"#\u003cMathWorks::Search::Field:0x00007faf33cb91b8\u003e":"\"cody:problem\""},"fields":{"#\u003cMathWorks::Search::Field:0x00007faf33cb9d98\u003e":1,"#\u003cMathWorks::Search::Field:0x00007faf33cb9cf8\u003e":50,"#\u003cMathWorks::Search::Field:0x00007faf33cb9c58\u003e":"map(difficulty_value,0,0,999) asc","#\u003cMathWorks::Search::Field:0x00007faf33cb9bb8\u003e":"sudoku difficulty_rating_bin:hard"},"user_query":{"#\u003cMathWorks::Search::Field:0x00007faf33cb9bb8\u003e":"sudoku difficulty_rating_bin:hard"},"queried_facets":{"#\u003cMathWorks::Search::Field:0x00007faf33cb9a78\u003e":["hard"]}},"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":"cody-search","password":"78X075ddcV44","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":"sudoku difficulty_rating_bin:hard","current_player":null,"sort":"map(difficulty_value,0,0,999) asc"},"parser":"MathWorks::Search::Solr::QueryParser","directives":{"term":{"directives":{"difficulty_rating_bin":[["difficulty_rating_bin:hard","","","hard",""]]}}},"facets":{"#\u003cMathWorks::Search::Field:0x00007faf33cb9b18\u003e":null,"#\u003cMathWorks::Search::Field:0x00007faf33cb9a78\u003e":["hard"]},"filters":{"#\u003cMathWorks::Search::Field:0x00007faf33cb91b8\u003e":"\"cody:problem\""},"fields":{"#\u003cMathWorks::Search::Field:0x00007faf33cb9d98\u003e":1,"#\u003cMathWorks::Search::Field:0x00007faf33cb9cf8\u003e":50,"#\u003cMathWorks::Search::Field:0x00007faf33cb9c58\u003e":"map(difficulty_value,0,0,999) asc","#\u003cMathWorks::Search::Field:0x00007faf33cb9bb8\u003e":"sudoku difficulty_rating_bin:hard"},"user_query":{"#\u003cMathWorks::Search::Field:0x00007faf33cb9bb8\u003e":"sudoku difficulty_rating_bin:hard"},"queried_facets":{"#\u003cMathWorks::Search::Field:0x00007faf33cb9a78\u003e":["hard"]}},"options":{"fields":["id","difficulty_rating"]},"join":" "},"results":[{"id":56588,"difficulty_rating":"medium-hard"},{"id":2085,"difficulty_rating":"medium-hard"},{"id":60751,"difficulty_rating":"medium-hard"},{"id":58458,"difficulty_rating":"hard"}]}}