MATLAB Answers

0

Integers without repeating consecutively

Asked by Jose Grimaldo on 15 Oct 2019
Latest activity Answered by Andrei Bobrov
on 17 Oct 2019
A pincode consists of N integers between 1 and 9. In a valid pincode, no integer is allowed to repeat consecutively. For example, 1, 4, 5, 5, 6, 7 is invalid because 5 occurs twice. I tested the example but it does not outputs what the example shows. Any suggestions.
Screenshot (142).png

  1 Comment

Insert code or attach the m-file. Make it easy for people to help you, not hard.

Sign in to comment.

2 Answers

Answer by Shivam Prasad on 17 Oct 2019
Edited by Shivam Prasad on 17 Oct 2019
 Accepted Answer

Hi Jose,
Check if the following code works for you:-
function [repPos, pinCodeFix] = pinCodeCheck(pinCode)
repPos = [];
pinCodeFix = [pinCode(1)];
for i=2:length(pinCode)
if pinCode(i-1) == pinCode(i)
repPos = [repPos i];
else
pinCodeFix = [pinCodeFix pinCode(i)];
end
end
end
Produces the output as:-
repPos =
3 5
pinCodeFix =
2 9 5 3

  0 Comments

Sign in to comment.


Answer by Andrei Bobrov
on 17 Oct 2019

function [repPos, pinCodeFix] = pinCodeCheck(pinCode)
lo = [true;diff(pinCode(:)) ~= 0];
repPos = find(~lo);
pinCodeFix = pinCode(lo);
end

  0 Comments

Sign in to comment.