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:
Serg Gini
2025-05-02 02:24:42 +03:00
committed by GitHub
parent df180e6f7b
commit 9ff7468e9e
22 changed files with 544 additions and 0 deletions

View File

@@ -0,0 +1,14 @@
module app;
import std;
T loop_sum(T[] arr) {
T total;
foreach(x; arr)
total += x;
return total;
}
void main() {
writeln(loop_sum([1,2,3,4]));
}

View File

@@ -0,0 +1,13 @@
module app;
import std;
T rec_sum(T[] list) {
if (list.empty)
return 0;
return list[0] + sum(list[1..$]);
}
void main() {
writeln(rec_sum([1,2,3,4]));
}

View File

@@ -0,0 +1,13 @@
module app;
import std;
T rec_count(T[] list) {
if (list.empty)
return 0;
return 1 + rec_count(list[1..$]);
}
void main() {
writeln(rec_count([1,2,3,4]));
}

View File

@@ -0,0 +1,19 @@
module app;
import std;
T rec_max(T)(T[] list) {
if (list.empty)
return T.init;
if (list.length == 1)
return list[0];
else {
auto max_num = rec_max(list[1..$]);
return list[0] > max_num ? list[0] : max_num;
}
}
void main() {
writeln(rec_max([1,2,3]));
}

View File

@@ -0,0 +1,18 @@
module app;
import std;
T[] quicksort(T[] arr) {
if (arr.length < 2)
return arr;
else {
auto pivot = arr[0];
auto less = arr.filter!(el => el < pivot).array;
auto greater = arr.filter!(el => el > pivot).array;
return quicksort(less) ~ arr.filter!(el => el == pivot).array ~ quicksort(greater);
}
}
void main() {
writeln(quicksort([10, 5, 2, 3]));
}