Pricing Calculations for Work & Projects

Recently I’ve been paying a lot more attention to the job market as I go through the finding, hiring, and gaining customers routine with a number of different companies, recruiters, and individuals I know within the industry. Conversations kick off a million different ways. In this post I’m going to tackle how I work pricing of a job for myself. Many companies have some criteria based on what others in the company are paid and someone in human resources often has some spreadsheet or whatever that tracks this stuff. This is the flip side of the coin, this is my pricing list. It’s really simple, it’s based on what I’m looking for in life and what a company can give me, which in turn dictates what I can do for a company and aligning with what they want too. In the end, this helps insure we both get a win out of the relationship.

Pricing

Work Desire — The first thing I always think to myself is, “Do I even want to do the work being discussed?” Even though it is more complex then a yes or now. For instance, the main work might be some horrid migration going from a legacy Oracle Database and moving to Postgresql as the core job. But outside of that core job there might be some excellent potential to work with data that is super interesting from a scientific perspective, or logistical freight perspective. (As I find the freight industry very interesting, it ranks high on my “let’s do this” list) Even though the later is vastly more interesting, both need done so it still ends up with a positive “let’s do this” rating. In every possible project I always give it a measure of somewhere on the “Nope” to “let’s do this” spectrum. More toward nope, the increase may go as high as 40% more, more toward the let’s do this, it might even move toward taking a lower price point by 15%. In summary, the desire to do the work on hand is a massive factor in deciding the price I’ll take for the work.

Skills —There are two aspects of jobs which can be broken out into the poorly worded phrases soft skill and hard skill categories. Generally one refers to a person’s communication, writing, and organizational skills and the other refers to someone’s understanding and knowledge around writing code, technical documentation knowledge (let’s be real, this is the expectation), and to some level rudimentary reasoning skills.

The truth is, if we’re honest with ourselves the divide of soft and hard skills is wrong for so many reasons, including the words soft and hard themselves. These designations ought to simply be called skills and discussed in the full context of the individual. The paradigms of the personality tests and introversion and extraversion come into play too, all important to determine effectively how these skills need to be used within a job. But the most important thing is to realize and accept the fact that all the skills an individual has are important in their interplay within the job. Belittling one or other other doesn’t do anybody any good.

My simple approach is listen, learn, and always aim to improve my skills. All of them in any way that I can at any point that I can. Maybe sometimes I need to focus on one skill, and I will, but overall I aim to keep a solid improving competence across what I do as a whole, it makes all of the individual skills that much more useful if I can deliver them with all the others.

If a company acknowledges that a whole package of skills is what is important, not delineating them into the false premise and broken ideals of soft and hard skills, they get a discount break on my demands. I work it out like this, if they’re understanding and it’s understood in general among the company I’m willing to be flexible by taking off about 2% of my asking prices. If I’m talking to a company that has a clearly naive understanding around communication, no blame or anything, but it’ll need to be an increase of 5–10% in my pricing.

Geography — Depending on remote or onsite work has some huge pricing implications. If a company is remote, they instantly get more attention for two reasons:

  1. It’s either highly likely they’re good at effective communication and waste a more minimal amount of time with meetings and such, or they’re at least open to doing better or becoming more efficient in their communication and being a remote first type of workplace.
  2. I work best, and can contribute most effectively when I control my geographic location. Forcing an office environment and respective time frame around that office environment immediately decreases what will and can be done.

As a side effect of being remote, not only am I able to be more productive for the vast majority of companies, I’m happy to cut about 2% off of my pricing.

What about onsite? I never rule out onsite work, because it can be extremely rewarding, useful, and in many cases if a company is ok with remote but wants a split of onsite work I’m very happy with that arrangement too. It often doesn’t even cut into my productivity as I can then get a bit of the best of both situations. But let’s talk about what area this applies to.

First, there is the I can bike, bus, train, tram, light rail, boat, or walk it to the office this gets high priority over anything else in the in office category. I only add on a small 2 or 4% to my pricing for this situation. If the company has thought intelligently about this and offers indoor arrangement for cyclists, showers, transit passes, or other amenities I’ll usually even decrease my increase by 50%. If a company is outside of a reasonable 30–50 minute bike, bus, train, tram, light rail, boat, or walk to the office I instantly add approximately ~15% to my pricing. One exception would be if I just fly to their office for a few days a week or per month and spend that time “bike, bus, train, tram, light rail, boat, or walking” to the office.

