Skip to main content

Posts

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