get all the naturals less than a number in ascending order - flutter

I'm making a quiz app, and the options are storage this way:
option1 = 'First option'
option2 = 'Second option'
option3 = 'Third option'
numberOfOptions = 3
As some questions will have 4 alternatives, others 2, I'm trying to create a method to dynamically retrieve the number of alternatives available.
For that, I have the numberOfOptions variable. Based on this number, I need to get all the natural numbers less than it until 1 (as in the example, [1, 2, 3]), where I need to add to 'option'. The final result must be:
['option1', 'option2', 'option3'];
For now, what I did was:
void options() {
int numberOfOptions = 3;
for (int i = 0; i < numberOfOptions; i++) {
print('option${i + 1}');
}
}
On the output I get option1, option2, option3, but I'm stuck trying to return it as a List

One elegant solution would be to make use of the List.generate() constructor and achieve our objective with just one line:
List<String>.generate(numberOfOptions, (int index) => 'option${index + 1}');
The List.generate() constructor takes two arguments:
length;
a generator function that has an index variable;
You can read more about it on the official documentation.
It can be useful as it's much easier to read and leaner than a for-loop.
As outlined by #jamesdlin, another option would be to use the collection-for:
List<String> options = [
for(int i = 0; i < numberOfOptions; i++) 'option${i + 1}'
];

You need to create a list in your options() functions and fill it in the foor loop like so:
List<String> options(int numberOfOptions) {
final List<String> options = [];
for (int i = 0; i < numberOfOptions; i++) {
print('option${i + 1}');
options[i] = 'option${i + 1}';
}
return options;
}

Related

Update or check a Custom List data where the list contains a given data

I am trying to update the data from a Custom List which gets the following
class NewMessageBadge {
final int index;
final String length;
final String chatRoomId;
NewMessageBadge({
this.index,
this.length,
this.chatRoomId,
});
}
and I later call it in the main page
List<NewMessageBadge> newMessageBadge = [];
I then added certain data using a for loop
String length = randomAlphaNumeric(12);
String chatRoomId = "room" + randomAlphaNumeric(12);
for (int i = 0; i < 5; i++) {
newMessageBadge.add(
NewMessageBadge(index: i, length: length, chatRoomId: chatRoomId));
}
I will then want to update the length where the index is lets say its 2, I tried using indexWhere as so:
newMessageBadge[
newMessageBadge.indexWhere((element) => element.index == 2)];
but can't change the specific length only of where the condition is met.
Also is there a way to check if after adding all the data, check if certain data is inside the list? I tried the following
newMessageBadge.contains(2);
But it keeps showing as false even when I know the condition should be true.

Flutter: Return list of Strings using single string in a specific format

I need to upload array of string to firestore. I need to return list of Strings using single string as input.
If the input is 'Firestore'.
I should return list like this:
[f,fi,fir,firs,first,firsto,firstor,firestore]
If the input is 'Google Cloud'.
I should return list like this:
[g,
go,
goo,
goo,
goog,
googl,
google,
c,
cl,
clo,
clou,
cloud]
For having [g, go, goo, goo, goog, googl, google, c, cl, clo, clou, cloud] as result
use this :
String test = 'Google Cloud';
List<String> chars = [];
for (var word in test.split(" ")) {
for (var i = 0; i <= word.length; i++) {
if (word.substring(0, i).isNotEmpty) {
chars.add(word.substring(0, i));
}
}
}
print(chars);
it's a good practice to share what you have already done. Here is a simple way of achieving what you want (excluding white spaces) :
String test = 'firestore';
List<String> chars = [];
for (var i = 0; i <= test.length; i++) {
if (test.substring(0, i).isNotEmpty) {
chars.add(test.substring(0, i));
}
}
print(chars);
The above prints [f, fi, fir, fire, fires, firest, firesto, firestor, firestore]

How to shuffling the order of a list from snapshot.docs from Stream in firestore [duplicate]

