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:
@@ -22,4 +22,5 @@ int? binarySearch(List<int> list, int item) {
|
|||||||
low = mid + 1;
|
low = mid + 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
|
|||||||
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);
|
||||||
|
}
|
||||||
21
04_quicksort/dart/04_quicksort/quicksort.dart
Normal file
21
04_quicksort/dart/04_quicksort/quicksort.dart
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
void main(List<String> args) {
|
||||||
|
final Stopwatch stopwatch = Stopwatch()..start();
|
||||||
|
print(quickSort([0, 21, 3, 1, 6, 5, 0, 81, 2, 14, 56, 32, 1, 9, 8]));
|
||||||
|
stopwatch.stop();
|
||||||
|
print(stopwatch.elapsedMilliseconds);
|
||||||
|
}
|
||||||
|
|
||||||
|
List<int> quickSort(List<int> toOrder) {
|
||||||
|
if (toOrder.length < 2) {
|
||||||
|
return toOrder;
|
||||||
|
}
|
||||||
|
final int mid = toOrder.length ~/ 2;
|
||||||
|
|
||||||
|
final int pivot = toOrder[mid];
|
||||||
|
toOrder.removeAt(mid);
|
||||||
|
final List<int> lowers =
|
||||||
|
List.from(toOrder.where((element) => element <= pivot));
|
||||||
|
final List<int> highers =
|
||||||
|
List.from(toOrder.where((element) => element > pivot));
|
||||||
|
return quickSort(lowers) + [pivot] + quickSort(highers);
|
||||||
|
}
|
||||||
15
05_hash_tables/dart/01_price_of_groceries.dart
Normal file
15
05_hash_tables/dart/01_price_of_groceries.dart
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
void main(List<String> args) {
|
||||||
|
final book = <String, double>{};
|
||||||
|
book.addAll(
|
||||||
|
{
|
||||||
|
'apple': 0.67,
|
||||||
|
'milk': 1.49,
|
||||||
|
'avocado': 1.49,
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
print(book);
|
||||||
|
print(book['apple']);
|
||||||
|
print(book['milk']);
|
||||||
|
print(book['avocado']);
|
||||||
|
}
|
||||||
16
05_hash_tables/dart/02_check_voter.dart
Normal file
16
05_hash_tables/dart/02_check_voter.dart
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
void main(List<String> args) {
|
||||||
|
final voted = <String, bool>{};
|
||||||
|
|
||||||
|
checkVoter('tom', voted);
|
||||||
|
checkVoter('mike', voted);
|
||||||
|
checkVoter('mike', voted);
|
||||||
|
}
|
||||||
|
|
||||||
|
void checkVoter(String name, Map<String, bool> voted) {
|
||||||
|
if (voted[name] != null) {
|
||||||
|
print('Kick them out!');
|
||||||
|
} else {
|
||||||
|
voted[name] = true;
|
||||||
|
print('Let them vote');
|
||||||
|
}
|
||||||
|
}
|
||||||
42
06_breadth-first_search/dart/01_breadth-first_search.dart
Normal file
42
06_breadth-first_search/dart/01_breadth-first_search.dart
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
import 'dart:collection';
|
||||||
|
|
||||||
|
void main(List<String> args) {
|
||||||
|
final graph = <String, List<String>>{};
|
||||||
|
graph.addAll(
|
||||||
|
<String, List<String>>{
|
||||||
|
'you': ['alice', 'bob', 'claire'],
|
||||||
|
'bob': ['anuj', 'peggy'],
|
||||||
|
'alice': ['peggy'],
|
||||||
|
'claire': ['thom', 'jonny'],
|
||||||
|
'anuj': [],
|
||||||
|
'peggy': [],
|
||||||
|
'thom': [],
|
||||||
|
'jonny': [],
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
search(graph, 'you');
|
||||||
|
}
|
||||||
|
|
||||||
|
bool search(Map<String, List<String>> graph, String name) {
|
||||||
|
final searchQueue = Queue()..addAll(graph[name] ?? []);
|
||||||
|
final searched = List<String>.empty(growable: true);
|
||||||
|
|
||||||
|
while (searchQueue.isNotEmpty) {
|
||||||
|
final String person = searchQueue.removeFirst();
|
||||||
|
if (searched.contains(person) == false) {
|
||||||
|
if (_personIsSeller(person)) {
|
||||||
|
print('$person is a Mango seller!');
|
||||||
|
return true;
|
||||||
|
} else {
|
||||||
|
searchQueue.addAll(graph[person] ?? []);
|
||||||
|
searched.add(person);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool _personIsSeller(String name) {
|
||||||
|
return name.endsWith('m');
|
||||||
|
}
|
||||||
72
07_dijkstras_algorithm/dart/01_djikstra_algorithm.dart
Normal file
72
07_dijkstras_algorithm/dart/01_djikstra_algorithm.dart
Normal file
@@ -0,0 +1,72 @@
|
|||||||
|
void main(List<String> args) {
|
||||||
|
final graph = <String, Map<String, double>>{}..addAll(
|
||||||
|
{
|
||||||
|
'start': {
|
||||||
|
'a': 6,
|
||||||
|
'b': 2,
|
||||||
|
},
|
||||||
|
'a': {
|
||||||
|
'end': 1,
|
||||||
|
},
|
||||||
|
'b': {
|
||||||
|
'a': 3,
|
||||||
|
'end': 5,
|
||||||
|
},
|
||||||
|
'end': {},
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
final costs = <String, double>{
|
||||||
|
'a': 6,
|
||||||
|
'b': 2,
|
||||||
|
'end': double.infinity,
|
||||||
|
};
|
||||||
|
|
||||||
|
final parents = <String, String?>{
|
||||||
|
'a': 'start',
|
||||||
|
'b': 'start',
|
||||||
|
'end': null,
|
||||||
|
};
|
||||||
|
|
||||||
|
djikstra(graph, costs, parents);
|
||||||
|
print(graph);
|
||||||
|
print(costs);
|
||||||
|
print(parents);
|
||||||
|
}
|
||||||
|
|
||||||
|
void djikstra(
|
||||||
|
Map<String, Map<String, double>> graph,
|
||||||
|
Map<String, double> costs,
|
||||||
|
Map<String, String?> parents,
|
||||||
|
) {
|
||||||
|
final processeds = <String>[];
|
||||||
|
String? node = findTheCheapestOne(costs, processeds);
|
||||||
|
|
||||||
|
while (node != null) {
|
||||||
|
final cost = costs[node];
|
||||||
|
final neighbors = graph[node];
|
||||||
|
for (String neighbor in neighbors!.keys) {
|
||||||
|
final double newCost = cost! + neighbors[neighbor]!;
|
||||||
|
if (costs[neighbor]! > newCost) {
|
||||||
|
costs[neighbor] = newCost;
|
||||||
|
parents[neighbor] = node;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
processeds.add(node);
|
||||||
|
node = findTheCheapestOne(costs, processeds);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
String? findTheCheapestOne(Map<String, double> costs, List<String> processed) {
|
||||||
|
double cheapestCost = double.infinity;
|
||||||
|
String? cheapestNode;
|
||||||
|
|
||||||
|
for (String node in costs.keys) {
|
||||||
|
final double cost = costs[node]!;
|
||||||
|
if (cost < cheapestCost && !processed.contains(node)) {
|
||||||
|
cheapestCost = cost;
|
||||||
|
cheapestNode = node;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return cheapestNode;
|
||||||
|
}
|
||||||
52
08_greedy_algorithms/dart/01_set_covering.dart
Normal file
52
08_greedy_algorithms/dart/01_set_covering.dart
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
void main(List<String> args) {
|
||||||
|
final fruits = {'avocado', 'tomato', 'banana'};
|
||||||
|
final vegetables = {'beet', 'carrot', 'tomato'};
|
||||||
|
|
||||||
|
print(fruits.union(vegetables));
|
||||||
|
print(fruits.intersection(vegetables));
|
||||||
|
print(fruits.difference(vegetables));
|
||||||
|
print(vegetables.difference(fruits));
|
||||||
|
|
||||||
|
final coverStates = {
|
||||||
|
'mt',
|
||||||
|
'wa',
|
||||||
|
'or',
|
||||||
|
'id',
|
||||||
|
'nv',
|
||||||
|
'ut',
|
||||||
|
'ca',
|
||||||
|
'az',
|
||||||
|
};
|
||||||
|
|
||||||
|
final stations = <String, Set<String>>{}..addAll(
|
||||||
|
{
|
||||||
|
'kone': {'id', 'nv', 'uy'},
|
||||||
|
'ktwo': {'wa', 'id', 'mt'},
|
||||||
|
'kthree': {'or', 'nv', 'ca'},
|
||||||
|
'kfour': {'nv', 'ut'},
|
||||||
|
'kfive': {'ca', 'az'},
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
final finalStations = stationSet(coverStates, stations);
|
||||||
|
print(finalStations);
|
||||||
|
}
|
||||||
|
|
||||||
|
Set<String> stationSet(
|
||||||
|
Set<String> coverStates, Map<String, Set<String>> stations) {
|
||||||
|
final finalStations = <String>{};
|
||||||
|
while (coverStates.isNotEmpty) {
|
||||||
|
String? bestStation;
|
||||||
|
Set<String> coveredStates = {};
|
||||||
|
for (String station in stations.keys) {
|
||||||
|
final covered = coverStates.intersection(stations[station] ?? {});
|
||||||
|
if (covered.length > coveredStates.length) {
|
||||||
|
bestStation = station;
|
||||||
|
coveredStates = covered;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
coverStates.removeWhere((element) => coveredStates.contains(element));
|
||||||
|
finalStations.add(bestStation!);
|
||||||
|
}
|
||||||
|
return finalStations;
|
||||||
|
}
|
||||||
@@ -0,0 +1,27 @@
|
|||||||
|
void main(List<String> args) {
|
||||||
|
final table = longestCommonSubsequence('blue', 'clues');
|
||||||
|
|
||||||
|
for (List<int> element in table) {
|
||||||
|
print(element);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
List<List<int>> longestCommonSubsequence(String word1, String word2) {
|
||||||
|
final tableWord1 = word1.split('');
|
||||||
|
final tableWord2 = word2.split('');
|
||||||
|
final table = List.generate(
|
||||||
|
tableWord2.length, (index) => List<int>.filled(tableWord1.length, 0));
|
||||||
|
|
||||||
|
for (int i = 0; i < tableWord1.length; i++) {
|
||||||
|
for (int j = 0; j < tableWord2.length; j++) {
|
||||||
|
if (tableWord2[j] == tableWord1[i]) {
|
||||||
|
table[j][i] = (j - 1 >= 0 && i - 1 >= 0) ? table[j - 1][i - 1] + 1 : 1;
|
||||||
|
} else {
|
||||||
|
final top = (j - 1 >= 0) ? table[j - 1][i] : 0;
|
||||||
|
final left = (i - 1 >= 0) ? table[j][i - 1] : 0;
|
||||||
|
table[j][i] = (top > left) ? top : left;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return table;
|
||||||
|
}
|
||||||
25
09_dynamic_programming/dart/02_longest_common_substring.dart
Normal file
25
09_dynamic_programming/dart/02_longest_common_substring.dart
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
void main(List<String> args) {
|
||||||
|
final table = longestCommonSubstring('fish', 'hish');
|
||||||
|
|
||||||
|
for (List<int> element in table) {
|
||||||
|
print(element);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
List<List<int>> longestCommonSubstring(String word1, String word2) {
|
||||||
|
final tableWord1 = word1.split('');
|
||||||
|
final tableWord2 = word2.split('');
|
||||||
|
final table = List.generate(
|
||||||
|
tableWord2.length, (index) => List<int>.filled(tableWord1.length, 0));
|
||||||
|
|
||||||
|
for (int i = 0; i < tableWord1.length; i++) {
|
||||||
|
for (int j = 0; j < tableWord2.length; j++) {
|
||||||
|
if (tableWord2[j] == tableWord1[i]) {
|
||||||
|
table[j][i] = table[j - 1][j - 1] + 1;
|
||||||
|
} else {
|
||||||
|
table[j][i] = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return table;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user