Showing posts with label Code Puzzle. Show all posts
Showing posts with label Code Puzzle. Show all posts

Friday 12 August 2016

Code puzzle

G'day:
Apropos of nothing, here's a code puzzle.

Rules:
  • You have an array of strings, eg: ['a', 'at', 'cat', 'scat', 'catch'].
  • Return the first value that matches a regex pattern, eg: '.+at' would match cat, scat, catch; but we want cat returned.
  • Do not use any looping statements (eg: do/for/while etc).
  • Bear in mind functions are not statements ;-)
  • The array could be very long, the strings could be very long, and the pattern could be complex. But the desired value could be early in the array.
  • Use any language.

The prize:
Nothing really. Well: nothing at all.

Put yer answer in a Gist (or similar) and the URL to the Gist in a comment. IE: do not post code in a comment.

Righto.

--
Adam

Friday 16 October 2015

Code puzzle: getting the next three elements of an array

G'day:
I've got 25min to write this, so it'll be a bit scrappy. There's an interesting question on Stack Overflow at the moment: "Get next three elements in a ColdFusion list". The basic premise is the user has a list of IDs, and - given one of the IDs in the list - wants to get back the next three IDs (or fewer if there aren't three left). Here are some test inputs and expectations:

IDsIDCountResult
[7141,6881,5821,8001,7904,6601,7961,6021,4721]71413[6881,5821,8001]
[7141,6881,5821]71413[6881,5821]
[7141]71413[]
[1111]99993[]


I didn't want to answer that using lists because they're cack, so I set about answering with arrays instead. This is still relevant as the punter starts with an array anyhow, and is turning it into a list.

I was not happy with my first answer, which was this:

function nextIds(ids, id, count){
    var thisIdIdx = ids.find(id);
    var nextIds = [];
    for (var i=thisIdIdx+1; i <= ids.len() && nextIds.len() < count; i++){
        nextIds.append(ids[i]);
    }
    return nextIds;
}

It works, but it has a loop in it and I didn't like that. It also has a shortcoming that it doesn't behave sensibly if id is not present in ids. I posted it anyhow.

I continued to mull-over the problem and then remembered CFML has an Array.slice() method these days, so knocked together a variation using that instead:

function nextIds(ids, id, count){
    var thisIdIdx = ids.find(id);
    var idsLen = ids.len();
    if (thisIdIdx == 0 || thisIdIdx == idsLen){
        return [];
    }
    return ids.slice(thisIdIdx+1, min(idsLen-thisIdIdx, count));
}

That works, but it's a lot of code for the task at hand. There must be a better way.

I wanted to use a filter() or a reduce() or something, but couldn't come up with a clean way of doing it.

How would you do it? If you'd like to submit some code, do so in a gist (not inline in the comment), and feel free to use any language you like.

Cheers!

--
Adam

Monday 25 May 2015

Some CFML code that doesn't work

G'day:
I was sitting at Lord's yesterday watching England v NZ (and, um, we'll have no comments about that, thank-you very much), and a sudden thought popped into my head "Adam Presley might've been onto something there... if I leverage that....I wonder if I could get that code down to one statement?"

And that of course will mean nothing to anyone.

Friday 9 January 2015

Another quick code puzzle (any language)

G'day:
Yeah, I'm a bit cheeky suggesting another puzzle when I've not yet found time to review all the answers for the last one (Something for the weekend? A wee code puzzle (in CFML, PHP, anything really...)), from back in November! Oh well. I'm sure it's the fun of the exercise rather than me reporting back on it that's the key bit anyone.

TBH I had forgotten about the other one, but will review another of the answers this weekend. I'll make a point of it.

Anyway... what's this one all about..?

Tuesday 23 December 2014

JavaScript: Jasmine for unit testing

G'day:
At work, I've been tasked with getting the team up to speed with TDD whilst we redevelop our website in PHP. I knocked together a presentation on the subject a coupla months ago, but before having a chance to present it, got shifted about in the internal dept structure for a month or so, and it kinda got temporarily shelved. I posted it online: "TDD presentation". I'm back on the PHP Team now, and need to update said presentation to be more work-requirement-specific, as well as cover unit testing our JavaScript. This has been on our agenda for a coupla years, but was never allowed to get any traction by the decision makers. Decision-making has improved now, so we're all go.

I have heard about Jasmine, and like the look of it, but have never actually downloaded / installed / ran it. I'm gonna do that today.

(Oh, blogging my work is not something I do... I'm actually off on sick leave at the moment - which I feel slightly guilty about - but I need to get this stuff done, so gonna do it today whilst I am unlikely to get interruptions. I figured as I'm doing it on my own time, I get to blog about it too ;-)

I am writing about this as I do it.

Jasmine


First up: Jasmine. This is what Wikipedia has to say about Jasmine:

Jasmine is an open source testing framework for JavaScript. It aims to run on any JavaScript-enabled platform, to not intrude on the application nor the IDE, and to have easy-to-read syntax. It is heavily influenced by other unit testing frameworks, such as ScrewUnit, JSSpec, JSpec, and RSpec.
And from its own website:

Jasmine is a behavior-driven development framework for testing JavaScript code. It does not depend on any other JavaScript frameworks.

And a code sample from the same page:

describe("A suite", function() {
  it("contains spec with an expectation", function() {
    expect(true).toBe(true);
  });
});

If you're familiar with TestBox (and if you're a CFML dev, you bloody should be!), then this will look comfortingly familiar. Indeed that code would run on TestBox. I know a bit about TestBox, so this is pleasing: I have a head start!

