코드로 우주평화

1202. Count by X 본문

나는 이렇게 학습한다/Algorithm & SQL

1202. Count by X

daco2020 2022. 12. 2. 23:19

Create a function with two arguments that will return an array of the first n multiples of x.

Assume both the given number and the number of times to count will be positive numbers greater than 0.

Return the results as an array or list ( depending on language ).

Examples

countBy(1,10) === [1,2,3,4,5,6,7,8,9,10]
countBy(2,5) === [2,4,6,8,10]



Solution:

List<int> countBy(int x, int n) {
  List<int> results = [];
  for (int i=1; i<=n; i++) {
    results.add(i*x);
  }
  return results;
}
List<int> countBy(int c, int x) => List.generate(x, (i) => (i + 1) * c);
List<int> countBy(int x, int n) {
  return [for(var i = 1; i<=n; i++) x*i];
}


'나는 이렇게 학습한다 > Algorithm & SQL' 카테고리의 다른 글

1204. Odd or Even?  (0) 2022.12.04
1203. String repeat  (0) 2022.12.04
1201. Is the string uppercase?  (0) 2022.12.01
1130. Square(n) Sum  (0) 2022.11.30
1129. Bin to Decimal  (0) 2022.11.29