Quantcast
Channel: Adam Cameron's Dev Blog
Viewing all articles
Browse latest Browse all 1333

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

$
0
0
G'day:
Just to demonstrate I have not an original thought in my head, I am plagiarising the topic of this article / quiz 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.

The question is a PHP-specific one, but I don't care about that: it's a good algorithmic question for any language, and isn't too hard.

This is the original question: "PHP array read out succession", however for the purposes of this exercise, I'm going to revise it slightly.

Problem

One has an array of numbers, eg:

[100, 300, 100, 50, 50, 50, 50, 50, 500, 200, 100]

One also has a threshold number, for example 500.

For a given array and a given threshold, return the subarray which contains the longest run of consecutive numbers which - in total - are equal-to or less than the threshold.

For example:

series = [100, 300, 100, 50, 50, 50, 50, 50, 500, 200, 100]
threshold = 500
subseries = getSubseries(series,threshold) // [100, 50, 50, 50, 50, 50]

In this example, 100,300,100 are less than or equal to 500, and has a length of three. 300,100,50,50 has a length of four, but 100,50,50,50,50,50 has a length of six, which is the longest sequence, so is the answer.

Rules:
  1. answer in any or as many languages as you like. If the language isn't one I'm au fait with (so one of CFML, PHP, JS or Ruby, really), you might need to explain it to me ;-)
  2. Answer via a gist (etc) or code on GitHub.
  3. No code submitted in comments to this article will be accepted, and I will actively pillory you for not following instructions if you do so.
  4. The code must work for any non-negative numeric array, with any numeric threshold. Demonstrate it for the example above.
  5. It's up to me who wins. I can't offer much by way of prize, although I'll name you, write an article about your answer, and buy you a drink next time I see you.

Things that will improve your chances of "winning":

  • Use Clean Code principles.
  • Use TDD (so submit your tests too).
  • Answers that I find more technically interesting are more likely to win than answers that are pedestrian or obvious. Scalpels rather than sledgehammers.

Note: I will be participating as well. I have not started yet, nor really even thought about my approach. I will write up my answer though once I've done it.

Please do circulate this to your colleagues: the more answers we get, the more interesting it'll be.

Cheers!

--
Adam

Viewing all articles
Browse latest Browse all 1333

Trending Articles