avatar Bite 76. The singledispatch countdown challenge

We are stoked and grateful to have Martin have you practice functools.singledispatch (new in Python 3.4).

PEP 443 defines generic programming / a single-dispatch generic function as:

A generic function is composed of multiple functions implementing the same operation for different types. Which implementation should be used during a call is determined by the dispatch algorithm. When the implementation is chosen based on the type of a single argument, this is known as single dispatch.

Let's write one ... Martin:

Your challenge today is to have count_down print any given input in the following manner:


Your solution should be able to handle any of these data types and still get the same results:

  [int]: 1234
  [str]: '1234'
 [list]: [1, 2, 3, 4] or ['1', '2', '3', '4']
[tuple]: (1, 2, 3, 4) or ('1', '2', '3', '4')
  [set]: {1, 2, 3, 4} or set([1, 2, 3, 4])
 [dict]: {1: 'one', 2: 'two', 3:'three', 4:'four'} or
         {'1': 'one', '2': 'two', '3':'three', '4':'four'}

Don't forget to support a float! Example for 12.34 it should work as:


If a data type is not supported (datetime, itertools.compress or re.compile for example), it should raise a ValueError.

Good luck, and have fun!

Login and get coding
go back Advanced level
Bitecoin 4X

Will you be Pythonista #137 to crack this Bite?
Resolution time: ~62 min. (avg. submissions of 5-240 min.)
Pythonistas rate this Bite 4.8 on a 1-10 difficulty scale.
» Up for a challenge? 💪

We use Python 3.8