Skip to content

Instantly share code, notes, and snippets.

View dmoore04's full-sized avatar

Danny Moore dmoore04

View GitHub Profile
@dmoore04
dmoore04 / dna.py
Last active October 10, 2021 22:53
CS50 Python: DNA - full solution
import sys
from helpers import create_lookup, profile_sequence
from load import load_files
# validate command line arguments
if len(sys.argv) < 3:
print("Usage: $ python dna.py [csv path] [dna path]")
# load the files into memory
database, sequence = load_files(sys.argv[1], sys.argv[2])
@dmoore04
dmoore04 / free-sig.c
Created September 30, 2021 17:40
free() signature
#include <stdlib.h>
void free(void *ptr); // free dynamically allocated memory
@dmoore04
dmoore04 / unload.c
Created September 30, 2021 17:35
CS50 PSET5: dictionary.c -> unload() final implementation
// Unloads dictionary from memory, returning true if successful, else false
bool unload(void)
{
for (int i = 0; i < N; i++) // iterate through buckets
{
node *p = table[i]; // initalize pointer at head of current bucket
while (p != NULL) // while there are nodes left in the bucket
{
node *tmp = p; // alias current node to temp
@dmoore04
dmoore04 / check.c
Created September 30, 2021 16:30
CS50 PSET 5: dictionary.c -> check() final implementation
// Returns true if word is in dictionary, else false
bool check(const char *word)
{
int hash_value = hash(word); // get a hash for the word
node *p = table[hash_value]; // point at the head of word's bucket
while (p != NULL) // while there is more nodes to look at
{
if (strcasecmp(word, p->word) == 0) // case insensitive string comparison
{
@dmoore04
dmoore04 / globals.c
Last active September 30, 2021 15:53
CS50 PSET5: dictionary.c -> globals
typedef struct node // Represents a node in a hash table
{
char word[LENGTH + 1];
struct node *next;
}
node;
const unsigned int N = 26; // Number of buckets in hash table
node *table[N]; // table of nodes, our Hash Table
@dmoore04
dmoore04 / default-globals.c
Created September 30, 2021 15:49
dictionary.c -> default global variables
typedef struct node // Represents a node in a hash table
{
char word[LENGTH + 1];
struct node *next;
}
node;
const unsigned int N = 1; // Number of buckets in hash table
node *table[N]; // table of nodes, our Hash Table
@dmoore04
dmoore04 / size.c
Created September 30, 2021 15:44
CS50 PSET5: dictionary.c -> size() final implementation
// Returns number of words in dictionary if loaded, else 0 if not yet loaded
unsigned int size(void)
{
if (dict_size > 0) // if a dictionary is loaded
{
return dict_size // return its size
}
return 0; // default return if no dict is loaded
}
@dmoore04
dmoore04 / hash.c
Created September 30, 2021 15:28
CS50 PSET5: dictionary.c -> hash() final implementation
// Hashes word to a number
unsigned int hash(const char *word)
{
/* djb2 by Dan Bernstring - adapted to be case insensitive */
unsigned long hash = 5381;
int c;
while ((c = tolower(*word++))) // convert chars to lowercase
{
hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
}
@dmoore04
dmoore04 / load.c
Last active September 30, 2021 14:20
CS50 PSET5: dictionary.c -> load() final implementation
// Loads dictionary into memory, returning true if successful, else false
bool load(const char *dictionary)
{
FILE *dict = fopen(dictionary, "r"); // initalize a pointer to our stream
if (dict == NULL) // check for errors
{
return false; // fail fast and return false; // fail fast and return false
}
char word[LENGTH + 1]; // create a buffer to hold our word
@dmoore04
dmoore04 / express-controller-snippet.json
Last active September 29, 2021 15:11
Async Express Controller Snippet
{
"Express Controller async Function": {
"prefix": "ctrla",
"body": [
"exports.$1 = async (req, res, next) => {\n\ttry {\n\t\t$0\n\t} catch(err) {\n\t\tnext(err)\n\t}\n}"
],
"description": "Creates a skeleton for an asynchronous Express controller function"
}
}