Compare commits
17 Commits
09e18f9ac9
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 488ea7960b | |||
| f3b0be9500 | |||
| e819633ba0 | |||
| 13932df1a8 | |||
| 2cc09b147c | |||
| 2a38f98d37 | |||
| 1e8301139a | |||
| fb08343f8c | |||
| bd0fc7054d | |||
| 08a4142b4a | |||
| 835dfb6314 | |||
| cd6bb7c39f | |||
| 6538a5f98f | |||
| b12a36f536 | |||
| 7bdaa33236 | |||
| dc8d382910 | |||
| 82d3973ade |
37
c/hash-table-pro.c
Normal file
37
c/hash-table-pro.c
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
typedef struct item {
|
||||||
|
char *key;
|
||||||
|
int value;
|
||||||
|
} item;
|
||||||
|
|
||||||
|
item *linear_search(item *items, size_t size, const char *key) {
|
||||||
|
for (size_t i = 0; i < size; i++) {
|
||||||
|
if (strcmp(items[i].key, key) == 0) {
|
||||||
|
return (&items[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return (NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
item items[] = {
|
||||||
|
{"foo", 10},
|
||||||
|
{"bar", 20},
|
||||||
|
{"hello", 30},
|
||||||
|
{"x", 40}};
|
||||||
|
|
||||||
|
size_t total_items = sizeof(items) / sizeof(items);
|
||||||
|
|
||||||
|
item *found = linear_search(items, total_items, "foo");
|
||||||
|
|
||||||
|
if (found == NULL) {
|
||||||
|
printf("linear search: value of 'foo' has no key\n");
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("linear search: value of 'foo' is: %d", found->value);
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
136
c/hash-table.c
Normal file
136
c/hash-table.c
Normal file
@@ -0,0 +1,136 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#define MAX_HASH_TABLE_SIZE 16
|
||||||
|
|
||||||
|
typedef struct KV {
|
||||||
|
char *key;
|
||||||
|
char *value;
|
||||||
|
} KV;
|
||||||
|
|
||||||
|
typedef struct Node {
|
||||||
|
KV kv;
|
||||||
|
struct Node *next;
|
||||||
|
} Node;
|
||||||
|
|
||||||
|
typedef struct HashTable {
|
||||||
|
Node *data[MAX_HASH_TABLE_SIZE];
|
||||||
|
} HashTable;
|
||||||
|
|
||||||
|
HashTable *new_hashtable() {
|
||||||
|
HashTable *ht = malloc(sizeof(HashTable));
|
||||||
|
for (int i = 0; i < MAX_HASH_TABLE_SIZE; i++) {
|
||||||
|
ht->data[i] = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
return (ht);
|
||||||
|
}
|
||||||
|
|
||||||
|
Node *new_node() {
|
||||||
|
Node *node = malloc(sizeof(Node));
|
||||||
|
node->next = NULL;
|
||||||
|
|
||||||
|
return (node);
|
||||||
|
}
|
||||||
|
|
||||||
|
int get_index_for_letter(char letter) {
|
||||||
|
if (letter >= 'a' && letter <= 'z') {
|
||||||
|
return (letter - 'a') + 1;
|
||||||
|
} else {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int hash(char *word) {
|
||||||
|
int word_len = strlen(word);
|
||||||
|
int accum = 1;
|
||||||
|
int index;
|
||||||
|
for (int i = 0; i < word_len; i++) {
|
||||||
|
index = get_index_for_letter(word[i]);
|
||||||
|
accum *= index;
|
||||||
|
}
|
||||||
|
|
||||||
|
return accum % 16;
|
||||||
|
}
|
||||||
|
|
||||||
|
KV *find_KV_in_ht(HashTable h, char *key) {
|
||||||
|
for (size_t i = 0; i < MAX_HASH_TABLE_SIZE; i++) {
|
||||||
|
if ((h.data[i] != NULL) && (strcmp(h.data[i]->kv.key, key) == 0)) {
|
||||||
|
return (&h.data[i]->kv);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return (NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
char *get_value(HashTable ht, char *key) {
|
||||||
|
int hash_key = hash(key);
|
||||||
|
|
||||||
|
Node *node_at_hash = ht.data[hash_key];
|
||||||
|
|
||||||
|
if (node_at_hash == NULL) {
|
||||||
|
fprintf(stderr, "ERROR: key not found in hashtable");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// if next is NULL then this is the only at this hash so just return it
|
||||||
|
if (node_at_hash->next == NULL) {
|
||||||
|
return node_at_hash->kv.value;
|
||||||
|
}
|
||||||
|
|
||||||
|
return kv->value;
|
||||||
|
}
|
||||||
|
|
||||||
|
void insert_kv(HashTable *ht, char *key, char *value) {
|
||||||
|
int hashed_key = hash(key);
|
||||||
|
|
||||||
|
Node *current = ht->data[hashed_key];
|
||||||
|
|
||||||
|
while (current) {
|
||||||
|
if (strcmp(current->kv.key, key) == 0) {
|
||||||
|
current->kv.value = value;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
current = current->next;
|
||||||
|
}
|
||||||
|
|
||||||
|
Node *new = new_node();
|
||||||
|
new->kv.key = key;
|
||||||
|
new->kv.value = value;
|
||||||
|
new->next = current;
|
||||||
|
ht->data[hashed_key] = new;
|
||||||
|
}
|
||||||
|
|
||||||
|
// char *get(HashTable *h, char *key) {
|
||||||
|
// int hash_val = hash(key);
|
||||||
|
// return h->data[hash_val];
|
||||||
|
// }
|
||||||
|
|
||||||
|
// void insert(HashTable *h, char *key, char *value) {
|
||||||
|
// int hashed_key = hash(key);
|
||||||
|
|
||||||
|
// if (h->data[hashed_key] != NULL) {
|
||||||
|
// fprintf(stderr, "ERROR: collision!");
|
||||||
|
// exit(1);
|
||||||
|
// }
|
||||||
|
|
||||||
|
// h->data[hashed_key] = value;
|
||||||
|
// }
|
||||||
|
|
||||||
|
// void delete(HashTable *h, char *key) {
|
||||||
|
// int hashed_key = hash(key);
|
||||||
|
// h->data[hashed_key] = NULL;
|
||||||
|
// }
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
HashTable *h = new_hashtable();
|
||||||
|
|
||||||
|
// test
|
||||||
|
insert_kv(h, "hello", "world");
|
||||||
|
char *result = get_value(*h, "hello");
|
||||||
|
printf("the value corresponding to the key 'hello' is: '%s'", result);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
33
c/insertion-sort.c
Normal file
33
c/insertion-sort.c
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
void insertion_sort(int arr[], int len) {
|
||||||
|
int temp;
|
||||||
|
int j;
|
||||||
|
for (int i = 1; i < len; i++) {
|
||||||
|
temp = arr[i];
|
||||||
|
j = i - 1;
|
||||||
|
while (j >= 0) {
|
||||||
|
if (temp < arr[j]) {
|
||||||
|
arr[j + 1] = arr[j];
|
||||||
|
j--;
|
||||||
|
} else {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
arr[j + 1] = temp;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void print_array(int arr[], int len) {
|
||||||
|
printf("[ ");
|
||||||
|
for (int i = 0; i < len; i++) {
|
||||||
|
printf("%d ", arr[i]);
|
||||||
|
}
|
||||||
|
printf("]\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
int arr[10] = {10, 9, 8, 7, 6, 5, 4, 3, 1, 2};
|
||||||
|
insertion_sort(arr, 10);
|
||||||
|
print_array(arr, 10);
|
||||||
|
}
|
||||||
69
c/intersection.c
Normal file
69
c/intersection.c
Normal file
@@ -0,0 +1,69 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
#define MAX_DATA 50
|
||||||
|
|
||||||
|
typedef struct ArrayList {
|
||||||
|
int cap;
|
||||||
|
int index;
|
||||||
|
int data[];
|
||||||
|
} ArrayList;
|
||||||
|
|
||||||
|
ArrayList *new_array_list(int cap) {
|
||||||
|
ArrayList *arr = malloc(sizeof(ArrayList) + cap * sizeof(int));
|
||||||
|
|
||||||
|
if (arr == NULL) {
|
||||||
|
fprintf(stderr, "ERROR: cannot allocate mem for arraylist\n");
|
||||||
|
}
|
||||||
|
arr->cap = cap;
|
||||||
|
arr->index = 0;
|
||||||
|
|
||||||
|
return (arr);
|
||||||
|
}
|
||||||
|
|
||||||
|
void push(ArrayList *arr, int val) {
|
||||||
|
if (arr->index == arr->cap) {
|
||||||
|
fprintf(stderr, "ERROR: cannot append to array at capacity");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
arr->data[arr->index] = val;
|
||||||
|
arr->index++;
|
||||||
|
}
|
||||||
|
|
||||||
|
void pop(ArrayList *arr) {
|
||||||
|
if (arr->index == 0) {
|
||||||
|
fprintf(stderr, "ERROR: canoot pop from empty array");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
arr->index--;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* for now we only return the indices on the left that that are in the right */
|
||||||
|
ArrayList *intersection(int left[], int right[], int left_len, int right_len) {
|
||||||
|
ArrayList *arr = new_array_list(left_len);
|
||||||
|
|
||||||
|
for (int i = 0; i < left_len; i++) {
|
||||||
|
for (int j = 0; j < right_len; j++) {
|
||||||
|
if (left[i] == right[j]) {
|
||||||
|
push(arr, left[i]);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return (arr);
|
||||||
|
}
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
int a[5] = {1, 2, 3, 4, 8};
|
||||||
|
int b[5] = {4, 5, 6, 7, 8};
|
||||||
|
|
||||||
|
ArrayList *arr = intersection(a, b, 5, 5);
|
||||||
|
for (int i = 0; i < 2; i++) {
|
||||||
|
printf("%d ", arr->data[i]);
|
||||||
|
}
|
||||||
|
printf("\n");
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
2
c/qs.c
2
c/qs.c
@@ -38,7 +38,7 @@ void quick_sort(int arr[], int lo, int hi) {
|
|||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
int array_len = 5;
|
int array_len = 5;
|
||||||
int arr[5] = {5, 4, 3, 2, 1};
|
int arr[5] = {0, 2, 5, 1, 6};
|
||||||
quick_sort(arr, 0, array_len - 1);
|
quick_sort(arr, 0, array_len - 1);
|
||||||
|
|
||||||
printf("[ ");
|
printf("[ ");
|
||||||
|
|||||||
51
c/quicksort.c
Normal file
51
c/quicksort.c
Normal file
@@ -0,0 +1,51 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
// the partition will weakly sort the array and return the index of
|
||||||
|
// the pivot
|
||||||
|
int partition(int arr[], int lo, int hi) {
|
||||||
|
int pivot_value = arr[hi];
|
||||||
|
int current_index = lo - 1;
|
||||||
|
int temp;
|
||||||
|
for (int i = lo; i < hi; i++) {
|
||||||
|
if (arr[i] <= pivot_value) {
|
||||||
|
current_index++;
|
||||||
|
|
||||||
|
temp = arr[i];
|
||||||
|
arr[i] = arr[current_index];
|
||||||
|
arr[current_index] = temp;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// once done with loop the current_index will hold
|
||||||
|
// the location where we need to place the pivot_value
|
||||||
|
current_index++;
|
||||||
|
arr[hi] = arr[current_index];
|
||||||
|
arr[current_index] = pivot_value;
|
||||||
|
|
||||||
|
return (current_index);
|
||||||
|
}
|
||||||
|
|
||||||
|
void qs(int arr[], int lo, int hi) {
|
||||||
|
if (lo < hi) {
|
||||||
|
int current_pivot = partition(arr, lo, hi);
|
||||||
|
qs(arr, lo, current_pivot - 1);
|
||||||
|
qs(arr, current_pivot + 1, hi);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void print_array(int arr[], int len) {
|
||||||
|
printf("[ ");
|
||||||
|
for (int i = 0; i < len; i++) {
|
||||||
|
printf("%d ", arr[i]);
|
||||||
|
}
|
||||||
|
printf("]\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
int arr[5] = {0, 2, 5, 1, 6};
|
||||||
|
print_array(arr, 5);
|
||||||
|
qs(arr, 0, 4);
|
||||||
|
print_array(arr, 5);
|
||||||
|
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
@@ -7,18 +7,16 @@ void swap(int *a, int *b) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void selection_sort(int arr[], int len) {
|
void selection_sort(int arr[], int len) {
|
||||||
int curr_low;
|
int lo_index;
|
||||||
int low_index = 0;
|
for (int i = 0; i < len; i++) { // keep track of pass
|
||||||
int pass_index = 0;
|
lo_index = i; // low index always starts as the pass iteration
|
||||||
|
for (int j = i + 1; j < len; j++) { // keep track of array comparisons
|
||||||
for (int i = 0; i < len; i++) {
|
if (arr[j] < arr[i]) {
|
||||||
low_index = i;
|
lo_index = j;
|
||||||
for (int j = i + 1; j < len; j++) {
|
|
||||||
if (arr[i] > arr[j]) { // if we encounter a new low
|
|
||||||
low_index = j;
|
|
||||||
}
|
}
|
||||||
}
|
} // after done with this loop we swap the value at lo_index to the current pass index
|
||||||
swap(&arr[i], &arr[low_index]);
|
|
||||||
|
swap(&arr[i], &arr[lo_index]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -31,7 +29,7 @@ void print_array(int arr[], int len) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
int arr[6] = {5, 3, 4, 2, 1, 7};
|
int arr[5] = {5, 4, 3, 2, 1};
|
||||||
selection_sort(arr, 6);
|
selection_sort(arr, 5);
|
||||||
print_array(arr, 6);
|
print_array(arr, 5);
|
||||||
}
|
}
|
||||||
54
odin/main.odin
Normal file
54
odin/main.odin
Normal file
@@ -0,0 +1,54 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import "core:fmt"
|
||||||
|
|
||||||
|
linear_search :: proc(arr: []int, len: int, needle: int) -> bool {
|
||||||
|
for index, val in arr {
|
||||||
|
if val == needle {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
partition :: proc(arr: ^[]int, lo: int, hi: int) -> int {
|
||||||
|
pivot_value := arr[hi]
|
||||||
|
current_index := lo - 1
|
||||||
|
|
||||||
|
for i := lo; i < hi; i +=1 {
|
||||||
|
if arr[i] <= pivot_value {
|
||||||
|
current_index += 1
|
||||||
|
tmp := arr[i]
|
||||||
|
arr[i] = arr[current_index]
|
||||||
|
arr[current_index] = tmp
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
current_index += 1
|
||||||
|
arr[hi] = arr[current_index]
|
||||||
|
arr[current_index] = pivot_value
|
||||||
|
|
||||||
|
return(current_index)
|
||||||
|
}
|
||||||
|
|
||||||
|
quick_sort :: proc(arr: ^[]int, lo: int, hi: int) {
|
||||||
|
if lo < hi {
|
||||||
|
current_pivot := partition(arr, lo, hi);
|
||||||
|
quick_sort(arr, lo, current_pivot - 1);
|
||||||
|
quick_sort(arr, current_pivot + 1, hi);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
main :: proc() {
|
||||||
|
fmt.println("hello there here is a for loop")
|
||||||
|
|
||||||
|
values: []int = {1, 2, 3, 4, 5}
|
||||||
|
res: bool = linear_search(values, 5, 2)
|
||||||
|
fmt.println(res)
|
||||||
|
|
||||||
|
fmt.println("quicksort ----------------")
|
||||||
|
arr: []int = {0, 2, 5, 1, 6}
|
||||||
|
fmt.println(arr);
|
||||||
|
quick_sort(&arr, 0, 4);
|
||||||
|
fmt.println(arr);
|
||||||
|
}
|
||||||
28
python/fib.py
Normal file
28
python/fib.py
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
def fib(n, counter):
|
||||||
|
if n == 0 or n == 1:
|
||||||
|
return n
|
||||||
|
|
||||||
|
else:
|
||||||
|
counter.append(1)
|
||||||
|
print("calling fib")
|
||||||
|
return fib(n - 1, counter) + fib(n - 2, counter)
|
||||||
|
|
||||||
|
|
||||||
|
def fib_memo(n, memo={}, counter=[]):
|
||||||
|
if n == 0 or n == 1:
|
||||||
|
return n
|
||||||
|
|
||||||
|
if not memo.get(n):
|
||||||
|
counter.append(1)
|
||||||
|
memo[n] = fib_memo(n - 1, memo, counter) + fib_memo(n - 2, memo, counter)
|
||||||
|
|
||||||
|
return memo[n]
|
||||||
|
|
||||||
|
|
||||||
|
count = []
|
||||||
|
fib(6, counter=count)
|
||||||
|
sum(count)
|
||||||
|
|
||||||
|
fib_counter = []
|
||||||
|
fib_memo(6, memo={}, counter=fib_counter)
|
||||||
|
sum(fib_counter)
|
||||||
52
python/hash-table.py
Normal file
52
python/hash-table.py
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
import string
|
||||||
|
|
||||||
|
alphabet = [i for i in string.ascii_lowercase]
|
||||||
|
|
||||||
|
alphabet.split()
|
||||||
|
|
||||||
|
|
||||||
|
def linear_search(val, arr):
|
||||||
|
for i, letter in enumerate(arr):
|
||||||
|
if val == letter:
|
||||||
|
return i + 1
|
||||||
|
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
def hash(word):
|
||||||
|
split_word = [i for i in word]
|
||||||
|
split_prods = [linear_search(i, alphabet) for i in split_word]
|
||||||
|
hash_key = 1
|
||||||
|
for i in split_prods:
|
||||||
|
hash_key *= i
|
||||||
|
|
||||||
|
return hash_key % 16
|
||||||
|
|
||||||
|
|
||||||
|
def get_at_hash(key, container):
|
||||||
|
return container[hash(key)]
|
||||||
|
|
||||||
|
|
||||||
|
def insert_at_hash(key, value, container):
|
||||||
|
key_hash = hash(key)
|
||||||
|
|
||||||
|
if container[key_hash] is None:
|
||||||
|
# no collision just insert and return
|
||||||
|
container[key_hash] = value
|
||||||
|
return container
|
||||||
|
else:
|
||||||
|
# resolve collision
|
||||||
|
# but I don't know what was the key, it could any permutation
|
||||||
|
# of the letters BAD in the example I am working through
|
||||||
|
existing_value = container[key_hash]
|
||||||
|
container[key_hash] = [[existing_value], [key, value]]
|
||||||
|
return container
|
||||||
|
|
||||||
|
|
||||||
|
container = [None for i in range(15)]
|
||||||
|
|
||||||
|
insert_at_hash("bad", "evil", container)
|
||||||
|
insert_at_hash("dab", "good", container)
|
||||||
|
|
||||||
|
|
||||||
|
get_at_hash("bad", container)
|
||||||
14
python/insertion-sort.py
Normal file
14
python/insertion-sort.py
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
def insertion_sort(array):
|
||||||
|
for index in range(1, len(array)):
|
||||||
|
temp_value = array[index]
|
||||||
|
position = index - 1
|
||||||
|
|
||||||
|
while position >= 0:
|
||||||
|
if array[position] > temp_value:
|
||||||
|
array[position + 1] = array[position]
|
||||||
|
position -= 1
|
||||||
|
else:
|
||||||
|
break
|
||||||
|
array[position + 1] = temp_value
|
||||||
|
|
||||||
|
return array
|
||||||
43
python/intersection.py
Normal file
43
python/intersection.py
Normal file
@@ -0,0 +1,43 @@
|
|||||||
|
from typing import List
|
||||||
|
|
||||||
|
|
||||||
|
def intersection(left: List[int], right: List[int]) -> List[int]:
|
||||||
|
result = []
|
||||||
|
for i in range(0, len(left)):
|
||||||
|
for j in range(0, len(right)):
|
||||||
|
if left[i] == right[j]:
|
||||||
|
result.append(left[i])
|
||||||
|
break
|
||||||
|
|
||||||
|
return result
|
||||||
|
|
||||||
|
|
||||||
|
def intersection2(left: List[int], right: List[int]) -> List[int]:
|
||||||
|
result = []
|
||||||
|
for left_val in left:
|
||||||
|
for right_val in right:
|
||||||
|
if left_val == right_val:
|
||||||
|
result.append(left_val)
|
||||||
|
break
|
||||||
|
|
||||||
|
return result
|
||||||
|
|
||||||
|
|
||||||
|
# silly to make intoa function but here we are
|
||||||
|
def intersection3(left: List[int], right: List[int]) -> List[int]:
|
||||||
|
return [i for i in left if i in right]
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
res = intersection([1, 2, 3, 4, 5], [5, 6, 7, 8, 9, 4])
|
||||||
|
res2 = intersection2([1, 2, 3, 4, 5], [5, 6, 7, 8, 9, 4, 5, 5, 5, 1, 2, 3, 4])
|
||||||
|
res3 = intersection3([1, 2, 3, 4, 5], [5, 6, 7, 8, 9, 4, 5, 5, 5, 1, 2, 3, 4])
|
||||||
|
|
||||||
|
# and the list comprehension
|
||||||
|
print(res)
|
||||||
|
print(res2)
|
||||||
|
print(res3)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
20
python/quicksort.py
Normal file
20
python/quicksort.py
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
def partition(arr, lo, hi):
|
||||||
|
pivot_value = arr[hi]
|
||||||
|
current_index = lo - 1
|
||||||
|
|
||||||
|
for i in range(lo, hi):
|
||||||
|
if arr[i] <= pivot_value:
|
||||||
|
current_index += 1
|
||||||
|
arr[current_index], arr[i] = arr[i], arr[current_index]
|
||||||
|
current_index += 1
|
||||||
|
arr[hi] = arr[current_index]
|
||||||
|
arr[current_index] = pivot_value
|
||||||
|
return current_index
|
||||||
|
|
||||||
|
def quicksort(arr, lo, hi):
|
||||||
|
if lo < hi:
|
||||||
|
part = partition(arr, lo, hi)
|
||||||
|
quicksort(arr, lo, part - 1)
|
||||||
|
quicksort(arr, part + 1, hi)
|
||||||
|
|
||||||
|
|
||||||
38
ts/bfs.test.ts
Normal file
38
ts/bfs.test.ts
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
import { tree } from "./tree"
|
||||||
|
|
||||||
|
type BinaryNode<T> = {
|
||||||
|
value: T;
|
||||||
|
left: BinaryNode<T> | null;
|
||||||
|
right: BinaryNode<T> | null;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
function bfs(head: BinaryNode<number> | null, needle: number): boolean {
|
||||||
|
const q: (BinaryNode<number> | null)[] = [head];
|
||||||
|
|
||||||
|
while (q.length) {
|
||||||
|
const current = q.shift() as BinaryNode<number>;
|
||||||
|
|
||||||
|
if (current.value === needle) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (current.left) {
|
||||||
|
q.push(current.left);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (current.right) {
|
||||||
|
q.push(current.right);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
test("bfs search works a number of values", function () {
|
||||||
|
expect(bfs(tree, 20)).toEqual(true);
|
||||||
|
expect(bfs(tree, -20)).toEqual(false);
|
||||||
|
expect(bfs(tree, 10)).toEqual(true);
|
||||||
|
expect(bfs(tree, 7)).toEqual(true);
|
||||||
|
})
|
||||||
47
ts/tree.ts
Normal file
47
ts/tree.ts
Normal file
@@ -0,0 +1,47 @@
|
|||||||
|
type BinaryNode<T> = {
|
||||||
|
value: T;
|
||||||
|
left: BinaryNode<T> | null;
|
||||||
|
right: BinaryNode<T> | null;
|
||||||
|
};
|
||||||
|
|
||||||
|
export const tree: BinaryNode<number> = {
|
||||||
|
value: 20,
|
||||||
|
right: {
|
||||||
|
value: 50,
|
||||||
|
right: {
|
||||||
|
value: 100,
|
||||||
|
right: null,
|
||||||
|
left: null,
|
||||||
|
},
|
||||||
|
left: {
|
||||||
|
value: 30,
|
||||||
|
right: {
|
||||||
|
value: 45,
|
||||||
|
right: null,
|
||||||
|
left: null,
|
||||||
|
},
|
||||||
|
left: {
|
||||||
|
value: 29,
|
||||||
|
right: null,
|
||||||
|
left: null,
|
||||||
|
}
|
||||||
|
},
|
||||||
|
},
|
||||||
|
left: {
|
||||||
|
value: 10,
|
||||||
|
right: {
|
||||||
|
value: 15,
|
||||||
|
right: null,
|
||||||
|
left: null,
|
||||||
|
},
|
||||||
|
left: {
|
||||||
|
value: 5,
|
||||||
|
right: {
|
||||||
|
value: 7,
|
||||||
|
right: null,
|
||||||
|
left: null,
|
||||||
|
},
|
||||||
|
left: null,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
44
ts/tree_traversal.test.ts
Normal file
44
ts/tree_traversal.test.ts
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
import { tree } from "./tree"
|
||||||
|
|
||||||
|
type BinaryNode<T> = {
|
||||||
|
value: T;
|
||||||
|
left: BinaryNode<T> | null;
|
||||||
|
right: BinaryNode<T> | null;
|
||||||
|
};
|
||||||
|
|
||||||
|
// pre order
|
||||||
|
function walk(current: BinaryNode<number> | null, path: number[]): number[] {
|
||||||
|
if (!current) {
|
||||||
|
return path;
|
||||||
|
}
|
||||||
|
|
||||||
|
path.push(current.value);
|
||||||
|
walk(current.left, path);
|
||||||
|
walk(current.right, path);
|
||||||
|
|
||||||
|
return path;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
function pre_order_search(head: BinaryNode<number>): number[] {
|
||||||
|
const path: number[] = [];
|
||||||
|
walk(head, path);
|
||||||
|
|
||||||
|
return path;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
test("Pre order", function () {
|
||||||
|
expect(pre_order_search(tree)).toEqual([
|
||||||
|
20,
|
||||||
|
10,
|
||||||
|
5,
|
||||||
|
7,
|
||||||
|
15,
|
||||||
|
50,
|
||||||
|
30,
|
||||||
|
29,
|
||||||
|
45,
|
||||||
|
100,
|
||||||
|
]);
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user