How to get interpolation values between two list values in flutter - flutter

I have two different list values i want get the corresponding one list between value to other list between values. Please look at the list below. Here i have 1st list value of 11 and 15 here i need to find the between values of 12, 13 and 15.
11 value =22
15 value =30
List<double> list1 =[11, 15];
List<double> list2 =[22, 30];

If I understand correct you need the interpolation function to generate values between 2 points:
List<double> interpolate(double start, double end, int count) {
if (count < 2) {
throw Exception("interpolate: illegal count!");
}
final array = List.generate(count + 1, (index) => 0.0);
for (int i = 0; i <= count; ++i) {
array[i] = start + i * (end - start) / count;
}
return array;
}
Usage:
void main() {
final input = [1.0, 15.0];
print(interpolate(input.first, input.last, 4));
}
Result:
[1.0, 4.5, 8.0, 11.5, 15.0]

Related

Is there a better way to calculate the moving sum of a list in flutter

Is there a better way to calculate a moving sum of a list?
List<double?> rollingSum({int window = 3, List data = const []}) {
List<double?> sum = [];
int i = 0;
int maxLength = data.length - window + 1;
while (i < maxLength) {
List tmpData = data.getRange(i, i + window).toList();
double tmpSum = tmpData.reduce((a, b) => a + b);
sum.add(tmpSum);
i++;
}
// filling the first n values with null
i = 0;
while (i < window - 1) {
sum.insert(0, null);
i++;
}
return sum;
}
Well, the code is already clean for what you need. Maybe just some improvements like:
Use a for loop
You can use the method sublist which creates a "view" of a list, which is more efficient
To insert some values in the left/right of a list, there is a specific Dart method called padLeft, where you specify the lenght of the list which you want it to become (first parameter), then the value you want to use to fill it (second parameter). For example, if you have an array of N elements, and you want to fill it with X "null"s to the left, use padLeft(N+X, null).
List<double?> rollingSum({int window = 3, List data = const []}) {
List<double?> sum = [];
for (int i = 0; i < data.length - window + 1; i++) {
List tmpData = data.sublist(i, i + window);
double tmpSum = tmpData.reduce((a, b) => a + b);
sum.add(tmpSum);
}
sum.padLeft(window - 1, null);
return sum;
}
if I understand your problem correctly you can just calculate the window one time and in one loop you can for each iteration you can add the current element to the sum and subtract i - (window - 1)
so for an input like this
data = [1,2,3,4,5,6]
window = 3
the below code will result in [6,9,12,15]
int sum = 0;
List<double> res = [];
for (int i = 0;i<data.length;i++) {
sum += data[i];
if (i < window - 1) {
continue;
}
res.add(sum);
sum -= data[i - (window - 1)]; // remove element that got out of the window size
}
this way you won't have to use getRange nor sublist nor reduce as all of those are expensive functions in terms of time and space complexity

Flutter find the sum of digits of int value

I want to find the sum of the digits of the number entered in Flutter. I want to encode this algorithm.
for example
x=1992
result=1+9+9+2=21
how can i do this with flutter
You can do in this way.
import 'dart:io';
void main() {
print('Enter X');
int X = int.parse(stdin.readLineSync()!);
int result = 0;
for (int i = X; i > 0; i = (i / 10).floor()) {
result += (i % 10);
}
print('Sum of digits\n$result');
}
Output
Enter X
123456
Sum of digits
21
transform the number into an String using String stringValue = x.toString();
create an array from each char using List<String> result = stringValue.split('');
sum each number transforming back using int.parse(result)
void main(){
int x = 1992;
String stringValue = x.toString();
List<String> result = stringValue.split('');
int sum = 0;
for(int i = 0 ; i < result.length; i++) {
int value = int.parse(result[i]);
sum = sum + value;
}
print(sum);
}
Result: 21

How to create a random list of doubles (Flutter)?

List<AudioWaveBar> bars = [];
var list = List<double>.generate(100, (i) => i as double)..shuffle();
for (var i = 0; i <= list.length; i++) {
bars.add(AudioWaveBar(
heightFactor: list[i],
color: widget.podcast.percentPlayed >= ((i + 1).toDouble() / list.length.toDouble())
? AppTheme.greenStart
: Colors.white10
)); }
I need to have a list of random doubles only (from 0 to 1), but random function doesn't add the numbers to a list and generate function only accepts int numbers that can't be casted. Any help? thanks!
The function Random().nextDouble() generates a random double between 0 and 1.
So just by specifying a max value u can get what u want, like so :
int maxValue = 1000;
var list = List<double>.generate(100, (i) => Random().nextDouble() * maxValue)..shuffle();

again traverse the array in dart

I have an array of 5 elements:-
var a=[1,2,3,4,5];
and a variable which will have some integer value from firebase. So, what I want is that when this value is less than the length of array then it should print the value of array at that index i.e.
int valueFromFireBase=3;
print(a[valueFromFireBase]);
//Result should be 4
and when it is greater than the length of array then it should again traverse the array i.e.
if value is 6 then it should print 1.
The code i have tried is :-
int val=0;
var a=[1,2,3,4,5,6];
int valueFromFireBase=7;
if(valueFromFireBase>=a.length)
{
int divident=valueFromFireBase%a.length;
valueFromFireBase-=divident;
val=a[valueFromFireBase];
}
else
{
val=a[valueFromFireBase];
}
print(val);
But this code is not working when valueFromFireBase is equal to the array length.
You need to use a modulo operator:
val = a[valueFromFireBase % a.length];
Note that arrays indexes are 0-based so if valueFromFireBase == 7 you'll get the 8th element.
void main() {
final a = [1, 2, 3, 4, 5, 6];
final valueFromFireBase = 7;
int val = a[valueFromFireBase % a.length];
print(val);
}
https://dartpad.dev/59c42e89348e45ca433bcab1e3930572?null_safety=true
Modulo docs: https://api.dart.dev/be/138352/dart-core/double/operator_modulo.html
You can do simply like this:
int val=0;
var a=[1,2,3,4,5,6];
int valueFromFireBase=7;
if(valueFromFireBase>=a.length)
{
val=a[valueFromFireBase % a.length];
}
else
{
val=a[valueFromFireBase];
}
print(val);

Iterating over a list in groups of two or more

I like to iterate over a list and split them up in couples like this:
List<String> list = [1,2,3,4,5,6,7,8];
List<Tuple2> listOfTuples = list.take2((value1,value2) => Tuple2(value1,value2));
print(listOfTuples.toString()); // output => [[1,2],[3,4],[5,6],[7,8]]
I know there is a take(count) in dart but I did not find a good example.
I know I can do it with a for loop etc. but I am wondering if there us a more elegant way.
There is nothing built in. The way I'd write this today is:
var list = [1, 2, 3, 4, 5, 6, 7, 8];
var tuples = [
for (int i = 0; i < list.length - 1; i += 2) Tuple2(list[i], list[i + 1]),
];
You could write an extension that gives an api take2 on List that could be used in the way you describe.
extension Take2<T> on List<T> {
List<R> take2<R>(R Function(T, T) transform) => [
for (int i = 0; i < this.length - 1; i += 2)
transform(this[i], this[i + 1]),
];
}