Wednesday, 17 December 2014

Weekend code puzzle: Adam Presley's answer (Go)

G'day:
At long last, I'm continuing to look at each person's submissions for the code puzzle ("Something for the weekend? A wee code puzzle (in CFML, PHP, anything really...)"). There's been a bit of a hiatus because I've been busy with other things, plus generally not that interested in keeping up with this blog. Oops.

OK, so Adam's effort is in Go. The github repo for it can be found here: https://github.com/adampresley/adamCameronCodeChallenge201411.



Now... I don't want to take anything away from Adam's very fine efforts, but I am immediately disqualifying him. Why? He's allowed himself too much "scope creep". I was after a function in a gist, not an entire application. He's also gone a bit overboard with the engineering side of things: coding it to run sections in parallel and to be able to run on multiple processors etc. This was not part of the remit, and there's a lesson to be learned in this: if given a requirement... stick to the requirement. In this instance there's simply no need to go to those lengths for a simple puzzle.

I'm sure the Go code is all very interesting, but gimme a break! I'm not going to look at it in any detail.

Again, this is not to say I'm not impressed with his efforts, and appreciate that he put so much time into it, but it's just OTT for a little code puzzle.

One good thing about go is that it can be compiled to an exe. This did require some horsing around on my part (read: actually researching what needs doing), and making sure my files were in the directory the compiler expected (source in a src subdir of the workspace). This wasn't so hard though. Ultimately it is just doing this:

C:\src\go>go install github.com/adampresley/adamCameronCodeChallenge201411

This created adamCameronCodeChallenge201411.exe in the bin subdirectory. The exe is 2MB, btw. This seems like an awful lot for simply skimming over an array. But we don't live in those days of scrabbling about tos ave every single byte though, I guess.

I did put the code through its paces, and it fared pretty well. Just one error, and one shortfall in the UI:


C:\src\go>test

C:\src\go>bin\adamCameronCodeChallenge201411.exe -input="100,300,100,50,50,50,50,50,500,200,100" -threshold=500
Sequence [100 50 50 50 50 50] wins with a length of 6  for a total of 350

PASS


C:\src\go>bin\adamCameronCodeChallenge201411.exe -input="600,100,100,100,600" -threshold=500
Sequence [100 100 100] wins with a length of 3  for a total of 300

PASS


C:\src\go>bin\adamCameronCodeChallenge201411.exe -input="600,100,100,100,600,100,100,100,100,600" -threshold=500
Sequence [100 100 100 100] wins with a length of 4  for a total of 400

PASS


C:\src\go>bin\adamCameronCodeChallenge201411.exe -input="600,100,100,100,600,200,100,100,100,100,100,600" -threshold=500
Sequence [100 100 100 100 100] wins with a length of 5  for a total of 500

PASS

C:\src\go>bin\adamCameronCodeChallenge201411.exe -input="" -threshold=500
ERROR - Unable to convert  to integer

PROBABLE UI SHORTFALL: DISCOUNTED


C:\src\go>bin\adamCameronCodeChallenge201411.exe -input="600,700,800,900" -threshold=500
panic: runtime error: index out of range

goroutine 1 [running]:
github.com/adampresley/adamCameronCodeChallenge201411/subarray.GetLongestSequence(0x5a5528, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0)
        C:/src/go/src/github.com/adampresley/adamCameronCodeChallenge201411/subarray/Calculator.go:29 +0x10e
main.main()
        C:/src/go/src/github.com/adampresley/adamCameronCodeChallenge201411/subArraySequence.go:43 +0x4a9

goroutine 5 [runnable]:
github.com/adampresley/adamCameronCodeChallenge201411/scheduler.func-À001()
        C:/src/go/src/github.com/adampresley/adamCameronCodeChallenge201411/scheduler/Scheduler.go:31 +0x21a
created by github.com/adampresley/adamCameronCodeChallenge201411/scheduler.GetSliceCounts
        C:/src/go/src/github.com/adampresley/adamCameronCodeChallenge201411/scheduler/Scheduler.go:39 +0x221

FAIL


