Skip to content

Commit

Permalink
this
Browse files Browse the repository at this point in the history
  • Loading branch information
bronifty committed Oct 13, 2024
1 parent 4e26786 commit f09eab9
Show file tree
Hide file tree
Showing 6 changed files with 174 additions and 3 deletions.
2 changes: 1 addition & 1 deletion docs/courses/cs50/_meta.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
["home", "c", "python", "javascript"]
["home", "c", "python"]
79 changes: 79 additions & 0 deletions docs/courses/cs50/c.mdx
Original file line number Diff line number Diff line change
@@ -1 +1,80 @@
# C

## Binary Search Tree

```c
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

typedef struct node {
int number;
struct node *left;
struct node *right;
} node;

bool search(node *tree, int number){
if (tree == NULL) return false;
if (tree->number == number) return true;
if (number < tree->number) return search(tree->left, number);
else return search(tree->right, number);
}

int main(void) {
// Create a sample binary search tree
node *root = (node *)malloc(sizeof(node));
root->number = 5;
root->left = (node *)malloc(sizeof(node));
root->left->number = 3;
root->left->left = (node *)malloc(sizeof(node));
root->left->left->number = 1;
root->left->left->left = NULL;
root->left->left->right = NULL;
root->left->right = (node *)malloc(sizeof(node));
root->left->right->number = 4;
root->left->right->left = NULL;
root->left->right->right = NULL;
root->right = (node *)malloc(sizeof(node));
root->right->number = 7;
root->right->left = (node *)malloc(sizeof(node));
root->right->left->number = 6;
root->right->left->left = NULL;
root->right->left->right = NULL;
root->right->right = (node *)malloc(sizeof(node));
root->right->right->number = 8;
root->right->right->left = NULL;
root->right->right->right = NULL;

// Test the search function
int numbers_to_search[] = {1, 4, 6, 9};
int num_searches = sizeof(numbers_to_search) / sizeof(numbers_to_search[0]);

for (int i = 0; i < num_searches; i++) {
int number = numbers_to_search[i];
bool found = search(root, number);
printf("Number %d is %s in the tree.\n", number, found ? "found" : "not found");
}


free(root->right->right);
free(root->right->left);
free(root->right);
free(root->left->right);
free(root->left->left);
free(root->left);
free(root);

return 0;
}
```
## Tree Structure Diagram
```
5
/ \
3 7
/ \ / \
1 4 6 8

```
1 change: 0 additions & 1 deletion docs/courses/cs50/javascript.mdx

This file was deleted.

63 changes: 63 additions & 0 deletions docs/courses/cs50/python.mdx
Original file line number Diff line number Diff line change
@@ -1 +1,64 @@
# Python

## Dictionary Search

Dictionary refers to a list of word keys without any values in a file separated by line breaks. The script loads the file, splits the set (collection of unique keys) at each line break, and runs a check in main to see if some test words are in the set.

````py
# dictionary.py
words = set()

def load(dictionary):
with open(dictionary, "r") as file:
words.update(file.read().splitlines())
return True

def check(word):
return word.lower() in words

def size():
return len(words)

def unload():
return True

def main():
# Load the dictionary
dictionary_file = "test_dictionary.txt"
if load(dictionary_file):
print(f"Loaded {size()} words from {dictionary_file}")
else:
print("Failed to load dictionary")
return

# Test some words
test_words = ["apple", "banana", "carrot", "DATE", "fig"]
for word in test_words:
if check(word):
print(f"'{word}' is in the dictionary")
else:
print(f"'{word}' is NOT in the dictionary")

# Unload the dictionary
if unload():
print("Dictionary unloaded successfully")
else:
print("Failed to unload dictionary")


# if __name__ == "__main__" `main()` is only called if the script is run directly (not imported as a module).
if __name__ == "__main__":
main()


# touch dictionary_file.txt
# echo "apple" >> dictionary_file.txt
# echo "banana" >> dictionary_file.txt
# echo "cherry" >> dictionary_file.txt
# echo "date" >> dictionary_file.txt
# echo "elderberry" >> dictionary_file.txt
# echo "fig" >> dictionary_file.txt
# echo "grape" >> dictionary_file.txt
# echo "honeydew" >> dictionary_file.txt
```py
````
2 changes: 1 addition & 1 deletion docs/reference/vscode/_meta.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
["container", "lldb"]
["container", "lldb", "launch.json"]
30 changes: 30 additions & 0 deletions docs/reference/vscode/launch.json.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
```json
{
"version": "0.2.0",
"configurations": [
{
"name": "lldb",
"type": "lldb",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}",
"cwd": "${workspaceFolder}",
"preLaunchTask": "clang++"
},
{
"name": "python",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"cwd": "${fileDirname}"
},
{
"name": "go",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${file}"
}
]
}
```

0 comments on commit f09eab9

Please sign in to comment.