Splitting integer array into multiple chunks in different length in Dart - flutter

List<int> mainList = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16];
List<int> chukLengths= [2,4,4,4,2]
Expected Result:
[[1,2],[3,4,5,6],[7,8,9,10],[11,12,13,14],[15,16]]
I wanna split mainList to 5 chunks with mentioned chunkLength.
How to resolve it in Dart?

You can iterate over the array of your chunks and slice off necessary chunks off the main array. Each iteration your get the chunk itself by using sublist() and then you delete the chunk from the initial array using removeRange()
Here is the code:
List<int> mainList = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16];
List<int> chukLengths= [2,4,4,4,2];
List<List<int>> splitIntoChunks(List<int> list, List<int> chunks) {
List<List<int>> result = [];
for (int chunk in chunks) {
result.add(list.sublist(0, chunk));
list.removeRange(0, chunk);
}
return result;
}
void main() {
print(splitIntoChunks(mainList, chukLengths)); // [[1, 2], [3, 4, 5, 6], [7, 8, 9, 10], [11, 12, 13, 14], [15, 16]]
}

Related

How to map an array into a List of Vector2s in Flutter for FlutterFlame?

I need to convert an array from Firebase into a List of Vector2s in Flutter for FlutterFlame.
From what I gather from the comments you have an array of integers like this:
final numbers = [1, 2, 3, 4, 5, 6, 1, 43];
You can convert this to a list of Vector2 in many ways, this is one way to do it:
final vectors = [
for(int i = 0; i < numbers.length-1; i+=2)
Vector2(numbers[i].toDouble(), numbers[i+1].toDouble())
];

Creating 2D array in Dart where only row length is defined

I want to implement below piece of code(Java) in Dart/Flutter.
Could you please tell me how to achieve this.?
Here rows = 8, colums = not defined.
int[][] myArray = new int[8][];
Dart represents arrays in the form of List objects. A List is simply an ordered group of objects. ... Each element in the List is identified by a unique number called the index.
To get exactly what you want, you could use double List.generate, i.e.:
const cols = 31;
const rows = 12;
final array = List.generate(rows,
(i) => List.generate(cols + 1, (j) => i + j * cols + 1,
growable: false),growable: false);
array.forEach((row) {
print(row);
});
// [1, 32, 63, ...
// [2, 33, 64, ...
// [3, 34, 65, ...
// ...
There is also List.filled, where you can fill the array with some initial value when created. The following init the 2d array with 0s.
final array = List.generate(rows + 1, (i) => List.filled(cols + 1, 0,growable: false), growable: false);

Return List of integers from one String consisting of numbers and commas

Dart/Flutter:
I have one string that contains integers, spaces, and maybe commas. How do I convert this to a list of integers?
Example: var StringOfNumbers = one of these strings:
'1, 2, 4, 7,'
'1, 2, 4 7'
'1 2 4 7'
After conversion, all should output to
[1,2,4,7]
I've played with map, parse, and split but can't figure it out.
void main(List<String> args) {
var string = '1, 2, 4, 7,';
var matches = RegExp(r'\d+').allMatches(string);
var myList = matches.map<int>((e) => int.parse(e.group(0))).toList();
print(myList);
}
Result:
[1, 2, 4, 7]
Try this
final string = '1, 2, 4, 7, 1, 2, 4 7 1 2 4 7';
final list = string.split(' ').map((p) => int.tryParse(p.replaceAll(',', '')) ?? 0);

Best way to find the largest amount of consecutive integers in a sorted array in swift, preferably not using a for loop

Given an array of integers for example let array = [1, 3, 4, 7, 8, 9, 12, 14, 15]
What is the best way of finding the largest amount of consecutive integers preferably without using a for-in loop. If we would pass this array into a function it would return 3 as '7, 8, 9' is the largest amount of consecutive integers.
let array = [1, 3, 4, 7, 8, 9, 12, 14, 15]
func getMaxConsecutives(from array: [Int]) -> Int {
var maxCount = 1
var tempMaxCount = 1
var currentNumber = array[0]
for number in array {
if currentNumber == number - 1 {
tempMaxCount += 1
maxCount = tempMaxCount > maxCount ? tempMaxCount : maxCount
currentNumber = number
} else {
tempMaxCount = 1
currentNumber = number
}
}
return maxCount
}
getMaxConsecutives(from: array)
This works as intended but I would like a more efficient solution something that is not O(n).
I appreciate any creative answers.
You can do it like this:
let array = [1, 3, 4, 7, 8, 9, 12, 14, 15]
if let maxCount = IndexSet(array).rangeView.max(by: {$0.count < $1.count})?.count {
print("The largest amount of consecutive integers: \(maxCount)")
//prints 3
}
I think I can write it more tightly (basically as a one-liner):
let array = [1, 3, 4, 7, 8, 9, 12, 14, 15]
let (_,_,result) = array.reduce((-1000,1,0)) {
$1 == $0.0+1 ? ($1,$0.1+1,max($0.2,$0.1+1)) : ($1,1,$0.2)
}
print(result) // 3
But we are still looping through the entire array — so that we are O(n) — and there is no way to avoid that. After all, think about what your eye does as it scans the array looking for the answer: it scans the whole array.
(One way to achieve some savings: You could short-circuit the loop when we are not in the middle of a run and the maximum run so far is longer than what remains of the array! But the gain might not be significant.)

How to find duplicate elements length in array flutter?

I want to implement add to checkout in which number of items added is displayed. Plus button adds elements in list and minus removes elements from list. Goal is just to display particular items added and its quantity. I have added items in list want to count length of duplicate items. How we can do that in flutter?
here is your solution. [Null Safe]
void main() {
List<int> items = [1, 1, 1, 2, 3, 4, 5, 5];
Map<int, int> count = {};
items.forEach((i) => count[i] = (count[i] ?? 0) + 1);
print(count.toString()); // {1: 3, 2: 1, 3: 1, 4: 1, 5: 2}
}