Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

D solutions #305

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
27 changes: 27 additions & 0 deletions 01_introduction_to_algorithms/d/01_binary_search.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
module app;

import std.stdio: writeln;
import std.range: assumeSorted, SortedRange;

long binary_search(T)(SortedRange!(T[]) list, T item) {
long low = 0;
long high = list.length - 1;

while (low <= high) {
auto mid = (low + high) / 2;
T guess = list[mid];
if (guess == item)
return mid;
else if (guess > item)
high = mid - 1;
else
low = mid + 1;
}
return -1;
}

void main() {
auto my_list = [1, 3, 5, 7, 9];
writeln(binary_search(assumeSorted(my_list), 3));
writeln(binary_search(assumeSorted(my_list), -1));
}
32 changes: 32 additions & 0 deletions 02_selection_sort/d/01_selection_sort.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
module app;

import std.stdio: writeln;
import std.algorithm: minIndex, remove;


// or it is possible to use minIndex
T findSmallest(T)(T[] arr) {
auto smallest = arr[0];
auto smallest_index = 0;
foreach(i; 0 .. cast(int)arr.length) {
if (arr[i] < smallest) {
smallest = arr[i];
smallest_index = i;
}
}
return smallest_index;
}

T[] selectionSort(T)(T[] arr) {
T[] newArr = [];
foreach(i; 0 .. cast(int)arr.length) {
auto smallest = findSmallest(arr); // or use minIndex(arr);
newArr ~= arr[smallest];
arr = arr.remove(smallest);
}
return newArr;
}

void main() {
writeln(selectionSort([5, 3, 6, 2, 10]));
}
18 changes: 18 additions & 0 deletions 03_recursion/d/01_countdown.d
Original file line number Diff line number Diff line change
@@ -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 changes: 22 additions & 0 deletions 03_recursion/d/02_greet.d
Original file line number Diff line number Diff line change
@@ -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 changes: 14 additions & 0 deletions 03_recursion/d/03_factorial.d
Original file line number Diff line number Diff line change
@@ -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 changes: 9 additions & 0 deletions 03_recursion/d/04_count.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module app;

import std;

int count(T[] arr) {
if (arr.empty)
return 0;
return 1 + count(arr[1..$]);
}
23 changes: 23 additions & 0 deletions 03_recursion/d/05_binary_search_recursive.d
Original file line number Diff line number Diff line change
@@ -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 changes: 10 additions & 0 deletions 03_recursion/d/06_find_max.d
Original file line number Diff line number Diff line change
@@ -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 changes: 5 additions & 0 deletions 03_recursion/d/07_sum_array.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
T sum_array(T)(T[] arr) {
if (arr.empty)
return 0;
return arr[0] + sum_array(arr[1..$]);
}
61 changes: 61 additions & 0 deletions 03_recursion/d/08_look_for_key.d
Original file line number Diff line number Diff line change
@@ -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);
}
14 changes: 14 additions & 0 deletions 04_quicksort/d/01_loop_sum.d
Original file line number Diff line number Diff line change
@@ -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]));
}
13 changes: 13 additions & 0 deletions 04_quicksort/d/02_recursive_sum.d
Original file line number Diff line number Diff line change
@@ -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]));
}
13 changes: 13 additions & 0 deletions 04_quicksort/d/03_recursive_count.d
Original file line number Diff line number Diff line change
@@ -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]));
}
19 changes: 19 additions & 0 deletions 04_quicksort/d/04_recursive_max.d
Original file line number Diff line number Diff line change
@@ -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]));
}

18 changes: 18 additions & 0 deletions 04_quicksort/d/05_quicksort.d
Original file line number Diff line number Diff line change
@@ -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]));
}
8 changes: 8 additions & 0 deletions 05_hash_tables/d/01_price_of_groceries.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module app;

import std;

void main() {
float[string] book = ["apple": 0.67, "milk": 1.49, "avocado": 1.49];
writeln(book);
}
21 changes: 21 additions & 0 deletions 05_hash_tables/d/02_check_voter.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
module app;

import std;

bool[string] voted;

void check_voter(string name) {
auto p = name in voted;
if (p !is null)
writeln("Kick them out!");
else {
voted[name] = true;
writeln("Let them vote!");
}
}

void main() {
check_voter("tom");
check_voter("mike");
check_voter("mike");
}
42 changes: 42 additions & 0 deletions 06_breadth-first_search/d/01_breadth_first_search.d
Original file line number Diff line number Diff line change
@@ -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");
}
Loading