Geographic Amenities —As real estate professionals always say price is about location, location, and location. This is so very true in choosing companies to work for also. The geographic area, as discussed in the previous point, also has another key element that drastically affects my pricing. It applies to in office requirements and works very simply, like this.

If the office where I’ll be commuting to on a regular basis (at least every other day or so) is located in a food desert then I have to instantly add ~15% to my pricing ask. It’s simple, if I’m to decrease my standard of life, decrease my opportunity to stay healthy, live well, and eat foods that help sustain my well being then I’m going to need to compensate for that unfortunate request on their behalf.

Another is if I need to commute outside of certain distance from where I live, which I’ve drawn a very rough map up below, which generally would place me near a prospective food desert, then again the additional risk, travel time, and related complexities just to get in office will force me to raise my pricing request by up to ~15%. In the map below for example, the northern end of the pink outlined area I need to increase in pricing for, but the further south and travel time areas would require an additional ~2 to upwards of 10% increase in pricing. The orange outlined areas would require a minimum of a 15% pricing bump just because of the inconvenience of trip time, congestion, increased costs on my behalf, and related unnecessary complexities just to work in office in those areas. Of course, I’ve decided where I live, and this is again to insure that we’re both matching expectations and meeting my needs as well as the company’s needs. If I’m killing myself slowly with some arduous commute and languishing with poor food options while in the office, it doesn’t do the client any better if I let myself fall into this situation without gaining recompense for this extra effort requested. There’s one more image there that shows where I would happily fly an a semi-frequent basis

Areas I’d Commute to in Seattle & surrounding areas on a mostly daily basis.

Contracting, Consulting or Full Time or Long Term Project Work — Now we get to the meat of how long a particular job is expected to last. This gets into the longevity of the pricing and even into the realm and expectation of price increases (i.e. raises, etc). Of course, getting to this point logically is an often high risk, volatile, and inaccurate practice. From SWAGs to every other method used, schedules almost always get messy the longer out something is on the calendar. Suffice for this, we’ve made a determination around X, and X it will be for the foreseeable future.

If a job is temporary, i.e. 1099 in the ole’ US of A, the 1099 has to cover insurance, gap and risk, the gaps before and after the contract from focusing on a singular thing for a temporary basis, also the medicare and social security share of both side (i.e. and employee) etc. So a 1099 rate must be at minimum 30% or more than a W-2 rate. A W-2 rate, depending on what comes along with that for benefits, can also vary dramatically. With the complexities of the ACA it can even be necessary to take concern with startup and shutdown of health care coverage, which means the rate may need to go up approximately 5–10% just for that gap position.

On the topic of a full time position, if it’s the case of diving directly into being full time I may or may not need to have an additional 5–10% added to the salary req, because I would prefer we meet halfway, complete a contract to hire to insure we both meet criteria for a good working relationship. Even if it’s just a few weeks of consulting. Depending on the mission of the company, what it builds, and what the need is, I’m happy to go ~10–15% over under on the median salary for the area.

Summary

Fortunately I’ve gotten good enough at this, that I can usually calculate this pricing in a matter of seconds. Before I really had my system down it was very cumbersome. If you put together your own pricing system, make sure to memorize and test out calculations beforehand so that you know when and what you’ll put on the table. It helps immensely if pricing is stated as early as possible, with firmness, and clarity around intent. Whatever you’re going for, I strongly proffer advice to determine what the median rate in the area is and pricing accordingly! It’ll work out better for you and your prospective client/employer/team.

ML4ALL (Machine Learning for All)

Alright, I’m sure everybody has a vast and expansive understanding of machine learning and all the algorithms involved and…

oh wait…

…not really? Yeah, me neither. Alright, I’ve got a conference for you. The ML4ALL Conference is lined up for May 27–29th where a reasonably sized group of machine learning practitioners and completely new to the field people are going to gather to speak, learn, discuss, and aspire to more and better machine learning. We’d love for you to join us, come speak on a machine learning topic dear to your heart, and enjoy the city, sites, cuisine, and relaxed nature of Portland while you’re at it.

