how can i access element of list in dart [closed] - list

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I have list of constructors .eg,
final data = [
new sale(price: 5, numOfSales: 1),
new sale(price: 10, numOfSales: 2),]
how can loop on the list and access numOfSales to do some operation on it.

If you're trying to calculate total numOfSales from data array you can do it like this
You can use fold data.fold(0, (t, e) =>( t as int) + e.numOfSales)
void main() {
final data = [
new sale(price: 5, numOfSales: 1),
new sale(price: 10, numOfSales: 2),
];
final totalNumOfSales= data.fold(0, (t, e) =>( t as int) + e.numOfSales);
print(totalNumOfSales);
}
class sale{
final int price;
final int numOfSales;
sale({this.price=0, this.numOfSales=0});
#override
toString()=>'price: $price, numOfSales: $numOfSales';
}

Related

How can i find the integers that can be written as a sum of a power of 2, a power of 3 and a power of 5? C++ [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed last year.
The community reviewed whether to reopen this question last year and left it closed:
Original close reason(s) were not resolved
Improve this question
So the program must compute all the numbers that can be written as a sum of a power of 2, a power of 3 and a power of 5 below 5.000.000.
For example 42 = 16 + 1 + 25 = 2^4 + 3^0 + 5^2. Any idea how can I do this?
you can get all powers of 2 and all powers of 3 and all powers of 5 under 5.000.000. first Then you can try all combinations
vector<int> solve(){
const int M = 5000000;
vector<int> p_2={1},p_3={1},p_5={1};
while(p_2.back()*2<M)p_2.push_back(p_2.back()*2);
while(p_3.back()*3<M)p_3.push_back(p_3.back()*3);
while(p_5.back()*5<M)p_5.push_back(p_5.back()*5);
set<int> st;//to remove duplicates
for(auto power_of_2 :p_2){
for(auto power_of_3:p_3){
for(auto power_of_5:p_5){
If(power_of_2+power_of_3+power_of_5<M)
st.insert(power_of_2+power_of_3+power_of_5);
}
}
}
return vector<int>(st.begin(),st.end());
}

Dart program to group consecutive number from given numbers in a list<list> [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
Suppose you are given a list of numbers like this
[11,2,7,6,17,13,8,9,3,5,12]
The result will be a group of numbers list containing sub-list i.e.,
[[2,3],[5,6,7,8,9],[11,12,13],[17]]
Here is my solution to the problem:
List<List<int>> consecutive_groups(List<int> a) {
a.sort();
List<List<int>> result = [];
List<int> temp = [];
temp.add(a[0]);
for (int i = 0; i < a.length - 1; i++) {
if (a[i + 1] == a[i] + 1) {
temp.add(a[i + 1]);
} else {
result.add(temp);
temp = [];
temp.add(a[i + 1]);
}
}
result.add(temp);
return result;
}

c++ printing the complete square numbers in an array [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
How can i print all complete square numbers available in the array
this is my array:
int main()
{
int array[6];
cout<<"Enter 6 #'s: "<<endl;
for(int i=0; i<6; i++) {
cin>>array[i];
}
Here's the algorithm:
For each slot in the array do:
if value in the slot is complete square number, print it.
The difficult part is determining a perfect square.
Hint: use sqrt (square root) function.

Reading int values from a vector in c++ [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I have a problem to read a large int ( 1 to 10^100 ) into vector the problem is I cannot read it as numeric data-type and split it into the vector so I want a solution to read the number separately into the vector
Example:
45686469
vec[0] = 4
vec[1] = 5
...
vec[7] = 9
Here's one possible way to do it:
std::string yourinput;
cin>>yourinput; //capture your large number as a string
std::vector<char> vch;
for(size_t st=0;st<yourinput.length();++st)
{
vch.push_back(yourinput[st]); //move each character into the vector
}

Sum relative numbers C++ [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
There is any function to sum relative numbers?
Example
I Have 5 and -10 So the result should be: 15
or
-5 (+) 15 -> 20
-1 (+) 1 -> 2
There is any function in C++ to sum numbers like that?
Do you intend absolute values? You can use the abs function.
abs(-5) + abs(15) gives 20 as a result.
I don't know about such function, however you can simply create one: just add absolute values:
#include <iostream>
int sumAbs( int a, int b) {
return std::abs( a) + std::abs( b);
}
int main() {
int a = -5;
int b = 10;
std::cout << sumAbs( a, b); // 15
return 0;
}
Do you mean the difference? abs(15 - (-5)) also gives 20.