Skip to content

Instantly share code, notes, and snippets.

@micedreams
Last active November 12, 2023 11:17
Show Gist options
  • Save micedreams/dfab3fe84a513c98ccd3afa56c7a2c5e to your computer and use it in GitHub Desktop.
Save micedreams/dfab3fe84a513c98ccd3afa56c7a2c5e to your computer and use it in GitHub Desktop.
void main() {
int resultFindNthSmallest;
int resultsumOf;
int resultPersistance;
resultFindNthSmallest = findNthSmallest([6, 2, 9, 1], 2);
print("findNthSmallest([6, 2, 9, 1], 2) =$resultFindNthSmallest");
resultFindNthSmallest =
findNthSmallest([57, 3, 0, -5, 2, 67, 32, 9, 1, 2], 5);
print(
"findNthSmallest([57, 3, 0, -5, 2, 67, 32, 9, 1, 2], 5) = $resultFindNthSmallest");
resultsumOf = sumOf([9, 34, -23, 0, 4, 8]);
print("sumOf([9, 34, -23, 0, 4, 8]) =$resultsumOf");
resultsumOf = sumOf([901, 492, -10223, 3791, 243, 2]);
print("sumOf([901, 492, -10223, 3791, 243, 2]) =$resultsumOf");
resultPersistance = multiplicativePersistenceOf(489);
print("multiplicativePersistenceOf(489) =$resultPersistance");
resultPersistance = multiplicativePersistenceOf(912);
print("multiplicativePersistenceOf(912) =$resultPersistance");
}
int findNthSmallest(List<int> integers, int nth) {
assert(integers.length >= nth);
int result = integers[0];
for(var i=0;i<integers.length;i++){
if (integers[i] < result) result = integers[i];
}
return result;
}
int sumOf(List<int> integers) {
int result = 0;
integers.map((e) => result = result + e).toList();
return result;
}
int multiplicativePersistenceOf(int value) {
assert(value > 0);
int temp = value;
int result = 0;
while (getLength(temp) > 1) {
temp = multiply(temp);
result++;
}
return result;
}
getLength(number) {
return "$number".length;
}
multiply(int number) {
int product = 1;
List<int> numberArray = number.toString().split("").map(int.parse).toList();
numberArray.map((e) => product = product * e).toList();
return product;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment