Created with <3 with dartpad.dev.
Created
August 1, 2023 16:57
-
-
Save ervinod/878be0c14ce076dfc2d595e85feeac5e to your computer and use it in GitHub Desktop.
problem-4
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| void main() { | |
| // Given the a list of unique numbers sorted in ascending order, | |
| // return a new list so that the values increment by 1 for each index from | |
| // the minimum value up to the maximum value (both included). | |
| // Example | |
| // Input: 1,3,5,6,7,8 | |
| // Output: 1,2,3,4,5,6,7,8 | |
| String pipeFix1(List<int> numbers) { | |
| String output = ''; | |
| for (int i = 0; i < numbers.length; i++) { | |
| int next = numbers[i] + 1; | |
| if (!numbers.contains(next)) { | |
| output = | |
| "${output.isNotEmpty ? "$output," : ''} ${numbers[i]}, ${numbers[i] + 1}"; | |
| } else { | |
| output = "${output.isNotEmpty ? "$output," : ''} ${numbers[i]}"; | |
| } | |
| } | |
| return output; | |
| } | |
| List<int> pipeFix2(List<int> numbers) { | |
| int min = numbers[0]; | |
| int max = numbers[numbers.length - 1]; | |
| int size = max - min + 1; | |
| List<int> result = List.generate(size, (int index) => index); | |
| for (int i = 0; i < size; i++) { | |
| result[i] = i + min; | |
| } | |
| return result; | |
| } | |
| List<int> input = [1, 3, 5, 6, 7, 8]; | |
| final output = pipeFix1(input); | |
| //final output = pipeFix2(input); | |
| print(output.toString()); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment