Skip to content

Commit 9ff7468

Browse files
authored
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
1 parent df180e6 commit 9ff7468

22 files changed

+544
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
module app;
2+
3+
import std.stdio: writeln;
4+
import std.range: assumeSorted, SortedRange;
5+
6+
long binary_search(T)(SortedRange!(T[]) list, T item) {
7+
long low = 0;
8+
long high = list.length - 1;
9+
10+
while (low <= high) {
11+
auto mid = (low + high) / 2;
12+
T guess = list[mid];
13+
if (guess == item)
14+
return mid;
15+
else if (guess > item)
16+
high = mid - 1;
17+
else
18+
low = mid + 1;
19+
}
20+
return -1;
21+
}
22+
23+
void main() {
24+
auto my_list = [1, 3, 5, 7, 9];
25+
writeln(binary_search(assumeSorted(my_list), 3));
26+
writeln(binary_search(assumeSorted(my_list), -1));
27+
}
+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
module app;
2+
3+
import std.stdio: writeln;
4+
import std.algorithm: minIndex, remove;
5+
6+
7+
// or it is possible to use minIndex
8+
T findSmallest(T)(T[] arr) {
9+
auto smallest = arr[0];
10+
auto smallest_index = 0;
11+
foreach(i; 0 .. cast(int)arr.length) {
12+
if (arr[i] < smallest) {
13+
smallest = arr[i];
14+
smallest_index = i;
15+
}
16+
}
17+
return smallest_index;
18+
}
19+
20+
T[] selectionSort(T)(T[] arr) {
21+
T[] newArr = [];
22+
foreach(i; 0 .. cast(int)arr.length) {
23+
auto smallest = findSmallest(arr); // or use minIndex(arr);
24+
newArr ~= arr[smallest];
25+
arr = arr.remove(smallest);
26+
}
27+
return newArr;
28+
}
29+
30+
void main() {
31+
writeln(selectionSort([5, 3, 6, 2, 10]));
32+
}

03_recursion/d/01_countdown.d

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
module app;
2+
3+
import std;
4+
5+
void countdown(int i) {
6+
// base case
7+
if (i <= 0)
8+
return 0;
9+
// recursive case
10+
else {
11+
writeln(i);
12+
return countdown(i-1);
13+
}
14+
}
15+
16+
void main() {
17+
countdown(5);
18+
}

03_recursion/d/02_greet.d

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
module app;
2+
3+
import std;
4+
5+
void greet2(string name) {
6+
writeln(i"how are you $(name)?");
7+
}
8+
9+
void bye() {
10+
writeln("ok bye!");
11+
}
12+
13+
void greet(string name) {
14+
writeln(i"hello, #(name)!");
15+
greet2(name);
16+
writeln("gettin ready to say bye...");
17+
bye();
18+
}
19+
20+
void main() {
21+
greet("adit");
22+
}

03_recursion/d/03_factorial.d

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
module app;
2+
3+
import std;
4+
5+
T fact(T x) {
6+
if (x == 1)
7+
return 1;
8+
else
9+
return x * fact(x-1);
10+
}
11+
12+
void main() {
13+
writeln(fact(5));
14+
}

03_recursion/d/04_count.d

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
module app;
2+
3+
import std;
4+
5+
int count(T[] arr) {
6+
if (arr.empty)
7+
return 0;
8+
return 1 + count(arr[1..$]);
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
module app;
2+
3+
import std;
4+
5+
int binary_search(T)(T[] arr, T target) {
6+
if (arr.length == 0)
7+
return -1;
8+
9+
int mid = cast(int)arr.length / 2;
10+
if (arr[mid] == target)
11+
return mid;
12+
else if (arr[mid] > target)
13+
return binary_search(arr[0..mid], target);
14+
else {
15+
int recursive_response = binary_search(arr[mid + 1 .. $], target);
16+
return recursive_response == -1 ? recursive_response : (mid + 1) + recursive_response;
17+
}
18+
}
19+
20+
void main() {
21+
writeln(binary_search([6, 7, 8, 9, 10], 8));
22+
writeln(binary_search([6, 7, 8, 9, 10], 6));
23+
}

03_recursion/d/06_find_max.d

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
T find_max(T)(T[] arr) {
2+
if (arr.length == 0)
3+
return 0;
4+
else if (arr.length == 1)
5+
return arr[0];
6+
else if (arr.length == 2)
7+
return arr[0] > arr[1] ? arr[0] : arr[1];
8+
auto sub_max = find_max(arr[1..$]);
9+
return arr[0] > sub_max ? arr[0] : sub_max;
10+
}

03_recursion/d/07_sum_array.d

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
T sum_array(T)(T[] arr) {
2+
if (arr.empty)
3+
return 0;
4+
return arr[0] + sum_array(arr[1..$]);
5+
}

03_recursion/d/08_look_for_key.d

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
modeul app;
2+
3+
import std;
4+
5+
class Item {
6+
bool is_key = false;
7+
Item[] items_in_box;
8+
9+
this() {
10+
this.is_key = false;
11+
}
12+
13+
this(bool key) {
14+
this.is_key = key;
15+
}
16+
17+
bool is_a_box() {
18+
return !this.is_key;
19+
}
20+
21+
bool is_a_key() {
22+
return this.is_key;
23+
}
24+
}
25+
26+
void look_for_key(Item box) {
27+
foreach(item; box.items_in_box)
28+
if (item.is_a_box())
29+
// recursive case
30+
look_for_key(item);
31+
else if (item.is_a_key())
32+
// base case
33+
writeln("found the key!");
34+
}
35+
36+
/*
37+
main_box
38+
├── box_A
39+
│ ├── box_B
40+
│ └── box_C
41+
└── box_D
42+
└── box_E
43+
└── key
44+
*/
45+
void main() {
46+
auto main_box = new Item();
47+
auto box_A = new Item();
48+
auto box_B = new Item();
49+
auto box_C = new Item();
50+
box_A.items_in_box = [box_B, box_C];
51+
52+
auto box_D = new Item();
53+
auto box_E = new Item();
54+
auto key = new Item(true);
55+
box_E.items_in_box = [key];
56+
box_D.items_in_box = [box_E];
57+
58+
main_box.items_in_box = [box_A, box_D];
59+
60+
look_for_key(main_box);
61+
}

04_quicksort/d/01_loop_sum.d

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
module app;
2+
3+
import std;
4+
5+
T loop_sum(T[] arr) {
6+
T total;
7+
foreach(x; arr)
8+
total += x;
9+
return total;
10+
}
11+
12+
void main() {
13+
writeln(loop_sum([1,2,3,4]));
14+
}

04_quicksort/d/02_recursive_sum.d

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
module app;
2+
3+
import std;
4+
5+
T rec_sum(T[] list) {
6+
if (list.empty)
7+
return 0;
8+
return list[0] + sum(list[1..$]);
9+
}
10+
11+
void main() {
12+
writeln(rec_sum([1,2,3,4]));
13+
}

04_quicksort/d/03_recursive_count.d

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
module app;
2+
3+
import std;
4+
5+
T rec_count(T[] list) {
6+
if (list.empty)
7+
return 0;
8+
return 1 + rec_count(list[1..$]);
9+
}
10+
11+
void main() {
12+
writeln(rec_count([1,2,3,4]));
13+
}

04_quicksort/d/04_recursive_max.d

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
module app;
2+
3+
import std;
4+
5+
T rec_max(T)(T[] list) {
6+
if (list.empty)
7+
return T.init;
8+
if (list.length == 1)
9+
return list[0];
10+
else {
11+
auto max_num = rec_max(list[1..$]);
12+
return list[0] > max_num ? list[0] : max_num;
13+
}
14+
}
15+
16+
void main() {
17+
writeln(rec_max([1,2,3]));
18+
}
19+

04_quicksort/d/05_quicksort.d

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
module app;
2+
3+
import std;
4+
5+
T[] quicksort(T[] arr) {
6+
if (arr.length < 2)
7+
return arr;
8+
else {
9+
auto pivot = arr[0];
10+
auto less = arr.filter!(el => el < pivot).array;
11+
auto greater = arr.filter!(el => el > pivot).array;
12+
return quicksort(less) ~ arr.filter!(el => el == pivot).array ~ quicksort(greater);
13+
}
14+
}
15+
16+
void main() {
17+
writeln(quicksort([10, 5, 2, 3]));
18+
}
+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module app;
2+
3+
import std;
4+
5+
void main() {
6+
float[string] book = ["apple": 0.67, "milk": 1.49, "avocado": 1.49];
7+
writeln(book);
8+
}

05_hash_tables/d/02_check_voter.d

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
module app;
2+
3+
import std;
4+
5+
bool[string] voted;
6+
7+
void check_voter(string name) {
8+
auto p = name in voted;
9+
if (p !is null)
10+
writeln("Kick them out!");
11+
else {
12+
voted[name] = true;
13+
writeln("Let them vote!");
14+
}
15+
}
16+
17+
void main() {
18+
check_voter("tom");
19+
check_voter("mike");
20+
check_voter("mike");
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
module app;
2+
3+
import std;
4+
5+
bool personIsSeller(string name) {
6+
return name[name.length - 1] == 'm';
7+
}
8+
9+
bool search(string name) {
10+
auto search_queue = DList!string();
11+
search_queue.insertBack(name);
12+
bool[string] searched;
13+
while (!search_queue.empty) {
14+
auto person = search_queue.front();
15+
search_queue.removeFront();
16+
auto found = person in searched;
17+
if (found !is null)
18+
continue;
19+
if (personIsSeller(person)) {
20+
writeln(person, " is a mango seller!");
21+
return true;
22+
}
23+
search_queue.insertBack(graph[person]);
24+
searched[person] = true;
25+
}
26+
return false;
27+
}
28+
29+
string[][string] graph;
30+
31+
void main() {
32+
graph["you"] = ["alice", "bob", "claire"];
33+
graph["bob"] = ["anuj", "peggy"];
34+
graph["alice"] = ["peggy"];
35+
graph["claire"] = ["thom", "jonny"];
36+
graph["anuj"] = [];
37+
graph["peggy"] = [];
38+
graph["thom"] = [];
39+
graph["jonny"] = [];
40+
41+
search("you");
42+
}

0 commit comments

Comments
 (0)