Skip to main content

Posts

Throughput, Latency, and Pipelines: Diagnosis Of A Fallacy

Source here . Latency is the time it takes for a job to complete from start to finish -- for example, if we're downloading a file from a server, we might define the latency of the download as the amount of time it takes from the initial request for the file to the time the last byte is received. Throughput is a measure of how much can be completed in a given time.  Following the same example, how many files could we download in an hour? What is the relationship between latency and throughput?   It make take, again, 1 hour to download a file.  Notice already that we have a relationship between some amount of work that can be completed and time -- if the file is, say, 2 GB, it takes 1 hour to download 2 GB in our example.  What we really want to do here is generalize: how long does it take to download 10 GB?  How many GB can we download in ten hours or one minute?  Generalizing over time, we derive latency; generalizing over work completed, we derive the...

Nailing Down The Meaning Of "This" In JavaScript

Sense-certainty itself has thus to be asked: What is the This? If we take it in the two-fold form of its existence, as the Now and as the Here, the dialectic it has in it will take a form as intelligible as the This itself. To the question, What is the Now? we reply, for example, the Now is night-time. To test the truth of this certainty of sense, a simple experiment is all we need: write that truth down. A truth cannot lose anything by being written down, and just as little by our preserving and keeping it. If we look again at the truth we have written down, look at it now, at this noon-time, we shall have to say it has turned stale and become out of date.   Hegel,  Phenomenology of Spirit  S. 95 Overview In philosophy, English words like "this" are referred to as indexicals, with the implication that (A) they are used to point at something and (B) what they point at is determined by context (just as, when I point at someone, who I am pointing at is determined ...

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