Pattern Recognition and Fun With Seven

August 27th, 2009

I admit it. I’m a math and science geek. I am, undoubtedly, a software engineer as well so I better be darn good at working with numbers and recognizing patterns.

The other night, I was playing around with math. When I was younger I would play games with license plate tag numbers trying to make equations out of them. It was pretty easy. By assigning numeric values to the letters using a base-10 system with A = 1, you could take a tag number of AC214D and come up with the individual numbers 123144. You’d then have to figure out some way to make the equation work. The equivalency operator and parenthesis could go anywhere in the equation and you could combine numbers. This particular tag number supports:

(1+2-3+1)*4 = 4
1+2-3+1 = 4/4
12/3*1*4 = 4

You get the idea, but back to the point.

The other day I noticed something about the number 7. Particularly about 7 and 21. I found it odd that the sum of the digits of 21 (2+1) is also the multiplier of 7 required to equal 21. Or 7 * (2+1) = 21. I started playing randomly with other numbers to see if it would work and then I realized that it applies to the following:

7*(4+2) =42
7*(6+3) = 63
7*(8+4) = 84
7*(10+5) = 105

7*(18+9) = 189

(My math language is likely going to start to break down here so bear with me. Anyone have a whitboard?)

The pattern, in case you don’t see it yet, is that the two numbers in the sum, when concatenated as strings, equal the product of the sum and 7. Further, the first number of the sum is always a multiple of two and the second number of the sum is 1/2 the first number (9 is half of 18).

I was pretty excited (yes, excited) until I reached the following:

7*(20+10) != 2010

Rats. Unthwarted, I started to pursue this a little further and realized there was a more subtle pattern. Instead of concatenating the string representation of the numbers, what if the first number was moved over one digit (multiplied by 10) and then added to the second number.
This would mean:

7*(2+1) = 21 = (2*10)+1
7*(10+5) = 105 = (10*10) + 5
7*(20+10) = 210 = (20*10) + 10

Low and behold, it worked. And it works for much larger numbers as well.

7*(14296+7148) = 150108 = (14296*10)+7148

One thing I noticed that I thought was neat until I could easily explain it away is that the product of the numbers abiding by this pattern was always divisible by three. The reason is that your multiplying 7 by (2x+x) This is the same as multiplying 7 by 3x and so all numbers are divisible by 3. Incidentally, if the sum of the digits of a number are divisible by three, then the number itself is divisible by 3.

Turns out there IS a simple reason this occurs. If you multiply 7 by (2x+x) you essentially have (7 * 3x). Thusly, 7*3x = 21x = 20x+1x. So the reason the pattern occurs is pretty simple to understand but I still think it’s pretty neat.

Did I mention I love math?

andy Uncategorized

Coding Smarter – Understand Your Medium

August 12th, 2009

There has been a lot of chatter lately about good design practices. I don’t know if this is because I’ve just noticed it more, if it’s part of the alt.net push on the community, or if there are actually more people interested in improving our craft.

Several sites exist out there that urge developers to think about what they’re doing and they range from the architectural perspective (codebetter.com) to the human factors perspective (codinghorror.com — an excellent site that never disappoints.)

Regardless of the reason, it appears to me that a fundamental piece of the puzzle is missing. That piece being coding better isn’t the same thing as coding smarter. It appears to me that some developers have a unique form of savant syndrome granting them all the architectural prowess for which one could ever hope but none of the common sense development practices that can actually make or break an application.

I'm an excellent developer.
I’m an excellent developer.
 

Let’s take the following scenario that, sadly, is one of four from an application I inherited.

The first scenario in “Coding Smarter” involves understanding your medium. By that, I mean that you should really understand how the technology you’re using functions.

This application wanted to maintain a user id once the user was logged in. I wrote about an expansion of this method on Stackoverflow.

The general idea is that you want to maintain as little (or as much, sadly) state as you can between requests and given that HTTP is a stateless protocol you need to store it somewhere. Typically this information is stored in the session on the server but the authors of the application didn’t deem the session worthy and decided that they would store the user id of the user making the request in… wait for it… the ViewState.

Facepalm - because expressing how dumb this is in words is impossible.
 

Think about this for a second. What do we know about the ViewState?

Well, for starters, the ViewState is sent back and forth between the server and the client. This means that for every item that is stored in the ViewState, that item must be returned from the client to the server with every request. (Incidentally, they also made liberal use of UpdatePanel). This might be “OK” — not really, but wait for it — because the user id is only an integer, but this application stored EVERYTHING in the ViewState that it needed. No use of session variables were involved.

The BIGGER issue with storing the user id in the ViewState is that it is not secure. ViewState is not encrypted by default, merely encoded. Not surprisingly, there was no encrypting of the ViewState taking place in the code so anyone that could access it could decode it.

In the developer’s defense, they did encrypt the individual fields that were going into the ViewState, but unless they were encrypting them properly they may as well not have encrypted them at all.

