/\    /\
     /  \  /  \
    /    \/    \
   /            \
  /              \
 /                
/                  
    

09/01/24

Rating: 9/10, Sleep: 6.5 hours


summary

Sunday 7km run + weights, finished NextJS course, started algorithms/leetcode

A productive day, but also humbling as I realize how basic a programmer I still am. Got up at 730am which is probably the earliest I've awoken since I moved here. I feel my understanding is much greater at a conceptual level, but when I actually go to code, I struggle with the most basic things. Watched several hours of NextJS to wrap up that tutorial, going to tackle Microservices next. Also started the algorithms/Leetcode Udemy course which has been very well structured. It was particularly helpful to code up the methods for LinkedLists, including push, pop. shift, unshift, get, set, insert, remove and reverse. Ran 7km and did deadlifts/squats as well at the gym. Had dakkalbi with dad, soup for dinner and toast for late night snack, together with three coffees. Really thinking about cutting back caffeine as I find my body is just too sensitive to it as I grow older.

Emotions

What I Learned

Exercise

Food

Slog

Code

Proud of myself for getting these methods for LinkedLists down.

class Node {
    constructor(value){
        this.value = value;
        this.next = null;
    }
}

class LinkedList {
    constructor(value) {
        const newNode = new Node(value);
        this.head = newNode;
        this.tail = this.head;
        this.length = 1;
    }

    printList() {
        let temp = this.head;
        while (temp !== null) {
            console.log(temp.value);
            temp = temp.next;
        }
    }

    getHead() {
        if (this.head === null) {
            console.log("Head: null");
        } else {
            console.log("Head: " + this.head.value);
        }
    }

    getTail() {
        if (this.tail === null) {
            console.log("Tail: null");
        } else {
            console.log("Tail: " + this.tail.value);
        }
    }

    getLength() {
        console.log("Length: " + this.length);
    }

    makeEmpty() {
        this.head = null;
        this.tail = null;
        this.length = 0;
    }

    push(value) {
        const newNode = new Node(value);
        if (!this.head) {
            this.head = newNode;
            this.tail = newNode;
        } else {
            this.tail.next = newNode;
            this.tail = newNode;
        }
        this.length++;
        return this;
    }

    pop() {
        if (this.length === 0) return undefined;
        let temp = this.head;
        let pre = this.head;
        while (temp.next) {
            pre = temp;
            temp = temp.next;
        }
        this.tail = pre;
        this.tail.next = null;
        this.length--;
        if (this.length === 0) {
            this.head = null;
            this.tail = null;
        }
        return temp;
    }

    unshift(value) {
        const newNode = new Node(value);
        if (!this.head) {
            this.head = newNode;
            this.tail = newNode;
        } else {
            newNode.next = this.head;
            this.head = newNode;
        }
        this.length++;
        return this;
    }

    shift() {
        if (this.length === 0) return undefined;
        let temp = this.head;
        this.head = this.head.next;
        this.length--;
        if (this.length === 0) {
            this.tail = null;
        }
        temp.next = null;
        return temp;
    }

    get(index) {
        if (index < 0 || index >= this.length) return undefined;
        let temp = this.head;
        for (let i = 0; i < index; i++) {
            temp = temp.next;
        }
        return temp;
    }

    set(index, value) {
        let temp = this.get(index);
        if (temp) {
            temp.value = value;
            return true;
        }
        return false;
    }

    insert(index, value) {
        if (index < 0 || index > this.length) return false;
        if (index === this.length) return this.push(value);
        if (index === 0) return this.unshift(value);

        const newNode = new Node(value);
        const temp = this.get(index - 1);
        newNode.next = temp.next;
        temp.next = newNode;
        this.length++;
        return true;
    }

    remove(index) {
        if (index < 0 || index >= this.length) return undefined;
        if (index === 0) return this.shift();
        if (index === this.length - 1) return this.pop();

        const before = this.get(index - 1);
        const temp = before.next;

        before.next = temp.next;
        temp.next = null;
        this.length--;
        return temp;
    }

	reverse() {
	    let temp = this.head;
	    this.head = this.tail;
	    this.tail = temp;
	    let next = temp.next;
	    let prev = null;
	    for (let i=0; i<this.length; i++){
	        next = temp.next
	        temp.next = prev
	        prev=temp
	        temp = next
	    }
	    return this
	}

}