5

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.

2 Answers 2

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".

8
  • 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, 2016 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 ;-)
    – epsilon
    Dec 25, 2016 at 22:00
  • @Graffito Ahhh, that phrasing is so much better! I guess that's what they meant to say. Thank you both. Dec 25, 2016 at 22:11
  • 1
    This sort of language is also common in mathematics. Dec 26, 2016 at 0:53
  • @HarryJohnston Yes indeed. To the uninitiated, saying something like "this is trivially true for the empty set" would be rather perplexing.
    – epsilon
    Dec 26, 2016 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 and acknowledge that you have read and understand our privacy policy and code of conduct.

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