I’ve got a couple excellent selling points on why you should attend ML4ALL.

  • Machine learning is going to be very much in demand, and this is an opportunity to spend a few days getting up to speed in the great city of Portland in a great setting of the Bossanova Ballroom with a lot of fun people on much of the technology involved!
  • Like all conferences I have a hand in, I’ll be sure to setup some outside of conference fun things to do for those interested — like a brewery ride, possible hike, geek train for Seattle to Portland, or any number of other peripheral activities that I enjoy adding to an event. I’ll be sure you get that lagniappe!

More to come, lot’s more, stay tuned, keep learning, and until then — happy algorithm processing.

Reading Up on Observability and Monitoring

In the next few weeks I’ll be doing some work for Honeycomb.io. In preparation I’ve been ramping up on a number of topics, namely the big one is the specific differences in things like observability and monitoring. The aside that makes this relatively easy, is I live and breath this space from a site reliability, apps, and services perspective. Not only am I fortunate in my situation to dive into this topic, I’m excited to do so as well.

I started this research just checking out what material Honeycomb.io has available onsite and additionally what Charity Majors has written, or spoken about at conferences. The first two were easy to find with a simple Google search and checking out the Honeycomb.io Blog. For each of these materials I’ve provided a summary before each linked reference. Enjoy.

Title: Charity Majors on Observability and Understanding the Operational Ramifications of a System

This article is in interview form, with Daniel Bryant interviewer and Charity Majors the interviewee. When asked the question

Can you explain a little about how operational and infrastructure monitoring has evolved over the last five years? How have cloud, containers, and new (old) modular architectures impacted monitoring?

Charity goes on to lay out a stance.

What’s interesting is that monitoring hasn’t really changed. Not in the past … 20 years.

You’ve still got metrics, dashboards, and logs. You’ve got much better ones! But monitoring is a very stable set of tools and techniques, with well known edge cases and best practices, all geared around monitoring and making sure the system is still in a known good state.

However, I would argue that the health of the system no longer matters. We’ve entered an era where what matters is the health of each individual event, or each individual user’s experience, or each shopping cart’s experience (or other high cardinality dimensions). With distributed systems you don’t care about the health of the system, you care about the health of the event or the slice.

This is why you’re seeing people talk about observability instead of monitoring, about unknown-unknowns instead of known-unknowns, and about distributed tracing, honeycomb, and other event-level tools aimed at describing the internal state of the system to external observers.

Let’s elaborate for a moment. Monitoring not changing in the last 20 years is truth. I worked at WebTrends years ago, which was a company that was formed around basically taking logs files and churning through them to get reports out of them. In the process they became one of the first web analytics company. But even way back 20 years ago almost WebTrends was basically laying out what we deal with and have for monitoring. Starting with log files and on up through basic pinging of services, with related APM style monitoring. This has been and continues to be effectively the same thing, needed, but unchanging for decades.

But jump over to her point about tracking events instead of worrying about the health of the overall system. Maybe stating “it no longer matters” is slightly hyperbolic, but she’s making a point, so don’t get derailed. The idea is we don’t actually care about the system, we care about what activity we’re trying to make possible; buying some food on Amazon, that rare book on a book finders site, issuing a search with esoteric terms, triggering a compute action of some sort, analyzing a flight path for an upcoming trip, or other event. In the end, I’d even add to the argument that we end users don’t even really care if it’s done by a computer or some magical sky pixie, the only thing that matters is we get the result of the event we’ve requested and that it is correct for us to take action on.

We want the event, the correct result, and the rest is irrelevant in the grand scheme of the universe. This is an extremely important point she’s brought up and it is key in understanding the difference in monitoring — the combing of data to determine the state or well-being of a system — versus observability — the view into and understanding of the state of events within a system.

Distributed systems also amplify, exacerbate with complexity, this intent to know the state of events versus the state of the system. Anybody who has dealt with any list of distributed systems, from Kubernetes to Riak to Cassandra to others, knows that the key is the event and data, as one may never really know when a system is under duress or what the state is of the entire system. Simply put, you cannot know the full state of a truly complex distributed system, and often if you do it still doesn’t tell you if your events are being performed accurately or at all. Sure, monitor it, but what we really need to know is what the data is, if it’s really the data we want, and the event the data is relevant to. This paradigm around distributed systems Charity also summarized up in an answer I found spot on.

