{"group":{"group":{"id":56,"name":"Logic","lockable":false,"created_at":"2018-08-27T18:43:39.000Z","updated_at":"2026-04-16T00:12:35.000Z","description":"Basic logic required.","is_default":false,"created_by":26769,"badge_id":73,"featured":false,"trending":false,"solution_count_in_trending_period":19,"trending_last_calculated":"2026-04-16T00:00:00.000Z","image_id":2885,"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":"{\"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\u003eBasic logic required.\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\"}]}","description_html":"\u003cdiv style = \"text-align: start; line-height: normal; 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=\"display: block; min-width: 0px; padding-top: 0px; perspective-origin: 289.5px 10.5px; transform-origin: 289.5px 10.5px; \"\u003e\u003cdiv style=\"font-family: Helvetica, Arial, sans-serif; line-height: 21px; margin-bottom: 9px; margin-left: 4px; margin-right: 10px; margin-top: 2px; text-align: left; white-space: pre-wrap; perspective-origin: 266.5px 10.5px; transform-origin: 266.5px 10.5px; margin-left: 4px; margin-top: 2px; margin-bottom: 9px; margin-right: 10px; \"\u003e\u003cspan style=\"display: inline; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003eBasic logic required.\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003c/div\u003e\u003c/div\u003e","published_at":"2019-05-08T20:16:03.000Z"},"current_player":null},"problems":[{"id":1179,"title":"Knights and Knaves (part 1)","description":"This is a Matlab adaptation of the \u003chttp://en.wikipedia.org/wiki/Knights_and_Knaves Knights and Knaves\u003e logical puzzles.\r\n\r\nYou are in an island where all inhabitants are either _Knights_, who always tell the truth, or _Knaves_, who always lie. The island inhabitants can always tell Knights and Knaves apart by their appearance, but to you, as an outsider, they look exactly the same.\r\n\r\nUpon arriving to this island you encounter two inhabitants standing at a fork in the road. One of them is a Knight, and the other is a Knave, but you do not know which. One of the roads will lead you to the castle, where you wish to go. Your job is to ask one or several yes/no questions that will allow you to know which one is the correct road. \r\n\r\n*Implementation*\r\n\r\nYou function will take two function handles as inputs (one per inhabitant), and must return a single scalar indicating the correct road (1 for the first road or 2 for the second road):\r\n\r\n    function correct_road = solver(fA, fB)\r\n\r\nYou may ask one inhabitant a question by evaluating his associated function handle on a char string (the 'question'). Strings must be a valid matlab commands that when evaluated  return a scalar logical value (yes/no questions, where _true_ is a yes, and _false_ is a no). Strings may refer to the following variables:\r\n\r\n*   A: true if the first inhabitant is a Knight, false if he is a Knave\r\n* \r\n*   B: true if the second inhabitant is a Knight, false if he is a Knave\r\n* \r\n*   X: 1 if the first road leads to the castle, 2 if the second road leads to the castle\r\n\r\n\r\n*Asking questions, examples*: \r\n\r\n    x=fA('A==true');\r\n\r\nasks the first inhabitant whether he is a Knight (note: this returns always _true_, since both Knights and Knaves would tell you they are Knights; remember, Knaves always lie)\r\n\r\n    x=fB('X==1');\r\n\r\nasks the second inhabitant whether the first road leads to the castle (not particularly useful by itself since we do not know whether he is going to respond truthfully or not)\r\n\r\nNext problem in this series: \u003chttp://www.mathworks.com/matlabcentral/cody/problems/1180-knights-and-knaves-part-2 Knights and Knaves (part 2)\u003e","description_html":"\u003cp\u003eThis is a Matlab adaptation of the \u003ca href = \"http://en.wikipedia.org/wiki/Knights_and_Knaves\"\u003eKnights and Knaves\u003c/a\u003e logical puzzles.\u003c/p\u003e\u003cp\u003eYou are in an island where all inhabitants are either \u003ci\u003eKnights\u003c/i\u003e, who always tell the truth, or \u003ci\u003eKnaves\u003c/i\u003e, who always lie. The island inhabitants can always tell Knights and Knaves apart by their appearance, but to you, as an outsider, they look exactly the same.\u003c/p\u003e\u003cp\u003eUpon arriving to this island you encounter two inhabitants standing at a fork in the road. One of them is a Knight, and the other is a Knave, but you do not know which. One of the roads will lead you to the castle, where you wish to go. Your job is to ask one or several yes/no questions that will allow you to know which one is the correct road.\u003c/p\u003e\u003cp\u003e\u003cb\u003eImplementation\u003c/b\u003e\u003c/p\u003e\u003cp\u003eYou function will take two function handles as inputs (one per inhabitant), and must return a single scalar indicating the correct road (1 for the first road or 2 for the second road):\u003c/p\u003e\u003cpre\u003e    function correct_road = solver(fA, fB)\u003c/pre\u003e\u003cp\u003eYou may ask one inhabitant a question by evaluating his associated function handle on a char string (the 'question'). Strings must be a valid matlab commands that when evaluated  return a scalar logical value (yes/no questions, where \u003ci\u003etrue\u003c/i\u003e is a yes, and \u003ci\u003efalse\u003c/i\u003e is a no). Strings may refer to the following variables:\u003c/p\u003e\u003cul\u003e\u003cli\u003eA: true if the first inhabitant is a Knight, false if he is a Knave\u003c/li\u003e\u003cli\u003e\u003c/li\u003e\u003cli\u003eB: true if the second inhabitant is a Knight, false if he is a Knave\u003c/li\u003e\u003cli\u003e\u003c/li\u003e\u003cli\u003eX: 1 if the first road leads to the castle, 2 if the second road leads to the castle\u003c/li\u003e\u003c/ul\u003e\u003cp\u003e\u003cb\u003eAsking questions, examples\u003c/b\u003e:\u003c/p\u003e\u003cpre\u003e    x=fA('A==true');\u003c/pre\u003e\u003cp\u003easks the first inhabitant whether he is a Knight (note: this returns always \u003ci\u003etrue\u003c/i\u003e, since both Knights and Knaves would tell you they are Knights; remember, Knaves always lie)\u003c/p\u003e\u003cpre\u003e    x=fB('X==1');\u003c/pre\u003e\u003cp\u003easks the second inhabitant whether the first road leads to the castle (not particularly useful by itself since we do not know whether he is going to respond truthfully or not)\u003c/p\u003e\u003cp\u003eNext problem in this series: \u003ca href = \"http://www.mathworks.com/matlabcentral/cody/problems/1180-knights-and-knaves-part-2\"\u003eKnights and Knaves (part 2)\u003c/a\u003e\u003c/p\u003e","function_template":"function correct_road = solver(fA,fB)\r\n  correct_road=1+fA('X\u003e1'); % only works if A is a Knight\r\nend","test_suite":"%%\r\nA=true; B=false; X=1;\r\n\r\nf=inline('logical(interp1([0,1],[0,x],1))','x','A','B','X');\r\nfA=@(str)xor(~A,f(eval(str),A,B,X));\r\nfB=@(str)xor(~B,f(eval(str),A,B,X));\r\nclear f A B X;\r\n\r\nassert(isequal(solver(fA,fB),1))\r\n%%\r\nA=true; B=false; X=2;\r\n\r\nf=inline('logical(interp1([0,1],[0,x],1))','x','A','B','X');\r\nfA=@(str)xor(~A,f(eval(str),A,B,X));\r\nfB=@(str)xor(~B,f(eval(str),A,B,X));\r\nclear f A B X;\r\n\r\nassert(isequal(solver(fA,fB),2))\r\n%%\r\nA=false; B=true; X=1;\r\n\r\nf=inline('logical(interp1([0,1],[0,x],1))','x','A','B','X');\r\nfA=@(str)xor(~A,f(eval(str),A,B,X));\r\nfB=@(str)xor(~B,f(eval(str),A,B,X));\r\nclear f A B X;\r\n\r\nassert(isequal(solver(fA,fB),1))\r\n%%\r\nA=false; B=true; X=2;\r\n\r\nf=inline('logical(interp1([0,1],[0,x],1))','x','A','B','X');\r\nfA=@(str)xor(~A,f(eval(str),A,B,X));\r\nfB=@(str)xor(~B,f(eval(str),A,B,X));\r\nclear f A B X;\r\n\r\nassert(isequal(solver(fA,fB),2))\r\n","published":true,"deleted":false,"likes_count":4,"comments_count":2,"created_by":43,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":55,"test_suite_updated_at":null,"rescore_all_solutions":false,"group_id":1,"created_at":"2013-01-05T22:49:40.000Z","updated_at":"2026-02-13T14:49:48.000Z","published_at":"2013-01-06T02:40:34.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\u003eThis is a Matlab adaptation of the\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/Knights_and_Knaves\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eKnights and Knaves\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e logical puzzles.\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\u003eYou are in an island where all inhabitants are either\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:i/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eKnights\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e, who always tell the truth, or\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:i/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eKnaves\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e, who always lie. The island inhabitants can always tell Knights and Knaves apart by their appearance, but to you, as an outsider, they look exactly the same.\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\u003eUpon arriving to this island you encounter two inhabitants standing at a fork in the road. One of them is a Knight, and the other is a Knave, but you do not know which. One of the roads will lead you to the castle, where you wish to go. Your job is to ask one or several yes/no questions that will allow you to know which one is the correct road.\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\u003eImplementation\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\u003eYou function will take two function handles as inputs (one per inhabitant), and must return a single scalar indicating the correct road (1 for the first road or 2 for the second road):\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[    function correct_road = solver(fA, fB)]]\u003e\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eYou may ask one inhabitant a question by evaluating his associated function handle on a char string (the 'question'). Strings must be a valid matlab commands that when evaluated return a scalar logical value (yes/no questions, where\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:i/\u003e\u003c/w:rPr\u003e\u003cw:t\u003etrue\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e is a yes, and\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:i/\u003e\u003c/w:rPr\u003e\u003cw:t\u003efalse\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e is a no). Strings may refer to the following variables:\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"ListParagraph\\\"/\u003e\u003cw:numPr\u003e\u003cw:numId w:val=\\\"1\\\"/\u003e\u003c/w:numPr\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eA: true if the first inhabitant is a Knight, false if he is a Knave\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"ListParagraph\\\"/\u003e\u003cw:numPr\u003e\u003cw:numId w:val=\\\"1\\\"/\u003e\u003c/w:numPr\u003e\u003c/w:pPr\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"ListParagraph\\\"/\u003e\u003cw:numPr\u003e\u003cw:numId w:val=\\\"1\\\"/\u003e\u003c/w:numPr\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eB: true if the second inhabitant is a Knight, false if he is a Knave\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"ListParagraph\\\"/\u003e\u003cw:numPr\u003e\u003cw:numId w:val=\\\"1\\\"/\u003e\u003c/w:numPr\u003e\u003c/w:pPr\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"ListParagraph\\\"/\u003e\u003cw:numPr\u003e\u003cw:numId w:val=\\\"1\\\"/\u003e\u003c/w:numPr\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eX: 1 if the first road leads to the castle, 2 if the second road leads to the castle\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\u003eAsking questions, examples\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=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[    x=fA('A==true');]]\u003e\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003easks the first inhabitant whether he is a Knight (note: this returns always\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:i/\u003e\u003c/w:rPr\u003e\u003cw:t\u003etrue\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e, since both Knights and Knaves would tell you they are Knights; remember, Knaves always lie)\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[    x=fB('X==1');]]\u003e\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003easks the second inhabitant whether the first road leads to the castle (not particularly useful by itself since we do not know whether he is going to respond truthfully or not)\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\u003eNext problem in this series:\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.mathworks.com/matlabcentral/cody/problems/1180-knights-and-knaves-part-2\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eKnights and Knaves (part 2)\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\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":1180,"title":"Knights and Knaves (part 2)","description":"_This is a Matlab adaptation of the_ \u003chttp://en.wikipedia.org/wiki/Knights_and_Knaves Knights and Knaves\u003e _logical puzzles_. \r\n\r\n_You are in an island where all inhabitants are either_ Knights, _who always tell the truth, or_ Knaves, _who always lie. The island inhabitants can always tell Knights and Knaves apart by their appearance, but to you, as an outsider, they look exactly the same_.\r\n\r\nPrevious problem in this series: \u003chttp://www.mathworks.com/matlabcentral/cody/problems/1179-knights-and-knaves-part-1 Knights and Knaves (part 1)\u003e\r\n\r\nNext problem in this series: \u003chttp://www.mathworks.com/matlabcentral/cody/problems/1189-knights-and-knaves-part-3 Knights and Knaves (part 3)\u003e\r\n\r\n*Chapter 2*\r\n\r\nAfter your initial encounter with two island inhabitants you follow the road to the castle. You were minding your own business when suddenly you find yourself surrounded by a small mob of dubious-looking islanders. Blissfully unaware of the latest mob-behavior theories, you decide it will be safe to stay if most of these islanders turn out to be friendly Knights, while you better start running if most of them turn out to be treacherous Knaves. What question(s) you could ask them to determine your best course of action? (note: you are always confronted by an odd number of islanders)\r\n\r\n*Details*\r\n\r\nYou function will take a cell array of function handles as input (one element per islander), and must return _true_ if you decide to run (if there are more Knaves than Knights) or _false_ if you decide to stay (if there are more Knights than Knaves). \r\n\r\n    function run = solver(f)\r\n\r\nYou may ask each inhabitant a question by evaluating his associated function handle on a char string (the 'question'). Strings must be a valid matlab commands that when evaluated  return a scalar logical value (yes/no questions, where _true_ is a yes, and _false_ is a no). Strings may refer to the following variables:\r\n\r\n*   A: an array (samel length as f) of logical values identifying each of the islanders as a Knight (true) or Knave (false)\r\n\r\n\r\n*Asking questions, examples*: \r\n\r\n    x=f{3}('A(3)==true');\r\n\r\nasks the third islander whether he is a Knight (note: this returns always _true_, since both Knights and Knaves would tell you they are Knights; remember, Knaves always lie)\r\n\r\n    x=f{1}('sum(A)\u003e2');\r\n\r\nasks the first islander whether there are more than two Knights in the group (not particularly useful by itself since we do not know whether he is going to respond truthfully or not)\r\n\r\n","description_html":"\u003cp\u003e\u003ci\u003eThis is a Matlab adaptation of the\u003c/i\u003e \u003ca href = \"http://en.wikipedia.org/wiki/Knights_and_Knaves\"\u003eKnights and Knaves\u003c/a\u003e \u003ci\u003elogical puzzles\u003c/i\u003e.\u003c/p\u003e\u003cp\u003e\u003ci\u003eYou are in an island where all inhabitants are either\u003c/i\u003e Knights, \u003ci\u003ewho always tell the truth, or\u003c/i\u003e Knaves, \u003ci\u003ewho always lie. The island inhabitants can always tell Knights and Knaves apart by their appearance, but to you, as an outsider, they look exactly the same\u003c/i\u003e.\u003c/p\u003e\u003cp\u003ePrevious problem in this series: \u003ca href = \"http://www.mathworks.com/matlabcentral/cody/problems/1179-knights-and-knaves-part-1\"\u003eKnights and Knaves (part 1)\u003c/a\u003e\u003c/p\u003e\u003cp\u003eNext problem in this series: \u003ca href = \"http://www.mathworks.com/matlabcentral/cody/problems/1189-knights-and-knaves-part-3\"\u003eKnights and Knaves (part 3)\u003c/a\u003e\u003c/p\u003e\u003cp\u003e\u003cb\u003eChapter 2\u003c/b\u003e\u003c/p\u003e\u003cp\u003eAfter your initial encounter with two island inhabitants you follow the road to the castle. You were minding your own business when suddenly you find yourself surrounded by a small mob of dubious-looking islanders. Blissfully unaware of the latest mob-behavior theories, you decide it will be safe to stay if most of these islanders turn out to be friendly Knights, while you better start running if most of them turn out to be treacherous Knaves. What question(s) you could ask them to determine your best course of action? (note: you are always confronted by an odd number of islanders)\u003c/p\u003e\u003cp\u003e\u003cb\u003eDetails\u003c/b\u003e\u003c/p\u003e\u003cp\u003eYou function will take a cell array of function handles as input (one element per islander), and must return \u003ci\u003etrue\u003c/i\u003e if you decide to run (if there are more Knaves than Knights) or \u003ci\u003efalse\u003c/i\u003e if you decide to stay (if there are more Knights than Knaves).\u003c/p\u003e\u003cpre\u003e    function run = solver(f)\u003c/pre\u003e\u003cp\u003eYou may ask each inhabitant a question by evaluating his associated function handle on a char string (the 'question'). Strings must be a valid matlab commands that when evaluated  return a scalar logical value (yes/no questions, where \u003ci\u003etrue\u003c/i\u003e is a yes, and \u003ci\u003efalse\u003c/i\u003e is a no). Strings may refer to the following variables:\u003c/p\u003e\u003cul\u003e\u003cli\u003eA: an array (samel length as f) of logical values identifying each of the islanders as a Knight (true) or Knave (false)\u003c/li\u003e\u003c/ul\u003e\u003cp\u003e\u003cb\u003eAsking questions, examples\u003c/b\u003e:\u003c/p\u003e\u003cpre\u003e    x=f{3}('A(3)==true');\u003c/pre\u003e\u003cp\u003easks the third islander whether he is a Knight (note: this returns always \u003ci\u003etrue\u003c/i\u003e, since both Knights and Knaves would tell you they are Knights; remember, Knaves always lie)\u003c/p\u003e\u003cpre\u003e    x=f{1}('sum(A)\u0026gt;2');\u003c/pre\u003e\u003cp\u003easks the first islander whether there are more than two Knights in the group (not particularly useful by itself since we do not know whether he is going to respond truthfully or not)\u003c/p\u003e","function_template":"function run = solver(f)\r\n  run=f{1}('~A(2)'); % Run if the first islander says the second one is a Knave\r\nend","test_suite":"%%\r\nA=[true true false false true false true];\r\n\r\ng=inline('logical(interp1([0,1],[0,x],1))','x','A');\r\nf=arrayfun(@(x)@(str)xor(~x,g(eval(str),A)),A,'uni',0);\r\nclear g A;\r\n\r\nassert(isequal(solver(f),false))\r\n\r\n%%\r\nA=[false true false true false];\r\n\r\ng=inline('logical(interp1([0,1],[0,x],1))','x','A');\r\nf=arrayfun(@(x)@(str)xor(~x,g(eval(str),A)),A,'uni',0);\r\nclear g A;\r\n\r\nassert(isequal(solver(f),true))\r\n\r\n%%\r\nA=[false false true true true];\r\n\r\ng=inline('logical(interp1([0,1],[0,x],1))','x','A');\r\nf=arrayfun(@(x)@(str)xor(~x,g(eval(str),A)),A,'uni',0);\r\nclear g A;\r\n\r\nassert(isequal(solver(f),false))\r\n\r\n%%\r\nA=[true false false true false false true true true];\r\n\r\ng=inline('logical(interp1([0,1],[0,x],1))','x','A');\r\nf=arrayfun(@(x)@(str)xor(~x,g(eval(str),A)),A,'uni',0);\r\nclear g A;\r\n\r\nassert(isequal(solver(f),false))\r\n\r\n%%\r\nA=rand(1,99)\u003c.5;\r\n\r\ng=inline('logical(interp1([0,1],[0,x],1))','x','A');\r\nf=arrayfun(@(x)@(str)xor(~x,g(eval(str),A)),A,'uni',0);\r\n\r\nassert(isequal(solver(f),mean(A)\u003c.5))\r\n\r\n%%\r\nA=rand(1,99)\u003c.5;\r\n\r\ng=inline('logical(interp1([0,1],[0,x],1))','x','A');\r\nf=arrayfun(@(x)@(str)xor(~x,g(eval(str),A)),A,'uni',0);\r\n\r\nassert(isequal(solver(f),mean(A)\u003c.5))\r\n\r\n%%\r\nA=rand(1,99)\u003c.5;\r\n\r\ng=inline('logical(interp1([0,1],[0,x],1))','x','A');\r\nf=arrayfun(@(x)@(str)xor(~x,g(eval(str),A)),A,'uni',0);\r\n\r\nassert(isequal(solver(f),mean(A)\u003c.5))\r\n\r\n%%\r\nA=rand(1,99)\u003c.5;\r\n\r\ng=inline('logical(interp1([0,1],[0,x],1))','x','A');\r\nf=arrayfun(@(x)@(str)xor(~x,g(eval(str),A)),A,'uni',0);\r\n\r\nassert(isequal(solver(f),mean(A)\u003c.5))\r\n\r\n","published":true,"deleted":false,"likes_count":3,"comments_count":0,"created_by":43,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":44,"test_suite_updated_at":"2013-01-06T04:54:25.000Z","rescore_all_solutions":false,"group_id":1,"created_at":"2013-01-06T04:34:22.000Z","updated_at":"2026-02-13T14:51:53.000Z","published_at":"2013-01-06T04:54:25.000Z","restored_at":"2017-11-13T15:02:26.000Z","restored_by":null,"spam":false,"simulink":false,"admin_reviewed":true,"description_opc":"{\"relationships\":[{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/document\",\"targetMode\":\"\",\"relationshipId\":\"rId1\",\"target\":\"/matlab/document.xml\"},{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/output\",\"targetMode\":\"\",\"relationshipId\":\"rId2\",\"target\":\"/matlab/output.xml\"}],\"parts\":[{\"partUri\":\"/matlab/document.xml\",\"relationship\":[],\"contentType\":\"application/vnd.mathworks.matlab.code.document+xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\"?\u003e\\n\u003cw:document xmlns:w=\\\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\\\"\u003e\u003cw:body\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:i/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eThis is a Matlab adaptation of the\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/Knights_and_Knaves\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eKnights and Knaves\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\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:i/\u003e\u003c/w:rPr\u003e\u003cw:t\u003elogical puzzles\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:i/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eYou are in an island where all inhabitants are either\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e Knights,\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:i/\u003e\u003c/w:rPr\u003e\u003cw:t\u003ewho always tell the truth, or\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e Knaves,\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:i/\u003e\u003c/w:rPr\u003e\u003cw:t\u003ewho always lie. The island inhabitants can always tell Knights and Knaves apart by their appearance, but to you, as an outsider, they look exactly the same\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003ePrevious problem in this series:\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.mathworks.com/matlabcentral/cody/problems/1179-knights-and-knaves-part-1\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eKnights and Knaves (part 1)\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\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\u003eNext problem in this series:\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.mathworks.com/matlabcentral/cody/problems/1189-knights-and-knaves-part-3\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eKnights and Knaves (part 3)\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\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\u003eChapter 2\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\u003eAfter your initial encounter with two island inhabitants you follow the road to the castle. You were minding your own business when suddenly you find yourself surrounded by a small mob of dubious-looking islanders. Blissfully unaware of the latest mob-behavior theories, you decide it will be safe to stay if most of these islanders turn out to be friendly Knights, while you better start running if most of them turn out to be treacherous Knaves. What question(s) you could ask them to determine your best course of action? (note: you are always confronted by an odd number of islanders)\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\u003eDetails\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\u003eYou function will take a cell array of function handles as input (one element per islander), and must return\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:i/\u003e\u003c/w:rPr\u003e\u003cw:t\u003etrue\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e if you decide to run (if there are more Knaves than Knights) or\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:i/\u003e\u003c/w:rPr\u003e\u003cw:t\u003efalse\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e if you decide to stay (if there are more Knights than Knaves).\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[    function run = solver(f)]]\u003e\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eYou may ask each inhabitant a question by evaluating his associated function handle on a char string (the 'question'). Strings must be a valid matlab commands that when evaluated return a scalar logical value (yes/no questions, where\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:i/\u003e\u003c/w:rPr\u003e\u003cw:t\u003etrue\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e is a yes, and\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:i/\u003e\u003c/w:rPr\u003e\u003cw:t\u003efalse\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e is a no). Strings may refer to the following variables:\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"ListParagraph\\\"/\u003e\u003cw:numPr\u003e\u003cw:numId w:val=\\\"1\\\"/\u003e\u003c/w:numPr\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eA: an array (samel length as f) of logical values identifying each of the islanders as a Knight (true) or Knave (false)\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\u003eAsking questions, examples\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=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[    x=f{3}('A(3)==true');]]\u003e\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003easks the third islander whether he is a Knight (note: this returns always\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:i/\u003e\u003c/w:rPr\u003e\u003cw:t\u003etrue\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e, since both Knights and Knaves would tell you they are Knights; remember, Knaves always lie)\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[    x=f{1}('sum(A)\u003e2');]]\u003e\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003easks the first islander whether there are more than two Knights in the group (not particularly useful by itself since we do not know whether he is going to respond truthfully or not)\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":1189,"title":"Knights and Knaves (part 3)","description":"_This is a Matlab adaptation of the_ \u003chttp://en.wikipedia.org/wiki/Knights_and_Knaves Knights and Knaves\u003e _logical puzzles_. \r\n\r\n_You are in an island where all inhabitants are either_ Knights, _who always tell the truth, or_ Knaves, _who always lie. The island inhabitants can always tell Knights and Knaves apart by their appearance, but to you, as an outsider, they look exactly the same_.\r\n\r\nPrevious problem in this series: \u003chttp://www.mathworks.com/matlabcentral/cody/problems/1180-knights-and-knaves-part-2 Knights and Knaves (part 2)\u003e\r\n\r\n*Chapter 3*\r\n\r\nThat mob of islanders turned out to be a friendly bunch after all, and with their help you reach the castle in no time. The doors to the castle are guarded by a sentinel who will only let you in if you answer correctly the day's password. Fortunately islanders are not particularly security conscious and the guard is happy to answer as many questions as you see fit. What is today's password?\r\n\r\n*Details*\r\n\r\nYou are given a function handle *QUESTION* that allows you to query the guard (either a Knight or a Knave, you do not know which). The function y=QUESTION(str) takes a string _str_ as input (the 'question'), and returns a logical value (the yes/no 'answer' the guard would give to this question). Valid questions are any valid matlab syntax. The guard has access to the following variables (his pool of 'knowledge'):\r\n\r\n* *KNIGHT*: Islander's type (true for a Knight, false for a Knave)\r\n* *PASSWORD*: Today's password (a char array of all-capital letters)\r\n\r\nYou may query the guard by evaluating the function *QUESTION*. For example x=QUESTION('KNIGHT==true') asks the guard whether he is a Knight.\r\n\r\nYour function should return the correct password.\r\n","description_html":"\u003cp\u003e\u003ci\u003eThis is a Matlab adaptation of the\u003c/i\u003e \u003ca href = \"http://en.wikipedia.org/wiki/Knights_and_Knaves\"\u003eKnights and Knaves\u003c/a\u003e \u003ci\u003elogical puzzles\u003c/i\u003e.\u003c/p\u003e\u003cp\u003e\u003ci\u003eYou are in an island where all inhabitants are either\u003c/i\u003e Knights, \u003ci\u003ewho always tell the truth, or\u003c/i\u003e Knaves, \u003ci\u003ewho always lie. The island inhabitants can always tell Knights and Knaves apart by their appearance, but to you, as an outsider, they look exactly the same\u003c/i\u003e.\u003c/p\u003e\u003cp\u003ePrevious problem in this series: \u003ca href = \"http://www.mathworks.com/matlabcentral/cody/problems/1180-knights-and-knaves-part-2\"\u003eKnights and Knaves (part 2)\u003c/a\u003e\u003c/p\u003e\u003cp\u003e\u003cb\u003eChapter 3\u003c/b\u003e\u003c/p\u003e\u003cp\u003eThat mob of islanders turned out to be a friendly bunch after all, and with their help you reach the castle in no time. The doors to the castle are guarded by a sentinel who will only let you in if you answer correctly the day's password. Fortunately islanders are not particularly security conscious and the guard is happy to answer as many questions as you see fit. What is today's password?\u003c/p\u003e\u003cp\u003e\u003cb\u003eDetails\u003c/b\u003e\u003c/p\u003e\u003cp\u003eYou are given a function handle \u003cb\u003eQUESTION\u003c/b\u003e that allows you to query the guard (either a Knight or a Knave, you do not know which). The function y=QUESTION(str) takes a string \u003ci\u003estr\u003c/i\u003e as input (the 'question'), and returns a logical value (the yes/no 'answer' the guard would give to this question). Valid questions are any valid matlab syntax. The guard has access to the following variables (his pool of 'knowledge'):\u003c/p\u003e\u003cul\u003e\u003cli\u003e\u003cb\u003eKNIGHT\u003c/b\u003e: Islander's type (true for a Knight, false for a Knave)\u003c/li\u003e\u003cli\u003e\u003cb\u003ePASSWORD\u003c/b\u003e: Today's password (a char array of all-capital letters)\u003c/li\u003e\u003c/ul\u003e\u003cp\u003eYou may query the guard by evaluating the function \u003cb\u003eQUESTION\u003c/b\u003e. For example x=QUESTION('KNIGHT==true') asks the guard whether he is a Knight.\u003c/p\u003e\u003cp\u003eYour function should return the correct password.\u003c/p\u003e","function_template":"function password = solver(QUESTION)\r\n  if QUESTION('length(PASSWORD)\u003c7'), password='1234';\r\n  else password='PASSWORD'; \r\n  end\r\nend","test_suite":"%%\r\nKNIGHT=true; PASSWORD='PASSWORD';\r\n\r\ng=inline('logical(interp1([0,1],[0,x],1))','x','KNIGHT','PASSWORD');\r\nF=@(str)xor(~KNIGHT,g(eval(str),KNIGHT,PASSWORD));\r\nclear g KNIGHT PASSWORD;\r\n\r\nassert(isequal(solver(F),'PASSWORD'))\r\n\r\n%%\r\nKNIGHT=false; PASSWORD='PASSWORD';\r\n\r\ng=inline('logical(interp1([0,1],[0,x],1))','x','KNIGHT','PASSWORD');\r\nF=@(str)xor(~KNIGHT,g(eval(str),KNIGHT,PASSWORD));\r\nclear g KNIGHT PASSWORD;\r\n\r\nassert(isequal(solver(F),'PASSWORD'))\r\n\r\n%%\r\nKNIGHT=false; PASSWORD='DONOTPANIC';\r\n\r\ng=inline('logical(interp1([0,1],[0,x],1))','x','KNIGHT','PASSWORD');\r\nF=@(str)xor(~KNIGHT,g(eval(str),KNIGHT,PASSWORD));\r\nclear g KNIGHT PASSWORD;\r\n\r\nassert(isequal(solver(F),'DONOTPANIC'))\r\n\r\n%%\r\nKNIGHT=true; PASSWORD='FORTYTWO';\r\n\r\ng=inline('logical(interp1([0,1],[0,x],1))','x','KNIGHT','PASSWORD');\r\nF=@(str)xor(~KNIGHT,g(eval(str),KNIGHT,PASSWORD));\r\nclear g KNIGHT PASSWORD;\r\n\r\nassert(isequal(solver(F),'FORTYTWO'))\r\n\r\n%%\r\nKNIGHT=false; PASSWORD='PLEASELETMEIN';\r\n\r\ng=inline('logical(interp1([0,1],[0,x],1))','x','KNIGHT','PASSWORD');\r\nF=@(str)xor(~KNIGHT,g(eval(str),KNIGHT,PASSWORD));\r\nclear g KNIGHT PASSWORD;\r\n\r\nassert(isequal(solver(F),'PLEASELETMEIN'))\r\n","published":true,"deleted":false,"likes_count":6,"comments_count":4,"created_by":43,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":36,"test_suite_updated_at":null,"rescore_all_solutions":false,"group_id":1,"created_at":"2013-01-08T23:51:16.000Z","updated_at":"2026-02-13T14:53:28.000Z","published_at":"2013-01-09T00:14:17.000Z","restored_at":null,"restored_by":null,"spam":false,"simulink":false,"admin_reviewed":false,"description_opc":"{\"relationships\":[{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/document\",\"targetMode\":\"\",\"relationshipId\":\"rId1\",\"target\":\"/matlab/document.xml\"},{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/output\",\"targetMode\":\"\",\"relationshipId\":\"rId2\",\"target\":\"/matlab/output.xml\"}],\"parts\":[{\"partUri\":\"/matlab/document.xml\",\"relationship\":[],\"contentType\":\"application/vnd.mathworks.matlab.code.document+xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\"?\u003e\\n\u003cw:document xmlns:w=\\\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\\\"\u003e\u003cw:body\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:i/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eThis is a Matlab adaptation of the\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/Knights_and_Knaves\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eKnights and Knaves\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\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:i/\u003e\u003c/w:rPr\u003e\u003cw:t\u003elogical puzzles\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:i/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eYou are in an island where all inhabitants are either\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e Knights,\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:i/\u003e\u003c/w:rPr\u003e\u003cw:t\u003ewho always tell the truth, or\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e Knaves,\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:i/\u003e\u003c/w:rPr\u003e\u003cw:t\u003ewho always lie. The island inhabitants can always tell Knights and Knaves apart by their appearance, but to you, as an outsider, they look exactly the same\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003ePrevious problem in this series:\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.mathworks.com/matlabcentral/cody/problems/1180-knights-and-knaves-part-2\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eKnights and Knaves (part 2)\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\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\u003eChapter 3\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\u003eThat mob of islanders turned out to be a friendly bunch after all, and with their help you reach the castle in no time. The doors to the castle are guarded by a sentinel who will only let you in if you answer correctly the day's password. Fortunately islanders are not particularly security conscious and the guard is happy to answer as many questions as you see fit. What is today's password?\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\u003eDetails\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\u003eYou are given a function handle\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eQUESTION\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e that allows you to query the guard (either a Knight or a Knave, you do not know which). The function y=QUESTION(str) takes a string\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:i/\u003e\u003c/w:rPr\u003e\u003cw:t\u003estr\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e as input (the 'question'), and returns a logical value (the yes/no 'answer' the guard would give to this question). Valid questions are any valid matlab syntax. The guard has access to the following variables (his pool of 'knowledge'):\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"ListParagraph\\\"/\u003e\u003cw:numPr\u003e\u003cw:numId w:val=\\\"1\\\"/\u003e\u003c/w:numPr\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eKNIGHT\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e: Islander's type (true for a Knight, false for a Knave)\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"ListParagraph\\\"/\u003e\u003cw:numPr\u003e\u003cw:numId w:val=\\\"1\\\"/\u003e\u003c/w:numPr\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003ePASSWORD\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e: Today's password (a char array of all-capital letters)\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\u003eYou may query the guard by evaluating the function\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eQUESTION\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e. For example x=QUESTION('KNIGHT==true') asks the guard whether he is a Knight.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eYour function should return the correct password.\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":1187,"title":"Knave in the middle attack","description":"This is a Matlab adaptation of the \u003chttp://en.wikipedia.org/wiki/Knights_and_Knaves Knights and Knaves\u003e logical puzzles, mixed with the famous \u003chttp://en.wikipedia.org/wiki/Man-in-the-middle_attack man-in-the-middle attack\u003e in computer security. \r\n\r\nYou are in an island where all inhabitants are either _Knights_, who always tell the truth, or _Knaves_, who always lie. Your job is to sit in the middle of an islander and a second person interviewing the islander, intercepting all questions posed to the islander, and answering to the interviewer in a way that will make him think that the islander is the opposite type of what he really is (answer as a Knave if the islander is a Knight, or answer as a Knight if the islander is a Knave). The problem is: a) you really do not know whether the islander is a Knight or a Knave; and b) the islander knows some secret that only he knows, so you may not be able to anticipate what he would answer even if you knew whether he was a Knight or a Knave! Luckily for you, you may ask the islander privately any questions that you wish before responding to the interviewer.  \r\n\r\n*Details:*\r\n\r\nYou are given a function handle F that will act as the islander. This function will answer any question the way the islander would. The function _function answer=F(question)_ takes a char array as input (the 'question'), and returns a logical value (the yes/no 'answer' this particular islander would give to this question; _true_ means 'yes' and _false_ means 'no'). Valid questions are any valid matlab string. The islander has access to the following variables (the things that he 'knows'):\r\n\r\n* *A*: Islander's type. A is a logical variable: true for a Knight, false for a Knave\r\n* *X*: A secret formula only islanders know. An unknown function on positive integer values that when evaluated returns a logical value (e.g. _x\u003e1_).\r\n* *F*: Introspection. F is the handle associated with this islander's answers, so he knows himself what he would respond to some hypothetical question\r\n\r\nThe function handles associated with a Knight and a Knave look, respectively, something like:\r\n\r\n  function answer = Knight(question)\r\n    A = true;\r\n    X = @(x)x\u003e10;\r\n    F = @Knight;\r\n    answer = eval(question);\r\n  end\r\n\r\n  function answer = Knave(question)\r\n    A = false;\r\n    X = @(x)x\u003e10;\r\n    F = @Knave;\r\n    answer = ~eval(question);\r\n  end\r\n\r\nOf course the values of X will be different and unknown to you.\r\n\r\nA few examples:\r\n\r\n Knight('A==true') == true\r\n\r\nThis question asks whether the islander is a Knight, and a Knight would respond affirmatively to such question. \r\n\r\n Knave('A|X(1)') == true\r\n\r\nThis question asks whether the islander is a Knight or the value of the secret formula at 1 is true. None of these are true, but the Knave will lie to you and respond 'yes'.\r\n\r\n Knave('F(''A'')') == false\r\n\r\nThis question asks whether the islander would respond affirmatively to the question of whether he is a knight. Both Knights and Knaves would actually respond affirmatively when questioned whether they are Knights, but a Knave would lie to you when telling you how he would respond to such question, so he would say 'no'.\r\n\r\nYou must implement a function that will take two inputs: 1) the function handle of an islander (either @Knight or @Knave); and 2) a question (as a char array). Your function should return the answer this same islander would give to this question if he was the opposite type than he really is. In other words:\r\n\r\n your_function(@Knight,str) should return Knave(str)\r\n your_function(@Knave,str) should return Knight(str)\r\n\r\nYour function might query the function handle of the islander with whatever questions it sees fit before responding.\r\n\r\n*Examples:*\r\n\r\n your_function(@Knight,'A==true') == true;\r\n\r\n your_function(@Knave,'A==true') == true; \r\n\r\nThis question asks whether the islander is a Knight; both Knights and Knaves would respond _true_ to this question.\r\n\r\n your_function(@Knight,'F(''A==true'')==true') == false; \r\n\r\n your_function (@Knave,'F(''A==true'')==true') == true; \r\n\r\nThis question asks if the islander would respond yes to the question of whether he is a Knight. A Knight would respond 'yes', while a Knave would (falsely) respond 'no', so your function should return exactly the opposite in each case.\r\n\r\n your_function(@Knight,'X(3)~=X(2)') == true\r\n\r\n your_function(@Knave,'X(3)~=X(2)') == false \r\n\r\n(Assuming X(2)==X(3); you do not know the values of X, only islanders do) This question asks to the islander whether the value of his secret formula X(2) is different from the value of his secret formula X(3). Assuming that they were actually the same value a Knight would respond negatively (telling you the truth), while a Knave would respond affirmatively (lying to you), so, again, your function should return exactly the opposite response in each case.  ","description_html":"\u003cdiv style = \"text-align: start; line-height: 20px; 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: normal; text-decoration: none; white-space: normal; \"\u003e\u003cdiv style=\"display: block; min-width: 0px; padding-top: 0px; transform-origin: 332px 914.5px; vertical-align: baseline; perspective-origin: 332px 914.5px; \"\u003e\u003cdiv style=\"font-family: Helvetica, Arial, sans-serif; line-height: 21px; margin-bottom: 9px; margin-left: 4px; margin-right: 10px; margin-top: 2px; text-align: left; transform-origin: 309px 21px; white-space: pre-wrap; perspective-origin: 309px 21px; margin-left: 4px; margin-top: 2px; margin-bottom: 9px; margin-right: 10px; \"\u003e\u003cspan style=\"display: inline; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; transform-origin: 0px 0px; perspective-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003eThis is a Matlab adaptation of the\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display: inline; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; transform-origin: 0px 0px; perspective-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003e \u003c/span\u003e\u003c/span\u003e\u003ca target='_blank' href = \"http://en.wikipedia.org/wiki/Knights_and_Knaves\"\u003e\u003cspan style=\"\"\u003e\u003cspan style=\"\"\u003eKnights and Knaves\u003c/span\u003e\u003c/span\u003e\u003c/a\u003e\u003cspan style=\"display: inline; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; transform-origin: 0px 0px; perspective-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003e logical puzzles, mixed with the famous\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display: inline; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; transform-origin: 0px 0px; perspective-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003e \u003c/span\u003e\u003c/span\u003e\u003ca target='_blank' href = \"http://en.wikipedia.org/wiki/Man-in-the-middle_attack\"\u003e\u003cspan style=\"\"\u003e\u003cspan style=\"\"\u003eman-in-the-middle attack\u003c/span\u003e\u003c/span\u003e\u003c/a\u003e\u003cspan style=\"display: inline; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; transform-origin: 0px 0px; perspective-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003e in computer security.\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"font-family: Helvetica, Arial, sans-serif; line-height: 21px; margin-bottom: 9px; margin-left: 4px; margin-right: 10px; margin-top: 2px; text-align: left; transform-origin: 309px 94.5px; white-space: pre-wrap; perspective-origin: 309px 94.5px; margin-left: 4px; margin-top: 2px; margin-bottom: 9px; margin-right: 10px; \"\u003e\u003cspan style=\"display: inline; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; transform-origin: 0px 0px; perspective-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003eYou are in an island where all inhabitants are either\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display: inline; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; transform-origin: 0px 0px; perspective-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003e \u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display: inline; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; transform-origin: 0px 0px; perspective-origin: 0px 0px; \"\u003e\u003cspan style=\"font-style: italic; \"\u003eKnights\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display: inline; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; transform-origin: 0px 0px; perspective-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003e, who always tell the truth, or\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display: inline; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; transform-origin: 0px 0px; perspective-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003e \u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display: inline; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; transform-origin: 0px 0px; perspective-origin: 0px 0px; \"\u003e\u003cspan style=\"font-style: italic; \"\u003eKnaves\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display: inline; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; transform-origin: 0px 0px; perspective-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003e, who always lie. Your job is to sit in the middle of an islander and a second person interviewing the islander, intercepting all questions posed to the islander, and answering to the interviewer in a way that will make him think that the islander is the opposite type of what he really is (answer as a Knave if the islander is a Knight, or answer as a Knight if the islander is a Knave). The problem is: a) you really do not know whether the islander is a Knight or a Knave; and b) the islander knows some secret that only he knows, so you may not be able to anticipate what he would answer even if you knew whether he was a Knight or a Knave! Luckily for you, you may ask the islander privately any questions that you wish before responding to the interviewer.\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"font-family: Helvetica, Arial, sans-serif; line-height: 21px; margin-bottom: 9px; margin-left: 4px; margin-right: 10px; margin-top: 2px; text-align: left; transform-origin: 309px 10.5px; white-space: pre-wrap; perspective-origin: 309px 10.5px; margin-left: 4px; margin-top: 2px; margin-bottom: 9px; margin-right: 10px; \"\u003e\u003cspan style=\"display: inline; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; transform-origin: 0px 0px; perspective-origin: 0px 0px; \"\u003e\u003cspan style=\"font-weight: bold; \"\u003eDetails:\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"font-family: Helvetica, Arial, sans-serif; line-height: 21px; margin-bottom: 9px; margin-left: 4px; margin-right: 10px; margin-top: 2px; text-align: left; transform-origin: 309px 52.5px; white-space: pre-wrap; perspective-origin: 309px 52.5px; margin-left: 4px; margin-top: 2px; margin-bottom: 9px; margin-right: 10px; \"\u003e\u003cspan style=\"display: inline; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; transform-origin: 0px 0px; perspective-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003eYou are given a function handle F that will act as the islander. This function will answer any question the way the islander would. The function\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display: inline; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; transform-origin: 0px 0px; perspective-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003e \u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display: inline; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; transform-origin: 0px 0px; perspective-origin: 0px 0px; \"\u003e\u003cspan style=\"font-style: italic; \"\u003efunction answer=F(question)\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display: inline; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; transform-origin: 0px 0px; perspective-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003e takes a char array as input (the 'question'), and returns a logical value (the yes/no 'answer' this particular islander would give to this question;\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display: inline; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; transform-origin: 0px 0px; perspective-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003e \u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display: inline; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; transform-origin: 0px 0px; perspective-origin: 0px 0px; \"\u003e\u003cspan style=\"font-style: italic; \"\u003etrue\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display: inline; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; transform-origin: 0px 0px; perspective-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003e means 'yes' and\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display: inline; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; transform-origin: 0px 0px; perspective-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003e \u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display: inline; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; transform-origin: 0px 0px; perspective-origin: 0px 0px; \"\u003e\u003cspan style=\"font-style: italic; \"\u003efalse\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display: inline; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; transform-origin: 0px 0px; perspective-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003e means 'no'). Valid questions are any valid matlab string. The islander has access to the following variables (the things that he 'knows'):\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cul style=\"font-family: Helvetica, Arial, sans-serif; list-style-type: square; margin-bottom: 20px; margin-top: 10px; transform-origin: 316px 50px; perspective-origin: 316px 50px; margin-top: 10px; margin-bottom: 20px; \"\u003e\u003cli style=\"display: list-item; margin-bottom: 0px; margin-left: 56px; margin-top: 0px; text-align: left; transform-origin: 288px 10px; white-space: pre-wrap; perspective-origin: 288px 10px; margin-left: 56px; \"\u003e\u003cspan style=\"display: inline; margin-left: 0px; transform-origin: 0px 0px; perspective-origin: 0px 0px; \"\u003e\u003cspan style=\"font-weight: bold; \"\u003eA\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display: inline; margin-left: 0px; transform-origin: 0px 0px; perspective-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003e: Islander's type. A is a logical variable: true for a Knight, false for a Knave\u003c/span\u003e\u003c/span\u003e\u003c/li\u003e\u003cli style=\"display: list-item; margin-bottom: 0px; margin-left: 56px; margin-top: 0px; text-align: left; transform-origin: 288px 20px; white-space: pre-wrap; perspective-origin: 288px 20px; margin-left: 56px; \"\u003e\u003cspan style=\"display: inline; margin-left: 0px; transform-origin: 0px 0px; perspective-origin: 0px 0px; \"\u003e\u003cspan style=\"font-weight: bold; \"\u003eX\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display: inline; margin-left: 0px; transform-origin: 0px 0px; perspective-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003e: A secret formula only islanders know. An unknown function on positive integer values that when evaluated returns a logical value (e.g.\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display: inline; margin-left: 0px; transform-origin: 0px 0px; perspective-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003e \u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display: inline; margin-left: 0px; transform-origin: 0px 0px; perspective-origin: 0px 0px; \"\u003e\u003cspan style=\"font-style: italic; \"\u003ex\u0026gt;1\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display: inline; margin-left: 0px; transform-origin: 0px 0px; perspective-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003e).\u003c/span\u003e\u003c/span\u003e\u003c/li\u003e\u003cli style=\"display: list-item; margin-bottom: 0px; margin-left: 56px; margin-top: 0px; text-align: left; transform-origin: 288px 20px; white-space: pre-wrap; perspective-origin: 288px 20px; margin-left: 56px; \"\u003e\u003cspan style=\"display: inline; margin-left: 0px; transform-origin: 0px 0px; perspective-origin: 0px 0px; \"\u003e\u003cspan style=\"font-weight: bold; \"\u003eF\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display: inline; margin-left: 0px; transform-origin: 0px 0px; perspective-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003e: Introspection. F is the handle associated with this islander's answers, so he knows himself what he would respond to some hypothetical question\u003c/span\u003e\u003c/span\u003e\u003c/li\u003e\u003c/ul\u003e\u003cdiv style=\"font-family: Helvetica, Arial, sans-serif; line-height: 21px; margin-bottom: 9px; margin-left: 4px; margin-right: 10px; margin-top: 2px; text-align: left; transform-origin: 309px 10.5px; white-space: pre-wrap; perspective-origin: 309px 10.5px; margin-left: 4px; margin-top: 2px; margin-bottom: 9px; margin-right: 10px; \"\u003e\u003cspan style=\"display: inline; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; transform-origin: 0px 0px; perspective-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003eThe function handles associated with a Knight and a Knave look, respectively, something like:\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"background-color: rgb(247, 247, 247); border-bottom-left-radius: 4px; border-bottom-right-radius: 4px; border-top-left-radius: 4px; border-top-right-radius: 4px; margin-bottom: 10px; margin-left: 3px; margin-right: 3px; margin-top: 10px; transform-origin: 329px 130px; perspective-origin: 329px 130px; margin-left: 3px; margin-top: 10px; margin-bottom: 10px; margin-right: 3px; \"\u003e\u003cdiv style=\"background-color: rgba(0, 0, 0, 0); border-bottom-left-radius: 0px; border-bottom-right-radius: 0px; 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-top-left-radius: 0px; border-top-right-radius: 0px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; min-height: 18px; padding-left: 4px; transform-origin: 329px 10px; white-space: nowrap; perspective-origin: 329px 10px; \"\u003e\u003cspan style=\"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-right: 45px; min-height: 0px; padding-left: 0px; tab-size: 4; transform-origin: 0px 0px; white-space: pre; perspective-origin: 0px 0px; margin-right: 45px; \"\u003e\u003cspan style=\"border-bottom-color: rgb(14, 0, 255); border-left-color: rgb(14, 0, 255); border-right-color: rgb(14, 0, 255); border-top-color: rgb(14, 0, 255); caret-color: rgb(14, 0, 255); color: rgb(14, 0, 255); margin-right: 0px; outline-color: rgb(14, 0, 255); text-decoration-color: rgb(14, 0, 255); column-rule-color: rgb(14, 0, 255); \"\u003efunction \u003c/span\u003e\u003cspan style=\"margin-right: 0px; \"\u003eanswer = Knight(question)\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"background-color: rgba(0, 0, 0, 0); border-bottom-left-radius: 0px; border-bottom-right-radius: 0px; 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-top-left-radius: 0px; border-top-right-radius: 0px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; min-height: 18px; padding-left: 4px; transform-origin: 329px 10px; white-space: nowrap; perspective-origin: 329px 10px; \"\u003e\u003cspan style=\"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-right: 45px; min-height: 0px; padding-left: 0px; tab-size: 4; transform-origin: 0px 0px; white-space: pre; perspective-origin: 0px 0px; margin-right: 45px; \"\u003e\u003cspan style=\"margin-right: 0px; \"\u003e  A = true;\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"background-color: rgba(0, 0, 0, 0); border-bottom-left-radius: 0px; border-bottom-right-radius: 0px; 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-top-left-radius: 0px; border-top-right-radius: 0px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; min-height: 18px; padding-left: 4px; transform-origin: 329px 10px; white-space: nowrap; perspective-origin: 329px 10px; \"\u003e\u003cspan style=\"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-right: 45px; min-height: 0px; padding-left: 0px; tab-size: 4; transform-origin: 0px 0px; white-space: pre; perspective-origin: 0px 0px; margin-right: 45px; \"\u003e\u003cspan style=\"margin-right: 0px; \"\u003e  X = @(x)x\u0026gt;10;\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"background-color: rgba(0, 0, 0, 0); border-bottom-left-radius: 0px; border-bottom-right-radius: 0px; 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-top-left-radius: 0px; border-top-right-radius: 0px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; min-height: 18px; padding-left: 4px; transform-origin: 329px 10px; white-space: nowrap; perspective-origin: 329px 10px; \"\u003e\u003cspan style=\"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-right: 45px; min-height: 0px; padding-left: 0px; tab-size: 4; transform-origin: 0px 0px; white-space: pre; perspective-origin: 0px 0px; margin-right: 45px; \"\u003e\u003cspan style=\"margin-right: 0px; \"\u003e  F = @Knight;\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"background-color: rgba(0, 0, 0, 0); border-bottom-left-radius: 0px; border-bottom-right-radius: 0px; 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-top-left-radius: 0px; border-top-right-radius: 0px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; min-height: 18px; padding-left: 4px; transform-origin: 329px 10px; white-space: nowrap; perspective-origin: 329px 10px; \"\u003e\u003cspan style=\"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-right: 45px; min-height: 0px; padding-left: 0px; tab-size: 4; transform-origin: 0px 0px; white-space: pre; perspective-origin: 0px 0px; margin-right: 45px; \"\u003e\u003cspan style=\"margin-right: 0px; \"\u003e  answer = eval(question);\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"background-color: rgba(0, 0, 0, 0); border-bottom-left-radius: 0px; border-bottom-right-radius: 0px; 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-top-left-radius: 0px; border-top-right-radius: 0px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; min-height: 18px; padding-left: 4px; transform-origin: 329px 10px; white-space: nowrap; perspective-origin: 329px 10px; \"\u003e\u003cspan style=\"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-right: 45px; min-height: 0px; padding-left: 0px; tab-size: 4; transform-origin: 0px 0px; white-space: pre; perspective-origin: 0px 0px; margin-right: 45px; \"\u003e\u003cspan style=\"border-bottom-color: rgb(14, 0, 255); border-left-color: rgb(14, 0, 255); border-right-color: rgb(14, 0, 255); border-top-color: rgb(14, 0, 255); caret-color: rgb(14, 0, 255); color: rgb(14, 0, 255); margin-right: 0px; outline-color: rgb(14, 0, 255); text-decoration-color: rgb(14, 0, 255); column-rule-color: rgb(14, 0, 255); \"\u003eend\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"background-color: rgba(0, 0, 0, 0); border-bottom-left-radius: 0px; border-bottom-right-radius: 0px; 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-top-left-radius: 0px; border-top-right-radius: 0px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; min-height: 18px; padding-left: 4px; transform-origin: 329px 10px; white-space: nowrap; perspective-origin: 329px 10px; \"\u003e\u003cspan style=\"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-right: 45px; min-height: 0px; padding-left: 0px; tab-size: 4; transform-origin: 0px 0px; white-space: pre; perspective-origin: 0px 0px; margin-right: 45px; \"\u003e\u003cspan style=\"margin-right: 0px; \"\u003e\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"background-color: rgba(0, 0, 0, 0); border-bottom-left-radius: 0px; border-bottom-right-radius: 0px; 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-top-left-radius: 0px; border-top-right-radius: 0px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; min-height: 18px; padding-left: 4px; transform-origin: 329px 10px; white-space: nowrap; perspective-origin: 329px 10px; \"\u003e\u003cspan style=\"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-right: 45px; min-height: 0px; padding-left: 0px; tab-size: 4; transform-origin: 0px 0px; white-space: pre; perspective-origin: 0px 0px; margin-right: 45px; \"\u003e\u003cspan style=\"border-bottom-color: rgb(14, 0, 255); border-left-color: rgb(14, 0, 255); border-right-color: rgb(14, 0, 255); border-top-color: rgb(14, 0, 255); caret-color: rgb(14, 0, 255); color: rgb(14, 0, 255); margin-right: 0px; outline-color: rgb(14, 0, 255); text-decoration-color: rgb(14, 0, 255); column-rule-color: rgb(14, 0, 255); \"\u003efunction \u003c/span\u003e\u003cspan style=\"margin-right: 0px; \"\u003eanswer = Knave(question)\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"background-color: rgba(0, 0, 0, 0); border-bottom-left-radius: 0px; border-bottom-right-radius: 0px; 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-top-left-radius: 0px; border-top-right-radius: 0px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; min-height: 18px; padding-left: 4px; transform-origin: 329px 10px; white-space: nowrap; perspective-origin: 329px 10px; \"\u003e\u003cspan style=\"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-right: 45px; min-height: 0px; padding-left: 0px; tab-size: 4; transform-origin: 0px 0px; white-space: pre; perspective-origin: 0px 0px; margin-right: 45px; \"\u003e\u003cspan style=\"margin-right: 0px; \"\u003e  A = false;\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"background-color: rgba(0, 0, 0, 0); border-bottom-left-radius: 0px; border-bottom-right-radius: 0px; 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-top-left-radius: 0px; border-top-right-radius: 0px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; min-height: 18px; padding-left: 4px; transform-origin: 329px 10px; white-space: nowrap; perspective-origin: 329px 10px; \"\u003e\u003cspan style=\"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-right: 45px; min-height: 0px; padding-left: 0px; tab-size: 4; transform-origin: 0px 0px; white-space: pre; perspective-origin: 0px 0px; margin-right: 45px; \"\u003e\u003cspan style=\"margin-right: 0px; \"\u003e  X = @(x)x\u0026gt;10;\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"background-color: rgba(0, 0, 0, 0); border-bottom-left-radius: 0px; border-bottom-right-radius: 0px; 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-top-left-radius: 0px; border-top-right-radius: 0px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; min-height: 18px; padding-left: 4px; transform-origin: 329px 10px; white-space: nowrap; perspective-origin: 329px 10px; \"\u003e\u003cspan style=\"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-right: 45px; min-height: 0px; padding-left: 0px; tab-size: 4; transform-origin: 0px 0px; white-space: pre; perspective-origin: 0px 0px; margin-right: 45px; \"\u003e\u003cspan style=\"margin-right: 0px; \"\u003e  F = @Knave;\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"background-color: rgba(0, 0, 0, 0); border-bottom-left-radius: 0px; border-bottom-right-radius: 0px; 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-top-left-radius: 0px; border-top-right-radius: 0px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; min-height: 18px; padding-left: 4px; transform-origin: 329px 10px; white-space: nowrap; perspective-origin: 329px 10px; \"\u003e\u003cspan style=\"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-right: 45px; min-height: 0px; padding-left: 0px; tab-size: 4; transform-origin: 0px 0px; white-space: pre; perspective-origin: 0px 0px; margin-right: 45px; \"\u003e\u003cspan style=\"margin-right: 0px; \"\u003e  answer = ~eval(question);\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"background-color: rgba(0, 0, 0, 0); border-bottom-left-radius: 0px; border-bottom-right-radius: 0px; 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-top-left-radius: 0px; border-top-right-radius: 0px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; min-height: 18px; padding-left: 4px; transform-origin: 329px 10px; white-space: nowrap; perspective-origin: 329px 10px; \"\u003e\u003cspan style=\"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-right: 45px; min-height: 0px; padding-left: 0px; tab-size: 4; transform-origin: 0px 0px; white-space: pre; perspective-origin: 0px 0px; margin-right: 45px; \"\u003e\u003cspan style=\"border-bottom-color: rgb(14, 0, 255); border-left-color: rgb(14, 0, 255); border-right-color: rgb(14, 0, 255); border-top-color: rgb(14, 0, 255); caret-color: rgb(14, 0, 255); color: rgb(14, 0, 255); margin-right: 0px; outline-color: rgb(14, 0, 255); text-decoration-color: rgb(14, 0, 255); column-rule-color: rgb(14, 0, 255); \"\u003eend\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003c/div\u003e\u003cdiv style=\"font-family: Helvetica, Arial, sans-serif; line-height: 21px; margin-bottom: 9px; margin-left: 4px; margin-right: 10px; margin-top: 10px; text-align: left; transform-origin: 309px 10.5px; white-space: pre-wrap; perspective-origin: 309px 10.5px; margin-left: 4px; margin-top: 10px; margin-bottom: 9px; margin-right: 10px; \"\u003e\u003cspan style=\"display: inline; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; transform-origin: 0px 0px; perspective-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003eOf course the values of X will be different and unknown to you.\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"font-family: Helvetica, Arial, sans-serif; line-height: 21px; margin-bottom: 9px; margin-left: 4px; margin-right: 10px; margin-top: 2px; text-align: left; transform-origin: 309px 10.5px; white-space: pre-wrap; perspective-origin: 309px 10.5px; margin-left: 4px; margin-top: 2px; margin-bottom: 9px; margin-right: 10px; \"\u003e\u003cspan style=\"display: inline; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; transform-origin: 0px 0px; perspective-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003eA few examples:\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"background-color: rgb(247, 247, 247); border-bottom-left-radius: 4px; border-bottom-right-radius: 4px; border-top-left-radius: 4px; border-top-right-radius: 4px; margin-bottom: 10px; margin-left: 3px; margin-right: 3px; margin-top: 10px; transform-origin: 329px 10px; perspective-origin: 329px 10px; margin-left: 3px; margin-top: 10px; margin-bottom: 10px; margin-right: 3px; \"\u003e\u003cdiv style=\"background-color: rgba(0, 0, 0, 0); border-bottom-left-radius: 0px; border-bottom-right-radius: 0px; 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-top-left-radius: 0px; border-top-right-radius: 0px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; min-height: 18px; padding-left: 4px; white-space: nowrap; \"\u003e\u003cspan style=\"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-right: 45px; min-height: 0px; padding-left: 0px; tab-size: 4; transform-origin: 0px 0px; white-space: pre; perspective-origin: 0px 0px; margin-right: 45px; \"\u003e\u003cspan style=\"margin-right: 0px; \"\u003e Knight(\u003c/span\u003e\u003cspan style=\"border-bottom-color: rgb(170, 4, 249); border-left-color: rgb(170, 4, 249); border-right-color: rgb(170, 4, 249); border-top-color: rgb(170, 4, 249); caret-color: rgb(170, 4, 249); color: rgb(170, 4, 249); margin-right: 0px; outline-color: rgb(170, 4, 249); text-decoration-color: rgb(170, 4, 249); column-rule-color: rgb(170, 4, 249); \"\u003e'A==true'\u003c/span\u003e\u003cspan style=\"margin-right: 0px; \"\u003e) == true\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003c/div\u003e\u003cdiv style=\"font-family: Helvetica, Arial, sans-serif; line-height: 21px; margin-bottom: 9px; margin-left: 4px; margin-right: 10px; margin-top: 10px; text-align: left; transform-origin: 309px 21px; white-space: pre-wrap; perspective-origin: 309px 21px; margin-left: 4px; margin-top: 10px; margin-bottom: 9px; margin-right: 10px; \"\u003e\u003cspan style=\"display: inline; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; transform-origin: 0px 0px; perspective-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003eThis question asks whether the islander is a Knight, and a Knight would respond affirmatively to such question.\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"background-color: rgb(247, 247, 247); border-bottom-left-radius: 4px; border-bottom-right-radius: 4px; border-top-left-radius: 4px; border-top-right-radius: 4px; margin-bottom: 10px; margin-left: 3px; margin-right: 3px; margin-top: 10px; transform-origin: 329px 10px; perspective-origin: 329px 10px; margin-left: 3px; margin-top: 10px; margin-bottom: 10px; margin-right: 3px; \"\u003e\u003cdiv style=\"background-color: rgba(0, 0, 0, 0); border-bottom-left-radius: 0px; border-bottom-right-radius: 0px; 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-top-left-radius: 0px; border-top-right-radius: 0px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; min-height: 18px; padding-left: 4px; white-space: nowrap; \"\u003e\u003cspan style=\"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-right: 45px; min-height: 0px; padding-left: 0px; tab-size: 4; transform-origin: 0px 0px; white-space: pre; perspective-origin: 0px 0px; margin-right: 45px; \"\u003e\u003cspan style=\"margin-right: 0px; \"\u003e Knave(\u003c/span\u003e\u003cspan style=\"border-bottom-color: rgb(170, 4, 249); border-left-color: rgb(170, 4, 249); border-right-color: rgb(170, 4, 249); border-top-color: rgb(170, 4, 249); caret-color: rgb(170, 4, 249); color: rgb(170, 4, 249); margin-right: 0px; outline-color: rgb(170, 4, 249); text-decoration-color: rgb(170, 4, 249); column-rule-color: rgb(170, 4, 249); \"\u003e'A|X(1)'\u003c/span\u003e\u003cspan style=\"margin-right: 0px; \"\u003e) == true\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003c/div\u003e\u003cdiv style=\"font-family: Helvetica, Arial, sans-serif; line-height: 21px; margin-bottom: 9px; margin-left: 4px; margin-right: 10px; margin-top: 10px; text-align: left; transform-origin: 309px 21px; white-space: pre-wrap; perspective-origin: 309px 21px; margin-left: 4px; margin-top: 10px; margin-bottom: 9px; margin-right: 10px; \"\u003e\u003cspan style=\"display: inline; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; transform-origin: 0px 0px; perspective-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003eThis question asks whether the islander is a Knight or the value of the secret formula at 1 is true. None of these are true, but the Knave will lie to you and respond 'yes'.\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"background-color: rgb(247, 247, 247); border-bottom-left-radius: 4px; border-bottom-right-radius: 4px; border-top-left-radius: 4px; border-top-right-radius: 4px; margin-bottom: 10px; margin-left: 3px; margin-right: 3px; margin-top: 10px; transform-origin: 329px 10px; perspective-origin: 329px 10px; margin-left: 3px; margin-top: 10px; margin-bottom: 10px; margin-right: 3px; \"\u003e\u003cdiv style=\"background-color: rgba(0, 0, 0, 0); border-bottom-left-radius: 0px; border-bottom-right-radius: 0px; 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-top-left-radius: 0px; border-top-right-radius: 0px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; min-height: 18px; padding-left: 4px; white-space: nowrap; \"\u003e\u003cspan style=\"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-right: 45px; min-height: 0px; padding-left: 0px; tab-size: 4; transform-origin: 0px 0px; white-space: pre; perspective-origin: 0px 0px; margin-right: 45px; \"\u003e\u003cspan style=\"margin-right: 0px; \"\u003e Knave(\u003c/span\u003e\u003cspan style=\"border-bottom-color: rgb(170, 4, 249); border-left-color: rgb(170, 4, 249); border-right-color: rgb(170, 4, 249); border-top-color: rgb(170, 4, 249); caret-color: rgb(170, 4, 249); color: rgb(170, 4, 249); margin-right: 0px; outline-color: rgb(170, 4, 249); text-decoration-color: rgb(170, 4, 249); column-rule-color: rgb(170, 4, 249); \"\u003e'F(''A'')'\u003c/span\u003e\u003cspan style=\"margin-right: 0px; \"\u003e) == false\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003c/div\u003e\u003cdiv style=\"font-family: Helvetica, Arial, sans-serif; line-height: 21px; margin-bottom: 9px; margin-left: 4px; margin-right: 10px; margin-top: 10px; text-align: left; transform-origin: 309px 42px; white-space: pre-wrap; perspective-origin: 309px 42px; margin-left: 4px; margin-top: 10px; margin-bottom: 9px; margin-right: 10px; \"\u003e\u003cspan style=\"display: inline; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; transform-origin: 0px 0px; perspective-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003eThis question asks whether the islander would respond affirmatively to the question of whether he is a knight. Both Knights and Knaves would actually respond affirmatively when questioned whether they are Knights, but a Knave would lie to you when telling you how he would respond to such question, so he would say 'no'.\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"font-family: Helvetica, Arial, sans-serif; line-height: 21px; margin-bottom: 9px; margin-left: 4px; margin-right: 10px; margin-top: 2px; text-align: left; transform-origin: 309px 42px; white-space: pre-wrap; perspective-origin: 309px 42px; margin-left: 4px; margin-top: 2px; margin-bottom: 9px; margin-right: 10px; \"\u003e\u003cspan style=\"display: inline; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; transform-origin: 0px 0px; perspective-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003eYou must implement a function that will take two inputs: 1) the function handle of an islander (either @Knight or @Knave); and 2) a question (as a char array). Your function should return the answer this same islander would give to this question if he was the opposite type than he really is. In other words:\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"background-color: rgb(247, 247, 247); border-bottom-left-radius: 4px; border-bottom-right-radius: 4px; border-top-left-radius: 4px; border-top-right-radius: 4px; margin-bottom: 10px; margin-left: 3px; margin-right: 3px; margin-top: 10px; transform-origin: 329px 20px; perspective-origin: 329px 20px; margin-left: 3px; margin-top: 10px; margin-bottom: 10px; margin-right: 3px; \"\u003e\u003cdiv style=\"background-color: rgba(0, 0, 0, 0); border-bottom-left-radius: 0px; border-bottom-right-radius: 0px; 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-top-left-radius: 0px; border-top-right-radius: 0px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; min-height: 18px; padding-left: 4px; transform-origin: 329px 10px; white-space: nowrap; perspective-origin: 329px 10px; \"\u003e\u003cspan style=\"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-right: 45px; min-height: 0px; padding-left: 0px; tab-size: 4; transform-origin: 0px 0px; white-space: pre; perspective-origin: 0px 0px; margin-right: 45px; \"\u003e\u003cspan style=\"margin-right: 0px; \"\u003e your_function(@Knight,str) should \u003c/span\u003e\u003cspan style=\"border-bottom-color: rgb(170, 4, 249); border-left-color: rgb(170, 4, 249); border-right-color: rgb(170, 4, 249); border-top-color: rgb(170, 4, 249); caret-color: rgb(170, 4, 249); color: rgb(170, 4, 249); margin-right: 0px; outline-color: rgb(170, 4, 249); text-decoration-color: rgb(170, 4, 249); column-rule-color: rgb(170, 4, 249); \"\u003ereturn Knave(str)\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"background-color: rgba(0, 0, 0, 0); border-bottom-left-radius: 0px; border-bottom-right-radius: 0px; 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-top-left-radius: 0px; border-top-right-radius: 0px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; min-height: 18px; padding-left: 4px; transform-origin: 329px 10px; white-space: nowrap; perspective-origin: 329px 10px; \"\u003e\u003cspan style=\"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-right: 45px; min-height: 0px; padding-left: 0px; tab-size: 4; transform-origin: 0px 0px; white-space: pre; perspective-origin: 0px 0px; margin-right: 45px; \"\u003e\u003cspan style=\"margin-right: 0px; \"\u003e your_function(@Knave,str) should \u003c/span\u003e\u003cspan style=\"border-bottom-color: rgb(170, 4, 249); border-left-color: rgb(170, 4, 249); border-right-color: rgb(170, 4, 249); border-top-color: rgb(170, 4, 249); caret-color: rgb(170, 4, 249); color: rgb(170, 4, 249); margin-right: 0px; outline-color: rgb(170, 4, 249); text-decoration-color: rgb(170, 4, 249); column-rule-color: rgb(170, 4, 249); \"\u003ereturn Knight(str)\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003c/div\u003e\u003cdiv style=\"font-family: Helvetica, Arial, sans-serif; line-height: 21px; margin-bottom: 9px; margin-left: 4px; margin-right: 10px; margin-top: 10px; text-align: left; transform-origin: 309px 21px; white-space: pre-wrap; perspective-origin: 309px 21px; margin-left: 4px; margin-top: 10px; margin-bottom: 9px; margin-right: 10px; \"\u003e\u003cspan style=\"display: inline; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; transform-origin: 0px 0px; perspective-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003eYour function might query the function handle of the islander with whatever questions it sees fit before responding.\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"font-family: Helvetica, Arial, sans-serif; line-height: 21px; margin-bottom: 9px; margin-left: 4px; margin-right: 10px; margin-top: 2px; text-align: left; transform-origin: 309px 10.5px; white-space: pre-wrap; perspective-origin: 309px 10.5px; margin-left: 4px; margin-top: 2px; margin-bottom: 9px; margin-right: 10px; \"\u003e\u003cspan style=\"display: inline; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; transform-origin: 0px 0px; perspective-origin: 0px 0px; \"\u003e\u003cspan style=\"font-weight: bold; \"\u003eExamples:\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"background-color: rgb(247, 247, 247); border-bottom-left-radius: 4px; border-bottom-right-radius: 4px; border-top-left-radius: 4px; border-top-right-radius: 4px; margin-bottom: 10px; margin-left: 3px; margin-right: 3px; margin-top: 10px; transform-origin: 329px 30px; perspective-origin: 329px 30px; margin-left: 3px; margin-top: 10px; margin-bottom: 10px; margin-right: 3px; \"\u003e\u003cdiv style=\"background-color: rgba(0, 0, 0, 0); border-bottom-left-radius: 0px; border-bottom-right-radius: 0px; 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-top-left-radius: 0px; border-top-right-radius: 0px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; min-height: 18px; padding-left: 4px; transform-origin: 329px 10px; white-space: nowrap; perspective-origin: 329px 10px; \"\u003e\u003cspan style=\"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-right: 45px; min-height: 0px; padding-left: 0px; tab-size: 4; transform-origin: 0px 0px; white-space: pre; perspective-origin: 0px 0px; margin-right: 45px; \"\u003e\u003cspan style=\"margin-right: 0px; \"\u003e your_function(@Knight,\u003c/span\u003e\u003cspan style=\"border-bottom-color: rgb(170, 4, 249); border-left-color: rgb(170, 4, 249); border-right-color: rgb(170, 4, 249); border-top-color: rgb(170, 4, 249); caret-color: rgb(170, 4, 249); color: rgb(170, 4, 249); margin-right: 0px; outline-color: rgb(170, 4, 249); text-decoration-color: rgb(170, 4, 249); column-rule-color: rgb(170, 4, 249); \"\u003e'A==true'\u003c/span\u003e\u003cspan style=\"margin-right: 0px; \"\u003e) == true;\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"background-color: rgba(0, 0, 0, 0); border-bottom-left-radius: 0px; border-bottom-right-radius: 0px; 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-top-left-radius: 0px; border-top-right-radius: 0px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; min-height: 18px; padding-left: 4px; transform-origin: 329px 10px; white-space: nowrap; perspective-origin: 329px 10px; \"\u003e\u003cspan style=\"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-right: 45px; min-height: 0px; padding-left: 0px; tab-size: 4; transform-origin: 0px 0px; white-space: pre; perspective-origin: 0px 0px; margin-right: 45px; \"\u003e\u003cspan style=\"margin-right: 0px; \"\u003e\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"background-color: rgba(0, 0, 0, 0); border-bottom-left-radius: 0px; border-bottom-right-radius: 0px; 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-top-left-radius: 0px; border-top-right-radius: 0px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; min-height: 18px; padding-left: 4px; transform-origin: 329px 10px; white-space: nowrap; perspective-origin: 329px 10px; \"\u003e\u003cspan style=\"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-right: 45px; min-height: 0px; padding-left: 0px; tab-size: 4; transform-origin: 0px 0px; white-space: pre; perspective-origin: 0px 0px; margin-right: 45px; \"\u003e\u003cspan style=\"margin-right: 0px; \"\u003e your_function(@Knave,\u003c/span\u003e\u003cspan style=\"border-bottom-color: rgb(170, 4, 249); border-left-color: rgb(170, 4, 249); border-right-color: rgb(170, 4, 249); border-top-color: rgb(170, 4, 249); caret-color: rgb(170, 4, 249); color: rgb(170, 4, 249); margin-right: 0px; outline-color: rgb(170, 4, 249); text-decoration-color: rgb(170, 4, 249); column-rule-color: rgb(170, 4, 249); \"\u003e'A==true'\u003c/span\u003e\u003cspan style=\"margin-right: 0px; \"\u003e) == true;\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003c/div\u003e\u003cdiv style=\"font-family: Helvetica, Arial, sans-serif; line-height: 21px; margin-bottom: 9px; margin-left: 4px; margin-right: 10px; margin-top: 10px; text-align: left; transform-origin: 309px 21px; white-space: pre-wrap; perspective-origin: 309px 21px; margin-left: 4px; margin-top: 10px; margin-bottom: 9px; margin-right: 10px; \"\u003e\u003cspan style=\"display: inline; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; transform-origin: 0px 0px; perspective-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003eThis question asks whether the islander is a Knight; both Knights and Knaves would respond\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display: inline; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; transform-origin: 0px 0px; perspective-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003e \u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display: inline; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; transform-origin: 0px 0px; perspective-origin: 0px 0px; \"\u003e\u003cspan style=\"font-style: italic; \"\u003etrue\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display: inline; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; transform-origin: 0px 0px; perspective-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003e to this question.\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"background-color: rgb(247, 247, 247); border-bottom-left-radius: 4px; border-bottom-right-radius: 4px; border-top-left-radius: 4px; border-top-right-radius: 4px; margin-bottom: 10px; margin-left: 3px; margin-right: 3px; margin-top: 10px; transform-origin: 329px 30px; perspective-origin: 329px 30px; margin-left: 3px; margin-top: 10px; margin-bottom: 10px; margin-right: 3px; \"\u003e\u003cdiv style=\"background-color: rgba(0, 0, 0, 0); border-bottom-left-radius: 0px; border-bottom-right-radius: 0px; 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-top-left-radius: 0px; border-top-right-radius: 0px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; min-height: 18px; padding-left: 4px; transform-origin: 329px 10px; white-space: nowrap; perspective-origin: 329px 10px; \"\u003e\u003cspan style=\"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-right: 45px; min-height: 0px; padding-left: 0px; tab-size: 4; transform-origin: 0px 0px; white-space: pre; perspective-origin: 0px 0px; margin-right: 45px; \"\u003e\u003cspan style=\"margin-right: 0px; \"\u003e your_function(@Knight,\u003c/span\u003e\u003cspan style=\"border-bottom-color: rgb(170, 4, 249); border-left-color: rgb(170, 4, 249); border-right-color: rgb(170, 4, 249); border-top-color: rgb(170, 4, 249); caret-color: rgb(170, 4, 249); color: rgb(170, 4, 249); margin-right: 0px; outline-color: rgb(170, 4, 249); text-decoration-color: rgb(170, 4, 249); column-rule-color: rgb(170, 4, 249); \"\u003e'F(''A==true'')==true'\u003c/span\u003e\u003cspan style=\"margin-right: 0px; \"\u003e) == false; \u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"background-color: rgba(0, 0, 0, 0); border-bottom-left-radius: 0px; border-bottom-right-radius: 0px; 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-top-left-radius: 0px; border-top-right-radius: 0px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; min-height: 18px; padding-left: 4px; transform-origin: 329px 10px; white-space: nowrap; perspective-origin: 329px 10px; \"\u003e\u003cspan style=\"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-right: 45px; min-height: 0px; padding-left: 0px; tab-size: 4; transform-origin: 0px 0px; white-space: pre; perspective-origin: 0px 0px; margin-right: 45px; \"\u003e\u003cspan style=\"margin-right: 0px; \"\u003e\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"background-color: rgba(0, 0, 0, 0); border-bottom-left-radius: 0px; border-bottom-right-radius: 0px; 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-top-left-radius: 0px; border-top-right-radius: 0px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; min-height: 18px; padding-left: 4px; transform-origin: 329px 10px; white-space: nowrap; perspective-origin: 329px 10px; \"\u003e\u003cspan style=\"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-right: 45px; min-height: 0px; padding-left: 0px; tab-size: 4; transform-origin: 0px 0px; white-space: pre; perspective-origin: 0px 0px; margin-right: 45px; \"\u003e\u003cspan style=\"margin-right: 0px; \"\u003e your_function (@Knave,\u003c/span\u003e\u003cspan style=\"border-bottom-color: rgb(170, 4, 249); border-left-color: rgb(170, 4, 249); border-right-color: rgb(170, 4, 249); border-top-color: rgb(170, 4, 249); caret-color: rgb(170, 4, 249); color: rgb(170, 4, 249); margin-right: 0px; outline-color: rgb(170, 4, 249); text-decoration-color: rgb(170, 4, 249); column-rule-color: rgb(170, 4, 249); \"\u003e'F(''A==true'')==true'\u003c/span\u003e\u003cspan style=\"margin-right: 0px; \"\u003e) == true;\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003c/div\u003e\u003cdiv style=\"font-family: Helvetica, Arial, sans-serif; line-height: 21px; margin-bottom: 9px; margin-left: 4px; margin-right: 10px; margin-top: 10px; text-align: left; transform-origin: 309px 31.5px; white-space: pre-wrap; perspective-origin: 309px 31.5px; margin-left: 4px; margin-top: 10px; margin-bottom: 9px; margin-right: 10px; \"\u003e\u003cspan style=\"display: inline; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; transform-origin: 0px 0px; perspective-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003eThis question asks if the islander would respond yes to the question of whether he is a Knight. A Knight would respond 'yes', while a Knave would (falsely) respond 'no', so your function should return exactly the opposite in each case.\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"background-color: rgb(247, 247, 247); border-bottom-left-radius: 4px; border-bottom-right-radius: 4px; border-top-left-radius: 4px; border-top-right-radius: 4px; margin-bottom: 10px; margin-left: 3px; margin-right: 3px; margin-top: 10px; transform-origin: 329px 30px; perspective-origin: 329px 30px; margin-left: 3px; margin-top: 10px; margin-bottom: 10px; margin-right: 3px; \"\u003e\u003cdiv style=\"background-color: rgba(0, 0, 0, 0); border-bottom-left-radius: 0px; border-bottom-right-radius: 0px; 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-top-left-radius: 0px; border-top-right-radius: 0px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; min-height: 18px; padding-left: 4px; transform-origin: 329px 10px; white-space: nowrap; perspective-origin: 329px 10px; \"\u003e\u003cspan style=\"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-right: 45px; min-height: 0px; padding-left: 0px; tab-size: 4; transform-origin: 0px 0px; white-space: pre; perspective-origin: 0px 0px; margin-right: 45px; \"\u003e\u003cspan style=\"margin-right: 0px; \"\u003e your_function(@Knight,\u003c/span\u003e\u003cspan style=\"border-bottom-color: rgb(170, 4, 249); border-left-color: rgb(170, 4, 249); border-right-color: rgb(170, 4, 249); border-top-color: rgb(170, 4, 249); caret-color: rgb(170, 4, 249); color: rgb(170, 4, 249); margin-right: 0px; outline-color: rgb(170, 4, 249); text-decoration-color: rgb(170, 4, 249); column-rule-color: rgb(170, 4, 249); \"\u003e'X(3)~=X(2)'\u003c/span\u003e\u003cspan style=\"margin-right: 0px; \"\u003e) == true\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"background-color: rgba(0, 0, 0, 0); border-bottom-left-radius: 0px; border-bottom-right-radius: 0px; 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-top-left-radius: 0px; border-top-right-radius: 0px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; min-height: 18px; padding-left: 4px; transform-origin: 329px 10px; white-space: nowrap; perspective-origin: 329px 10px; \"\u003e\u003cspan style=\"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-right: 45px; min-height: 0px; padding-left: 0px; tab-size: 4; transform-origin: 0px 0px; white-space: pre; perspective-origin: 0px 0px; margin-right: 45px; \"\u003e\u003cspan style=\"margin-right: 0px; \"\u003e\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"background-color: rgba(0, 0, 0, 0); border-bottom-left-radius: 0px; border-bottom-right-radius: 0px; 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-top-left-radius: 0px; border-top-right-radius: 0px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; min-height: 18px; padding-left: 4px; transform-origin: 329px 10px; white-space: nowrap; perspective-origin: 329px 10px; \"\u003e\u003cspan style=\"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-right: 45px; min-height: 0px; padding-left: 0px; tab-size: 4; transform-origin: 0px 0px; white-space: pre; perspective-origin: 0px 0px; margin-right: 45px; \"\u003e\u003cspan style=\"margin-right: 0px; \"\u003e your_function(@Knave,\u003c/span\u003e\u003cspan style=\"border-bottom-color: rgb(170, 4, 249); border-left-color: rgb(170, 4, 249); border-right-color: rgb(170, 4, 249); border-top-color: rgb(170, 4, 249); caret-color: rgb(170, 4, 249); color: rgb(170, 4, 249); margin-right: 0px; outline-color: rgb(170, 4, 249); text-decoration-color: rgb(170, 4, 249); column-rule-color: rgb(170, 4, 249); \"\u003e'X(3)~=X(2)'\u003c/span\u003e\u003cspan style=\"margin-right: 0px; \"\u003e) == false\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003c/div\u003e\u003cdiv style=\"font-family: Helvetica, Arial, sans-serif; line-height: 21px; margin-bottom: 9px; margin-left: 4px; margin-right: 10px; margin-top: 10px; text-align: left; transform-origin: 309px 52.5px; white-space: pre-wrap; perspective-origin: 309px 52.5px; margin-left: 4px; margin-top: 10px; margin-bottom: 9px; margin-right: 10px; \"\u003e\u003cspan style=\"display: inline; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; transform-origin: 0px 0px; perspective-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003e(Assuming X(2)==X(3); you do not know the values of X, only islanders do) This question asks to the islander whether the value of his secret formula X(2) is different from the value of his secret formula X(3). Assuming that they were actually the same value a Knight would respond negatively (telling you the truth), while a Knave would respond affirmatively (lying to you), so, again, your function should return exactly the opposite response in each case.\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003c/div\u003e\u003c/div\u003e","function_template":"function answer = AnswerGenerator(F,str)\r\n  answer=F(str);\r\nend","test_suite":"%%\r\n% Ask a Knight whether 4 is prime\r\n% (he will respond false; your function should respond true)\r\nA=true; X=@isprime; str='X(4)';\r\nf0=inline('logical(interp1([0,1],[0,x],1))','x','A','X');\r\nF=@(str)xor(~A,f0(eval(str),A,X));\r\nclear A X;\r\nassert(isequal(AnswerGenerator(F,str),true))\r\n\r\n%%\r\n% Ask a Knave whether 4 is prime \r\n% (he will respond true; your function should respond false)\r\nA=false; X=@isprime; str='X(4)';\r\nf0=inline('logical(interp1([0,1],[0,x],1))','x','A','X');\r\nF=@(str)xor(~A,f0(eval(str),A,X));\r\nclear A X;\r\nassert(isequal(AnswerGenerator(F,str),false))\r\n\r\n%%\r\n% Ask a Knight whether he is a Knight \r\n% (both Knights and Knaves would respond true and so should your function)\r\nA=true; X=@isprime; str='A==true';\r\nf0=inline('logical(interp1([0,1],[0,x],1))','x','A','X');\r\nF=@(str)xor(~A,f0(eval(str),A,X));\r\nclear A X;\r\nassert(isequal(AnswerGenerator(F,str),true))\r\n\r\n%%\r\n% Ask a Knave whether he is a Knight \r\n% (both Knights and Knaves would respond true and so should your function)\r\nA=false; X=@isprime; str='A==true';\r\nf0=inline('logical(interp1([0,1],[0,x],1))','x','A','X');\r\nF=@(str)xor(~A,f0(eval(str),A,X));\r\nclear A X;\r\nassert(isequal(AnswerGenerator(F,str),true))\r\n\r\n%%\r\n% Ask a Knight whether he would respond affirmatively to the question of whether he is a Knight\r\n% (a Knave would respond false to this same question, and so should your function)\r\n% A=true; X=@isprime; str='F(''A==true'')';\r\n% f0=inline('logical(interp1([0,1],[0,x],1))','x','A','X');\r\n% F=@(str)xor(~A,f0(eval(str),A,X));\r\n% clear A X;\r\n% assert(isequal(AnswerGenerator(F,str),false))\r\n\r\n%%\r\n% Ask a Knave whether he would respond affirmatively to the question of whether he is a Knight\r\n% (a Knight would respond true to this same question, and so should your function)\r\n% A=false; X=@isprime; str='F(''A==true'')';\r\n% f0=inline('logical(interp1([0,1],[0,x],1))','x','A','X');\r\n% F=@(str)xor(~A,f0(eval(str),A,X));\r\n% clear A X;\r\n% assert(isequal(AnswerGenerator(F,str),true))\r\n\r\n%%\r\nA=true; X=@isprime; str='diff(X(2:3))';\r\nf0=inline('logical(interp1([0,1],[0,x],1))','x','A','X');\r\nF=@(str)xor(~A,f0(eval(str),A,X));\r\nclear A X;\r\nassert(isequal(AnswerGenerator(F,str),true))\r\n\r\n%%\r\nA=false; X=@isprime; str='diff(X(2:3))';\r\nf0=inline('logical(interp1([0,1],[0,x],1))','x','A','X');\r\nF=@(str)xor(~A,f0(eval(str),A,X));\r\nclear A X;\r\nassert(isequal(AnswerGenerator(F,str),false))\r\n\r\n%%\r\nA=true; X=@isprime; str='A==X(6)';\r\nf0=inline('logical(interp1([0,1],[0,x],1))','x','A','X');\r\nF=@(str)xor(~A,f0(eval(str),A,X));\r\nclear A X;\r\nassert(isequal(AnswerGenerator(F,str),false))\r\n\r\n%%\r\nA=false; X=@isprime; str='A==X(6)';\r\nf0=inline('logical(interp1([0,1],[0,x],1))','x','A','X');\r\nF=@(str)xor(~A,f0(eval(str),A,X));\r\nclear A X;\r\nassert(isequal(AnswerGenerator(F,str),false))\r\n\r\n%%\r\nA=true; X=@isprime; str='A\u0026any(X(1:3))';\r\nf0=inline('logical(interp1([0,1],[0,x],1))','x','A','X');\r\nF=@(str)xor(~A,f0(eval(str),A,X));\r\nclear A X;\r\nassert(isequal(AnswerGenerator(F,str),true))\r\n\r\n%%\r\nA=false; X=@isprime; str='A\u0026any(X(1:3))';\r\nf0=inline('logical(interp1([0,1],[0,x],1))','x','A','X');\r\nF=@(str)xor(~A,f0(eval(str),A,X));\r\nclear A X;\r\nassert(isequal(AnswerGenerator(F,str),true))\r\n\r\n%%\r\n% A=true; X=@(x)rem(x,2); str='F(''F(''''X(3)'''')'')';\r\n% f0=inline('logical(interp1([0,1],[0,x],1))','x','A','X');\r\n% F=@(str)xor(~A,f0(eval(str),A,X));\r\n% clear A X;\r\n% assert(isequal(AnswerGenerator(F,str),false))\r\n\r\n%%\r\n% A=false; X=@(x)rem(x,2); str='F(''F(''''X(3)'''')'')';\r\n% f0=inline('logical(interp1([0,1],[0,x],1))','x','A','X');\r\n% F=@(str)xor(~A,f0(eval(str),A,X));\r\n% clear A X;\r\n% assert(isequal(AnswerGenerator(F,str),true))\r\n\r\n","published":true,"deleted":false,"likes_count":2,"comments_count":14,"created_by":43,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":19,"test_suite_updated_at":"2020-09-29T00:03:34.000Z","rescore_all_solutions":false,"group_id":1,"created_at":"2013-01-07T22:12:02.000Z","updated_at":"2026-04-15T04:00:57.000Z","published_at":"2013-01-08T03:23:02.000Z","restored_at":"2017-11-13T15:02:29.000Z","restored_by":null,"spam":false,"simulink":false,"admin_reviewed":true,"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\u003eThis is a Matlab adaptation of the\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/Knights_and_Knaves\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eKnights and Knaves\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e logical puzzles, mixed with the famous\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/Man-in-the-middle_attack\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eman-in-the-middle attack\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e in computer security.\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\u003eYou are in an island where all inhabitants are either\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:i/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eKnights\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e, who always tell the truth, or\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:i/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eKnaves\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e, who always lie. Your job is to sit in the middle of an islander and a second person interviewing the islander, intercepting all questions posed to the islander, and answering to the interviewer in a way that will make him think that the islander is the opposite type of what he really is (answer as a Knave if the islander is a Knight, or answer as a Knight if the islander is a Knave). The problem is: a) you really do not know whether the islander is a Knight or a Knave; and b) the islander knows some secret that only he knows, so you may not be able to anticipate what he would answer even if you knew whether he was a Knight or a Knave! Luckily for you, you may ask the islander privately any questions that you wish before responding to the interviewer.\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:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eDetails:\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\u003eYou are given a function handle F that will act as the islander. This function will answer any question the way the islander would. The function\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:i/\u003e\u003c/w:rPr\u003e\u003cw:t\u003efunction answer=F(question)\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e takes a char array as input (the 'question'), and returns a logical value (the yes/no 'answer' this particular islander would give to this question;\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:i/\u003e\u003c/w:rPr\u003e\u003cw:t\u003etrue\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e means 'yes' and\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:i/\u003e\u003c/w:rPr\u003e\u003cw:t\u003efalse\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e means 'no'). Valid questions are any valid matlab string. The islander has access to the following variables (the things that he 'knows'):\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"ListParagraph\\\"/\u003e\u003cw:numPr\u003e\u003cw:numId w:val=\\\"1\\\"/\u003e\u003c/w:numPr\u003e\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eA\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e: Islander's type. A is a logical variable: true for a Knight, false for a Knave\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"ListParagraph\\\"/\u003e\u003cw:numPr\u003e\u003cw:numId w:val=\\\"1\\\"/\u003e\u003c/w:numPr\u003e\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eX\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e: A secret formula only islanders know. An unknown function on positive integer values that when evaluated returns a logical value (e.g.\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:i/\u003e\u003c/w:rPr\u003e\u003cw:t\u003ex\u0026gt;1\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=\\\"ListParagraph\\\"/\u003e\u003cw:numPr\u003e\u003cw:numId w:val=\\\"1\\\"/\u003e\u003c/w:numPr\u003e\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eF\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e: Introspection. F is the handle associated with this islander's answers, so he knows himself what he would respond to some hypothetical question\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 function handles associated with a Knight and a Knave look, respectively, something like:\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[function answer = Knight(question)\\n  A = true;\\n  X = @(x)x\u003e10;\\n  F = @Knight;\\n  answer = eval(question);\\nend\\n\\nfunction answer = Knave(question)\\n  A = false;\\n  X = @(x)x\u003e10;\\n  F = @Knave;\\n  answer = ~eval(question);\\nend]]\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\u003eOf course the values of X will be different and unknown to you.\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 few examples:\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[ Knight('A==true') == true]]\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\u003eThis question asks whether the islander is a Knight, and a Knight would respond affirmatively to such question.\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[ Knave('A|X(1)') == true]]\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\u003eThis question asks whether the islander is a Knight or the value of the secret formula at 1 is true. None of these are true, but the Knave will lie to you and respond 'yes'.\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[ Knave('F(''A'')') == false]]\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\u003eThis question asks whether the islander would respond affirmatively to the question of whether he is a knight. Both Knights and Knaves would actually respond affirmatively when questioned whether they are Knights, but a Knave would lie to you when telling you how he would respond to such question, so he would say 'no'.\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\u003eYou must implement a function that will take two inputs: 1) the function handle of an islander (either @Knight or @Knave); and 2) a question (as a char array). Your function should return the answer this same islander would give to this question if he was the opposite type than he really is. In other words:\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[ your_function(@Knight,str) should return Knave(str)\\n your_function(@Knave,str) should return Knight(str)]]\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\u003eYour function might query the function handle of the islander with whatever questions it sees fit before responding.\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:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eExamples:\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[ your_function(@Knight,'A==true') == true;\\n\\n your_function(@Knave,'A==true') == true;]]\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\u003eThis question asks whether the islander is a Knight; both Knights and Knaves would respond\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:i/\u003e\u003c/w:rPr\u003e\u003cw:t\u003etrue\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e to this question.\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[ your_function(@Knight,'F(''A==true'')==true') == false; \\n\\n your_function (@Knave,'F(''A==true'')==true') == true;]]\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\u003eThis question asks if the islander would respond yes to the question of whether he is a Knight. A Knight would respond 'yes', while a Knave would (falsely) respond 'no', so your function should return exactly the opposite in each case.\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[ your_function(@Knight,'X(3)~=X(2)') == true\\n\\n your_function(@Knave,'X(3)~=X(2)') == false]]\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\u003e(Assuming X(2)==X(3); you do not know the values of X, only islanders do) This question asks to the islander whether the value of his secret formula X(2) is different from the value of his secret formula X(3). Assuming that they were actually the same value a Knight would respond negatively (telling you the truth), while a Knave would respond affirmatively (lying to you), so, again, your function should return exactly the opposite response in each case.\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":1341,"title":"Track Logic","description":"A sensor produces either a hit (1) or a miss (0) for a given target once per scan. The sensor is also equiped with a tracker which uses the following logic: If a target is detected in 3 out of 8 scans, a track is started. If a target in track is not seen for 6 scans, the track is dropped. Write a function to give the track state (1 = in track, 0 = not in track) given an arbitrary array of hits and misses.","description_html":"\u003cp\u003eA sensor produces either a hit (1) or a miss (0) for a given target once per scan. The sensor is also equiped with a tracker which uses the following logic: If a target is detected in 3 out of 8 scans, a track is started. If a target in track is not seen for 6 scans, the track is dropped. Write a function to give the track state (1 = in track, 0 = not in track) given an arbitrary array of hits and misses.\u003c/p\u003e","function_template":"function trk = TrkState(HM)\r\n  trk = HM;\r\nend","test_suite":"%%\r\nHM =  [1 1 0 1 0 0 0 1 1 0 1 1 0 0 0];\r\ntrk = [0 0 0 1 1 1 1 1 1 1 1 1 1 1 1];\r\nassert(isequal(TrkState(HM),trk))\r\n\r\n%%\r\nHM =  [1 0 0 0 0 0 0 1 1 0 0 0 0 0 1 0 0];\r\ntrk = [0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1];\r\nassert(isequal(TrkState(HM),trk))\r\n\r\n%%\r\nHM =  [1 1 1 0 0 0 0 0 0 0 0 1 1 0 0 0 1];\r\ntrk = [0 0 1 1 1 1 1 1 0 0 0 0 0 0 0 0 1];\r\nassert(isequal(TrkState(HM),trk))","published":true,"deleted":false,"likes_count":7,"comments_count":0,"created_by":3096,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":52,"test_suite_updated_at":"2013-03-13T15:16:17.000Z","rescore_all_solutions":false,"group_id":1,"created_at":"2013-03-13T15:13:28.000Z","updated_at":"2026-04-09T10:58:39.000Z","published_at":"2013-03-13T15:16:17.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\",\"relationshipId\":\"rId1\",\"target\":\"/matlab/document.xml\"},{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/output\",\"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\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\u003eA sensor produces either a hit (1) or a miss (0) for a given target once per scan. The sensor is also equiped with a tracker which uses the following logic: If a target is detected in 3 out of 8 scans, a track is started. If a target in track is not seen for 6 scans, the track is dropped. Write a function to give the track state (1 = in track, 0 = not in track) given an arbitrary array of hits and misses.\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":1478,"title":"Hamiltonian Cycle","description":"This is related to the Travelling Salesman Problem 1339 created by Alex P.\r\nA Hamiltonian cycle or traceable cycle is a path that visits each vertex exactly once and returns to the starting vertex.\r\nGiven an Adjacency Matrix A, and a tour T, determine if the tour is Hamiltonian, ie a valid tour for the travelling salesman problem.\r\nA is a matrix with 1 and 0 indicating presence of edge from ith vertex to jth vertex. T is a row vector representing the trip containing list of vertices visited in order. The trip from the last vertex in T to the first one is implicit.\r\nProblem 4) Prev: 1477 Next: 1481","description_html":"\u003cdiv style = \"text-align: start; line-height: 20.4333px; 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: rgb(0, 0, 0); white-space: normal; \"\u003e\u003cdiv style=\"block-size: 183px; display: block; min-width: 0px; padding-block-start: 0px; padding-top: 0px; perspective-origin: 407px 91.5px; transform-origin: 407px 91.5px; 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: 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: 158px 8px; transform-origin: 158px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003eThis is related to the Travelling Salesman Problem\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: 2px 8px; transform-origin: 2px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003e \u003c/span\u003e\u003c/span\u003e\u003ca target='_blank' href = \"/#null\"\u003e\u003cspan style=\"\"\u003e\u003cspan style=\"\"\u003e1339\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: 35.5px 8px; transform-origin: 35.5px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003e created by\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: 2px 8px; transform-origin: 2px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003e \u003c/span\u003e\u003c/span\u003e\u003ca target='_blank' href = \"/#null\"\u003e\u003cspan style=\"\"\u003e\u003cspan style=\"\"\u003eAlex P\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: 2px 8px; transform-origin: 2px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003e.\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: 4.5px 8px; transform-origin: 4.5px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\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: 2px 8px; transform-origin: 2px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003e \u003c/span\u003e\u003c/span\u003e\u003ca target='_blank' href = \"/#null\"\u003e\u003cspan style=\"\"\u003e\u003cspan style=\"\"\u003eHamiltonian cycle\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: 305px 8px; transform-origin: 305px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003e or traceable cycle is a path that visits each vertex exactly once and returns to the starting vertex.\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: 28.5px 8px; transform-origin: 28.5px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003eGiven an\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: 2px 8px; transform-origin: 2px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003e \u003c/span\u003e\u003c/span\u003e\u003ca target='_blank' href = \"/#null\"\u003e\u003cspan style=\"\"\u003e\u003cspan style=\"\"\u003eAdjacency Matrix\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: 295px 8px; transform-origin: 295px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003e A, and a tour T, determine if the tour is Hamiltonian, ie a valid tour for the travelling salesman problem.\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: 376.5px 8px; transform-origin: 376.5px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003eA is a matrix with 1 and 0 indicating presence of edge from ith vertex to jth vertex. T is a row vector representing the trip containing list of vertices visited in order. The trip from the last vertex in T to the first one is implicit.\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: 53px 8px; transform-origin: 53px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003eProblem 4) Prev:\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: 2px 8px; transform-origin: 2px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003e \u003c/span\u003e\u003c/span\u003e\u003ca target='_blank' href = \"/#null\"\u003e\u003cspan style=\"\"\u003e\u003cspan style=\"\"\u003e1477\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: 17.5px 8px; transform-origin: 17.5px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003e Next:\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: 2px 8px; transform-origin: 2px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003e \u003c/span\u003e\u003c/span\u003e\u003ca target='_blank' href = \"/#null\"\u003e\u003cspan style=\"\"\u003e\u003cspan style=\"\"\u003e1481\u003c/span\u003e\u003c/span\u003e\u003c/a\u003e\u003c/div\u003e\u003c/div\u003e\u003c/div\u003e","function_template":"function l = isHamiltoniaCycle(A,T)\r\n  l=true;\r\nend","test_suite":"%%\r\nA=[0 1 0 0 ; 0 0 1 0 ; 0 0 0 1 ; 1 0 0 0];\r\nT=[1 2 3 4];\r\ny_correct = true;\r\nassert(isequal(isHamiltoniaCycle(A,T),y_correct))\r\n\r\n%%\r\nA=[0 1 0 0 ; 0 0 1 0 ; 0 0 0 1 ; 0 1 0 0];\r\nT=[1 2 3 4];\r\ny_correct = false;\r\nassert(isequal(isHamiltoniaCycle(A,T),y_correct))\r\n\r\n%%\r\nA=[0 1 0 1 ; 1 1 1 0 ; 0 1 1 0 ; 0 0 1 0];\r\nT=[1 2 3 4];\r\ny_correct = false;\r\nassert(isequal(isHamiltoniaCycle(A,T),y_correct))\r\n\r\n%%\r\nA=[0 1 0 1 ; 1 1 1 0 ; 0 1 1 0 ; 0 0 1 0];\r\nT=[3 2 1 4 ];\r\ny_correct = true;\r\nassert(isequal(isHamiltoniaCycle(A,T),y_correct))\r\n\r\n%%\r\nA=[0 1 0 1 ; 1 1 1 0 ; 0 1 1 0 ; 0 0 1 0];\r\nT=[2 3 2 3 ];\r\ny_correct = false;\r\nassert(isequal(isHamiltoniaCycle(A,T),y_correct))\r\n","published":true,"deleted":false,"likes_count":2,"comments_count":0,"created_by":11275,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":39,"test_suite_updated_at":"2022-01-05T09:30:05.000Z","rescore_all_solutions":false,"group_id":1,"created_at":"2013-04-30T18:45:21.000Z","updated_at":"2026-04-09T10:56:30.000Z","published_at":"2013-04-30T18:45:21.000Z","restored_at":null,"restored_by":null,"spam":false,"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\u003eThis is related to the Travelling Salesman Problem\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=\\\"\\\"\u003e\u003cw:r\u003e\u003cw:t\u003e1339\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e created by\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=\\\"\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eAlex P\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\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eA\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=\\\"\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eHamiltonian cycle\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e or traceable cycle is a path that visits each vertex exactly once and returns to the starting vertex.\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\u003eGiven an\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=\\\"\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eAdjacency Matrix\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e A, and a tour T, determine if the tour is Hamiltonian, ie a valid tour for the travelling salesman problem.\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 is a matrix with 1 and 0 indicating presence of edge from ith vertex to jth vertex. T is a row vector representing the trip containing list of vertices visited in order. The trip from the last vertex in T to the first one is implicit.\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\u003eProblem 4) Prev:\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=\\\"\\\"\u003e\u003cw:r\u003e\u003cw:t\u003e1477\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e Next:\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=\\\"\\\"\u003e\u003cw:r\u003e\u003cw:t\u003e1481\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\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":876,"title":"Sum the entries of each column of a matrix which satisfy a logical condition.","description":"Given a numeric matrix A and a logical array L of the same size as A, return a row vector S containing the columnwise sums of the entries of A which satisfy L (i.e. where there is a true value in the logical array L). If L contains a column of false values, then the corresponding entry of S should be zero.\r\n\r\nExamples:\r\n\r\n1) If A = [1, 2; 3, 4] and L = [true false; false true], then S should be the row vector [1 4].\r\n\r\n2) If A = triu(ones(3)) and L = logical(A), then S should be the row vector [1 2 3].\r\n\r\n3) If A = repmat(1:3, 3, 1) and L = [false(3,1), true(3, 2)], then S should be the row vector [0 6 9].","description_html":"\u003cp\u003eGiven a numeric matrix A and a logical array L of the same size as A, return a row vector S containing the columnwise sums of the entries of A which satisfy L (i.e. where there is a true value in the logical array L). If L contains a column of false values, then the corresponding entry of S should be zero.\u003c/p\u003e\u003cp\u003eExamples:\u003c/p\u003e\u003cp\u003e1) If A = [1, 2; 3, 4] and L = [true false; false true], then S should be the row vector [1 4].\u003c/p\u003e\u003cp\u003e2) If A = triu(ones(3)) and L = logical(A), then S should be the row vector [1 2 3].\u003c/p\u003e\u003cp\u003e3) If A = repmat(1:3, 3, 1) and L = [false(3,1), true(3, 2)], then S should be the row vector [0 6 9].\u003c/p\u003e","function_template":"function S = columnwise_logical_sums(A, L)\r\n\r\n\r\nend","test_suite":"%%\r\nA = [1 2; 3 4]; L = logical(eye(2));\r\nS_correct = [1 4];\r\nassert(isequal(columnwise_logical_sums(A, L), S_correct))\r\n\r\n%%\r\nA = triu(ones(3)); L = logical(triu(ones(3)));\r\nS_correct = 1:3;\r\nassert(isequal(columnwise_logical_sums(A, L), S_correct))\r\n\r\n%% \r\nA = repmat(1:3, 3, 1); L = [false(3,1) true(3,2)];\r\nS_correct = [0 6 9];\r\nassert(isequal(columnwise_logical_sums(A, L), S_correct))\r\n\r\n%%\r\nA = 1:5; L = logical([0 1 0 1 0]);\r\nS_correct = [0 2 0 4 0];\r\nassert(isequal(columnwise_logical_sums(A, L), S_correct))\r\n\r\n%% \r\nA = (1:5).'*(1:5); L = logical(diag(ones(1,4),-1));\r\nS_correct = [2 6 12 20 0];\r\nassert(isequal(columnwise_logical_sums(A, L), S_correct))","published":true,"deleted":false,"likes_count":4,"comments_count":0,"created_by":2328,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":175,"test_suite_updated_at":null,"rescore_all_solutions":false,"group_id":1,"created_at":"2012-08-04T12:53:20.000Z","updated_at":"2026-02-27T10:28:45.000Z","published_at":"2012-08-04T12:53:20.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\",\"relationshipId\":\"rId1\",\"target\":\"/matlab/document.xml\"},{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/output\",\"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\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\u003eGiven a numeric matrix A and a logical array L of the same size as A, return a row vector S containing the columnwise sums of the entries of A which satisfy L (i.e. where there is a true value in the logical array L). If L contains a column of false values, then the corresponding entry of S should be zero.\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\u003eExamples:\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) If A = [1, 2; 3, 4] and L = [true false; false true], then S should be the row vector [1 4].\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) If A = triu(ones(3)) and L = logical(A), then S should be the row vector [1 2 3].\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) If A = repmat(1:3, 3, 1) and L = [false(3,1), true(3, 2)], then S should be the row vector [0 6 9].\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":44500,"title":"Find the starting index of a consecutive condition","description":"Given a logical vector |v|, and a positive integer |n|, return the smallest index |i| that satisfies:\r\n\r\n  all( v(i : i+n-1) ) == true\r\n\r\nFor Example,\r\n\r\n  v = logical([0, 1, 1, 0, 0, 1, 1, 1, 1, 1, 0]);\r\n  i = consecutiveFind(v, 4);\r\n\r\nThen:\r\n\r\n  i = 6\r\n\r\nIf there is no sequence of |n| consecutive |true|, then |i| should be an empty array.","description_html":"\u003cp\u003eGiven a logical vector \u003ctt\u003ev\u003c/tt\u003e, and a positive integer \u003ctt\u003en\u003c/tt\u003e, return the smallest index \u003ctt\u003ei\u003c/tt\u003e that satisfies:\u003c/p\u003e\u003cpre class=\"language-matlab\"\u003eall( v(i : i+n-1) ) == true\r\n\u003c/pre\u003e\u003cp\u003eFor Example,\u003c/p\u003e\u003cpre class=\"language-matlab\"\u003ev = logical([0, 1, 1, 0, 0, 1, 1, 1, 1, 1, 0]);\r\ni = consecutiveFind(v, 4);\r\n\u003c/pre\u003e\u003cp\u003eThen:\u003c/p\u003e\u003cpre class=\"language-matlab\"\u003ei = 6\r\n\u003c/pre\u003e\u003cp\u003eIf there is no sequence of \u003ctt\u003en\u003c/tt\u003e consecutive \u003ctt\u003etrue\u003c/tt\u003e, then \u003ctt\u003ei\u003c/tt\u003e should be an empty array.\u003c/p\u003e","function_template":"function i = consecutiveFind(v, n)\r\n    i = [];\r\nend","test_suite":"%%\r\nv = logical([0, 1, 1, 0, 0, 1, 1, 1, 1, 1, 0]);\r\nn = 1;\r\ni = consecutiveFind(v, n);\r\ni_correct = 2;\r\nassert(isequal(i, i_correct))\r\n\r\n%%\r\nv = logical([0, 1, 1, 0, 0, 1, 1, 1, 1, 1, 0]);\r\nn = 4;\r\ni = consecutiveFind(v, n);\r\ni_correct = 6;\r\nassert(isequal(i, i_correct))\r\n\r\n%%\r\nv = logical([0, 1, 1, 0, 0, 1, 1, 1, 1, 1, 0]);\r\nn = 6;\r\ni = consecutiveFind(v, n);\r\nassert(isempty(i))\r\n\r\n%%\r\nv = false(1, 10);\r\nn = 1;\r\ni = consecutiveFind(v, n);\r\nassert(isempty(i))\r\n\r\n%%\r\nx = 0 : pi/4 : 2*pi;\r\nv = sin(x) \u003c 1;\r\nn = 5;\r\ni = consecutiveFind(v, n);\r\ni_correct = 4;\r\nassert(isequal(i, i_correct))\r\n","published":true,"deleted":false,"likes_count":2,"comments_count":1,"created_by":140356,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":63,"test_suite_updated_at":null,"rescore_all_solutions":false,"group_id":1,"created_at":"2018-01-15T13:39:34.000Z","updated_at":"2026-02-13T15:03:50.000Z","published_at":"2018-01-15T13:39:34.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\u003eGiven a logical vector\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:rFonts w:cs=\\\"monospace\\\"/\u003e\u003c/w:rPr\u003e\u003cw:t\u003ev\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e, and a positive integer\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:rFonts w:cs=\\\"monospace\\\"/\u003e\u003c/w:rPr\u003e\u003cw:t\u003en\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e, return the smallest index\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:rFonts w:cs=\\\"monospace\\\"/\u003e\u003c/w:rPr\u003e\u003cw:t\u003ei\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e that satisfies:\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[all( v(i : i+n-1) ) == true]]\u003e\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eFor Example,\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[v = logical([0, 1, 1, 0, 0, 1, 1, 1, 1, 1, 0]);\\ni = consecutiveFind(v, 4);]]\u003e\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eThen:\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[i = 6]]\u003e\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eIf there is no sequence of\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:rFonts w:cs=\\\"monospace\\\"/\u003e\u003c/w:rPr\u003e\u003cw:t\u003en\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e consecutive\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:rFonts w:cs=\\\"monospace\\\"/\u003e\u003c/w:rPr\u003e\u003cw:t\u003etrue\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e, then\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:rFonts w:cs=\\\"monospace\\\"/\u003e\u003c/w:rPr\u003e\u003cw:t\u003ei\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e should be an empty array.\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":2676,"title":"Find the next state of a JK Flip-Flop","description":"Find the next state (NS) of a JK Flip-Flop based on previous state (PS), inputs. \r\n\r\nLearn more about JK Flip-Flop theory here: \u003chttp://www.electronics-tutorials.ws/sequential/seq_2.html/ JK Flip-Flop\u003e.\r\n\r\nExample\r\n\r\nPs=0, J=1, K=1, NS will be 1 \r\n\r\n","description_html":"\u003cp\u003eFind the next state (NS) of a JK Flip-Flop based on previous state (PS), inputs.\u003c/p\u003e\u003cp\u003eLearn more about JK Flip-Flop theory here: \u003ca href = \"http://www.electronics-tutorials.ws/sequential/seq_2.html/\"\u003eJK Flip-Flop\u003c/a\u003e.\u003c/p\u003e\u003cp\u003eExample\u003c/p\u003e\u003cp\u003ePs=0, J=1, K=1, NS will be 1\u003c/p\u003e","function_template":"function Q_NS = JK_FF(Q_PS,J,K)\r\n \r\nend","test_suite":"%%\r\nQ_PS = 0;\r\nJ=1;\r\nK=1;\r\ny_correct=1;\r\nassert(isequal(JK_FF(Q_PS,J,K),y_correct))\r\n%%\r\nQ_PS = 1;\r\nJ=1;\r\nK=1;\r\ny_correct=0;\r\nassert(isequal(JK_FF(Q_PS,J,K),y_correct))\r\n%%\r\nQ_PS = 0;\r\nJ=0;\r\nK=1;\r\ny_correct=0;\r\nassert(isequal(JK_FF(Q_PS,J,K),y_correct))\r\n%% \r\nQ_PS = 1;\r\nJ=0;\r\nK=1;\r\ny_correct=0;\r\nassert(isequal(JK_FF(Q_PS,J,K),y_correct))\r\n%% \r\nQ_PS = 1;\r\nJ=1;\r\nK=0;\r\ny_correct=1;\r\nassert(isequal(JK_FF(Q_PS,J,K),y_correct))\r\n%% \r\nQ_PS = 0;\r\nJ=1;\r\nK=0;\r\ny_correct=1;\r\nassert(isequal(JK_FF(Q_PS,J,K),y_correct))\r\n%% \r\nQ_PS = 0;\r\nJ=0;\r\nK=0;\r\ny_correct=0;\r\nassert(isequal(JK_FF(Q_PS,J,K),y_correct))\r\n%% \r\nQ_PS = 1;\r\nJ=0;\r\nK=0;\r\ny_correct=1;\r\nassert(isequal(JK_FF(Q_PS,J,K),y_correct))\r\n","published":true,"deleted":false,"likes_count":3,"comments_count":0,"created_by":27760,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":74,"test_suite_updated_at":null,"rescore_all_solutions":false,"group_id":1,"created_at":"2014-11-18T04:38:47.000Z","updated_at":"2026-02-19T10:29:41.000Z","published_at":"2014-11-18T04:38:47.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\u003eFind the next state (NS) of a JK Flip-Flop based on previous state (PS), inputs.\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\u003eLearn more about JK Flip-Flop theory here:\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.electronics-tutorials.ws/sequential/seq_2.html/\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eJK Flip-Flop\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:t\u003eExample\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\u003ePs=0, J=1, K=1, NS will be 1\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003c/w:body\u003e\u003c/w:document\u003e\"},{\"partUri\":\"/matlab/output.xml\",\"contentType\":\"text/xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\" standalone=\\\"no\\\" ?\u003e\u003cembeddedOutputs\u003e\u003cmetaData\u003e\u003cevaluationState\u003emanual\u003c/evaluationState\u003e\u003clayoutState\u003ecode\u003c/layoutState\u003e\u003coutputStatus\u003eready\u003c/outputStatus\u003e\u003c/metaData\u003e\u003coutputArray type=\\\"array\\\"/\u003e\u003cregionArray type=\\\"array\\\"/\u003e\u003c/embeddedOutputs\u003e\"}]}"},{"id":2677,"title":"Find out next state (NS) of T Flip-Flop. ","description":"Find out next state (NS) of T Flip-Flop based on previous state (PS), inputs. \r\n\r\nLearn more about T Flip-Flop theory here: \u003chttp://wearcam.org/ece385/lectureflipflops/flipflops// T Flip-Flop\u003e.\r\n\r\nExamples\r\n\r\nPs=0, T=1, NS will be 1 \r\n\r\nPs=1, T=1, NS will be 0 \r\n\r\nPs=0, T=0, NS will be 0 \r\n\r\nPs=1, T=0,   NS will be 1 \r\n","description_html":"\u003cp\u003eFind out next state (NS) of T Flip-Flop based on previous state (PS), inputs.\u003c/p\u003e\u003cp\u003eLearn more about T Flip-Flop theory here: \u003ca href = \"http://wearcam.org/ece385/lectureflipflops/flipflops//\"\u003eT Flip-Flop\u003c/a\u003e.\u003c/p\u003e\u003cp\u003eExamples\u003c/p\u003e\u003cp\u003ePs=0, T=1, NS will be 1\u003c/p\u003e\u003cp\u003ePs=1, T=1, NS will be 0\u003c/p\u003e\u003cp\u003ePs=0, T=0, NS will be 0\u003c/p\u003e\u003cp\u003ePs=1, T=0,   NS will be 1\u003c/p\u003e","function_template":"function Q_NS = T_FF(Q_PS,T)\r\n   \r\nend","test_suite":"%%\r\nQ_PS=0;\r\nT=0;\r\ny_correct = 0;\r\nassert(isequal(T_FF(Q_PS,T),y_correct))\r\n%%\r\nQ_PS=1;\r\nT=0;\r\ny_correct = 1;\r\nassert(isequal(T_FF(Q_PS,T),y_correct))\r\n%%\r\nQ_PS=0;\r\nT=1;\r\ny_correct = 1;\r\nassert(isequal(T_FF(Q_PS,T),y_correct))\r\n%%\r\nQ_PS=1;\r\nT=1;\r\ny_correct = 0;\r\nassert(isequal(T_FF(Q_PS,T),y_correct))\r\n","published":true,"deleted":false,"likes_count":2,"comments_count":0,"created_by":27760,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":97,"test_suite_updated_at":null,"rescore_all_solutions":false,"group_id":1,"created_at":"2014-11-18T04:47:59.000Z","updated_at":"2026-02-13T15:06:54.000Z","published_at":"2014-11-18T04:47:59.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\u003eFind out next state (NS) of T Flip-Flop based on previous state (PS), inputs.\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\u003eLearn more about T Flip-Flop theory here:\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://wearcam.org/ece385/lectureflipflops/flipflops//\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eT Flip-Flop\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:t\u003eExamples\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\u003ePs=0, T=1, NS will be 1\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\u003ePs=1, T=1, NS will be 0\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\u003ePs=0, T=0, NS will be 0\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\u003ePs=1, T=0, NS will be 1\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003c/w:body\u003e\u003c/w:document\u003e\"},{\"partUri\":\"/matlab/output.xml\",\"contentType\":\"text/xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\" standalone=\\\"no\\\" ?\u003e\u003cembeddedOutputs\u003e\u003cmetaData\u003e\u003cevaluationState\u003emanual\u003c/evaluationState\u003e\u003clayoutState\u003ecode\u003c/layoutState\u003e\u003coutputStatus\u003eready\u003c/outputStatus\u003e\u003c/metaData\u003e\u003coutputArray type=\\\"array\\\"/\u003e\u003cregionArray type=\\\"array\\\"/\u003e\u003c/embeddedOutputs\u003e\"}]}"},{"id":2678,"title":"Find out sum and carry of Binary adder","description":"Find out sum and carry of a binary adder if previous carry is given with two bits (x and y) for addition. \r\n\r\nExamples\r\n\r\nPrevious carry is 1 and  x=y=1. Addition is 1 and carry is also 1. \r\n\r\nPrevious carry is 0 and  x=y=1. Addition is 0 and carry is also 1. \r\n\r\nPrevious carry is 0 and  x=1, y=0. Addition is 1 and carry is also 0. ","description_html":"\u003cp\u003eFind out sum and carry of a binary adder if previous carry is given with two bits (x and y) for addition.\u003c/p\u003e\u003cp\u003eExamples\u003c/p\u003e\u003cp\u003ePrevious carry is 1 and  x=y=1. Addition is 1 and carry is also 1.\u003c/p\u003e\u003cp\u003ePrevious carry is 0 and  x=y=1. Addition is 0 and carry is also 1.\u003c/p\u003e\u003cp\u003ePrevious carry is 0 and  x=1, y=0. Addition is 1 and carry is also 0.\u003c/p\u003e","function_template":"function [sum, carry] = bin_sum_carry(pc,x,y)\r\n  \r\nend","test_suite":"%%\r\nx = 1;\r\ny=1;\r\npc=1;\r\n[sum, carry]=bin_sum_carry(pc,x,y)\r\nassert(isequal(sum,1))\r\nassert(isequal(carry,1))\r\n%%\r\nx = 1;\r\ny=1;\r\npc=0;\r\n[sum, carry]=bin_sum_carry(pc,x,y)\r\nassert(isequal(sum,0))\r\nassert(isequal(carry,1))\r\n%%\r\nx = 1;\r\ny=0;\r\npc=0;\r\n[sum, carry]=bin_sum_carry(pc,x,y)\r\nassert(isequal(sum,1))\r\nassert(isequal(carry,0))\r\n%%\r\nx = 0;\r\ny=0;\r\npc=0;\r\n[sum, carry]=bin_sum_carry(pc,x,y)\r\nassert(isequal(sum,0))\r\nassert(isequal(carry,0))\r\n%%\r\nx = 1;\r\ny=1;\r\npc=0;\r\n[sum, carry]=bin_sum_carry(pc,x,y)\r\nassert(isequal(sum,0))\r\nassert(isequal(carry,1))\r\n","published":true,"deleted":false,"likes_count":12,"comments_count":3,"created_by":27760,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":1707,"test_suite_updated_at":null,"rescore_all_solutions":false,"group_id":1,"created_at":"2014-11-18T04:58:05.000Z","updated_at":"2026-04-12T20:17:12.000Z","published_at":"2014-11-18T04:58:05.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\",\"relationshipId\":\"rId1\",\"target\":\"/matlab/document.xml\"},{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/output\",\"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\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\u003eFind out sum and carry of a binary adder if previous carry is given with two bits (x and y) for addition.\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\u003eExamples\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\u003ePrevious carry is 1 and x=y=1. Addition is 1 and carry is also 1.\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\u003ePrevious carry is 0 and x=y=1. Addition is 0 and carry is also 1.\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\u003ePrevious carry is 0 and x=1, y=0. Addition is 1 and carry is also 0.\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":2494,"title":"What is the nth step in Conway's Life?","description":"Based on \u003cProblem 52. What is the next step in Conway's Life?\u003e\r\n\r\nGeneralize your solution to Problem 52 - Give the state of the game at nth step for any size board.\r\n\r\nNote: This problem uses the MATLAB convention for indexing (t=1 is the initial state).","description_html":"\u003cdiv style = \"text-align: start; line-height: 20px; 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: normal; text-decoration: none; white-space: normal; \"\u003e\u003cdiv style=\"display: block; min-width: 0px; padding-top: 0px; transform-origin: 332px 40.5px; vertical-align: baseline; perspective-origin: 332px 40.5px; \"\u003e\u003cdiv style=\"font-family: Helvetica, Arial, sans-serif; line-height: 21px; margin-bottom: 9px; margin-left: 4px; margin-right: 10px; margin-top: 2px; text-align: left; transform-origin: 309px 10.5px; white-space: pre-wrap; perspective-origin: 309px 10.5px; margin-left: 4px; margin-top: 2px; margin-bottom: 9px; margin-right: 10px; \"\u003e\u003cspan style=\"display: inline; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; transform-origin: 0px 0px; perspective-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003eBased on\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display: inline; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; transform-origin: 0px 0px; perspective-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003e \u003c/span\u003e\u003c/span\u003e\u003ca target='_blank' href = \"https://www.mathworks.com/matlabcentral/cody/problems/52\"\u003e\u003cspan style=\"\"\u003e\u003cspan style=\"\"\u003e52. What is the next step in Conway's Life?\u003c/span\u003e\u003c/span\u003e\u003c/a\u003e\u003c/div\u003e\u003cdiv style=\"font-family: Helvetica, Arial, sans-serif; line-height: 21px; margin-bottom: 9px; margin-left: 4px; margin-right: 10px; margin-top: 2px; text-align: left; transform-origin: 309px 10.5px; white-space: pre-wrap; perspective-origin: 309px 10.5px; margin-left: 4px; margin-top: 2px; margin-bottom: 9px; margin-right: 10px; \"\u003e\u003cspan style=\"display: inline; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; transform-origin: 0px 0px; perspective-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003eGeneralize your solution to Problem 52 - Give the state of the game at nth step for any size board.\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"font-family: Helvetica, Arial, sans-serif; line-height: 21px; margin-bottom: 9px; margin-left: 4px; margin-right: 10px; margin-top: 2px; text-align: left; transform-origin: 309px 10.5px; white-space: pre-wrap; perspective-origin: 309px 10.5px; margin-left: 4px; margin-top: 2px; margin-bottom: 9px; margin-right: 10px; \"\u003e\u003cspan style=\"display: inline; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; transform-origin: 0px 0px; perspective-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003eNote: This problem uses the MATLAB convention for indexing (t=1 is the initial state).\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003c/div\u003e\u003c/div\u003e","function_template":"function M = life(L,n)\r\n\r\n function L = conway(L)\r\n end\r\n\r\n while t\u003cn\r\n end\r\n\r\nend","test_suite":"%%\r\nL = [\r\n   0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\r\n   0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0\r\n   0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0\r\n   0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0\r\n   0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0\r\n   0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0\r\n   0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0\r\n   0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0\r\n   0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0\r\n   0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0\r\n   0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0\r\n   0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0\r\n   0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0\r\n   0 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0\r\n   0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0\r\n   0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0\r\n   0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0\r\n   0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0\r\n   0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0\r\n   0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\r\n];\r\nn = 6;\r\nM_correct = [\r\n 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\r\n 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0\r\n 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0\r\n 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0\r\n 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0\r\n 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0\r\n 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0\r\n 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0\r\n 0 0 0 0 0 0 1 1 1 1 1 0 0 0 0 0 0 0 0 0\r\n 0 0 0 0 0 0 1 1 1 1 1 0 0 0 0 0 0 0 0 0\r\n 0 0 0 0 0 0 1 1 0 1 1 0 0 0 0 0 0 0 0 0\r\n 0 0 1 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 0\r\n 0 1 0 1 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 0\r\n 0 1 1 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 0\r\n 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0\r\n 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\r\n 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\r\n 0 0 0 0 1 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0\r\n 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0\r\n 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0\r\n];\r\nassert(isequal(life(L,n),M_correct))\r\n%%\r\nL = [\r\n0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 \r\n0 1 1 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 \r\n0 0 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 \r\n0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 \r\n0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 \r\n0 0 0 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 \r\n0 0 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 \r\n0 1 1 0 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 \r\n0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 \r\n0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 \r\n0 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 1 1 0 \r\n0 0 0 0 0 0 1 0 0 0 0 1 0 0 0 1 0 0 1 \r\n0 0 0 0 0 0 1 1 0 0 0 0 0 1 0 1 0 0 1 \r\n0 1 0 0 1 0 0 1 1 1 1 0 1 0 0 0 1 1 0 \r\n1 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 \r\n1 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 \r\n1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\r\n];\r\nn = 13;\r\nM_correct = [\r\n 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\r\n 0 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1\r\n 0 0 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1\r\n 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\r\n 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\r\n 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\r\n 1 0 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 1 1\r\n 1 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 1 1\r\n 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 1 0 1\r\n 1 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\r\n 0 1 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0\r\n 0 1 1 0 0 0 0 0 0 1 1 1 1 0 1 0 1 1 0\r\n 1 1 0 0 0 0 0 0 0 1 1 0 1 1 0 1 1 1 0\r\n 1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 1\r\n 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 1 1 1\r\n 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 1 0\r\n 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\r\n];\r\nassert(isequal(life(L,n),M_correct))\r\n%%\r\nL = [\r\n 0 0 0 0 1 1 1 1 0 0 0 0 0 0 0\r\n 0 0 0 1 1 1 1 1 1 0 0 0 0 0 0\r\n 0 0 1 1 0 1 1 1 1 0 0 0 0 0 0\r\n 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0\r\n 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\r\n 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0\r\n 0 1 0 0 0 0 0 0 0 0 0 0 0 0 1\r\n 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0\r\n 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1\r\n 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0\r\n 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\r\n 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\r\n 0 0 0 0 1 1 1 1 0 0 0 0 0 0 0\r\n 0 0 0 1 1 1 1 1 1 0 0 0 0 0 0\r\n 0 0 1 1 0 1 1 1 1 0 0 0 0 0 0\r\n 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0\r\n];\r\nn = 9;\r\nM_correct = [\r\n 0 0 0 0 0 1 1 0 1 1 0 0 0 0 0\r\n 0 0 0 0 0 1 0 0 1 0 0 0 0 0 0\r\n 0 1 1 0 0 0 1 1 0 0 0 0 0 0 0\r\n 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\r\n 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1\r\n 0 0 1 0 0 1 1 1 1 0 0 0 0 0 0\r\n 0 0 1 0 1 0 0 0 0 0 1 0 0 1 0\r\n 1 1 0 1 0 0 0 0 0 0 0 0 1 1 0\r\n 0 0 0 0 1 0 0 0 0 0 1 0 1 0 0\r\n 0 0 0 1 0 0 0 0 0 0 0 0 1 1 0\r\n 0 0 0 1 0 0 0 0 0 0 0 1 1 0 0\r\n 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0\r\n 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0\r\n 0 0 0 0 0 1 0 0 0 1 0 0 0 0 0\r\n 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0\r\n 0 0 0 0 0 0 1 0 0 1 0 0 0 0 0\r\n];\r\nassert(isequal(life(L,n),M_correct))\r\n%%\r\nL = [\r\n0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 \r\n0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0 0 0 0 0 \r\n0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 1 1 \r\n0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 1 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 1 1 \r\n1 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 \r\n1 1 0 0 0 0 0 0 0 0 1 0 0 0 1 0 1 1 0 0 0 0 1 0 1 0 0 0 0 0 0 0 0 0 0 0 \r\n0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 \r\n0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 \r\n0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 \r\n];\r\nn = 1;\r\nM_correct = [\r\n0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 \r\n0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0 0 0 0 0 \r\n0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 1 1 \r\n0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 1 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 1 1 \r\n1 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 \r\n1 1 0 0 0 0 0 0 0 0 1 0 0 0 1 0 1 1 0 0 0 0 1 0 1 0 0 0 0 0 0 0 0 0 0 0 \r\n0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 \r\n0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 \r\n0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 \r\n];\r\nassert(isequal(life(L,n),M_correct))","published":true,"deleted":false,"likes_count":2,"comments_count":2,"created_by":379,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":29,"test_suite_updated_at":null,"rescore_all_solutions":false,"group_id":1,"created_at":"2014-08-09T05:29:47.000Z","updated_at":"2026-04-08T14:56:31.000Z","published_at":"2014-08-09T05:31:45.000Z","restored_at":null,"restored_by":null,"spam":false,"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\u003eBased on\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=\\\"https://www.mathworks.com/matlabcentral/cody/problems/52\\\"\u003e\u003cw:r\u003e\u003cw:t\u003e52. What is the next step in Conway's Life?\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\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\u003eGeneralize your solution to Problem 52 - Give the state of the game at nth step for any size board.\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\u003eNote: This problem uses the MATLAB convention for indexing (t=1 is the initial state).\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":2147,"title":"Langston's Ant","description":"The \u003chttp://en.wikipedia.org/wiki/Langton%27s_ant Langston's Ant\u003e Challenge is to determine the number of Black squares after K Ant moves. \r\n\r\nAn infinite white board has an Ant at the center. Direction is not required.\r\n\r\nMovement and Affect:\r\n\r\n  1) At a white square, turn 90° right, White goes to Black, move forward one unit\r\n  2) At a black square, turn 90° left, Black goes to White, move forward one unit\r\n\r\nAfter a given number of moves how many Black squares are present. \r\n\r\n*Input:* K,  number of moves from 100 to 12000\r\n\r\n*Output:* BLK, number of Black squares\r\n\r\n*Example:* K=5, BLK=3  [00000;00010;00110]\r\n\r\n*Note:*  After 11000 moves a pattern emerges\r\n\r\n","description_html":"\u003cp\u003eThe \u003ca href = \"http://en.wikipedia.org/wiki/Langton%27s_ant\"\u003eLangston's Ant\u003c/a\u003e Challenge is to determine the number of Black squares after K Ant moves.\u003c/p\u003e\u003cp\u003eAn infinite white board has an Ant at the center. Direction is not required.\u003c/p\u003e\u003cp\u003eMovement and Affect:\u003c/p\u003e\u003cpre class=\"language-matlab\"\u003e1) At a white square, turn 90° right, White goes to Black, move forward one unit\r\n2) At a black square, turn 90° left, Black goes to White, move forward one unit\r\n\u003c/pre\u003e\u003cp\u003eAfter a given number of moves how many Black squares are present.\u003c/p\u003e\u003cp\u003e\u003cb\u003eInput:\u003c/b\u003e K,  number of moves from 100 to 12000\u003c/p\u003e\u003cp\u003e\u003cb\u003eOutput:\u003c/b\u003e BLK, number of Black squares\u003c/p\u003e\u003cp\u003e\u003cb\u003eExample:\u003c/b\u003e K=5, BLK=3  [00000;00010;00110]\u003c/p\u003e\u003cp\u003e\u003cb\u003eNote:\u003c/b\u003e  After 11000 moves a pattern emerges\u003c/p\u003e","function_template":"function BLK=Langston_Ant(L)\r\n BLK=0;\r\nend","test_suite":"%%\r\nassert(isequal(Langston_Ant(100),20))\r\n%%\r\nassert(isequal(Langston_Ant(200),40))\r\n%%\r\nassert(isequal(Langston_Ant(300),48))\r\n%%\r\nassert(isequal(Langston_Ant(400),62))\r\n%%\r\nassert(isequal(Langston_Ant(500),62))\r\n%%\r\nassert(isequal(Langston_Ant(600),72))\r\n%%\r\nassert(isequal(Langston_Ant(700),80))\r\n%%\r\nassert(isequal(Langston_Ant(800),96))\r\n%%\r\nassert(isequal(Langston_Ant(900),108))\r\n%%\r\nassert(isequal(Langston_Ant(1000),118))\r\n%%\r\nassert(isequal(Langston_Ant(12000),952))\r\n","published":true,"deleted":false,"likes_count":7,"comments_count":0,"created_by":3097,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":39,"test_suite_updated_at":null,"rescore_all_solutions":false,"group_id":1,"created_at":"2014-02-01T22:20:21.000Z","updated_at":"2026-04-01T13:02:15.000Z","published_at":"2014-02-01T22:35:43.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\u003eThe\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/Langton%27s_ant\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eLangston's Ant\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e Challenge is to determine the number of Black squares after K Ant moves.\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\u003eAn infinite white board has an Ant at the center. Direction is not required.\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\u003eMovement and Affect:\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[1) At a white square, turn 90° right, White goes to Black, move forward one unit\\n2) At a black square, turn 90° left, Black goes to White, move forward one unit]]\u003e\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eAfter a given number of moves how many Black squares are present.\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 K, number of moves from 100 to 12000\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 BLK, number of Black squares\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\u003cw:r\u003e\u003cw:t\u003e K=5, BLK=3 [00000;00010;00110]\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\u003eNote:\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e After 11000 moves a pattern emerges\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":1966,"title":"Kaggle: Reverse Game of Life - Single Move to One Cell Case","description":"\u003chttp://www.kaggle.com/c/conway-s-reverse-game-of-life Kaggle's Conway's Reverse Game of Life\u003e contest inspires this Single Reverse step in Life challenge. The kaggle contest runs from Oct-14-2013 thru Mar-02-2014. References: \u003chttp://mathworld.wolfram.com/GameofLife.html Game of Life at Wolfram\u003e. \u003chttp://en.wikipedia.org/wiki/Conway%27s_Game_of_Life Wiki Life\u003e.\r\n\r\n\r\n  1. Any live cell with fewer than two live neighbors dies, as if caused by under-population.\r\n  2. Any live cell with two or three live neighbors lives on to the next generation.\r\n  3. Any live cell with more than three live neighbors dies, as if by overcrowding.\r\n  4. Any dead cell with exactly three live neighbors becomes a live cell, as if by reproduction.\r\n5. No wrap around. Beyond edge is zero. Eight Neighbors.\r\n\r\n\r\n\r\nFind all the 4x4 Life matrices that upon one evolution produce a single living cell. This is a single evolution reversal set. The 4x4 gets appended by a ring of zeros for processing. The single living cell may be anywhere in the final 6x6 matrix, but none are expected to occur in the outer ring for a valid solution.\r\n\r\n*Input:* None\r\n\r\n*Output:* Cell array of unique 4x4 matrices that evolve to a single cell\r\n\r\n*Scoring:* 600 - (Number of valid unique 4x4 matrices)\r\n\r\n*Examples:*  A few valid matrices that produce a single cell\r\n\r\n  0000  0010  1101\r\n  0001  1000  1110\r\n  0000  0000  0100\r\n  0101  0110  1001","description_html":"\u003cp\u003e\u003ca href = \"http://www.kaggle.com/c/conway-s-reverse-game-of-life\"\u003eKaggle's Conway's Reverse Game of Life\u003c/a\u003e contest inspires this Single Reverse step in Life challenge. The kaggle contest runs from Oct-14-2013 thru Mar-02-2014. References: \u003ca href = \"http://mathworld.wolfram.com/GameofLife.html\"\u003eGame of Life at Wolfram\u003c/a\u003e. \u003ca href = \"http://en.wikipedia.org/wiki/Conway%27s_Game_of_Life\"\u003eWiki Life\u003c/a\u003e.\u003c/p\u003e\u003cpre class=\"language-matlab\"\u003e1. Any live cell with fewer than two live neighbors dies, as if caused by under-population.\r\n2. Any live cell with two or three live neighbors lives on to the next generation.\r\n3. Any live cell with more than three live neighbors dies, as if by overcrowding.\r\n4. Any dead cell with exactly three live neighbors becomes a live cell, as if by reproduction.\r\n5. No wrap around. Beyond edge is zero. Eight Neighbors.\r\n\u003c/pre\u003e\u003cp\u003eFind all the 4x4 Life matrices that upon one evolution produce a single living cell. This is a single evolution reversal set. The 4x4 gets appended by a ring of zeros for processing. The single living cell may be anywhere in the final 6x6 matrix, but none are expected to occur in the outer ring for a valid solution.\u003c/p\u003e\u003cp\u003e\u003cb\u003eInput:\u003c/b\u003e None\u003c/p\u003e\u003cp\u003e\u003cb\u003eOutput:\u003c/b\u003e Cell array of unique 4x4 matrices that evolve to a single cell\u003c/p\u003e\u003cp\u003e\u003cb\u003eScoring:\u003c/b\u003e 600 - (Number of valid unique 4x4 matrices)\u003c/p\u003e\u003cp\u003e\u003cb\u003eExamples:\u003c/b\u003e  A few valid matrices that produce a single cell\u003c/p\u003e\u003cpre class=\"language-matlab\"\u003e0000  0010  1101\r\n0001  1000  1110\r\n0000  0000  0100\r\n0101  0110  1001\r\n\u003c/pre\u003e","function_template":"function mcell=Life\r\n  mcell{1}=zeros(4);\r\nend","test_suite":"%%\r\nassignin('caller','score',200);\r\n\r\n%%\r\n[mcell] = Life;\r\n\r\n[nr,nc]=size(mcell);\r\n% check for uniqueness\r\nvalid=1;\r\ntic\r\nfor k=1:nc-1\r\n  mk=mcell{k};\r\n for p=k+1:nc\r\n  mp=mcell{p};\r\n  mkp=mk==mp;\r\n  if all(mkp(:))\r\n   valid=0;\r\n  end\r\n end\r\nend\r\nassert(valid==1,sprintf('Not all unique solutions'));\r\ntoc\r\n\r\n% run an evolution and verify\r\ntic\r\nfor k=1:nc\r\n  m=mcell{k};\r\n  m=[zeros(1,6);zeros(4,1) m zeros(4,1);zeros(1,6)];\r\n  mc=conv2(m,[1 1 1;1 0 1;1 1 1],'same');\r\n  m=~(mc\u003c2 | mc\u003e3) \u0026 ((m \u0026 mc==2) | (m \u0026 mc==3) | (~m \u0026 mc==3)); \r\n  assert(isequal(nnz(m),1),sprintf('Non-Single survivor solution'))\r\nend\r\ntoc\r\nassignin('caller','score',min(200,max(0,600-nc)));\r\n","published":true,"deleted":false,"likes_count":2,"comments_count":3,"created_by":3097,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":25,"test_suite_updated_at":"2019-07-16T14:11:36.000Z","rescore_all_solutions":false,"group_id":1,"created_at":"2013-10-31T02:08:53.000Z","updated_at":"2026-02-13T15:19:14.000Z","published_at":"2013-10-31T03:12:32.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:hyperlink w:docLocation=\\\"http://www.kaggle.com/c/conway-s-reverse-game-of-life\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eKaggle's Conway's Reverse Game of Life\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e contest inspires this Single Reverse step in Life challenge. The kaggle contest runs from Oct-14-2013 thru Mar-02-2014. References:\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://mathworld.wolfram.com/GameofLife.html\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eGame of Life at Wolfram\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\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:hyperlink w:docLocation=\\\"http://en.wikipedia.org/wiki/Conway%27s_Game_of_Life\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eWiki Life\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=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[1. Any live cell with fewer than two live neighbors dies, as if caused by under-population.\\n2. Any live cell with two or three live neighbors lives on to the next generation.\\n3. Any live cell with more than three live neighbors dies, as if by overcrowding.\\n4. Any dead cell with exactly three live neighbors becomes a live cell, as if by reproduction.\\n5. No wrap around. Beyond edge is zero. Eight Neighbors.]]\u003e\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eFind all the 4x4 Life matrices that upon one evolution produce a single living cell. This is a single evolution reversal set. The 4x4 gets appended by a ring of zeros for processing. The single living cell may be anywhere in the final 6x6 matrix, but none are expected to occur in the outer ring for a valid 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\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 None\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 Cell array of unique 4x4 matrices that evolve to a single cell\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 600 - (Number of valid unique 4x4 matrices)\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\u003eExamples:\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e A few valid matrices that produce a single cell\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[0000  0010  1101\\n0001  1000  1110\\n0000  0000  0100\\n0101  0110  1001]]\u003e\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":1970,"title":"Kaggle: Reverse Game of Life - Periods of Oscillators","description":"\u003chttp://www.kaggle.com/c/conway-s-reverse-game-of-life Kaggle's Conway's Reverse Game of Life\u003e contest inspires this Period of Life challenge. The kaggle contest runs from Oct-14-2013 thru Mar-02-2014. References: \u003chttp://mathworld.wolfram.com/GameofLife.html Game of Life at Wolfram\u003e. \u003chttp://en.wikipedia.org/wiki/Conway%27s_Game_of_Life Wiki Life\u003e.\r\n\r\n\r\n  1. Any live cell with fewer than two live neighbors dies, as if caused by under-population.\r\n  2. Any live cell with two or three live neighbors lives on to the next generation.\r\n  3. Any live cell with more than three live neighbors dies, as if by overcrowding.\r\n  4. Any dead cell with exactly three live neighbors becomes a live cell, as if by reproduction.\r\n5. No wrap around. Beyond edge is zero. Eight Neighbors.\r\n\r\n\r\n\r\nDetermine the period of life for a given binary matrix. A stable configuration, Still Life, has a cycle of 1.\r\n\r\n*Input:* M , an [m,n] array\r\n\r\n*Output:* N, Period of Life cycle (Period \u003c11)\r\n\r\n\r\n*Examples:*  A few matrices of varying periods\r\n\r\n  N=1   N=2    N=3 Caterer\r\n  0000  00000  0000000000\r\n  0110  00000  0001000000\r\n  0110  01110  0100011110\r\n  0000  00000  0100010000\r\n        00000  0100000000\r\n               0000100000\r\n               0011000000\r\n               0000000000\r\n\r\n*Additional References:*\r\n\u003chttp://www.conwaylife.com/wiki/Oscillator Oscillators\u003e, \u003chttp://www.conwaylife.com/wiki/Still_life Still Life\u003e","description_html":"\u003cp\u003e\u003ca href = \"http://www.kaggle.com/c/conway-s-reverse-game-of-life\"\u003eKaggle's Conway's Reverse Game of Life\u003c/a\u003e contest inspires this Period of Life challenge. The kaggle contest runs from Oct-14-2013 thru Mar-02-2014. References: \u003ca href = \"http://mathworld.wolfram.com/GameofLife.html\"\u003eGame of Life at Wolfram\u003c/a\u003e. \u003ca href = \"http://en.wikipedia.org/wiki/Conway%27s_Game_of_Life\"\u003eWiki Life\u003c/a\u003e.\u003c/p\u003e\u003cpre class=\"language-matlab\"\u003e1. Any live cell with fewer than two live neighbors dies, as if caused by under-population.\r\n2. Any live cell with two or three live neighbors lives on to the next generation.\r\n3. Any live cell with more than three live neighbors dies, as if by overcrowding.\r\n4. Any dead cell with exactly three live neighbors becomes a live cell, as if by reproduction.\r\n5. No wrap around. Beyond edge is zero. Eight Neighbors.\r\n\u003c/pre\u003e\u003cp\u003eDetermine the period of life for a given binary matrix. A stable configuration, Still Life, has a cycle of 1.\u003c/p\u003e\u003cp\u003e\u003cb\u003eInput:\u003c/b\u003e M , an [m,n] array\u003c/p\u003e\u003cp\u003e\u003cb\u003eOutput:\u003c/b\u003e N, Period of Life cycle (Period \u0026lt;11)\u003c/p\u003e\u003cp\u003e\u003cb\u003eExamples:\u003c/b\u003e  A few matrices of varying periods\u003c/p\u003e\u003cpre class=\"language-matlab\"\u003eN=1   N=2    N=3 Caterer\r\n0000  00000  0000000000\r\n0110  00000  0001000000\r\n0110  01110  0100011110\r\n0000  00000  0100010000\r\n      00000  0100000000\r\n             0000100000\r\n             0011000000\r\n             0000000000\r\n\u003c/pre\u003e\u003cp\u003e\u003cb\u003eAdditional References:\u003c/b\u003e \u003ca href = \"http://www.conwaylife.com/wiki/Oscillator\"\u003eOscillators\u003c/a\u003e, \u003ca href = \"http://www.conwaylife.com/wiki/Still_life\"\u003eStill Life\u003c/a\u003e\u003c/p\u003e","function_template":"function N=Life_Period(M)\r\n%\r\nN=0;\r\n\r\nend","test_suite":"%Block  Still 1\r\nm=[0 0 0 0;0 1 1 0;0 1 1 0;0 0 0 0];\r\nn=Life_Period(m);\r\nassert(n==1)\r\n%%\r\n%Beehive  Still 1\r\nm=[0 0 0 0 0 0;0 0 1 1 0 0;0 1 0 0 1 0;0 0 1 1 0 0;0 0 0 0 0 0];\r\nn=Life_Period(m);\r\nassert(n==1)\r\n%%\r\n%Loaf  Still 1\r\nm=[0 0 0 0 0 0;0 0 1 1 0 0;0 1 0 0 1 0;0 0 1 0 1 0;0 0 0 1 0 0;0 0 0 0 0 0];\r\nn=Life_Period(m);\r\nassert(n==1)\r\n%%\r\n%Boat  Still 1\r\nm=[0 0 0 0 0;0 1 1 0 0;0 1 0 1 0;0 0 1 0 0;0 0 0 0 0];\r\nn=Life_Period(m);\r\nassert(n==1)\r\n%%\r\n%Table Still 1\r\nm=[0 0 0 0 0 0 0;0 1 1 0 1 1 0;0 0 1 0 1 0 0;0 0 1 0 1 0 0;0 1 1 0 1 1 0;0 0 0 0 0 0 0];\r\nn=Life_Period(m);\r\nassert(n==1)\r\n%%\r\n%Blinker  Osc 2\r\nm=[0 0 0 0 0;0 0 0 0 0;0 1 1 1 0;0 0 0 0 0;0 0 0 0 0];\r\nn=Life_Period(m);\r\nassert(n==2)\r\n%%\r\n%Toad  Osc 2\r\nm=[0 0 0 0 0 0;0 0 0 0 0 0;0 0 1 1 1 0;0 1 1 1 0 0;0 0 0 0 0 0;0 0 0 0 0 0];\r\nn=Life_Period(m);\r\nassert(n==2)\r\n%%\r\n%Beacon  Osc 2\r\nm=[0 0 0 0 0 0;0 1 1 0 0 0;0 1 1 0 0 0;0 0 0 1 1 0;0 0 0 1 1 0;0 0 0 0 0 0];\r\nn=Life_Period(m);\r\nassert(n==2)\r\n%%\r\n%Caterer  Osc 3\r\nm=[0 0 0 0 0 0 0 0 0 0;0 0 0 1 0 0 0 0 0 0;0 1 0 0 0 1 1 1 1 0;0 1 0 0 0 1 0 0 0 0;0 1 0 0 0 0 0 0 0 0;0 0 0 0 1 0 0 0 0 0;0 0 1 1 0 0 0 0 0 0;0 0 0 0 0 0 0 0 0 0;];\r\nn=Life_Period(m);\r\nassert(n==3)","published":true,"deleted":false,"likes_count":2,"comments_count":1,"created_by":3097,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":25,"test_suite_updated_at":null,"rescore_all_solutions":false,"group_id":1,"created_at":"2013-11-04T04:25:13.000Z","updated_at":"2026-02-13T15:20:35.000Z","published_at":"2013-11-04T04:46:57.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:hyperlink w:docLocation=\\\"http://www.kaggle.com/c/conway-s-reverse-game-of-life\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eKaggle's Conway's Reverse Game of Life\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e contest inspires this Period of Life challenge. The kaggle contest runs from Oct-14-2013 thru Mar-02-2014. References:\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://mathworld.wolfram.com/GameofLife.html\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eGame of Life at Wolfram\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\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:hyperlink w:docLocation=\\\"http://en.wikipedia.org/wiki/Conway%27s_Game_of_Life\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eWiki Life\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=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[1. Any live cell with fewer than two live neighbors dies, as if caused by under-population.\\n2. Any live cell with two or three live neighbors lives on to the next generation.\\n3. Any live cell with more than three live neighbors dies, as if by overcrowding.\\n4. Any dead cell with exactly three live neighbors becomes a live cell, as if by reproduction.\\n5. No wrap around. Beyond edge is zero. Eight Neighbors.]]\u003e\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eDetermine the period of life for a given binary matrix. A stable configuration, Still Life, has a cycle of 1.\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 , an [m,n] 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\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 N, Period of Life cycle (Period \u0026lt;11)\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\u003eExamples:\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e A few matrices of varying periods\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[N=1   N=2    N=3 Caterer\\n0000  00000  0000000000\\n0110  00000  0001000000\\n0110  01110  0100011110\\n0000  00000  0100010000\\n      00000  0100000000\\n             0000100000\\n             0011000000\\n             0000000000]]\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\u003eAdditional References:\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.conwaylife.com/wiki/Oscillator\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eOscillators\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\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:hyperlink w:docLocation=\\\"http://www.conwaylife.com/wiki/Still_life\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eStill Life\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\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":1975,"title":"Kaggle: Reverse Game of Life - Zoo of Stills and Oscillators","description":"\u003chttp://www.kaggle.com/c/conway-s-reverse-game-of-life Kaggle's Conway's Reverse Game of Life\u003e contest inspires this Reverse Life Zoo challenge. The kaggle contest runs from Oct-14-2013 thru Mar-02-2014. References: \u003chttp://mathworld.wolfram.com/GameofLife.html Game of Life at Wolfram\u003e. \u003chttp://en.wikipedia.org/wiki/Conway%27s_Game_of_Life Wiki Life\u003e.\r\n\r\n\r\n  1. Any live cell with fewer than two live neighbors dies, as if caused by under-population.\r\n  2. Any live cell with two or three live neighbors lives on to the next generation.\r\n  3. Any live cell with more than three live neighbors dies, as if by overcrowding.\r\n  4. Any dead cell with exactly three live neighbors becomes a live cell, as if by reproduction.\r\n5. No wrap around. Beyond edge is zero. Eight Neighbors.\r\n\r\n\r\n\r\nDetermine the Zoo of life at its previous Snapshot. The output must evolve to the given Zoo Snapshot.  It is a Zoo since all animals are in their private enclosures and do not interact with any other animals. Animals are stable or cyclic oscillators.\r\n\r\n*Input:* Zoo , an [m,n] array\r\n\r\n*Output:* ZooPre, image of the Zoo at its prior iteration\r\n\r\n\r\n*Example:*\r\n\r\n  Zoo        ZooPre\r\n  0000000000  000000000  \r\n  0110000100  011000000  \r\n  0110000100  011001110  \r\n  0000000100  000000000  \r\n  0000000000  000000000\r\n\r\n*Additional References:*\r\n\u003chttp://www.conwaylife.com/wiki/Oscillator Oscillators\u003e, \u003chttp://www.conwaylife.com/wiki/Still_life Still Life\u003e\r\n\r\n*Next:* Small Island - Prior Snapshot Prediction","description_html":"\u003cp\u003e\u003ca href = \"http://www.kaggle.com/c/conway-s-reverse-game-of-life\"\u003eKaggle's Conway's Reverse Game of Life\u003c/a\u003e contest inspires this Reverse Life Zoo challenge. The kaggle contest runs from Oct-14-2013 thru Mar-02-2014. References: \u003ca href = \"http://mathworld.wolfram.com/GameofLife.html\"\u003eGame of Life at Wolfram\u003c/a\u003e. \u003ca href = \"http://en.wikipedia.org/wiki/Conway%27s_Game_of_Life\"\u003eWiki Life\u003c/a\u003e.\u003c/p\u003e\u003cpre class=\"language-matlab\"\u003e1. Any live cell with fewer than two live neighbors dies, as if caused by under-population.\r\n2. Any live cell with two or three live neighbors lives on to the next generation.\r\n3. Any live cell with more than three live neighbors dies, as if by overcrowding.\r\n4. Any dead cell with exactly three live neighbors becomes a live cell, as if by reproduction.\r\n5. No wrap around. Beyond edge is zero. Eight Neighbors.\r\n\u003c/pre\u003e\u003cp\u003eDetermine the Zoo of life at its previous Snapshot. The output must evolve to the given Zoo Snapshot.  It is a Zoo since all animals are in their private enclosures and do not interact with any other animals. Animals are stable or cyclic oscillators.\u003c/p\u003e\u003cp\u003e\u003cb\u003eInput:\u003c/b\u003e Zoo , an [m,n] array\u003c/p\u003e\u003cp\u003e\u003cb\u003eOutput:\u003c/b\u003e ZooPre, image of the Zoo at its prior iteration\u003c/p\u003e\u003cp\u003e\u003cb\u003eExample:\u003c/b\u003e\u003c/p\u003e\u003cpre class=\"language-matlab\"\u003eZoo        ZooPre\r\n0000000000  000000000  \r\n0110000100  011000000  \r\n0110000100  011001110  \r\n0000000100  000000000  \r\n0000000000  000000000\r\n\u003c/pre\u003e\u003cp\u003e\u003cb\u003eAdditional References:\u003c/b\u003e \u003ca href = \"http://www.conwaylife.com/wiki/Oscillator\"\u003eOscillators\u003c/a\u003e, \u003ca href = \"http://www.conwaylife.com/wiki/Still_life\"\u003eStill Life\u003c/a\u003e\u003c/p\u003e\u003cp\u003e\u003cb\u003eNext:\u003c/b\u003e Small Island - Prior Snapshot Prediction\u003c/p\u003e","function_template":"function ZooPre=Zoo_prior(Zoo)\r\n ZooPre=Zoo;\r\nend","test_suite":"%%\r\n block=[0 0 0 0;0 1 1 0;0 1 1 0;0 0 0 0];\r\n Zoo=repmat(block,3,1);\r\n\r\n ZooPre=Zoo_prior(Zoo);\r\n mc=conv2(single(ZooPre),[1 1 1;1 0 1;1 1 1],'same');\r\n ZooChk=~(mc\u003c2 | mc\u003e3) \u0026 ((ZooPre \u0026 mc==2) | (ZooPre \u0026 mc==3) | (~ZooPre \u0026 mc==3)); \r\n\r\n \r\n   assert(all(all(ZooChk==Zoo))) \r\n%  figure(1);imagesc(ZooPre)\r\n%  figure(2);imagesc(Zoo)\r\n\r\n%%\r\ncaterer1=[0 0 0 0 0 0 0 0 0 0;0 0 0 1 0 0 0 0 0 0;0 1 0 0 0 1 1 1 1 0;0 1 0 0 0 1 0 0 0 0;0 1 0 0 0 0 0 0 0 0;0 0 0 0 1 0 0 0 0 0;0 0 1 1 0 0 0 0 0 0;0 0 0 0 0 0 0 0 0 0];\r\n\r\ncaterer2=[0     0     0     0     0     0     0     0     0     0;\r\n     0     0     0     0     0     0     1     1     0     0;\r\n     0     0     1     0     1     1     1     1     0     0;\r\n     1     1     1     0     0     1     0     1     0     0;\r\n     0     0     0     0     0     0     0     0     0     0;\r\n     0     0     1     1     0     0     0     0     0     0;\r\n     0     0     0     1     0     0     0     0     0     0;\r\n     0     0     0     0     0     0     0     0     0     0];\r\n\r\ncaterer3=[0     0     0     0     0     0     0     0     0     0;\r\n     0     0     0     0     0     0     0     1     0     0;\r\n     0     0     1     1     1     0     0     0     1     0;\r\n     0     1     1     1     1     1     0     1     0     0;\r\n     0     0     0     1     0     0     0     0     0     0;\r\n     0     0     1     1     0     0     0     0     0     0;\r\n     0     0     1     1     0     0     0     0     0     0;\r\n     0     0     0     0     0     0     0     0     0     0];\r\n\r\n Zoo=[caterer1 zeros(8,1) caterer2 zeros(8,1) caterer3] ;\r\n\r\n ZooPre=Zoo_prior(Zoo);\r\n mc=conv2(single(ZooPre),[1 1 1;1 0 1;1 1 1],'same');\r\n ZooChk=~(mc\u003c2 | mc\u003e3) \u0026 ((ZooPre \u0026 mc==2) | (ZooPre \u0026 mc==3) | (~ZooPre \u0026 mc==3)); \r\n\r\n \r\n   assert(all(all(ZooChk==Zoo))) \r\n%  figure(1);imagesc(ZooPre)\r\n%  figure(2);imagesc(Zoo)\r\n\r\n%%\r\ncaterer1=[0 0 0 0 0 0 0 0 0 0;0 0 0 1 0 0 0 0 0 0;0 1 0 0 0 1 1 1 1 0;0 1 0 0 0 1 0 0 0 0;0 1 0 0 0 0 0 0 0 0;0 0 0 0 1 0 0 0 0 0;0 0 1 1 0 0 0 0 0 0;0 0 0 0 0 0 0 0 0 0]; % 8x10\r\nLoaf=[0 0 0 0 0 0;0 0 1 1 0 0;0 1 0 0 1 0;0 0 1 0 1 0;0 0 0 1 0 0;0 0 0 0 0 0]; % 6x6\r\nblinker=[0 0 0 0 0;0 0 0 0 0;0 1 1 1 0;0 0 0 0 0;0 0 0 0 0]; % 5x5\r\n\r\n\r\n\r\nZoo=[[Loaf;Loaf'] zeros(12,1) [caterer1;zeros(4,10)] [blinker;blinker';zeros(2,5)] ] ;\r\n\r\n ZooPre=Zoo_prior(Zoo);\r\n mc=conv2(single(ZooPre),[1 1 1;1 0 1;1 1 1],'same');\r\n ZooChk=~(mc\u003c2 | mc\u003e3) \u0026 ((ZooPre \u0026 mc==2) | (ZooPre \u0026 mc==3) | (~ZooPre \u0026 mc==3)); \r\n\r\n \r\n   assert(all(all(ZooChk==Zoo))) \r\n%  figure(1);imagesc(ZooPre)\r\n%  figure(2);imagesc(Zoo)\r\n\r\n","published":true,"deleted":false,"likes_count":3,"comments_count":1,"created_by":3097,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":19,"test_suite_updated_at":null,"rescore_all_solutions":false,"group_id":1,"created_at":"2013-11-09T04:53:18.000Z","updated_at":"2026-02-13T15:21:36.000Z","published_at":"2013-11-09T05:22:10.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:hyperlink w:docLocation=\\\"http://www.kaggle.com/c/conway-s-reverse-game-of-life\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eKaggle's Conway's Reverse Game of Life\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e contest inspires this Reverse Life Zoo challenge. The kaggle contest runs from Oct-14-2013 thru Mar-02-2014. References:\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://mathworld.wolfram.com/GameofLife.html\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eGame of Life at Wolfram\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\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:hyperlink w:docLocation=\\\"http://en.wikipedia.org/wiki/Conway%27s_Game_of_Life\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eWiki Life\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=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[1. Any live cell with fewer than two live neighbors dies, as if caused by under-population.\\n2. Any live cell with two or three live neighbors lives on to the next generation.\\n3. Any live cell with more than three live neighbors dies, as if by overcrowding.\\n4. Any dead cell with exactly three live neighbors becomes a live cell, as if by reproduction.\\n5. No wrap around. Beyond edge is zero. Eight Neighbors.]]\u003e\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eDetermine the Zoo of life at its previous Snapshot. The output must evolve to the given Zoo Snapshot. It is a Zoo since all animals are in their private enclosures and do not interact with any other animals. Animals are stable or cyclic oscillators.\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 Zoo , an [m,n] 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\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 ZooPre, image of the Zoo at its prior iteration\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[Zoo        ZooPre\\n0000000000  000000000  \\n0110000100  011000000  \\n0110000100  011001110  \\n0000000100  000000000  \\n0000000000  000000000]]\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\u003eAdditional References:\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.conwaylife.com/wiki/Oscillator\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eOscillators\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\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:hyperlink w:docLocation=\\\"http://www.conwaylife.com/wiki/Still_life\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eStill Life\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\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\u003eNext:\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e Small Island - Prior Snapshot Prediction\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":1976,"title":"Kaggle: Reverse Game of Life - Create Isle prior State","description":"\u003chttp://www.kaggle.com/c/conway-s-reverse-game-of-life Kaggle's Conway's Reverse Game of Life\u003e contest inspires this Isle Single Evolution step in Life challenge. The kaggle contest runs from Oct-14-2013 thru Mar-02-2014. References: \u003chttp://mathworld.wolfram.com/GameofLife.html Game of Life at Wolfram\u003e. \u003chttp://en.wikipedia.org/wiki/Conway%27s_Game_of_Life Wiki Life\u003e.\r\n\r\n\r\n  1. Any live cell with fewer than two live neighbors dies, as if caused by under-population.\r\n  2. Any live cell with two or three live neighbors lives on to the next generation.\r\n  3. Any live cell with more than three live neighbors dies, as if by overcrowding.\r\n  4. Any dead cell with exactly three live neighbors becomes a live cell, as if by reproduction.\r\n5. No wrap around. Beyond edge is zero. Eight Neighbors.\r\n\r\n\r\nCreate an Isle that will evolve in one step to the given Isle state. An Isle is a morphing matrix that may contain non-periodic and interacting animals. The Isle is pre-evolved and will have \u003e10 live cells. Multiple solutions are possible. Imperfect solutions are allowed. This is a Performance Challenge.\r\n\r\n*Input:* Isle, 10x10 binary, 100 Isles\r\n\r\n*Output:* Isle_Predict,  a matrix that will evolve to Isle\r\n\r\n*Scoring:* 1000 * ( Errors / Total_points )\r\n\r\n*Example:*  (Errors=0 and 100 Total Points)\r\n\r\n  Isle        Isle_Predict\r\n\r\n  0001000000 0001000000\r\n  0010100000 0010100000\r\n  0100100000 0100100000\r\n  1101100000 0101000000\r\n  0100100000 0111000000\r\n  0000101000 0010100000\r\n  0010111100 0010111100\r\n  0001000010 0001000100\r\n  0000110100 0000111100\r\n  0000110000 0000010000\r\n\r\n*Next:* Isle_Predict vs Actual Specific Isle Predecessor. Study in Probability.","description_html":"\u003cp\u003e\u003ca href = \"http://www.kaggle.com/c/conway-s-reverse-game-of-life\"\u003eKaggle's Conway's Reverse Game of Life\u003c/a\u003e contest inspires this Isle Single Evolution step in Life challenge. The kaggle contest runs from Oct-14-2013 thru Mar-02-2014. References: \u003ca href = \"http://mathworld.wolfram.com/GameofLife.html\"\u003eGame of Life at Wolfram\u003c/a\u003e. \u003ca href = \"http://en.wikipedia.org/wiki/Conway%27s_Game_of_Life\"\u003eWiki Life\u003c/a\u003e.\u003c/p\u003e\u003cpre class=\"language-matlab\"\u003e1. Any live cell with fewer than two live neighbors dies, as if caused by under-population.\r\n2. Any live cell with two or three live neighbors lives on to the next generation.\r\n3. Any live cell with more than three live neighbors dies, as if by overcrowding.\r\n4. Any dead cell with exactly three live neighbors becomes a live cell, as if by reproduction.\r\n5. No wrap around. Beyond edge is zero. Eight Neighbors.\r\n\u003c/pre\u003e\u003cp\u003eCreate an Isle that will evolve in one step to the given Isle state. An Isle is a morphing matrix that may contain non-periodic and interacting animals. The Isle is pre-evolved and will have \u0026gt;10 live cells. Multiple solutions are possible. Imperfect solutions are allowed. This is a Performance Challenge.\u003c/p\u003e\u003cp\u003e\u003cb\u003eInput:\u003c/b\u003e Isle, 10x10 binary, 100 Isles\u003c/p\u003e\u003cp\u003e\u003cb\u003eOutput:\u003c/b\u003e Isle_Predict,  a matrix that will evolve to Isle\u003c/p\u003e\u003cp\u003e\u003cb\u003eScoring:\u003c/b\u003e 1000 * ( Errors / Total_points )\u003c/p\u003e\u003cp\u003e\u003cb\u003eExample:\u003c/b\u003e  (Errors=0 and 100 Total Points)\u003c/p\u003e\u003cpre class=\"language-matlab\"\u003eIsle        Isle_Predict\r\n\u003c/pre\u003e\u003cpre class=\"language-matlab\"\u003e0001000000 0001000000\r\n0010100000 0010100000\r\n0100100000 0100100000\r\n1101100000 0101000000\r\n0100100000 0111000000\r\n0000101000 0010100000\r\n0010111100 0010111100\r\n0001000010 0001000100\r\n0000110100 0000111100\r\n0000110000 0000010000\r\n\u003c/pre\u003e\u003cp\u003e\u003cb\u003eNext:\u003c/b\u003e Isle_Predict vs Actual Specific Isle Predecessor. Study in Probability.\u003c/p\u003e","function_template":"function Isle_Predict=predict_prior_isle(Isle)\r\n% Given Isle find prior Isle state to produce minimal Isle errors\r\n Isle_Predict=zeros(size(Isle));\r\nend","test_suite":"assignin('caller','score',200);\r\n%%\r\ntic\r\nN=10; % size of Isle\r\nQisles=100; % Qty of Isles to process to find error rate\r\n\r\nrng(15,'twister'); % creation fixed set of Isles\r\n\r\nvalid=0; \r\n\r\nwhile valid\u003cQisles\r\n q=floor(N/4)+randi(floor(N*N/4));\r\n idx=randperm(N*N);\r\n \r\n mb=zeros(N);\r\n mb(idx(1:q))=1;\r\n\r\n for i=1:10 % Pre-Evolve\r\n  %mb0=mb;\r\n  mc=conv2(single(mb),[1 1 1;1 0 1;1 1 1],'same');\r\n  mb=~(mc\u003c2 | mc\u003e3) \u0026 ((mb \u0026 mc==2) | (mb \u0026 mc==3) | (~mb \u0026 mc==3)); \r\n end\r\n\r\n if nnz(mb)\u003eN*N/10  % Avoid empty isles\r\n%   figure(1);imagesc(mb0)\r\n%   figure(2);imagesc(mb)\r\n  valid=valid+1;\r\n  Isle{valid}=mb;\r\n  \r\n end\r\n\r\nend % while  to create cases\r\n\r\nerrTot=0;\r\nfor i=1:Qisles\r\n\r\n isle_predict=predict_prior_isle(Isle{i});\r\n  \r\n m1=isle_predict; % Evolve to calculate errors\r\n mc=conv2(single(m1),[1 1 1;1 0 1;1 1 1],'same');\r\n isle_created=~(mc\u003c2 | mc\u003e3) \u0026 ((m1 \u0026 mc==2) | (m1 \u0026 mc==3) | (~m1 \u0026 mc==3));\r\n  \r\n err=N*N-nnz(Isle{i}==isle_created);\r\n errTot=errTot+err;\r\n\r\nend % Qisles\r\ntoc\r\nassignin('caller','score',min(200,floor(1000*errTot/N/N/Qisles)));\r\n","published":true,"deleted":false,"likes_count":2,"comments_count":6,"created_by":3097,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":17,"test_suite_updated_at":"2013-11-10T02:23:31.000Z","rescore_all_solutions":false,"group_id":1,"created_at":"2013-11-09T19:43:29.000Z","updated_at":"2026-02-13T15:23:16.000Z","published_at":"2013-11-09T20:16:15.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:hyperlink w:docLocation=\\\"http://www.kaggle.com/c/conway-s-reverse-game-of-life\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eKaggle's Conway's Reverse Game of Life\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e contest inspires this Isle Single Evolution step in Life challenge. The kaggle contest runs from Oct-14-2013 thru Mar-02-2014. References:\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://mathworld.wolfram.com/GameofLife.html\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eGame of Life at Wolfram\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\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:hyperlink w:docLocation=\\\"http://en.wikipedia.org/wiki/Conway%27s_Game_of_Life\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eWiki Life\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=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[1. Any live cell with fewer than two live neighbors dies, as if caused by under-population.\\n2. Any live cell with two or three live neighbors lives on to the next generation.\\n3. Any live cell with more than three live neighbors dies, as if by overcrowding.\\n4. Any dead cell with exactly three live neighbors becomes a live cell, as if by reproduction.\\n5. No wrap around. Beyond edge is zero. Eight Neighbors.]]\u003e\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eCreate an Isle that will evolve in one step to the given Isle state. An Isle is a morphing matrix that may contain non-periodic and interacting animals. The Isle is pre-evolved and will have \u0026gt;10 live cells. Multiple solutions are possible. Imperfect solutions are allowed. This is a Performance Challenge.\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 Isle, 10x10 binary, 100 Isles\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 Isle_Predict, a matrix that will evolve to Isle\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 1000 * ( Errors / Total_points )\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\u003cw:r\u003e\u003cw:t\u003e (Errors=0 and 100 Total Points)\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[Isle        Isle_Predict\\n\\n0001000000 0001000000\\n0010100000 0010100000\\n0100100000 0100100000\\n1101100000 0101000000\\n0100100000 0111000000\\n0000101000 0010100000\\n0010111100 0010111100\\n0001000010 0001000100\\n0000110100 0000111100\\n0000110000 0000010000]]\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\u003eNext:\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e Isle_Predict vs Actual Specific Isle Predecessor. Study in Probability.\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\"}]}"}],"no_progress_badge":{"id":53,"name":"Unknown","symbol":"unknown","description":"Partially completed groups","description_html":null,"image_location":"/images/responsive/supporting/matlabcentral/cody/badges/problem_groups_unknown_2.png","bonus":null,"players_count":0,"active":false,"created_by":null,"updated_by":null,"deleted_by":null,"deleted_at":null,"restored_by":null,"restored_at":null,"created_at":"2018-01-10T23:20:29.000Z","updated_at":"2018-01-10T23:20:29.000Z","community_badge_id":null,"award_multiples":false}}