On 17/01/2021 02:15, Dan Stromberg wrote:

IMO a good set of tests is much more important than type annotations ;)

   def get_longest(string: str) -> typing.Tuple[int, typing.List[str]]:
       """Get the longest run of a single consecutive character."""

May I ask why you artificially limit the signature to str?
If there were a signature, shouldn't it be something like

Iterable[E] --> Tuple[int, List[E]]

where E may be anything that has an equality operation?
As someone who has not yet looked seriously into typing: is there a way
to spell this?

       if not string:
           return (0, [])
       grouped = itertools.groupby(string)
       grouped_with_lengths = [(len(list(value)), key) for key, value in
grouped]
       max_count_and_letter = max(grouped_with_lengths)
       max_count = max_count_and_letter[0]
       result = (max_count, sorted(list_ for count, list_ in
grouped_with_lengths if count == max_count))
       return result

If you want to dabble some more here's a test case you might strive for
your function to pass:

"""
    >>> class A:
    ...     def __init__(self, x):
    ...         self.x = x
    ...     def __eq__(self, other):
    ...         return self.x == other.x
    ...     def __repr__(self):
    ...         return self.x

    >>> get_longest(map(A, "aaabbcccdaa"))
    (3, [a, c])

"""
--
https://mail.python.org/mailman/listinfo/python-list

Reply via email to