D solutions (#305)
* Added 1 and 2 chapters for D lang * [D] Added recursion code All examples were ported from the Python code directly to D. Naming convention of Dlang is not allowing to run code files, which started with numbers. To run examples rename the files * [D] quicksort code * [D] Added hashtable example Based on the Python code * Create 01_breadth_first_search.d Added D example for breadth-first search * Create 01_filesystem_dfs.d Filesystem example in D * Create 01_dijkstras_algorithm.d Added Dijkstras algorithm implementation for D * Create 01_set_covering.d Added greedy algorythm for D * Create 01_longest_common_sub.d Added dynamic programming example for D language. The code is based on Rust example * Added modules definition Required to run code with names starting with numbers * Fixed proper unsigned handling
This commit is contained in:
42
06_breadth-first_search/d/01_breadth_first_search.d
Normal file
42
06_breadth-first_search/d/01_breadth_first_search.d
Normal file
@@ -0,0 +1,42 @@
|
||||
module app;
|
||||
|
||||
import std;
|
||||
|
||||
bool personIsSeller(string name) {
|
||||
return name[name.length - 1] == 'm';
|
||||
}
|
||||
|
||||
bool search(string name) {
|
||||
auto search_queue = DList!string();
|
||||
search_queue.insertBack(name);
|
||||
bool[string] searched;
|
||||
while (!search_queue.empty) {
|
||||
auto person = search_queue.front();
|
||||
search_queue.removeFront();
|
||||
auto found = person in searched;
|
||||
if (found !is null)
|
||||
continue;
|
||||
if (personIsSeller(person)) {
|
||||
writeln(person, " is a mango seller!");
|
||||
return true;
|
||||
}
|
||||
search_queue.insertBack(graph[person]);
|
||||
searched[person] = true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
string[][string] graph;
|
||||
|
||||
void main() {
|
||||
graph["you"] = ["alice", "bob", "claire"];
|
||||
graph["bob"] = ["anuj", "peggy"];
|
||||
graph["alice"] = ["peggy"];
|
||||
graph["claire"] = ["thom", "jonny"];
|
||||
graph["anuj"] = [];
|
||||
graph["peggy"] = [];
|
||||
graph["thom"] = [];
|
||||
graph["jonny"] = [];
|
||||
|
||||
search("you");
|
||||
}
|
||||
Reference in New Issue
Block a user