Skip to main content

Posts

Showing posts from May, 2018

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