Skip to content

Commit

Permalink
Started setting up blog, some learning materials
Browse files Browse the repository at this point in the history
  • Loading branch information
illourr committed Jan 29, 2024
1 parent 6917e20 commit 7c052e4
Show file tree
Hide file tree
Showing 23 changed files with 349 additions and 91 deletions.
3 changes: 2 additions & 1 deletion astro.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ import { defineConfig } from "astro/config";
import react from "@astrojs/react";
import tailwind from "@astrojs/tailwind";
import vercel from "@astrojs/vercel/serverless";
import mdx from "@astrojs/mdx";

// https://astro.build/config
export default defineConfig({
integrations: [react(), tailwind()],
integrations: [react(), tailwind(), mdx()],
output: "hybrid",
adapter: vercel({
webAnalytics: {
Expand Down
Binary file modified bun.lockb
Binary file not shown.
123 changes: 71 additions & 52 deletions learning/datastructures/LinkedList.ts
Original file line number Diff line number Diff line change
@@ -1,76 +1,95 @@
type NodeVal = number;
class ListNode {
val: NodeVal;
next: ListNode | null;
constructor(value: NodeVal) {
this.val = value;
class MyNode {
val: number;
next: MyNode | null;
constructor(val: number) {
this.val = val;
this.next = null;
}
}

class MyLinkedList {
list: ListNode | null;
head: MyNode | null;
length: number;
constructor() {
this.list = null;
}
valueOf() {
return this.list;
this.head = null;
this.length = 0;
}

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

let current = this.head;
for (let i = 0; i < index; i++) {
current = current!.next;
}
return -1;

return current!.val;
}

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

addAtTail(val: number): void {
if (this.list == null) {
const newNode = new MyNode(val);

if (!this.head) {
this.head = newNode;
} else {
let current = this.head;
while (current.next) {
current = current.next;
}
current.next = newNode;
}

this.length++;
}

addAtIndex(index: number, val: number): void {
if (index < 0 || index > this.length) {
return;
}

let currentNode = this.list;
while (currentNode.next) {
currentNode = currentNode.next;
if (index === 0) {
this.addAtHead(val);
} else if (index === this.length) {
this.addAtTail(val);
} else {
let current = this.head;
for (let i = 0; i < index - 1; i++) {
current = current!.next;
}

const newNode = new MyNode(val);
newNode.next = current!.next;
current!.next = newNode;

this.length++;
}
currentNode.next = new ListNode(val);
}

addAtIndex(index: number, val: number): void {}
deleteAtIndex(index: number): void {
if (index < 0 || index >= this.length) {
return;
}

deleteAtIndex(index: number): void {}
}
if (index === 0) {
this.head = this.head!.next;
} else {
let current = this.head;
for (let i = 0; i < index - 1; i++) {
current = current!.next;
}

/**
* 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));
current!.next = current!.next!.next;
}

this.length--;
}
}
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
#include <inttypes.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <pthread.h>

struct happy_result {
size_t count;
uint64_t * nums;
};

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;
}

uint64_t sum_digits_squared(uint64_t num) {
uint64_t total = 0;
while (num > 0) {
uint64_t num_mod_base = num % 10;
total += num_mod_base * num_mod_base;
num = num / 10;
}
return total;
}

bool is_happy(uint64_t num) {
while (num != 1 && num != 4) {
num = sum_digits_squared(num);
}
return num == 1;
}

bool is_happycoin(uint64_t num) {
return is_happy(num) && num % 10000 == 0;
}

void * get_happycoins(void * arg) {
int attempts = *(int *)arg;
int limit = attempts/10000;
uint32_t seed = time(NULL);
uint64_t * nums = malloc(limit * sizeof(uint64_t));
struct happy_result * result = malloc(sizeof(struct happy_result));
result->nums = nums;
result->count = 0;
for(int i = 1; i < attempts; i++) {
if (result->count == limit) {
break;
}
uint64_t random_num = random64(&seed);
if (is_happycoin(random_num)) {
result->nums[result->count++] = random_num;
}
}
return (void *)result;
}

#define THREAD_COUNT 4

int main() {
pthread_t thread [THREAD_COUNT];

int attempts = 10000000/THREAD_COUNT;
int count = 0;
for(int i = 0; i < THREAD_COUNT; i++) {
pthread_create(&thread[i], NULL, get_happycoins, &attempts);
}

for(int j = 0; j < THREAD_COUNT; j++) {
struct happy_result * result;
pthread_join(thread[j], (void **)&result);
count += result->count;
for(int k = 0; k < result->count; k++) {
printf("%" PRIu64 " ", result->nums[k]);
}
free(result->nums);
free(result);
}
printf("\ncount %d\n", count);
return 0;
}
35 changes: 35 additions & 0 deletions learning/multithreaded-javascript/ch1-c-threads/happycoin.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,39 @@ uint64_t random64(uint32_t * seed) {
result8[i] = rand_r(seed);
}
return result;
}

uint64_t sum_digits_squared(uint64_t num) {
uint64_t total = 0;
while (num > 0) {
uint64_t num_mod_base = num % 10;
total += num_mod_base * num_mod_base;
num = num / 10;
}
return total;
}

bool is_happy(uint64_t num) {
while (num != 1 && num != 4) {
num = sum_digits_squared(num);
}
return num == 1;
}

bool is_happycoin(uint64_t num) {
return is_happy(num) && num % 10000 == 0;
}

int main() {
uint32_t seed = time(NULL);
int count = 0;
for (int i = 1; i < 10000000; i++) {
uint64_t random_num = random64(&seed);
if (is_happycoin(random_num)) {
printf("%" PRIu64 " ", random_num);
count++;
}
}
printf("\ncount %d\n", count);
return 0;
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<html>
<head>
<title>Shared Workers Blue</title>
<script src="blue.js"></script>
</head>
</html>
7 changes: 7 additions & 0 deletions learning/multithreaded-javascript/ch2-shared-workers/blue.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
console.log("blue.js");

const worker = new SharedWorker("shared-worker.js");

worker.port.onmessage = (event) => {
console.log("EVENT", event.data);
};
6 changes: 6 additions & 0 deletions learning/multithreaded-javascript/ch2-shared-workers/red.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<html>
<head>
<title>Shared Workers Red</title>
<script src="red.js"></script>
</head>
</html>
7 changes: 7 additions & 0 deletions learning/multithreaded-javascript/ch2-shared-workers/red.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
console.log("red.js");

const worker = new SharedWorker("shared-worker.js");

worker.port.onmessage = (event) => {
console.log("EVENT", event.data);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const ID = Math.floor(Math.random() * 999999);
console.log("shared-worker.js", ID);

const ports = new Set();

self.onconnect = (event) => {
const port = event.ports[0];
ports.add(port);
console.log("CONN", ID, ports.size);

port.onmessage = (event) => {
console.log("MESSAGE", ID, event.data);

for (let p of ports) {
p.postMessage([ID, event.data]);
}
};
};
6 changes: 6 additions & 0 deletions learning/multithreaded-javascript/ch2-web-workers/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<html>
<head>
<title>Web Workers Hello World</title>
<script src="main.js"></script>
</head>
</html>
10 changes: 10 additions & 0 deletions learning/multithreaded-javascript/ch2-web-workers/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
console.log("hello from main.js");

const worker = new Worker("worker.js");
worker.onmessage = (msg) => {
console.log("message received from worker:", msg.data);
};

worker.postMessage("message sent to worker");

console.log("hello from end of main.js");
6 changes: 6 additions & 0 deletions learning/multithreaded-javascript/ch2-web-workers/worker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
console.log("hello from worker.js");

self.onmessage = (msg) => {
console.log("message from main:", msg.data);
postMessage("message sent from worker");
};
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
},
"dependencies": {
"@astrojs/check": "^0.3.4",
"@astrojs/mdx": "^2.1.0",
"@astrojs/react": "^3.0.9",
"@astrojs/tailwind": "^5.1.0",
"@astrojs/vercel": "^7.0.1",
Expand Down
Loading

0 comments on commit 7c052e4

Please sign in to comment.