The BIGGEST problem is that there is, essentially, no user authentication timeout. That means that the user can be using the site, step away from the computer for a week, a month, even a year, and then come back and continue on their merry way on the site. Or, go about browsing other sites, hit back in their browser to load a page from the site (there was also no client-side cache expiration), perform some new action and automatically be logged into the site. This might be fine on a site that’s not handling sensitive information. Unfortunately, this site is.

The lesson is that just because a framework or platform makes something available to you doesn’t mean that you have to use it. If you want to use it, make sure you understand what it is, how it functions, and what the repercussions of using it actually are.

I’m debating on turning this into a series given the number of issues I typically stumble upon when inheriting projects. If you would like to see more of this, let me know.

andy Coding Smarter

Comments Now Enabled

July 14th, 2009

I’ve enabled comments as I’m changing the direction of the blog. I’ll be posting more information shortly.

andy Uncategorized

Oops. Bump for ASP.NET WebResource.axd, and Reverse Proxies.

July 13th, 2009

So I hopped in my way-back machine trying to determine what posts, if any, to cull before I take a new approach to this blog — and attempt to be active in its updates. I found an old post, ASP.NET, Web Resource.axd, and Reverse Proxies, and noticed that it was marked as private. So… oops?
It’s now available for all to consume and deals with hiding IIS behind a reverse proxy, specifically Apache for me.

andy Uncategorized

Meadows Design Site Updated

February 26th, 2009

The Meadows Design site, meadowsdesign.com, has been updated with a newer look and a moderately thorough client list. Additionally, Andy Meadows — owner of Meadows Design — has posted his resume at meadowsdesign.com/resume-full.html.

andy Announcements, Clients

New MVC Tutorials Posted

February 26th, 2009

Steven Walther has posted a new set of MVC tutorials @ http://www.asp.net/learn/mvc/#MVC_SampleApp that are almost full-lifecycle. Might be worth a read.

andy ASP.NET, Development

SharePoint / KnowledgeBin Bridge

February 18th, 2009

Just finished a SharePoint / KnowledgeBin bridge for KnowledgeBin. The company provides a custom document management solution but they needed some larger integration with SharePoint to handle some issues outside of their domain. The bridge is only a Proof-of-Concept at the moment, but it works well and can certainly be hardened to be an exceptionally robust solution to their particular problem — and that of their client.

andy .Net Development, ASP.NET, SharePoint

JQuery autocomplete bug fix

December 16th, 2008

Submitted a bug for jquery’s autocomplete plugin that would occur when multiple selections were allowed but the selected had to exist before (multiple and mustMatch both set to true).  Simply put, when the user would try and type in a case-sensitive manner and the word after a space was capitalized, hitting the shift key would result in an invalid selection and would, thus, clear what had been typed.

I have no idea if this fix will be accepted and incorporated into the codebase, but it can be downloaded here if you need it.

andy Uncategorized

VMware Player and Bridged Connections

July 1st, 2008

Lost an hour today moving a VM from Fusion to VMware’s free VMware Player on Windows. The VPN I have to connect to will not work unless VMware’s connection is set to Bridged. On a machine with multiple network connections, this will not always be an active connection — for instance, getting the “best” connection from the OS could result in a wired connection that isn’t plugged in or it could, in my case, result in binding the bridged connection a Parallels virtual network adapter.

So, how does one go about fixing this? Enter vmnetcfg.exe. If you go to “%PROGRAMFILES%\VMWare\VMWare Player” and launch vmnetcfg.exe, you can configure all of your connections for virtual machines hosted in VMWare Player. In my case, I simply modified connection 0 to always use the same adapter — taking away the choice from the software — and restarted. Since then, all has been good.

andy Software

Using ImageMagick to create overlayed thumbnails

March 26th, 2008

Gotta keep this short, but it’s been a while and I wanted to throw this on the blog before I forgot.

I have a client that wanted to have a PDF icon displayed over a thumbnail of a pdf document. These thumbnails are generated dynamically from documents that my client was uploading to a custom CMS site. The goal was to place the PDF icon over the thumbnail in the way Windows puts the arrow overlay on a shortcut except the pdf icon overlay needed to be placed on the bottom right and offset from the image. All I have to say is thank God for ImageMagick.

Using ImageMagick, I wrote a little bash shell script that creates a matte for the background, resizes the pdf and places it on the top left of the matte and then overlays the pdf icon on the bottom right.  When it’s done, it deletes the matte.  You can change sizes and background colors through the variables at the top of the script.

#!/bin/bash

appname=`which $0`
appdir=`dirname $appname`

backgroundcolor=white
compositesize='88x100'
coversize='69x90'
overlay=${appdir}'/adobe-overlay.png'

echo $overlay

convert -size $compositesize xc:$backgroundcolor background.jpg

composite -size $compositesize -compose atop -gravity NorthWest -resize $coversize $1.pdf background.jpg $1.jpg

composite -compose atop -gravity SouthEast $overlay $1.jpg $1.jpg

rm -f background.jpg

This script was written for ImageMagick 5.4.7-10 running on RedHat 9 (yes, it’s old). It only works for single page pdfs and the overlay is assumed to exist in the same directory as the shell script. Contact me with any questions or comments.

andy Uncategorized