Skip to main content

Posts

Showing posts from June, 2018

Implementing A Queue With An Array

I'm working on coding an array implementation of a queue in Java.  The implementation should have the following features: The array has a head  which marks the item least recently inserted. The array has a tail  which marks* the item most recently inserted. Enqueue() adds an item to the tail of the array. Dequeue() removes an item from the head of the array. The array will resize()  dynamically as it gets too small or too large. Size()  returns the size of the queue from head to tail. IsEmpty()  returns whether or not the queue contains anything at all. You start out by initializing an empty array.  It looks like this: [0]  null  (head / tail) So at the beginning, the head is set to zero.  I guess the tail can also be zero.  Then you enqueue. [0] "To" (head / tail) So the first time you try to enqueue, the head is null.  Fill the head.  Now you want the tail to move up.   H...

Diary

Morning: reviewed and implemented an example of a stack based on the linked list data-type.  Learned about inner classes in Java. Afternoon: spent time talking with and helping other students.  Began working on a React implementation of Minesweeper (it was only yesterday that I figured out how to actually play the game!) Evening: went to a talk on GraphQL. Article: make a list of all the technologies you know, organized by category, then cross out the ones you aren't interested in -- BUT those technologies become "R&D" -- the industry keeps working on them, even if you don't, and eventually the fruits of its labors redound (word choice?) to the technologies you are familiar with.  The next implementation of JavaScript will incorporate whatever is good in Elm, even if you never get a chance to study Elm.  (Everything is connected and at some level everything is the same -- same concepts, similar problems.  Similarities before differences.) Goals: We...

Working Through The Efficient Three Sum Algorithm

This morning I spent an hour or so working on recreating the Three Sum algorithm in Java (I know because, for the sake of motivation, I timed myself).  I wanted to recreate the solution suggested in the class I'm following: go through all the possible pairs in the array and select, from the remaining values, all elements that sum to zero when added together with the pairs[1] -- using binary search. That's the solution that takes you from O(n^3) to O(n^2 log n) -- if I'm getting the notation right.  Because binary search for N elements is a log N algorithm: if you look through N elements by dividing them into halves, you can do that at worst log N times.  (8 elements = 2 halves of 4 elements = 4 parts of 2 elements = 8 singletons, or 8 => 4 => 2 => 1 = 3 steps -- I guess, not including 8 itself = log 8). Again, I had a few problems: I forgot to put my midpoint value inside the loop (it was not being recalculated each time the high and low points changed). ...

The Jump Algorithm

Meetup Went to a Coding Whiteboard Meetup tonight.  It was pretty great.  One of the leaders was even a CS master's student.  At first, honestly, I felt a little bit frustrated, especially because everyone around me seemed to be using pretty high level concepts / approaches that I wasn't familiar with.  But I found someone and relentlessly talked him through his approach until we both kind of realized there were issues in the problem we hadn't worked out yet.  I guess it just reinforces my feeling that when something seems too difficult, if you can, you need to find someone and force him to explain it to you in terms you can understand.  If the people around you really understand what they're about, they will have no problem and you'll learn a lot (assuming they're patient, I guess).  If they don't, you'll realize you aren't as alone as you thought you were.  Bit of the old Socrates. Problem So imagine you have an array with a bunch of numbe...

A Mistake In Binary Search / Making Three Sum More Efficient (Quick Note)

Not a lot of updates for today.  This morning I worked through a little bit of the algorithm course, including coding a very  sloppy version of binary search.  The Java...I'm still learning the Java.  But the main problem I was having was my binary search getting stuck in an endless loop.  It was because I was updating my high and low values improperly.  What was happening was that I was setting my low as the average of the difference between high and low, rather than setting my low as the current low plus  the average of the difference between high and low.  But once I figured that out I saw a much nicer implementation from the course instructor himself.  The big take away from the lesson, though, was that you can use binary search, which is pretty efficient (I forget exactly how efficient -- something something log N), to make the three-sum algorithm much more efficient.  Instead of searching through loops j, k, and l, for every unique...

Problems Come In Three Sums

Odyssey Today was a somewhat frustrating day.  I got held up 4 hours this morning with algorithms.  I've been trying to work through bits of Princeton's Algorithm course on Coursera .  So this morning I decided I was going to try and start following along with the code examples in Java.  Well I started with a sudo apt-get java-sdk  (or equivalent) -- simple, right?  Then I created my first "Hello World" application (a blast back to my high school days), figured out how to compile it with javac , and things were moving along.  Then I tried to follow along with the algorithm we were learning about, a brute force solution to the Three Sum problem, which included the following code: In in = new In(args[0]); I tried to compile it, naive as I was, and got an "unknown symbol" error (for that "In").  Stack Overflow -- oh, the Princeton course uses their own custom made libraries.  So I'll just add them to my working directory and import them, ...