Skip to main content
added 659 characters in body
Source Link
hc_dev
  • 864
  • 6
  • 15

A few improvements following principles of Cleancode and Divide & Conquer:

  1. Split into small tasks, each one a function.
  2. Use meaningful names.
  3. Leverage the language's power.

Split into small tasks, each one a function

The challenge's description explicitly names 2 tasks, thus implemented in functions:

  1. convert_to_binary(number)
  2. max_consecutive_1s_of_binary(binary)

Above functions already have meaningful names (see 2), expressing what they do.

Besides you have other tasks, given implicitly:

  1. Read a (decimal) number from STDIN
  2. Determine length of a sequence (of max consecutive 1s)
  3. Print number to STDOUT (length of max consecutive 1s)

These side-tasks can be implemented using existing functions (in Python).

Use meaningful names

From above functional design you can derive data-structures and variables. Name them to express what (in context of your program) they contain:

  1. number_input
  2. binary_representation
  3. max_consecutive_1s
  4. length_of_max_consecutive_1s

Leverage the language's power

Use already built-in functions where suitable:

  1. slicing Use bin and slicing for stripping of the binary marker; or use format(number, 'b') directly
  2. string-splitting by regexstring-splitting by regex for split the binary representation into consecutive 1s using 0 as split-marker (very smart solutionsolution by Roman)
  3. max for finding the maximum of a list based on some key criterion (here the length)

See also

A few improvements:

  1. Split into small tasks, each one a function.
  2. Use meaningful names.
  3. Leverage the language's power.

Split into small tasks, each one a function

The challenge's description explicitly names 2 tasks, thus implemented in functions:

  1. convert_to_binary(number)
  2. max_consecutive_1s_of_binary(binary)

Above functions already have meaningful names (see 2), expressing what they do.

Besides you have other tasks, given implicitly:

  1. Read a (decimal) number from STDIN
  2. Determine length of a sequence (of max consecutive 1s)
  3. Print number to STDOUT (length of max consecutive 1s)

These side-tasks can be implemented using existing functions (in Python).

Use meaningful names

From above functional design you can derive data-structures and variables. Name them to express what (in context of your program) they contain:

  1. number_input
  2. binary_representation
  3. max_consecutive_1s
  4. length_of_max_consecutive_1s

Leverage the language's power

Use already built-in functions where suitable:

  1. slicing for stripping of the binary marker
  2. string-splitting by regex for split the binary representation into consecutive 1s using 0 as split-marker (very smart solution)
  3. max for finding the maximum of a list based on some key criterion (here the length)

A few improvements following principles of Cleancode and Divide & Conquer:

  1. Split into small tasks, each one a function.
  2. Use meaningful names.
  3. Leverage the language's power.

Split into small tasks, each one a function

The challenge's description explicitly names 2 tasks, thus implemented in functions:

  1. convert_to_binary(number)
  2. max_consecutive_1s_of_binary(binary)

Above functions already have meaningful names (see 2), expressing what they do.

Besides you have other tasks, given implicitly:

  1. Read a (decimal) number from STDIN
  2. Determine length of a sequence (of max consecutive 1s)
  3. Print number to STDOUT (length of max consecutive 1s)

These side-tasks can be implemented using existing functions (in Python).

Use meaningful names

From above functional design you can derive data-structures and variables. Name them to express what (in context of your program) they contain:

  1. number_input
  2. binary_representation
  3. max_consecutive_1s
  4. length_of_max_consecutive_1s

Leverage the language's power

Use already built-in functions where suitable:

  1. Use bin and slicing for stripping of the binary marker; or use format(number, 'b') directly
  2. string-splitting by regex for split the binary representation into consecutive 1s using 0 as split-marker (very smart solution by Roman)
  3. max for finding the maximum of a list based on some key criterion (here the length)

See also

Source Link
hc_dev
  • 864
  • 6
  • 15

A few improvements:

  1. Split into small tasks, each one a function.
  2. Use meaningful names.
  3. Leverage the language's power.

Split into small tasks, each one a function

The challenge's description explicitly names 2 tasks, thus implemented in functions:

  1. convert_to_binary(number)
  2. max_consecutive_1s_of_binary(binary)

Above functions already have meaningful names (see 2), expressing what they do.

Besides you have other tasks, given implicitly:

  1. Read a (decimal) number from STDIN
  2. Determine length of a sequence (of max consecutive 1s)
  3. Print number to STDOUT (length of max consecutive 1s)

These side-tasks can be implemented using existing functions (in Python).

Use meaningful names

From above functional design you can derive data-structures and variables. Name them to express what (in context of your program) they contain:

  1. number_input
  2. binary_representation
  3. max_consecutive_1s
  4. length_of_max_consecutive_1s

Leverage the language's power

Use already built-in functions where suitable:

  1. slicing for stripping of the binary marker
  2. string-splitting by regex for split the binary representation into consecutive 1s using 0 as split-marker (very smart solution)
  3. max for finding the maximum of a list based on some key criterion (here the length)