C:\src\go>bin\adamCameronCodeChallenge201411.exe -input="50,60,70,80,90" -threshold=100
Sequence [90] wins with a length of 1  for a total of 90

PASS


C:\src\go>bin\adamCameronCodeChallenge201411.exe -input="50,60,70,80,90" -threshold=400
Sequence [50 60 70 80 90] wins with a length of 5  for a total of 350

PASS


C:\src\go>bin\adamCameronCodeChallenge201411.exe -input="100,100,100,100,100,100,600,150,150,150,150" -threshold=600
Sequence [100 100 100 100 100 100] wins with a length of 6  for a total of 600

PASS


C:\src\go>bin\adamCameronCodeChallenge201411.exe -input="600,150,150,150,150,100,100,100,100,100,100" -threshold=600
Sequence [100 100 100 100 100 100] wins with a length of 6  for a total of 600

PASS

C:\src\go>bin\adamCameronCodeChallenge201411.exe -input="100,200,300,400" -threshold=100
Sequence [100] wins with a length of 1  for a total of 100

PASS


C:\src\go>bin\adamCameronCodeChallenge201411.exe -input="400,300,200,100" -threshold=100
Sequence [100] wins with a length of 1  for a total of 100

PASS


C:\src\go>bin\adamCameronCodeChallenge201411.exe -input="100,300,100,50,50,50,50,50,500,200,100" -threshold=500
Sequence [100 50 50 50 50 50] wins with a length of 6  for a total of 350

PASS


C:\src\go>bin\adamCameronCodeChallenge201411.exe -input="100,50,50,50,50,50,500,100,60,60,60,60,60,500" -threshold=500
Sequence [100 60 60 60 60 60] wins with a length of 6  for a total of 400

PASS


C:\src\go>bin\adamCameronCodeChallenge201411.exe -input="100,60,60,60,60,60,500,100,50,50,50,50,50,500" -threshold=500
Sequence [100 60 60 60 60 60] wins with a length of 6  for a total of 400

PASS


C:\src\go>

The glitch was down to one of the tests taking an empty array, and the UI not allowing me to enter an empty array.

The error... well I'm guessing the logic doesn't allow for the threshold to be lower than all of the individual array entries. I'm pretty sure I had this same glitch during dev of my logic too. It goes to show the merits of edge-case testing.

Adam's also used a BDD-styled testing framework: GoConvey. It looks good, but I was slightly put off by the self-congratulatory tone of the docs (it reminded me of some of Ortus's *Box docs, in that regard). Advice: don't congratulate yourself when writing something about yourself; let other people do that for you. It sounds less hubristic that way.

I did not get a chance to see said tests in action though, because following Adam's instructions:

C:\src\go\src\github.com\adampresley\adamCameronCodeChallenge201411>go test ./... -v

I just got this after a few seconds:

? github.com/adampresley/adamCameronCodeChallenge201411 [no test files]
go build github.com/jtolds/gls: no buildable Go source files in
FAIL github.com/adampresley/adamCameronCodeChallenge201411/scheduler [build failed]
FAIL github.com/adampresley/adamCameronCodeChallenge201411/subarray [build failed]

Undoubtledly this is just me not having set something up correctly, but I'm feeling lazy so didn't investigate further. I probably will investigate when I come to write my own solution. It'll be good to have a testing framework up and running.

The Go language does look interesting, so I will try to find some time to look over Adam's stuff at some point, and also perhaps try to come up with a simple solution for myself.

Shudder. The next answer is Charlie Griefer's one - the file name is damn-you-cameron.clj (chuckle), and is in Clojure. Clojure looks like a sea of parentheses with a random smattering of other characters peppered about the place to me. Apparently it makes sense to some people. Not me. Yet. I think I'll perhaps fiddle around with the - more familiar looking - Go for a bit yet.

That said... I've got some CFML and some PHP stuff to get done first. I will however get to it though.

Adam, if you still feel like it after I've dismissed your entry so readily, submit a short-form answer (just a function and a coupla quick tests, mate!), and I'll write that up as well.

Okey doke. On with some CFML now. And then a bit of PHP.

--
Adam