how to set condition in String Flutter - flutter

I am looping Text Field hundreds times
for (var i = 0; i < 100; i++)
my label Text is look like this labelText: '${i + 1}',
I want to set Condition if i is below 10 then add extra 0 before number
I did try with like this
'${i < 10 ? "0"i + 1 : i + 1}',
getting error Expected to Find '}'

Here is a another solution by using String's padLeft method.
for (var i = 0 ; i < 100 ; i++) {
print(i.toString().padLeft(2, '0'));
}

for (var i = 0; i < 100; i++){
print('${i+1 < 10 ? "0${i + 1}" : i + 1}');
}

Related

How to print star pattern using Dart [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 10 months ago.
Improve this question
Im trying to figure out to print star pattern using Dart language which implementing logic code. The existing code I use as indent so that the star have some space. Is this the right method to do so?
void main(){
for(int i = 0 ; i< 7; i++){
var stars='';
for(int j = (7-i); j > 1 ;j--) {
stars += ' ';
}
for(int j = 0; j <= i ;j++){
stars += '* ';
}
print(stars);
}
}
Here is my answer write in dartpad, change starWidth to adjust star size.
Idea is get string of the star and it padding per row then printing its.
EDIT: Updated description comment for each functional
void main() {
const starWidth = 7;
// return `*` or `space` if even/odd
starGenerator(i) => i % 2 == 0 ? "*" : " ";
// just return a string full of `space`
printPad(w) => " " * w;
// cause we need `space` between `*`, length is `w * 2 - 1`,
// return a string build of star
printStars(int w) => List.generate(w * 2 - 1, starGenerator).join('');
for (int row = 1; row <= starWidth; row++) {
// cause our width with space is `starWidth * 2`,
// padding left is `padding = (our width with space - star with with space) / 2`,
// but we only need print left side (/2) so the math is simple
// `padding = width - star with without space`
var padding = starWidth - row;
print("$row:" + printPad(padding) + printStars(row));
}
}
void main(){
for(int i = 0 ; i< 7; i++){
var stars='';
for(int j = (7-i); j > 1 ;j--) {
stars += ' ';
}
for(int j = 0; j <= i ;j++){
stars += '* ';
}
print(stars);
}
}

How to print in the console that figure?

I want to print that in the console:
*
**
***
**
*
so, my code is:
Scanner input = new Scanner(System.in);
int n = input.nextInt();
char c = '*';
if (1 < n && n < 20) {
for (int row = 1; row <= n; row++) {
for (int col = 1; col <= row; col++) {
System.out.print(c);
}
System.out.println();
}
any proposals how to finish?
You want to print "* **". Write this:
System.out.print("* **");
If my answer is not that what you excpected, than give us more information. What is your actual problem. See StackOverflowFAQ
If n is your "*" length,below code:
if (1 < n && n < 20) {
for (int row = 1; row <= n; row++) {
for (int col = 1; col <= row; col++) {
System.out.print(c);
}
System.out.println();
}
for (int row =1; row <= n; row++) {
for (int col = n-row; col >0 ; col--) {
System.out.print(c);
}
System.out.println();
}
}

Simple cryptographic puzzle [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm looking for a way to solve this crypt arithmetic problem of:
ROBERT + GERALD = DONALD
and potentially others as well, where each letter represents a digit.
How would you go about solving this by hand and how does that relate to solving it programmatically?
Thank you in advance
You can actually work this out as a sum:
robert
+ gerald
------
= donald
and use basic mathematical knowledge.
For example, there's no carry possible in the right column (6) and we have T + D = D. That means T must be zero.
Similarly, for column 5, there's no carry from column 6 and R + L = L means R is zero as well, and no carry to column 4.
Same with column 4, E + A = A so E is zero.
So we now have:
0ob000
+ g00ald
------
= donald
From there, we can infer from columns 3 and 1 that b==n and g==d and they (along with o/a/l/d) can be any value since every digit is being added to zero so there is no chance of carry anywhere. So let's just make them all one:
011000
+ 100111
------
= 111111
In fact, you could make them all zero and end up with 000000 + 000000 = 000000.
But that's hardly programming related, so let's make it so:
#include <stdio.h>
int main (void) {
int robert, gerald, donald;
for (int r = 0; r < 10; r++) {
for (int o = 0; o < 10; o++) {
for (int b = 0; b < 10; b++) {
for (int e = 0; e < 10; e++) {
for (int t = 0; t < 10; t++) {
for (int g = 0; g < 10; g++) {
for (int a = 0; a < 10; a++) {
for (int l = 0; l < 10; l++) {
for (int d = 0; d < 10; d++) {
for (int n = 0; n < 10; n++) {
robert = r * 100000 + o * 10000 + b * 1000 + e * 100 + r * 10 + t;
gerald = g * 100000 + e * 10000 + r * 1000 + a * 100 + l * 10 + d;
donald = d * 100000 + o * 10000 + n * 1000 + a * 100 + l * 10 + d;
if (robert + gerald == donald) {
printf (" %06d\n", robert);
printf ("+ %06d\n", gerald);
printf (" ------\n");
printf ("= %06d\n", donald);
printf ("........\n");
}
}
}
}
}
}
}
}
}
}
}
return 0;
}
That will give you a whole host of solutions.
And, before you complain that you cannot have repeated digits, there is no solution if that's the case, since mathematically both T and R must be zero, as shown in the original reasoning above. And you can prove this empirically with:
#include <stdio.h>
int main (void) {
int robert, gerald, donald;
for (int r = 0; r < 10; r++) {
for (int o = 0; o < 10; o++) {
if (o==r) continue;
for (int b = 0; b < 10; b++) {
if ((b==r) || (b==o)) continue;
for (int e = 0; e < 10; e++) {
if ((e==r) || (e==o) || (e==b)) continue;
for (int t = 0; t < 10; t++) {
if ((t==r) || (t==o) || (t==b) || (t==e)) continue;
for (int g = 0; g < 10; g++) {
if ((g==r) || (g==o) || (g==b) || (g==e) || (g==t)) continue;
for (int a = 0; a < 10; a++) {
if ((a==r) || (a==o) || (a==b) || (a==e) || (a==t) || (a==g)) continue;
for (int l = 0; l < 10; l++) {
if ((l==r) || (l==o) || (l==b) || (l==e) || (l==t) || (l==g) || (l==a)) continue;
for (int d = 0; d < 10; d++) {
if ((d==r) || (d==o) || (d==b) || (d==e) || (d==t) || (d==g) || (d==a) || (d==l)) continue;
for (int n = 0; n < 10; n++) {
if ((n==r) || (n==o) || (n==b) || (n==e) || (n==t) || (n==g) || (n==a) || (n==l) || (n==d)) continue;
robert = r * 100000 + o * 10000 + b * 1000 + e * 100 + r * 10 + t;
gerald = g * 100000 + e * 10000 + r * 1000 + a * 100 + l * 10 + d;
donald = d * 100000 + o * 10000 + n * 1000 + a * 100 + l * 10 + d;
if (robert + gerald == donald) {
printf (" %06d\n", robert);
printf ("+ %06d\n", gerald);
printf (" ------\n");
printf ("= %06d\n", donald);
printf ("........\n");
}
}
}
}
}
}
}
}
}
}
}
return 0;
}
which outputs no solutions.
Now DONALD + GERALD = ROBERT, that's a different matter but you can solve that simply by modifying the code above slightly, making the if statement into:
if (donald + gerald == robert) {
printf (" %06d\n", donald);
printf ("+ %06d\n", gerald);
printf (" ------\n");
printf ("= %06d\n", robert);
printf ("........\n");
}
and you get the single solution:
526485
+ 197485
------
= 723970

highchart - average value of serie for shown period

Using Highstock, I've a serie timestamp / value
with different rangeselectors (hour, day, week, month,...) or zoomX
I want to display the average value for the displayed time period.
Now, I can compute the average of the overall series data:
for (i = 0; i < chart.series[0].yData.length; i++) {
total += chart.series[0].yData[i];
}
seriesAvg = (total / chart.series[0].yData.length).toFixed(4); // fix decimal to 4 places
$('#report1').html('<b>Average:</b>: '+ seriesAvg);
How to compute the average only on the displayed datapoints ? And to refresh automatically after zoom ?
Thank you
Use series.processedYData or series.points.
I implemented the same but I notice that processedYdata is loading also one data more than the one showed at the screen.
for that reason i added one additional command to remove the first processedYData:
processedYData.splice(0, 1);
below is the final result of the function
function showStat(objHighStockchart) {
for (j = 0; j < (json_data.length); j++) {
var seriesAvg = 0,
processedYData = objHighStockchart.series[j].processedYData;
processedYData.splice(0, 1);
var seriesMin = Math.min.apply(null, processedYData);
var seriesMax = Math.max.apply(null, processedYData);
var i = 0
var total = 0;
console.log(processedYData);
for (i = 1; i < processedYData.length; i++) {
total += processedYData[i];
}
seriesAvg = (total / processedYData.length).toFixed(2); // fix decimal to 4 places
$('#container_stat' + j).html(
'<br>Statistics for ' + objHighStockchart.series[j].name + '<br>' +
'Total: ' + total + ' logs<br>' +
'Min: ' + seriesMin + ' logs<br>' +
'Avg: ' + seriesAvg + ' logs<br>' +
'Max: ' + seriesMax + ' logs<br>'
+ '---' + processedYData
);
}
};

coffeescript for in bug?

On coffeescript loop 'for'
eg.
if 1 < x, code like below:
console.debug i for i in [1..0]
Generated code is:
var i;
for (i = 1; i >= 0; i--) {
console.debug(i);
}
if 1 > x,code like below:
console.debug i for i in [1..2]
Generated code is:
var i;
for (i = 1; i <= 2; i++) {
console.debug(i);
}
If i want write that javascript.How to ?
for(var i=1;i<=0;i++){
console.debug(i);
}
Because i don't know the condition is greater than left side or less than left side.
But i just want it i++
What's wrong with me?
EDIT BELOW:
For coffeescript's feature,I add condition before the loop or add condition on for loop.
eg:
if x - y >=1
console.debug i for i in [1..x-y]
or
console.debug i for i in [1..x-y] and x-y >=1
That's my way.Some one have good advice?
It looks like you want to do this:
console.debug i for i in [1..x-y] by 1
Which gets compiled to:
var i, _i, _ref;
for (i = _i = 1, _ref = x - y; _i <= _ref; i = _i += 1) {
console.debug(i);
}
for(var i=1;i<=0;i++){
console.debug(i);
}
is equivalent to
var i = 1;
while(true) {
console.debug(i);
i++;
}
which in coffeescript is written as
i = 1
while true
console.debug(i);
i++;