| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 0a08414 commit fba13bf
1 file changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,68 @@ | |||
| 1 | + import 'package:test/test.dart'; | ||
| 2 | + | ||
| 3 | + bool checkIsSubSequence(List<int> array, List<int> sequence) { | ||
| 4 | + if (array.isEmpty) { | ||
| 5 | + return false; | ||
| 6 | + } | ||
| 7 | + | ||
| 8 | + if (sequence.isEmpty) { | ||
| 9 | + return true; | ||
| 10 | + } | ||
| 11 | + int arrayIndex = 0; | ||
| 12 | + int sequenceIndex = 0; | ||
| 13 | + | ||
| 14 | + while (sequenceIndex < sequence.length && arrayIndex < array.length) { | ||
| 15 | + if (sequence[sequenceIndex] == array[arrayIndex]) { | ||
| 16 | + sequenceIndex += 1; | ||
| 17 | + } | ||
| 18 | + arrayIndex += 1; | ||
| 19 | + } | ||
| 20 | + return sequenceIndex == sequence.length; | ||
| 21 | + } | ||
| 22 | + | ||
| 23 | + void main() { | ||
| 24 | + List<int> array; | ||
| 25 | + List<int> sequence; | ||
| 26 | + | ||
| 27 | + test('test 1', () { | ||
| 28 | + array = [5, 1, 22, 25, 6, -1, 8, 10]; | ||
| 29 | + sequence = [1, 6, -1, 10]; | ||
| 30 | + expect(checkIsSubSequence(array, sequence), isTrue); | ||
| 31 | + }); | ||
| 32 | + | ||
| 33 | + test('test 2', () { | ||
| 34 | + array = [5, 1, 22, 25, 6, -1, 8, 10]; | ||
| 35 | + sequence = [5, -1, 8, 10]; | ||
| 36 | + expect(checkIsSubSequence(array, sequence), isTrue); | ||
| 37 | + }); | ||
| 38 | + | ||
| 39 | + test('test 3', () { | ||
| 40 | + array = [1, 1, 1, 1, 1]; | ||
| 41 | + sequence = [0, 0, 0, 0]; | ||
| 42 | + expect(checkIsSubSequence(array, sequence), isFalse); | ||
| 43 | + }); | ||
| 44 | + | ||
| 45 | + test('test 4', () { | ||
| 46 | + array = [1, 6, -1, 10]; | ||
| 47 | + sequence = [1, 6, -1, 10]; | ||
| 48 | + expect(checkIsSubSequence(array, sequence), isTrue); | ||
| 49 | + }); | ||
| 50 | + | ||
| 51 | + test('test 5', () { | ||
| 52 | + array = [1, 1, 6, 1]; | ||
| 53 | + sequence = [0]; | ||
| 54 | + expect(checkIsSubSequence(array, sequence), isFalse); | ||
| 55 | + }); | ||
| 56 | + | ||
| 57 | + test('test 6', () { | ||
| 58 | + array = []; | ||
| 59 | + sequence = [0]; | ||
| 60 | + expect(checkIsSubSequence(array, sequence), isFalse); | ||
| 61 | + }); | ||
| 62 | + | ||
| 63 | + test('test 7', () { | ||
| 64 | + array = [1, 1, 6, 1]; | ||
| 65 | + sequence = []; | ||
| 66 | + expect(checkIsSubSequence(array, sequence), isTrue); | ||
| 67 | + }); | ||
| 68 | + } | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments