4

There's an exercise on Hackerrank, a programming website, that asks me to find the number of consecutive 1s in a binary representation of a decimal number (e.g. 5 is 101 in binary). The author of the problem gave sample inputs and outputs along with his explanations:

Sample Case 1: The binary representation of 5 is 101, so the maximum number of consecutive 1's is 1.

Sample Case 2: The binary representation of 13 is 1101, so the maximum number of consecutive 1's is 2.

Why are there 1 consecutive 1s in the number 101? The term consecutive by its very definition implies sequential multiplicity. It makes sense to say that there are two consecutive 1s in the second example because there is a single "1" followed by yet another "1", but why does it make sense to say there are 1 consecutive 1s? I've never heard anyone use the term "consecutive" to describe a single item.

8

First note the context in which these examples are presented - namely in the context of programming.

It is true that colloquially we would not say that there is one consecutive instance of the digit 1. However, in the more formal setting of programming it is desirable to handle exceptions without dealing with special cases. This is because case handling is generally considered to be inelegant in programming.

Therefore, a programmer will most likely write a method (called, for example countConsecutive(String, String)) that simply counts the number of "consecutive" instances of some string in another string and then call this the number of consecutive instances.

For example, the programmer's method would most likely say that there are zero consecutive twos in the string "101".

  • 4
    +1. To be more formal, the author of the problem should have ask for the length of the longest sequence of 1s. – Graffito Dec 25 '16 at 21:56
  • 3
    @Graffito Yes, this would certainly make these examples more transparent. Unfortunately, in my experience, a programmer's ability to express themself formally ends at their coding ;-) – Nick Dec 25 '16 at 22:00
  • @Graffito Ahhh, that phrasing is so much better! I guess that's what they meant to say. Thank you both. – AleksandrH Dec 25 '16 at 22:11
  • This sort of language is also common in mathematics. – Harry Johnston Dec 26 '16 at 0:53
  • @HarryJohnston Yes indeed. To the uninitiated, saying something like "this is trivially true for the empty set" would be rather perplexing. – Nick Dec 26 '16 at 0:58
4

Think of 'consecutive' here as 'in a row'. In 1101 you have three ones, but only two in a row.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.