Monitor everything”. Dude, you can’t. You *can’t*. People waste so much time doing this that they lose track of the critical path, and their important alerts drown in fluff and cruft. In the chaotic future we’re all hurtling toward, you actually have to have the discipline to have radically *fewer* paging alerts … not more. Request rate, latency, error rate, saturation. Maybe some end-to-end checks that stress critical Key Performance Indicator (KPI) code paths.

That’s just a review of some of the discussion, definitely suggest reading more…

https://www.infoq.com/articles/charity-majors-observability-failure

Title: Monitoring and Observability

Next up I found an article by Cindy Sridharan (@copyconstruct on the twitters). Cindy brings up an often snarky emoted statement, as per the tweet…

Cindy writes a really great post, but before even diving into that, follow the thread of comments on that tweet. Some of them are pretty good. I’ve included a few gems.

Cindy goes on to bring up three other articles that I also highly recommend. These posts are all over on the Twitter blog: Observability at Twitter, Observability at Twitter: technical overview part 1, and Observability at Twitter: technical overview part 2. In this she points to the quote, “…observability is a superset of monitoring…” which is succinctly put. Again, so many great phrases describing observability versus what monitoring is. It seems like the nebulous snarky quotes like “Observability — because devs don’t like to do “monitoring” we need to package it in new nomenclature to make it palatable and trendy.” would just be laid to rest. But I digress, read Cindy’s article, it’s long and got great links, descriptions, and is well worth the read.

https://www.infoq.com/articles/charity-majors-observability-failure

Title: Observability vs. Monitoring, is it about Active vs. Passive or Dev vs. Ops?

This post is by Steve Mushero, and starts off by referring to Cindy Sridharan’s post reviewed above. Steve posits the question in the title of the piece, which my first thought is that it isn’t vs. but complementary right? Reading throught the beginning of the post I get to another gem of a quote.

I Monitor you, but you make yourself Observable.

Onward in the piece Steve hits home some specific detailed points.

For Observability, the system, code, developers, etc. are taking step to make things available to make the system more observable. This often starts with increasingly rich and structured logs, plus events or markers, JMX data points, and Etsy-style emitted metrics. Loved and tended to by Developers and the most modern Ops.

…and…

Observability elements, on the other hand, are often much detailed, more diverse, and used more for debugging, complex troubleshooting, performance analyses, and generally going ‘deeper’.

Both points are very interesting, the first getting into some details of what exactly observability and the second one a little obfuscated but driving home the idea behind observability versus just monitoring.

Toward the end Steve writes, “In the end, we need them all, and what it’s called doesn’t matter much.” which I disagree with, but not wanting to delve into pedantic response I’ll hold off, but suffice it to say, words matter and have meanings that help us describe our world and communicate, so it does indeed matter what we call things.

https://www.infoq.com/articles/charity-majors-observability-failure

That’s it for my current reading up on observability and monitoring. As always, if you’ve found this useful (or just interesting) follow me Adron Hall here on Medium, or one of my other mediums depending on how you like to consume content.

Building a Data Thrashing CLI Tool in Go

I need a tool just to do some testing against an API end point. I figured I’d throw one together real quick in Go. With a few libraries it’s just a few steps to get the job done. The following is that project. Eventually I’ll create the services that will run in some containers I’ll throw into a Kubernetes cluster, but for now, it’s all CLI. Onward.The first thing I’ll need is Cobra.

cobra

DigbyShadows

Before Cobra, there was one prerequisite that I already installed. I had the Go dep command installed for dependency management. You can install or upgrade your dep with brew, with the following commands.

brew install dep
brew upgrade dep

If you’re not using MacOS and brew, you can just use the standard go get route and run go get -u github.com/golang/dep/cmd/dep. The first command at that point to start managing dependencies within the root directory of your project is dep init. More on that in a minute.

First I need to insure I have the Cobra CLI to use to generate out the initial code for the CLI itself. To get Cobra installed use the standard go get for this too with go get -u github.com/spf13/cobra/cobra. Now I can type cobra init github.com/adron/thrasher to generate the initial skeleton of my CLI.