Download & Install

I'm gonna use 2.1.3, which is - at time of writing - the latest version of Jasmine. The download page is here: jasmine 2.1.3. I've D/Led that and unzipped it into a public directory.

Running

This is too easy... it ships with a file SpecRunner.html, and browsing to that runs the tests. Here are the samples:



Nice!

Example Code

Looking at the code within SpecRunner.html, we see this:

Monday 22 December 2014

Weekend code puzzle: my answer (Go version)

G'day:
I sat down on Saturday / Sunday and taught myself enough Go to be able to answer the puzzle question from "Something for the weekend? A wee code puzzle (in CFML, PHP, anything really...)". I've certainly got a lot of mileage out of that one topic, eh? Oh well: I think it's a good baseline exercise for researchng new languages, and I like seeing other people's code, too. I'm so pleased I got such a good range of submissions. However it does mean it's taking me an age to slog through them all.

Right, so I discussed Adam Presley's two Go entries over the last few days: "Weekend puzzle: Adam Presley's answer (Go)" and "Weekend puzzle: Adam Presley's answer - redux (Go)", and I did a "G'day World in Go" exercise in prep for understanding his code. Not that a "G'day world" exercise was sufficient for that, but at least I got Go installed and compiling, and located the docs etc.

Sunday 21 December 2014

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

G'day:
Adam Presley is a bit of a star. I had a look at his original entry to the code puzzle and decided I didn't like it as it was too long-winded ("Weekend code puzzle: Adam Presley's answer (Go)"). He's subsequently come back to me with a simplified version, listed on his own blog: Response To Adam Cameron's Code Review. He took my comments in good grace, which is very good of him.

Let's have a look at this new version:

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.

Monday 24 November 2014

Weekend code puzzle: my answer (Python version)

G'day:
This is a companion exercise to my earlier articles:
I'll be using exactly the same logic as in those two, just working within Python's constraints. I freely admit to not knowing Python from a bar of soap - indeed this is the third bit of Python I have ever written - so I would not vouch for this being anything other than a comparison to the other two pieces of code, and not a demonstration of what a Python dev might consider "good code". This is not a Python tutorial.

Sunday 23 November 2014

Weekend code puzzle: Dave's answer (Python)

G'day:
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...)").

Dave's done a Python version. Like Chris just before him, Dave got his answer in before I varied the rules slightly, so his answer just finds the first longest subseries within the threshold from within the series; it does not check same-lengthed subseries for which has the highest within-threshold total. "Within" three times in a sentence. Sorry about that.

Thursday 20 November 2014

Weekend code puzzle: ChrisG's answer (CFML)

G'day:
OK, now I'm gonna look at each person's submissions for the code puzzle ("Something for the weekend? A wee code puzzle (in CFML, PHP, anything really...)").

Chris has done a CFML version. He was very quick off the mark, and had his submission in before I varied the rules, so this one just solves the initial challenge which is to find the longest subseries within a given threshold, but does not consider equal-length longest subseries. Fair enough.

Tuesday 18 November 2014

Weekend code puzzle: my answer (Ruby version)

G'day:
I'll move on from this topic soon, rest assured. Having come up with a CFML answer for my code puzzle question ("Weekend code puzzle: my answer (CFML version)"), I decided to stretch myself (albeit slightly) and work out how to do it in Ruby too.

For the sake of completeness, here it is.

Weekend code puzzle: my answer (CFML version)

G'day:
This is another follow-up to "Something for the weekend? A wee code puzzle (in CFML, PHP, anything really...)". Yesterday I posted my PHP version of a solution: "Weekend code puzzle: my "answer" (PHP version... which doesn't quite work)" and now here's my CFML one. NB: It's Railo-specific CFML: it won't run on ColdFusion.

Sunday 16 November 2014

Weekend code puzzle: my "answer" (PHP version... which doesn't quite work)

G'day:
Again, apologies for the delay here. I had a busy week and then yesterday was sidelined due to needing to go to the pub to watch the rugby. Still: better late than never... here's the beginning of my follow-up to "Something for the weekend? A wee code puzzle (in CFML, PHP, anything really...)".

Thursday 13 November 2014

Weekend code puzzle... what's happened?

G'day:
Yeah, I know I've been slack with the results from the weekend code puzzle ("Something for the weekend? A wee code puzzle (in CFML, PHP, anything really...)" and "Something after the weekend..."), but I've had a crazy week at work: no time before I start my day, nor lunch breaks; and I've been tied up in the evenings. But I still intend to follow this up as soon as I can, which will probably now be Saturday.

Sorry for the delay, and - again - thanks for all your entries.

Stay tuned...

--
Adam

Monday 10 November 2014

Something after the weekend...

G'day:
This is just a follow-up to my Friday article: "Something for the weekend? A wee code puzzle (in CFML, PHP, anything really...)". There's a bunch of good entries: cheers for that. There's CFML, PHP, Go, Python and Clojure examples there. However I'll still looking for more though: it's not too late to give it a bash. I have not looked at any of them (beyond superficially) yet, as I didn't want other people's work influencing my own.

Friday 7 November 2014

Something for the weekend? A wee code puzzle (in CFML, PHP, anything really...)

G'day:
Just to demonstrate I have not an original thought in my head, I am plagiarising the topic of this article / code puzzle from a question on Stack Overflow. It's also inspired by Duncan's "Project Euler" series of blog posts.

But just to get your brains stretching (albeit: only slightly), perhaps you might want to answer the question on my terms (which opens it to a broader audience), rather than those specific to the question.