From 3e99cccfc08b22895de53218e94401504f94a6b0 Mon Sep 17 00:00:00 2001 From: Gabriel Santos <87038139+01Explorer@users.noreply.github.com> Date: Tue, 8 Aug 2023 20:10:15 -0300 Subject: [PATCH] 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 --- .../dart/binary_search.dart | 1 + 03_recursion/dart/01_countdown/countdown.dart | 14 ++++ 03_recursion/dart/02_sum/sum.dart | 14 ++++ 03_recursion/dart/03_factorial/factorial.dart | 13 ++++ 04_quicksort/dart/04_quicksort/quicksort.dart | 21 ++++++ .../dart/01_price_of_groceries.dart | 15 ++++ 05_hash_tables/dart/02_check_voter.dart | 16 +++++ .../dart/01_breadth-first_search.dart | 42 +++++++++++ .../dart/01_djikstra_algorithm.dart | 72 +++++++++++++++++++ .../dart/01_set_covering.dart | 52 ++++++++++++++ .../dart/01_longest_common_subsequence.dart | 27 +++++++ .../dart/02_longest_common_substring.dart | 25 +++++++ 12 files changed, 312 insertions(+) create mode 100644 03_recursion/dart/01_countdown/countdown.dart create mode 100644 03_recursion/dart/02_sum/sum.dart create mode 100644 03_recursion/dart/03_factorial/factorial.dart create mode 100644 04_quicksort/dart/04_quicksort/quicksort.dart create mode 100644 05_hash_tables/dart/01_price_of_groceries.dart create mode 100644 05_hash_tables/dart/02_check_voter.dart create mode 100644 06_breadth-first_search/dart/01_breadth-first_search.dart create mode 100644 07_dijkstras_algorithm/dart/01_djikstra_algorithm.dart create mode 100644 08_greedy_algorithms/dart/01_set_covering.dart create mode 100644 09_dynamic_programming/dart/01_longest_common_subsequence.dart create mode 100644 09_dynamic_programming/dart/02_longest_common_substring.dart diff --git a/01_introduction_to_algorithms/dart/binary_search.dart b/01_introduction_to_algorithms/dart/binary_search.dart index a0bcf91..6204139 100644 --- a/01_introduction_to_algorithms/dart/binary_search.dart +++ b/01_introduction_to_algorithms/dart/binary_search.dart @@ -22,4 +22,5 @@ int? binarySearch(List list, int item) { low = mid + 1; } } + return null; } diff --git a/03_recursion/dart/01_countdown/countdown.dart b/03_recursion/dart/01_countdown/countdown.dart new file mode 100644 index 0000000..9c93f39 --- /dev/null +++ b/03_recursion/dart/01_countdown/countdown.dart @@ -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); +} diff --git a/03_recursion/dart/02_sum/sum.dart b/03_recursion/dart/02_sum/sum.dart new file mode 100644 index 0000000..b1d4c8a --- /dev/null +++ b/03_recursion/dart/02_sum/sum.dart @@ -0,0 +1,14 @@ +void main(List 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 array) { + if (array.isEmpty) { + return 0; + } + final List newArray = [...array]..removeAt(0); + return array[0] + recursiveSum(newArray); +} diff --git a/03_recursion/dart/03_factorial/factorial.dart b/03_recursion/dart/03_factorial/factorial.dart new file mode 100644 index 0000000..b6bce64 --- /dev/null +++ b/03_recursion/dart/03_factorial/factorial.dart @@ -0,0 +1,13 @@ +void main(List 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); +} diff --git a/04_quicksort/dart/04_quicksort/quicksort.dart b/04_quicksort/dart/04_quicksort/quicksort.dart new file mode 100644 index 0000000..a780307 --- /dev/null +++ b/04_quicksort/dart/04_quicksort/quicksort.dart @@ -0,0 +1,21 @@ +void main(List 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 quickSort(List toOrder) { + if (toOrder.length < 2) { + return toOrder; + } + final int mid = toOrder.length ~/ 2; + + final int pivot = toOrder[mid]; + toOrder.removeAt(mid); + final List lowers = + List.from(toOrder.where((element) => element <= pivot)); + final List highers = + List.from(toOrder.where((element) => element > pivot)); + return quickSort(lowers) + [pivot] + quickSort(highers); +} diff --git a/05_hash_tables/dart/01_price_of_groceries.dart b/05_hash_tables/dart/01_price_of_groceries.dart new file mode 100644 index 0000000..1aff6ad --- /dev/null +++ b/05_hash_tables/dart/01_price_of_groceries.dart @@ -0,0 +1,15 @@ +void main(List args) { + final book = {}; + book.addAll( + { + 'apple': 0.67, + 'milk': 1.49, + 'avocado': 1.49, + }, + ); + + print(book); + print(book['apple']); + print(book['milk']); + print(book['avocado']); +} diff --git a/05_hash_tables/dart/02_check_voter.dart b/05_hash_tables/dart/02_check_voter.dart new file mode 100644 index 0000000..b491294 --- /dev/null +++ b/05_hash_tables/dart/02_check_voter.dart @@ -0,0 +1,16 @@ +void main(List args) { + final voted = {}; + + checkVoter('tom', voted); + checkVoter('mike', voted); + checkVoter('mike', voted); +} + +void checkVoter(String name, Map voted) { + if (voted[name] != null) { + print('Kick them out!'); + } else { + voted[name] = true; + print('Let them vote'); + } +} diff --git a/06_breadth-first_search/dart/01_breadth-first_search.dart b/06_breadth-first_search/dart/01_breadth-first_search.dart new file mode 100644 index 0000000..4b741c4 --- /dev/null +++ b/06_breadth-first_search/dart/01_breadth-first_search.dart @@ -0,0 +1,42 @@ +import 'dart:collection'; + +void main(List args) { + final graph = >{}; + graph.addAll( + >{ + 'you': ['alice', 'bob', 'claire'], + 'bob': ['anuj', 'peggy'], + 'alice': ['peggy'], + 'claire': ['thom', 'jonny'], + 'anuj': [], + 'peggy': [], + 'thom': [], + 'jonny': [], + }, + ); + + search(graph, 'you'); +} + +bool search(Map> graph, String name) { + final searchQueue = Queue()..addAll(graph[name] ?? []); + final searched = List.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'); +} diff --git a/07_dijkstras_algorithm/dart/01_djikstra_algorithm.dart b/07_dijkstras_algorithm/dart/01_djikstra_algorithm.dart new file mode 100644 index 0000000..3561287 --- /dev/null +++ b/07_dijkstras_algorithm/dart/01_djikstra_algorithm.dart @@ -0,0 +1,72 @@ +void main(List args) { + final graph = >{}..addAll( + { + 'start': { + 'a': 6, + 'b': 2, + }, + 'a': { + 'end': 1, + }, + 'b': { + 'a': 3, + 'end': 5, + }, + 'end': {}, + }, + ); + + final costs = { + 'a': 6, + 'b': 2, + 'end': double.infinity, + }; + + final parents = { + 'a': 'start', + 'b': 'start', + 'end': null, + }; + + djikstra(graph, costs, parents); + print(graph); + print(costs); + print(parents); +} + +void djikstra( + Map> graph, + Map costs, + Map parents, +) { + final processeds = []; + 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 costs, List 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; +} diff --git a/08_greedy_algorithms/dart/01_set_covering.dart b/08_greedy_algorithms/dart/01_set_covering.dart new file mode 100644 index 0000000..de3a964 --- /dev/null +++ b/08_greedy_algorithms/dart/01_set_covering.dart @@ -0,0 +1,52 @@ +void main(List 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 = >{}..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 stationSet( + Set coverStates, Map> stations) { + final finalStations = {}; + while (coverStates.isNotEmpty) { + String? bestStation; + Set 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; +} diff --git a/09_dynamic_programming/dart/01_longest_common_subsequence.dart b/09_dynamic_programming/dart/01_longest_common_subsequence.dart new file mode 100644 index 0000000..ae621ab --- /dev/null +++ b/09_dynamic_programming/dart/01_longest_common_subsequence.dart @@ -0,0 +1,27 @@ +void main(List args) { + final table = longestCommonSubsequence('blue', 'clues'); + + for (List element in table) { + print(element); + } +} + +List> longestCommonSubsequence(String word1, String word2) { + final tableWord1 = word1.split(''); + final tableWord2 = word2.split(''); + final table = List.generate( + tableWord2.length, (index) => List.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; +} diff --git a/09_dynamic_programming/dart/02_longest_common_substring.dart b/09_dynamic_programming/dart/02_longest_common_substring.dart new file mode 100644 index 0000000..7a7d958 --- /dev/null +++ b/09_dynamic_programming/dart/02_longest_common_substring.dart @@ -0,0 +1,25 @@ +void main(List args) { + final table = longestCommonSubstring('fish', 'hish'); + + for (List element in table) { + print(element); + } +} + +List> longestCommonSubstring(String word1, String word2) { + final tableWord1 = word1.split(''); + final tableWord2 = word2.split(''); + final table = List.generate( + tableWord2.length, (index) => List.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; +}