cobra-init-blueland-cli

The folder is now generated in my Go directory, under the standard convention /src/github.com/adron/blueland-cli/. Now I navigate into that directory and get some of the command stubs started. I want the following commands available with my CLI.

thrasher thrash

This command I’ll use to start calls of generated data against an API end point.

thrasher config set

This command can be used to set the end point in which to send data to.

thrasher config view

This command can be used to check what the URI is that the requests will be sent against.

thrasher config verify

This command can be used to verify that the configured URI is set appropriately.

At this point, this seems to be all I’ll need. The commands I’ll issue to get these are as follows.

cobra add thrash
cobra add config
cobra add set -p 'configCmd'
cobra add view -p 'configCmd'
cobra add verify -p 'configCmd'

To test these commands out, verifying the skeleton code is in place, build the CLI with a simple go build in the root. The result looks like this.

$ go build
$ ./thrasher thrashthrash called
$ ./thrasher configconfig called
$ ./thrasher config set
set called
$ ./thrasher config view
view called
$ ./thrasher config verify
verify called
$

Now a quick git init and addition of a .gitignore file with he following contents.

# Binaries for programs and plugins
*.exe
*.dll
*.so
*.dylib

# Test binary, build with `go test -c`
*.test

# Output of the go coverage tool, specifically when used with LiteIDE
*.out

.idea
.DS_Store

Wrapping that up I added the code to the repository here.

Next thing I setup the project with the dependencies I have so far. Using dep that included the dep init command. That looks something like this on the first pull of dependencies.

adding-dependencies

When I ran this command, dep completed the folowing actions.

  1. Looked for existing dependency management files to convert.
  2. Checked if the dependencies use dep.
  3. Identified the dependencies.
  4. Backed up the existing vendor directory if it existed to _vendor-TIMESTAMP/
  5. Picked the highest compatible version for each dependency.
  6. Generated Gopkg.toml (the manifest) and Gopkg.lock files.
  7. Installed the dependencies in vendor/.

For more information on dep check out the project README.md.

The next dependency I’ll add is faker with the following dep command.

dep ensure -add github.com/bxcodec/faker

Now I’m ready to get started with some actual implementation. First let’s setup the configuration. This is a simple single setting that needs to be set, or retrieved from a configuration setting. Here’s the basic flow for the configuration.

Managing Configuration

The CLI starts with one of two states. It either has a testing URI that the CLI will execute tests against or it doesn’t. The CLI need to check for this every time a command is executed. If it doesn’t have or can’t find the configuration a message should appear stating what is missing and then exit out. If the configuration information exists then we can continue forward.

viper

For this, I did a quick search and realized that I would likely benefit from using the Viper Project. This project is already included with the Cobra Project so I can just dive right in. Which is exactly what I’ll do now, but I must add that I’ll elaborate more extensively on what all has been generated for us after a little code is written.

First I’ll open up the main CLI code that I’ll use just to try out some fake data examples. The generated code file that Cobra built for me looks like what is below. It’s located where Cobra generates it, inside the cmd directory of the project, in a file called root.go.

// Copyright © 2017 Adron Hall
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package cmd

import (
    "fmt"
    "os"

    homedir "github.com/mitchellh/go-homedir"
    "github.com/spf13/cobra"
    "github.com/spf13/viper"
)

var cfgFile string

// RootCmd represents the base command when called without any subcommands
var RootCmd = &cobra.Command{
    Use:   "thrasher",
    Short: "This CLI is built around testing API end points by submitting data, " +
        "etc, generated at time of testing.",
    Long: `This CLI is built around sending randomly generated data at API end points for testing.
The idea is for the data to be issued against end points using various HTTP verbs such as GET, POST, or others.

For example a similar command using curl.
  curl -d "param1=value1&param2=value2" -X POST http://localhost:3000/data
  curl -d "param1=value1&param2=value2" -H "Content-Type: application/x-www-form-urlencoded" -X POST http://localhost:3000/data`,
}

// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
    if err := RootCmd.Execute(); err != nil {
        fmt.Println(err)
        os.Exit(1)
    }
}

