Skip to content

Commit

Permalink
Merge pull request #2 from illourr/some-changes
Browse files Browse the repository at this point in the history
Some updates to the repo
  • Loading branch information
illourr authored Jan 27, 2024
2 parents f75bd19 + 6917e20 commit d98005b
Show file tree
Hide file tree
Showing 13 changed files with 398 additions and 2 deletions.
14 changes: 14 additions & 0 deletions .github/workflows/bun-tests.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
name: bun-tests
on: [push]
jobs:
run-bun-tests:
name: run-bun-tests
runs-on: ubuntu-latest
steps:
# ...
- uses: actions/checkout@v3
- uses: oven-sh/setup-bun@v1

# run any `bun` or `bunx` command
- run: bun install
- run: bun test
2 changes: 2 additions & 0 deletions .vercelignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
learning/
.github/
Binary file modified bun.lockb
Binary file not shown.
76 changes: 76 additions & 0 deletions learning/datastructures/LinkedList.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
type NodeVal = number;
class ListNode {
val: NodeVal;
next: ListNode | null;
constructor(value: NodeVal) {
this.val = value;
this.next = null;
}
}
class MyLinkedList {
list: ListNode | null;
constructor() {
this.list = null;
}
valueOf() {
return this.list;
}

get(index: number): number {
let position = 0;
let currentNode = this.list;
while (position !== index && currentNode !== null) {
currentNode = currentNode.next;
position++;
}
if (position === index && currentNode !== null) {
return currentNode.val;
}
return -1;
}

addAtHead(val: number): void {
let next = this.list;
const newNode = new ListNode(val);
if (next !== null) {
newNode.next = next;
}
this.list = newNode;
}

addAtTail(val: number): void {
if (this.list == null) {
return;
}

let currentNode = this.list;
while (currentNode.next) {
currentNode = currentNode.next;
}
currentNode.next = new ListNode(val);
}

addAtIndex(index: number, val: number): void {}

deleteAtIndex(index: number): void {}
}

/**
* Your MyLinkedList object will be instantiated and called as such:
* var obj = new MyLinkedList()
* var param_1 = obj.get(index)
* obj.addAtHead(val)
* obj.addAtTail(val)
* obj.addAtIndex(index,val)
* obj.deleteAtIndex(index)
*/

const list = new MyLinkedList();

list.addAtHead(2);
list.addAtTail(5);
list.addAtHead(1);
list.addAtTail(7);
list.addAtTail(9);

console.log(list.get(3));
105 changes: 105 additions & 0 deletions learning/datastructures/Tries.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
class TrieNode {
character: string;
end: boolean;
children: Array<TrieNode>;

constructor(char = "#root", end = false) {
this.character = char;
this.end = end;
this.children = [];
}
addChild(node: TrieNode): void {
if (!this.hasChild(node.character)) {
this.children.push(node);
}
}
hasChild(char: string): boolean {
return this.children.some((childNode) => childNode.character === char);
}
}
class Trie {
root: TrieNode;
constructor() {
this.root = new TrieNode();
}

insert(word: string): void {
let characters = word.split("");
let currentNode = this.root;
while (characters.length > 0) {
const char = characters.shift()!;
if (!currentNode.hasChild(char)) {
const newNode = new TrieNode(char, characters.length === 0);
currentNode.addChild(newNode);
currentNode = newNode;
} else {
currentNode = currentNode.children.find((n) => n.character === char)!;
if (characters.length === 0) {
currentNode.end = true;
}
}
}
}

search(word: string): boolean {
console.log("start search");
let characters = word.split("");
let currentNode = this.root;
while (characters.length > 0) {
// get the head of the characters
const char = characters.shift()!;

// short circuit if we don't have a match
if (!currentNode.hasChild(char)) return false;

currentNode = currentNode.children.find((n) => n.character === char)!;

if (currentNode.end && characters.length === 0) {
return true;
}
}
return false;
}

startsWith(prefix: string): boolean {
console.log("start search");
let characters = prefix.split("");
let currentNode = this.root;
while (characters.length > 0) {
// get the head of the characters
const char = characters.shift()!;

// short circuit if we don't have a match
if (!currentNode.hasChild(char)) return false;

currentNode = currentNode.children.find((n) => n.character === char)!;

if (characters.length === 0) {
return true;
}
}
return false;
}
print() {
return console.log(JSON.stringify(this.root, null, 2));
}
}

