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:
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');
|
||||
}
|
||||
Reference in New Issue
Block a user