func init() {
    cobra.OnInitialize(initConfig)

    // Here you will define your flags and configuration settings.
    // Cobra supports persistent flags, which, if defined here,
    // will be global for your application.
    RootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.thrasher.yaml)")

    // Cobra also supports local flags, which will only run
    // when this action is called directly.
    RootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}

// initConfig reads in config file and ENV variables if set.
func initConfig() {
    if cfgFile != "" {
        // Use config file from the flag.
        viper.SetConfigFile(cfgFile)
    } else {
        // Find home directory.
        home, err := homedir.Dir()
        if err != nil {
            fmt.Println(err)
            os.Exit(1)
        }

        // Search config in home directory with name ".thrasher" (without extension).
        viper.AddConfigPath(home)
        viper.SetConfigName(".thrasher")
    }

    viper.AutomaticEnv() // read in environment variables that match

    // If a config file is found, read it in.
    if err := viper.ReadInConfig(); err == nil {
        fmt.Println("Using config file:", viper.ConfigFileUsed())
    }
}

In this file there’s a number of things to notice, but again, I’ll get back to that. For now, I went ahead and added some code in the init function to initialize and set a default for the environment variable. The init function now looks like this.

func init() {
    cobra.OnInitialize(initConfig)

    defaultUri := "localhost:3000"
    viper.SetEnvPrefix("thrasher")  // Uppercased automatically? Maybe just make it uppercase?
    viper.BindEnv("uri")

    uri := viper.Get("uri")

    if uri == nil {
        os.Setenv("THRASHER_URI", defaultUri)
        fmt.Printf("Set environment variable THRASHER_URI to %s.\n\n", defaultUri)
    }

    // Here you will define your flags and configuration settings.
    // Cobra supports persistent flags, which, if defined here,
    // will be global for your application.
    RootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.thrasher.yaml)")

    // Cobra also supports local flags, which will only run
    // when this action is called directly.
    RootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}

Using Viper I set an environment variable prefix to thrasher. This means that any environment variables I set with a prefix of thrasher will be picked up as environment variables specifically for my application. I then set a bind to the environment variable I want to name uri. This will give me the value of the environment variable named uri. To set this value I’ll need to actually set the value THRASHER_URI since the Viper library automatically uppercases the environment variables. It’s all just slightly confusing, but in the end I end up with an environment variable that I can set in my ~/.bashrc or ~/.bash_profile settings locally and if I don’t set it, the if control structure will set the environment variable THRASHER_URI for me to the default that I’ve set in code localhost:3000.

Also notice in the code above, the generated section shows a config file default location for $HOME/.thrasher.yaml. Currently I’m not using this default, but it’s setup that way by convention.

I’ll want to be able to find out what my configuration setting is for the environment variable using the thrasher config view command. To do this, I add a few lines of code in the view.go file. Inside the view.go file there is the variable assigned to the pointer location of the command, which I’ve edited to print out the evnironment variable that is set for the URI.

var viewCmd = &cobra.Command{
    Use:   "view",
    Short: "The view command is used to retrieve the configuration settings.",
    Long: `The view command can be used to retrieve and view the configuration
        settings that the thrasher CLI currently has active and in use.`,
    Run: func(cmd *cobra.Command, args []string) {
        uri := viper.Get("uri")
        fmt.Printf("Current URI to issue commands and data against is %s.", uri)
    },
}

Alright, view is setup, now I want to setup verify. In verify I want the uri checked, an HTTP get issued against the URI for verification, and a respective response from the API end point. If that doesn’t come back, and an error occurs I want to handle the issues that might come up.

The first bit of code I’ll add is getting the URI path with viper and issuing a get request against the URI end point. I’ll add this to the verify command pointer func.

uri := viper.GetString("uri")
resp, err := http.Get(uri)

I want some of the body to display, so I’ve written a small function to handle the processing and return of some of the body. That code looks like this.

func keepTopVariableLines(s string, n int) string {
    bodyResult := strings.Join(strings.Split(s, "\n")[:n], "\n")
    return strings.Replace(bodyResult, "\r", "", -1)
}

Now I’ll add some error and if else logic to display appropriate messages, display of response, and related information.

