A blog by J. Weyh
The last two days were rough. But I try to keep going, try to keep learning.
My last entry was from Tuesday. Today is Friday. On Wednesday, I mainly worked on connecting my Spring Boot project to the H2 database. Before I did that, I watched the tutorial for a little while to see how the connection to the external database would go, and the host connected his project to a local Postgres database while using Docker. However, my database will live on Opa's server, so my setup is going to differ. I tried researching how exactly these steps look practically, but due to me not knowing my way around any of those frameworks and tools and the information differing in so many details, it was hard to find the information I was looking for - especially since I didn't really know what to look for. After 2 hours of reading I decided that this approach didn't seem very fruitful, and decided to just go with the flow instead. So that's when I started to write the Java code that lets my tiny API talk to the H2 database. This turned out to be surprisingly fun, until I ran into a weird compiler error.
IntelliJ keeps complaining that it doesn't recognize the parameter "id" which is the primary key of my database table. This @GetMapping method filters entries in my table by ID. If my understanding is correct, the JDBC methods map my custom class properties to the table's properties and vice versa. In this particular method, I'm using the param() method to filter the table, but due to the error I can't pass the id. I hope I'll find a solution later.
I also solved my second LeetCode problem on Wednesday. It was another array problem, where I was supposed to filter out values of an int array that matched a passed int parameter (in place). I ended up iterating over the array backwards, pulling values unequal to the value forward. I passed all tests, but when inspecting the most popular solutions, I felt kinda bad about myself seeing how smart those solutions were and how unelegant mine was in comparison. However, I'm supposed to look at the bright side: by looking at those solutions I learnt about the concept of having two "pointers" onto the array, where you use one to read values and the other one to write values. And it was good that I learnt that, because I was able to implement that new knowledge in my third problem that I worked on yesterday. And it paid out! Look at this:
A near perfect solution! I was so proud - especially since I didn't google anything, I didn't look at anything, I didn't talk to anyone about it - I worked on it all by myself; sketching out the problem with notebook and pencil first and then turning the idea into code. The code is elegant and concise and I'm really proud. It probably sounds ridiculous, but this is the first time I came up with a solution that didn't read like "student learning programming" but "person who does programming". And since I'm so proud, I'm gonna share my code (even if it's just an "easy" problem):
class Solution {
public int removeDuplicates(int[] nums) {
int writer = 1;
for (int reader = 1; reader < nums.length; reader++) {
if (nums[reader] != nums[writer-1]) {
nums[writer] = nums[reader];
writer++;
}
}
return writer;
}
}
Unfortunately that's the only thing I've accomplished yesterday. Today I want to work on my blog layout and therefore my HTML & CSS skills a bit, so I'm gonna include a screenshot of what the blog currently looks like. I managed to depict my blog entries in a (makeshift) card design!