Skip to main content

Posts

Showing posts from 2018

Compiling and Executing Java Files With -cp

I decided I was going to "man up" and figure out how to compile a java program with an external dependency from the command line instead of relying on an IDE-- the DOS command line, to be more specific. I ran into a few problems: 1.  The external dependency was given to me as a java file.  I experimented compiling it as a .jar, but I wasn't sure how to import a class from a .jar, so I ended up compiling it into a class. 2.  When I tried to run the file, I got an error saying that the class had been compiled with a different version of Java than my JRE.  The Internet told me to check my path variable for Java.  It sure looked like it was pointing to the latest JRE (and the same version of Java as my compiler).  I asked the Internet again and found the following command: for %I in (java.exe) do @echo %~$PATH:I I'm not exactly sure what the syntax of that magic command is (intuitively it's returning the path that executes when I run the "java" com...

Shell Sort Notes

I don't understand this passage.  What are the two alternatives? ( Algorithms , 258) One way to implement shellsort would be, for each h, to use insertion sort indepen- dently on each of the h subsequences. Because the subsequences are independent, we can use an even simpler approach: when h-sorting the array, we insert each item among the previous items in its h-subsequence by exchanging it with those that have larger keys (moving them each one position to the right in the subsequence). Have to research different implementations of Shell Sort.  Would also help to clarify the mistake I made in my discussion yesterday. Having said that, I can think of two possibilities.  Given, for instance, h  of 3   and a sequence: 1 2 3 4 5 6 7 8 9 -- two methods would be first : Compare and exchange 1 and 4... ...2 and 5 ...3 and 6 ...4 and 7 ...5 and 8 ...6 and 9 -- or second : Do an insertion sort on 1, 4, and 7 Do an insertion sort on 2, 5, and 8 ...

Shell Sort

Today I spent a little bit of time researching the "Shell" sort.  I wanted to post a few notes about the Princeton Algorithms Course's implementation to help me solidify my understanding. First, a little tidbit.  When I first heard about this algorithm, I thought it had something to do with shell games.  Turns out a man named Donald Shell discovered this method of sorting, whence the name. The Algorithms  book gives the following explanation (Sedgewick and Wayne,  Algorithms, 4th ed., p. 258): The idea is to rearrange the array to give it the property that taking every hth entry (starting anywhere) yields a sorted subsequence. Such an array is said to be h-sorted. Put another way, an h-sorted array is h independent sorted subsequences, interleaved together. By h-sorting for some large values of h, we can move items in the array long distances and thus make it easier to h-sort for smaller values of h. Using such a procedure for any sequence of values o...

Diary

First day as a TA at Coding Dojo Worked up through the automated testing portion of the Django official tutorial Didn't have time to do any algorithms -- learned about a path-finding problem (2D array represents connections between nodes -- determine whether there is a path from A to B for arbitrary A and B) e.g. for nodes A, B, and C [ [0, 1, 0] , [0 , 0, 1], [0, 0, 0] ] First entry says A is connected to B, second that B is connected to C -- so there's a path from A to C. The connection relationship is not robust.  A can be connected to B without B being connected to A.  It must however in some sense give rise to another relationship that is at least transitive.  If A and B are connected, and B and C are connected, then I can get from A to C.  This does not mean, however, that I can get back from C to A. Initial thought was do something with recursion.  Too complicated.  And what if you have loops?  Now it seems you just need a way to trans...

How To Get More Out Of Tutorials

We all follow tutorials.  Coming up with an idea for a new app, including the scaffolding and the specific functionality, is hard.  Struggling through something on your own can be valuable and helps you internalize it, but you also risk "reinventing the wheel" if you fail to take advantage of the resources and experience available from those who have "done it before."  Tutorials help you work through an ambitious project step by step and "learn while doing."   But blindly copying someone else's code without knowing why you are doing what you are doing can severely limit your learning.  Use the following steps to get the best of both worlds -- a dedicated teacher who will guide you every step of the way through the app you want to make AND a system that will hold you responsible for what you learn and help you to internalize it so that you can generalize specific steps for future pet projects (or work assignments). 1. Pause The Tutorial Pause t...

Starting A New Project In Django

Purpose What follows are streamlined notes following Django's web app tutorial . Project Assuming that you have Python and Django installed and, if needed, are running the appropriate environment, you begin a project by navigating to the desired project directory and running the command django-admin startproject <project name here>  This creates a directory for your project with the basic file structure. App A Django app encapsulates some site function (e.g. registration). To create a Django app, run the following command in the directory where you want to implement it: python manage.py startapp <app name here> As of Django 2, it appears that the app is automatically registered with the setup (settings.py), saving time. Errata.   Above I stated that an app does not need to be "installed" in Django 2.  This is not correct.  To install an app (necessary for using app models), add <app name>.apps.<app name>Config (e.g. Example...

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, ...

Quick Find / Quick Union (Connected Nodes)

Setup This week I learned about the "Quick Find" or "Quick Union" algorithm. Imagine an NxN grid of nodes, some of which are connected by lines. A connection can be interpreted as accessibility: if two nodes are connected, you can get from one to the other. Every node is accessible to itself: to get where you already are, stay there. Also, If you can get from A to B, you can go back from B to A. And if you can get from A to B and from B to C, then you can get from A to C. As a consequence, the connection between nodes divides the grid into regions of mutually accessible nodes. You can travel from any node in a given region to any other node in that region -- but not to any nodes outside that region (exercise to reader -- proof by contradiction). The problem has two parts. First, find a way to represent this grid structure and the accessibility relation; second, use your schema to efficiently calculate whether two given nodes are accessible to each other. ...

Microsoft's EF Tutorial: Seeding Your Database

Quick note on a solution to what could have been a game-breaking problem.  Was building out Microsoft's tutorial app for .NET Core with the EF Framework and got to the section on seeding the database.  To set up your connection string, you get the following line of code: (In Startup.cs ) public void ConfigureServices(IServiceCollection services) {  services.AddDbContext<SchoolContext>(   options =>   options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")) );  services.AddMvc(); } When I ran my build, in conjunction with the other seeding stuff the tutorial gives you, I got all sorts of errors.  For example, I would set up my connection string as follows: server=localhost;user=root;password=root;database=mydb;port=3306 And get an error: " Keyword is not supported: port. " I panicked.  Microsoft's system for building out the database wasn't going to work, and I was going to have to figure out a workaround ...

The Easy Way To Create Custom Validation In .NET Core MVC

I'm finding a lot of tutorials out there with tons to slog through, but for basic validations it doesn't have to be that complicated.  Somewhere in your model (if you want), define a class for the attribute you'd like.  The class will inherit from  ValidationAttribute .  It will have a method, public override bool IsValid , that takes an object as its argument.  The object is what the field gives you.  You can then define some logic in the method to get from that object to a truth value.   Below is a working attribute I cooked up to test whether an entry in a field (conceived as a decimal) is not zero.  Note that the object has to be converted to decimal form before it can be tested (and an explicit cast doesn't seem like the way to go here). public class NotZero:ValidationAttribute {         public override bool IsValid(object value)         {             var d = C...

Cash Register Algorithm

Goal: Given a price, cash tendered, and two dimensional array of change ordered by denomination (pennies, dimes, quarters, etc.), return an array of exact change. Exceptions: return "insufficient funds" if there isn't exact change; return "closed" if exact change is equivalent to the contents of the array; return a warning if the price is greater than the cash tendered. Analysis First, check the difference between the price and cash tendered. If the price is greater than cash tendered, return an error. If the price equals the cash tendered, no change is needed. From this point, we assume the price is less than the cash tendered. It helps to consider a simpler case where the cash tendered and the change on hand is all pennies and dimes.  You could proceed like this: (1) see if all of the change can be given in pennies.  If it can't (2) see if the remainder can be made up with dimes.  We could get the following cases: A. All your pennies and som...

Flattening An Array

How do you flatten an array?  That is, how do you take something like this: [1, [2, [3, [4]]]] and change it into something like this? [1, 2, 3, 4] The first thing that occurs to me is that you would go through each element of the given array and check to see whether it is an array.  So we go through: 1 (NO) [2, [3, [4]]] (YES) We're done with the non-arrays -- we can keep them somewhere (another array) but the arrays require further processing.  We process them in exactly the way we processed the first array.  The trick is figuring out how to do that organically: we need the computer to keep processing elements of the array and members of those members recursively (!) on down to the elements. What I think you would do is create a general function that processes an array into elements, then call that function not only on the array itself, but on the members of that array.  How would that look? Well, if we have an element, we don't call it. ...

How Does A Calculator Work?

A calculator has 11 numeric entry buttons (0-9 and the decimal), 4 operation buttons (addition, multiplication, division, subtraction), two clear buttons (clear entry, clear everything), and a result button (equals). At the start of operations, the entry reads "0". The user then presses one of the buttons.  If the user presses "0", the entry continues to read "0".  If the user presses any other digit, the entry displays that digit.  The user can then compound that digit into a numeral by pressing additional digits. The user might choose, however, to press a non-numeral button.  Let's think about what happens if the user presses one of the operation buttons.  To make things more concrete, let's list some actions the user might take in a sequence.  (For the sake abbreviation, I will include entire numerals as the end-product of a sequence of actions (entering digits) rather than the individual entries by which the numeral is compounded.) So the...

What Is Success, For Me?

The secret to career success is self-branding: deciding what you represent, realizing those values, and communicating your achievement to colleagues and potential employers. Deciding what you represent can be the most difficult but is also the most important part of the entire process.  Success has many dimensions -- financial, social, philosophical (for lack of a better word), even moral; carefully determining what you value will help you to synthesize all these dimensions so that you can present a united front to people in your network – and so that in the end you can achieve something you will feel proud of.  Your values help to tell your story: accreditations, experience, accomplishments, and transitions are not so many bullet points to put on a resume but represent something for those who know what they’re about.  Those who don’t, on the other hand, become lists and amalgamations; they follow the market, get certificates, get an MBA, not because they feel this is the...

JavaScript Function For Symmetric Difference

EDIT: Note that the code below works (at least as far as I know) so long as you assume each array is formatted like a set.  If either of the arrays have repeating entries -- i.e. behave the way arrays are allowed to behave -- the algorithm won't work and is seriously in error.  Oh well -- back to the drawing board. The symmetric difference of two sets is the difference between their union and their intersection. Consider the following sets: A = {1, 2, 3, 4, 5} B = {1, 3, 5, 7, 9}  The union of A and B is {1, 2, 3, 4, 5, 7, 9}. The intersection of A and B is {1, 3, 5}. So the symmetric difference of A and B is {1, 2, 3, 4, 5, 7, 9} - {1, 3, 5} = {2, 4, 7, 9}. My job was to construct a two argument JavaScript function that would correctly return this result.  I decided to proceed as follows: 1. Concatenate the two arrays to produce their "union" (with duplicate values). 2. Remove any value that has a duplicate. Here is my code: function diffArray(...

Databased

I decided to add code to store the results from my scraper into a database instead of just printing them on the screen.  I also added a bit to capture the full review text as well as the title.  In the process, I learned a few things about SQL calls: You can't use WHERE with INSERT INTO If you want to add something to an existing row, you have to use UPDATE...WHERE I spent a lot of time trying to insert the review into the same row as its associated title.  At first, I was creating a new row for each review.  Then I tried to link the review with my i  counter and insert it into the i- th row.  But that went nowhere because (A) the database wasn't associating any numbers with the rows and (B) i  counts review pages.  I fumbled around a lot, got my count INTEGER NOT NULL PRIMARY KEY  (probably overkill -- I wanted to AUTOINCREMENT  too, but I guess I was putting in the wrong command), and then after watching dubiously as the databas...