I'm looking every where on the web (dart website, stackoverflow, forums, etc), and I can't find my answer.
So there is my problem: I need to write a function, that print a random sort of a list, witch is provided as an argument. : In dart as well.
I try with maps, with Sets, with list ... I try the method with assert, with sort, I look at random method with Math on dart librabry ... nothing can do what I wana do.
Can some one help me with this?
Here some draft:
var element03 = query('#exercice03');
var uneliste03 = {'01':'Jean', '02':'Maximilien', '03':'Brigitte', '04':'Sonia', '05':'Jean-Pierre', '06':'Sandra'};
var alluneliste03 = new Map.from(uneliste03);
assert(uneliste03 != alluneliste03);
print(alluneliste03);
var ingredients = new Set();
ingredients.addAll(['Jean', 'Maximilien', 'Brigitte', 'Sonia', 'Jean-Pierre', 'Sandra']);
var alluneliste03 = new Map.from(ingredients);
assert(ingredients != alluneliste03);
//assert(ingredients.length == 4);
print(ingredients);
var fruits = <String>['bananas', 'apples', 'oranges'];
fruits.sort();
print(fruits);
There is a shuffle method in the List class. The methods shuffles the list in place. You can call it without an argument or provide a random number generator instance:
var list = ['a', 'b', 'c', 'd'];
list.shuffle();
print('$list');
The collection package comes with a shuffle function/extension that also supports specifying a sub range to shuffle:
void shuffle (
List list,
[int start = 0,
int end]
)
Here is a basic shuffle function. Note that the resulting shuffle is not cryptographically strong. It uses Dart's Random class, which produces pseudorandom data not suitable for cryptographic use.
import 'dart:math';
List shuffle(List items) {
var random = new Random();
// Go through all elements.
for (var i = items.length - 1; i > 0; i--) {
// Pick a pseudorandom number according to the list length
var n = random.nextInt(i + 1);
var temp = items[i];
items[i] = items[n];
items[n] = temp;
}
return items;
}
main() {
var items = ['foo', 'bar', 'baz', 'qux'];
print(shuffle(items));
}
You can use shuffle() with 2 dots like Vinoth Vino said.
List cities = ["Ankara","London","Paris"];
List mixed = cities..shuffle();
print(mixed);
// [London, Paris, Ankara]

CS50 pset 3: Tideman sort_pairs function

I need some assistance in understanding the logic behind this function. This is my current sort_pairs function in Tideman:
// Sort pairs in decreasing order by the strength of victory
void sort_pairs(void)
{
qsort(pairs, pair_count, sizeof(pair), compare);
return;
}
// Function for sort_pairs
int compare(const void *a, const void *b)
{
const pair *p1 = (const pair *) a;
const pair *p2 = (const pair *) b;
if (p1->winner < p2->winner)
{
return -1;
}
else if (p1->winner > p2->winner)
{
return 1;
}
else
{
return 0;
}
}
This does not clear check50 and I looked online to find how to approach this problem. It seems that most functions compare the values from the preferences array instead (eg preferences[pairs[i].winner][pairs[i].loser]) . My previous functions vote, record_preferences, and add_pairs all clear check50. I have not advanced beyond sort_pairs yet.
Why can't I compare the strength of victory directly from the pairs array instead since I already have the data stored there?
You don't need to make this so complex, you can use your own sorting here. Let's try a simple insertion sort-
void sort_pairs()
{
pair temp;
for (int i = 1, j; i < pair_count; i++)
{
temp = pairs[i];
j = i - 1;
for (; j >= 0 && preferences[pairs[j].winner][pairs[j].loser] < preferences[temp.winner][temp.loser]; j--)
{
pairs[j + 1] = pairs[j];
}
pairs[j + 1] = temp;
}
}
The pair struct looks like-
typedef struct
{
int winner;
int loser;
}
pair;
Explanation:-
We go through each pair of elements inside the pairs array - starting at 1 since I'm going to compare with the previous element (j = i - 1)
Now we check all the previous elements from the current element and compare them with the key - preferences[pairs[INDEX].winner][pairs[INDEX].loser]
This is the key you should be sorting by. preferences[WINNER_ID][LOSER_ID] means the amount of people that prefer WINNER_ID over LOSER_ID.
And that's pretty much it!, it's simply a insertion sort but the key is the important part.

Adding color to Netsuite suitelet's sublist row depending upon the result

Is there any way to add colors to a sublist's row depending upon a condition. I have loaded a saved search to show output on a sublist. But now I want to highlight the rows if the difference between todays date and audit date(search output) is more than 100 days.
var search = nlapiLoadSearch('customrecord_cseg_properties', 'customsearch52');
var columns=search.getColumns();
var sublist = form.addSubList('customsublist', 'staticlist', 'List of properties');
for(var i = 0; i< columns.length; i++){
sublist.addField('customcolumn'+i, 'text', columns[i].getLabel());
}
var result= search.runSearch();
var resultIndex = 0,resultStep = 1000,resultSet,resultSets = [];
do {
resultSet = result.getResults(resultIndex, resultIndex + resultStep);
resultSets = resultSets.concat(resultSet);
resultIndex = resultIndex + resultStep;
} while (resultSet.length > 0);
nlapiLogExecution('DEBUG','The Total number of rows is',resultSets.length);
for(var w= 0; w<resultSets.length ;w++){
for(var x=0; x<columns.length; x++){
var temp;
temp=resultSets[w].getText(columns[x]);
if(temp==null || temp==''){
temp=resultSets[w].getValue(columns[x]);
}
sublist.setLineItemValue('customcolumn'+x, Number(w)+1,temp);
}
I couldn't find any functions in UI Builder API for Netsuite for doing this. Please let me know if there is any other way to do this. Above is the code which I have used to display search result in suitelet.
There is no native api for that.
You can do it by mainpulating the DOM on the client script onInit function.
Just keep in mind that DOM manipulation are risky since they can break if NetSuite will chnage the DOM structure.