if err != nil {
    fmt.Printf("Configured URI has errors: \n\n%s", err)
} else {
    defer resp.Body.Close()

    body, err := ioutil.ReadAll(resp.Body)

    if err != nil {
        fmt.Printf("Response body caused error: %s", err)
    } else {
        fmt.Println("Get:\n", keepTopVariableLines(string(body), 2))
        fmt.Println("\n\nURI has been retrieved, URI verified.\n\n")
    }
}

Now, I’ve got all the pieces for verify in place. The completed keeptTopVaribleLines and verify command pointer func look like this now.

func keepTopVariableLines(s string, n int) string {
    bodyResult := strings.Join(strings.Split(s, "\n")[:n], "\n")
    return strings.Replace(bodyResult, "\r", "", -1)
}

// verifyCmd represents the verify command
var verifyCmd = &cobra.Command{
    Use:   "verify",
    Short: "Verify will test the URI end point with a get call issued.",
    Long: `Verify will test the URI end point with an HTTP get call request against the end point.`,
    Run: func(cmd *cobra.Command, args []string) {
        fmt.Println("verify called")

        uri := viper.GetString("uri")
        resp, err := http.Get(uri)

        if err != nil {
            fmt.Printf("Configured URI has errors: \n\n%s", err)
        } else {
            defer resp.Body.Close()

            body, err := ioutil.ReadAll(resp.Body)

            if err != nil {
                fmt.Printf("Response body caused error: %s", err)
            } else {
                fmt.Println("Get:\n", keepTopVariableLines(string(body), 3))
                fmt.Println("\n\nURI has been retrieved, URI verified.\n\n")
            }
        }
    },
}

Alright. Now with a quick go build this will give me a partially functioning CLI tool.

cli-part-1

In the next post I’ll wrap this up and get some of the data generation to post data against the end point. For now, happy Go hacking. Any questions, ping me via the Twitters @Adron.

Algorithms 101 Roads & Town Centers

Alright, there’s this one algorithm that I’ve solved before. I’ve always found it to be a rather fun little exercise to work out. It popped into my head recently and I wanted to recollect how the logic of it went, but my Google-fu wasn’t so great. In the end, I didn’t find the algorithm problem statement but I’ve recollected it as best as I could from memory. If you know what the name of this challenge is I’d love to know what it’s called or if I’ve put it back together correctly. Ping me @Adron.So the story goes something like this. There once were some nations with a number of cities in each nation. Every citizen has access to every city and every city has a town center for all the citizens to enjoy. Recently the roads were damaged from a lack of maintenance work, ya know, like in real life. Meanwhile there was a revolution that led to catastrophic war that destroyed all the town centers in the nations. So now none of the cities have reachable town centers or functional working town centers anymore! The citizens of the world are angry at the nations and demand immediate fixes to their roads and town centers, with a priority on the town centers! The leaders have decided that the roads shall be repaired and have hired me (you) to assist!The nation has n cities, we’ll number 1 to n. The cities have two way roads, totalling m roads. A citizen has access to the town center if: their city contains a town center and their city has a road to travel from their city with a town center to another city with a town center.

The following is a map of one great nation of cities with currently impassable roads that must be repaired.

nation

The cost of road repair is croad and to build a town center is ctown center. To start off with, you’re given q queries, where each query consists of a map of the nation and the value of croad and ctown center. For each of the queries the minimum cost of a town center accessible to all the citizens should be printed on a line.

The Input

The first line is an integer, q, denoting the number of queries. The subsequent lines describe the queries in a particular format. Each query is basically formatted like this:

  • The first line of each set for a query will have four integers describing the number of cities n, the number of roads, m, and the cost to build a town center ctown center, and the cost to repair a road croad. Each integer is space seperated.
  • Each line following that will have two integers describing a road for use between cities ui and vi. Each integer is space seperated.

The Output

Each query should have a result displayed that is the integer denoting the minimum cost of making town centers accessible to all the citizens.

The following example includes three queries (denoted by the first line having a 3). The subsequent queries then have a definition line of [6,4,2,1], [7,6,2,1], and [9,7,3,4] respectively. Then below those query definition lines are the road definitions between the cities, and respectively the town centers.

Sample Input

3
6 4 2 1
1 2
2 3
4 5
5 6
7 6 2 1
1 2
2 3
3 4
2 4
5 6
6 7
9 7 3 4
1 2
1 5
2 4
2 3
3 9
6 8

