Added Dart examples for chapter 3 to chapter 9 (#265)
* fix: corrected method return value following Dart's newest linter version * feat: added Dart recursion examples for chapter 3 * feat: added quicksort example in Dart for chapter 4 * feat: added examples in Dart for the chapter 5 * feat: added Dart example for chapter 6 bfs * feat: added djikstra example in Dart for chapter 7 * feat: added example of set covering in Dart for chapter 8 * feat: added examples for dynamic programming in dart for chapter 9
This commit is contained in:
14
03_recursion/dart/01_countdown/countdown.dart
Normal file
14
03_recursion/dart/01_countdown/countdown.dart
Normal file
@@ -0,0 +1,14 @@
|
||||
main() {
|
||||
final Stopwatch stopwatch = Stopwatch()..start();
|
||||
print(recursiveCount([0, 21, 3, 1, 6, 5, 81, 2, 14, 56, 32, 1, 9, 8]));
|
||||
stopwatch.stop();
|
||||
print(stopwatch.elapsedMilliseconds);
|
||||
}
|
||||
|
||||
int recursiveCount(List array) {
|
||||
if (array.isEmpty) {
|
||||
return 0;
|
||||
}
|
||||
final List newArray = [...array]..removeAt(0);
|
||||
return 1 + recursiveCount(newArray);
|
||||
}
|
||||
14
03_recursion/dart/02_sum/sum.dart
Normal file
14
03_recursion/dart/02_sum/sum.dart
Normal file
@@ -0,0 +1,14 @@
|
||||
void main(List<String> args) {
|
||||
final Stopwatch stopwatch = Stopwatch()..start();
|
||||
print(recursiveSum([0, 21, 3, 1, 6, 5, 81, 2, 14, 56, 32, 1, 9, 8]));
|
||||
stopwatch.stop();
|
||||
print(stopwatch.elapsedMilliseconds);
|
||||
}
|
||||
|
||||
int recursiveSum(List<int> array) {
|
||||
if (array.isEmpty) {
|
||||
return 0;
|
||||
}
|
||||
final List<int> newArray = [...array]..removeAt(0);
|
||||
return array[0] + recursiveSum(newArray);
|
||||
}
|
||||
13
03_recursion/dart/03_factorial/factorial.dart
Normal file
13
03_recursion/dart/03_factorial/factorial.dart
Normal file
@@ -0,0 +1,13 @@
|
||||
void main(List<String> args) {
|
||||
final Stopwatch stopwatch = Stopwatch()..start();
|
||||
print(recursiveFactorial(5));
|
||||
stopwatch.stop();
|
||||
print(stopwatch.elapsedMilliseconds);
|
||||
}
|
||||
|
||||
int recursiveFactorial(int value) {
|
||||
if (value == 1) {
|
||||
return 1;
|
||||
}
|
||||
return value * recursiveFactorial(value - 1);
|
||||
}
|
||||
Reference in New Issue
Block a user