본문 바로가기

DART23

1207. Vowel Count Return the number (count) of vowels in the given string. We will consider a, e, i, o, u as vowels for this Kata (but not y). The input string will only consist of lower case letters and/or spaces. Solution: int getCount(String inputStr){ List arr = []; for (String s in inputStr.toLowerCase().split('')) { if (["a","e","i","o","u"].contains(s)) { arr.add(s); } } return arr.length; } import "dart:c.. 2022. 12. 7.
1206. Sum of positive ou get an array of numbers, return the sum of all of the positives ones. Example [1,-4,7,12] => 1 + 7 + 12 = 20 Note: if there is nothing to sum, the sum is default to 0. Solution: int positiveSum(List arr) { int result = 0; for (int a in arr) { if (a > 0) { result += a; } } return result; } int positiveSum(List arr) { return arr.where((l) => l > 0).fold(0, (p, c) => p + c); } import "dart:math".. 2022. 12. 6.
1205. Sum of Cubes Write a function that takes a positive integer n, sums all the cubed values from 1 to n, and returns that sum. Assume that the input n will always be a positive integer. Examples: (Input --> output) 2 --> 9 (sum of the cubes of 1 and 2 is 1 + 8) 3 --> 36 (sum of the cubes of 1, 2, and 3 is 1 + 8 + 27) Solution: int sumCubes(int n) { int result = 0; for (int i=1; i i * i * i); return l.sum; } 2022. 12. 5.
1204. Odd or Even? Task: Given a list of integers, determine whether the sum of its elements is odd or even. Give your answer as a string matching "odd" or "even". If the input array is empty consider it as: [0] (array with a zero). Examples: Input: [0] Output: "even" Input: [0, 1, 4] Output: "odd" Input: [0, -1, -5] Output: "even" Solution: String oddOrEven(List array) { int result = _sum_array(array); return res.. 2022. 12. 4.
1203. String repeat Write a function that accepts an integer n and a string s as parameters, and returns a string of s repeated exactly n times. Examples (input -> output) 6, "I" -> "IIIIII" 5, "Hello" -> "HelloHelloHelloHelloHello" Solution: String repeatString(int n, String s) { String result = ''; for (int i=0; i s * n; 2022. 12. 4.
1202. Count by X 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 countBy(int x, int n) { List results = [.. 2022. 12. 2.