Integers without repeating consecutively

33 ビュー (過去 30 日間)
Jose Grimaldo
Jose Grimaldo 2019 年 10 月 15 日
コメント済み: Blaine Warner 2021 年 10 月 8 日
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 件のコメント
Image Analyst
Image Analyst 2019 年 10 月 15 日
Insert code or attach the m-file. Make it easy for people to help you, not hard.

サインインしてコメントする。

採用された回答

Shivam Prasad
Shivam Prasad 2019 年 10 月 17 日
編集済み: Shivam Prasad 2019 年 10 月 17 日
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

その他の回答 (1 件)

Andrei Bobrov
Andrei Bobrov 2019 年 10 月 17 日
function [repPos, pinCodeFix] = pinCodeCheck(pinCode)
lo = [true;diff(pinCode(:)) ~= 0];
repPos = find(~lo);
pinCodeFix = pinCode(lo);
end
  1 件のコメント
Blaine Warner
Blaine Warner 2021 年 10 月 8 日
i don't think we are supposed to have learned any of that yet, if we are going in order of what we've learned how to impliment in the chapters, (and this homework requires using arrays which arent taught until the next chapter anyway, to the professor made these extra credit) all we are supposed to use is loops, arrays, and branches

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeSolver Outputs and Iterative Display についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by