/**
* Your Trie object will be instantiated and called as such:
* var obj = new Trie()
* obj.insert(word)
* var param_2 = obj.search(word)
* var param_3 = obj.startsWith(prefix)
*/

var obj = new Trie();

obj.insert("apple");
obj.insert("apply");
obj.insert("app");
const s = obj.startsWith("ap");

console.log({ s });

// obj.print();
14 changes: 14 additions & 0 deletions learning/multithreaded-javascript/ch1-c-threads/happycoin.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include <inttypes.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

uint64_t random64(uint32_t * seed) {
uint64_t result;
uint8_t * result8 = (uint8_t *)&result;
for (size_t i = 0; i< sizeof(result); i++) {
result8[i] = rand_r(seed);
}
return result;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/// <reference types="astro/client" />
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
// Bun Snapshot v1, https://goo.gl/fbAQLP

exports[`big ole bigints work 1`] = `
[
3,
0,
3,
9,
2,
4,
8,
3,
2,
0,
4,
8,
3,
2,
9,
0,
4,
3,
8,
2,
0,
4,
9,
3,
2,
0,
4,
9,
8,
3,
2,
0,
4,
8,
3,
2,
4,
8,
2,
3,
0,
4,
8,
3,
2,
0,
9,
4,
8,
0,
9,
2,
4,
8,
0,
9,
3,
2,
8,
4,
0,
9,
3,
2,
8,
4,
0,
9,
8,
3,
2,
4,
0,
9,
8,
3,
2,
4,
9,
0,
3,
2,
9,
4,
8,
2,
3,
4,
8,
]
`;
25 changes: 25 additions & 0 deletions learning/utilities/__tests__/getDigits.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { expect, test } from "bun:test";
import { getDigits } from "../getDigits";

test("positive number", () => {
const value = getDigits(100);
expect(value).toEqual([1, 0, 0]);
});

test("negative number", () => {
const value = getDigits(-234);
expect(value).toEqual([2, 3, 4]);
});

test("0 is 0", () => {
const value = getDigits(0);
expect(value).toEqual([0]);
});

test("a big number should throw", () => {
expect(() => {
// function that should throw
const value =
getDigits(30923243243434343434333434334343434343123123213433432442234243);
}).toThrow();
});
15 changes: 15 additions & 0 deletions learning/utilities/__tests__/getDigitsBigInt.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { test, expect } from "bun:test";
import { getDigitsBigInt } from "../getDigits";

test("big ole bigints work", () => {
expect(
getDigitsBigInt(
30392483204832904382049320498320483248230483209480924809328409328409832409832490329482348n,
),
).toEqual([
3, 0, 3, 9, 2, 4, 8, 3, 2, 0, 4, 8, 3, 2, 9, 0, 4, 3, 8, 2, 0, 4, 9, 3, 2,
0, 4, 9, 8, 3, 2, 0, 4, 8, 3, 2, 4, 8, 2, 3, 0, 4, 8, 3, 2, 0, 9, 4, 8, 0,
9, 2, 4, 8, 0, 9, 3, 2, 8, 4, 0, 9, 3, 2, 8, 4, 0, 9, 8, 3, 2, 4, 0, 9, 8,
3, 2, 4, 9, 0, 3, 2, 9, 4, 8, 2, 3, 4, 8,
]);
});
Loading

0 comments on commit d98005b

Please sign in to comment.