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:
18
03_recursion/d/01_countdown.d
Normal file
18
03_recursion/d/01_countdown.d
Normal file
@@ -0,0 +1,18 @@
|
||||
module app;
|
||||
|
||||
import std;
|
||||
|
||||
void countdown(int i) {
|
||||
// base case
|
||||
if (i <= 0)
|
||||
return 0;
|
||||
// recursive case
|
||||
else {
|
||||
writeln(i);
|
||||
return countdown(i-1);
|
||||
}
|
||||
}
|
||||
|
||||
void main() {
|
||||
countdown(5);
|
||||
}
|
||||
22
03_recursion/d/02_greet.d
Normal file
22
03_recursion/d/02_greet.d
Normal file
@@ -0,0 +1,22 @@
|
||||
module app;
|
||||
|
||||
import std;
|
||||
|
||||
void greet2(string name) {
|
||||
writeln(i"how are you $(name)?");
|
||||
}
|
||||
|
||||
void bye() {
|
||||
writeln("ok bye!");
|
||||
}
|
||||
|
||||
void greet(string name) {
|
||||
writeln(i"hello, #(name)!");
|
||||
greet2(name);
|
||||
writeln("gettin ready to say bye...");
|
||||
bye();
|
||||
}
|
||||
|
||||
void main() {
|
||||
greet("adit");
|
||||
}
|
||||
14
03_recursion/d/03_factorial.d
Normal file
14
03_recursion/d/03_factorial.d
Normal file
@@ -0,0 +1,14 @@
|
||||
module app;
|
||||
|
||||
import std;
|
||||
|
||||
T fact(T x) {
|
||||
if (x == 1)
|
||||
return 1;
|
||||
else
|
||||
return x * fact(x-1);
|
||||
}
|
||||
|
||||
void main() {
|
||||
writeln(fact(5));
|
||||
}
|
||||
9
03_recursion/d/04_count.d
Normal file
9
03_recursion/d/04_count.d
Normal file
@@ -0,0 +1,9 @@
|
||||
module app;
|
||||
|
||||
import std;
|
||||
|
||||
int count(T[] arr) {
|
||||
if (arr.empty)
|
||||
return 0;
|
||||
return 1 + count(arr[1..$]);
|
||||
}
|
||||
23
03_recursion/d/05_binary_search_recursive.d
Normal file
23
03_recursion/d/05_binary_search_recursive.d
Normal file
@@ -0,0 +1,23 @@
|
||||
module app;
|
||||
|
||||
import std;
|
||||
|
||||
int binary_search(T)(T[] arr, T target) {
|
||||
if (arr.length == 0)
|
||||
return -1;
|
||||
|
||||
int mid = cast(int)arr.length / 2;
|
||||
if (arr[mid] == target)
|
||||
return mid;
|
||||
else if (arr[mid] > target)
|
||||
return binary_search(arr[0..mid], target);
|
||||
else {
|
||||
int recursive_response = binary_search(arr[mid + 1 .. $], target);
|
||||
return recursive_response == -1 ? recursive_response : (mid + 1) + recursive_response;
|
||||
}
|
||||
}
|
||||
|
||||
void main() {
|
||||
writeln(binary_search([6, 7, 8, 9, 10], 8));
|
||||
writeln(binary_search([6, 7, 8, 9, 10], 6));
|
||||
}
|
||||
10
03_recursion/d/06_find_max.d
Normal file
10
03_recursion/d/06_find_max.d
Normal file
@@ -0,0 +1,10 @@
|
||||
T find_max(T)(T[] arr) {
|
||||
if (arr.length == 0)
|
||||
return 0;
|
||||
else if (arr.length == 1)
|
||||
return arr[0];
|
||||
else if (arr.length == 2)
|
||||
return arr[0] > arr[1] ? arr[0] : arr[1];
|
||||
auto sub_max = find_max(arr[1..$]);
|
||||
return arr[0] > sub_max ? arr[0] : sub_max;
|
||||
}
|
||||
5
03_recursion/d/07_sum_array.d
Normal file
5
03_recursion/d/07_sum_array.d
Normal file
@@ -0,0 +1,5 @@
|
||||
T sum_array(T)(T[] arr) {
|
||||
if (arr.empty)
|
||||
return 0;
|
||||
return arr[0] + sum_array(arr[1..$]);
|
||||
}
|
||||
61
03_recursion/d/08_look_for_key.d
Normal file
61
03_recursion/d/08_look_for_key.d
Normal file
@@ -0,0 +1,61 @@
|
||||
modeul app;
|
||||
|
||||
import std;
|
||||
|
||||
class Item {
|
||||
bool is_key = false;
|
||||
Item[] items_in_box;
|
||||
|
||||
this() {
|
||||
this.is_key = false;
|
||||
}
|
||||
|
||||
this(bool key) {
|
||||
this.is_key = key;
|
||||
}
|
||||
|
||||
bool is_a_box() {
|
||||
return !this.is_key;
|
||||
}
|
||||
|
||||
bool is_a_key() {
|
||||
return this.is_key;
|
||||
}
|
||||
}
|
||||
|
||||
void look_for_key(Item box) {
|
||||
foreach(item; box.items_in_box)
|
||||
if (item.is_a_box())
|
||||
// recursive case
|
||||
look_for_key(item);
|
||||
else if (item.is_a_key())
|
||||
// base case
|
||||
writeln("found the key!");
|
||||
}
|
||||
|
||||
/*
|
||||
main_box
|
||||
├── box_A
|
||||
│ ├── box_B
|
||||
│ └── box_C
|
||||
└── box_D
|
||||
└── box_E
|
||||
└── key
|
||||
*/
|
||||
void main() {
|
||||
auto main_box = new Item();
|
||||
auto box_A = new Item();
|
||||
auto box_B = new Item();
|
||||
auto box_C = new Item();
|
||||
box_A.items_in_box = [box_B, box_C];
|
||||
|
||||
auto box_D = new Item();
|
||||
auto box_E = new Item();
|
||||
auto key = new Item(true);
|
||||
box_E.items_in_box = [key];
|
||||
box_D.items_in_box = [box_E];
|
||||
|
||||
main_box.items_in_box = [box_A, box_D];
|
||||
|
||||
look_for_key(main_box);
|
||||
}
|
||||
Reference in New Issue
Block a user