Alright, this one has a nubmer of elements to resolve. Here’s how I’ve worked through these three samples to get answers. I’ve drawn out the three examples so that there is a visual understanding of where the cities are also.

The first nation described by the section [6 4 2 1] would look something like this based on the road data of [1,2], [2,3], [4,5], and [5,6].

cities-1

The next nation in section [7,6,2,1] would look like this with the road data of [1,2], [2,3], [3,4], [2,4], [5,6], and [6,7].

cities-2

The last nation defined as [9,7,3,4] with road data of [1,2], [1,5], [2,4], [2,3], [3,9], and [6,8] looks like this.

cities-3

Observations and Solutions

Dataset 1

Here’s how I worked these out. In the first example there are n=6 cities, m=4 roads connecting those cities, with the price of building a town center set at ctown center = 2 and the price to repair a road set at croad = 1. To meet the requirement of having access to a town center in every city, I could acheive this through several options, which I can then pick the cheapest from those options.

Rebuild the roads and build one town center in each connected part of the nation. In this scenario that would included 4 roads at 1 cost each, and one town center in each island of the nation at 2 cost each. The total would equal

roads x croad = total

4 x 1 = 4 to rebuild the roads.

cities x ctown center = total

2 x 2 = 4

total croad + total ctown center = combined total.

4 + 4 = 8

The next option would be to possible just rebuild town centers in each city and not rebuild the roads. This is quick to determine the cost of, as it just requires calculating the town center cost.

cities x ctown center = total

6 x 2 = 12

Out of the two solutions, the cheapest is to go with option one, rebuilding the roads and building one town center for access in each of the island segments of the nation.

Dataset 2

Alright, the next data set shows a city of 7 cities, among two islands, and 6 roads connecting those cities. Just a quick look, I’m guessing this one will work out the same as the first one. Working through I could build town centers in each city.

cities x ctown center = total

7 x 2 = 14

But I could also rebuild road [1,2], [2,4], and [2,3] on the left island with a single town center build, which would mean I need one town center and three roads for island one. The second I could build one town center in Tupelo and then rebuild road [5,6] and [6,7]. Total is two town centers and five roads.

Town centers would cost 2 x 2 = 4. The roads would be 5 x 1 = 5, adding 5 and 4 gives a total of 9 to rebuild enough so that all cities can regain access to a town center.

Dataset 3

This third dataset has three island areas. The first has 6 cities with 6 roads. The next island has 2 cities and 1 road, and then final island has one city with no roads. This last city, Solace, just needs a town center built to gain access to a town center.

For the island with two cities will need at least one town center. But with the prices increased let’s take a look to be sure. If I replace one town center, we’ll say in Tupelo, and rebuild the road that would be ctown center of 3 and road cost of 4 giving a total cost of 7. But if I rebuild a town center in San Francisco and Tupelo, that would only be 6. The previous plus this solution gives me three town centers at a cost of 5.

Now considering the last island segment I have 6 cities, and a number of roads that in some cases connect just one city. For the stretch that connects Legoland and York I could rebuild a town center in York, then the road [1,5] and [1,2] which would give Lenia, Legoland, and York direct access to a town center. The total would be 3 + 4 + 4 = 11 for a single town center and two roads. Then looking at things, the cheap scenario would be to rebuild a town center in Odessa and road [3,4] and [3,9] for another 11 total. Overall for this island of cities within the nation I’ll have 4 roads rebuilt with 2 town centers, for a total of 22 cost.

Adding that together for the entire nation of Dataset 3, that’s 22 + 5 + 3, giving a total cost for direct access to town centers of 30 cost.

The question now is, what’s the best algorithm to solve all of this. I’m going to make this algorithm a two parter, because this is such a whopper of a problem to lay out that I’ll give readers a chance

Sample Output

With each of these resolved, the output of the algorithm should print to stdout with the following results.

8
9
30

So…what’s your take on this one. If you build a solution, I’ll add it to my blog post in a few days. Let me know and we’ll sync up on it and talk about what an ideal solution might look like. Ping me @Adron.

Also, with any involvement in coding & hacks I’ll send a Thrashing Code sticker, kudos, and next beer is on me!

https://twitter.com/ThrashingCode/status/958799254901768192