954,545 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?

Counting digit triples without modules

0
By Tony Veijalainen on Jul 24th, 2011 7:42 pm

Another task from C++ forum is trivial in Python even taking handicap of not using Counter.

"""Count nonoverlapping consecutive three digit numbers inside string
   without using modules

   """

s = '[@@@123124123123125@@@]'
f = next(n for n,c in enumerate(s) if c.isdigit())
numbers = (s for s in (''.join(x) for x in zip(s[f::3], s[f+1::3], s[f+2::3])) if s.isdigit())
counts = {}
for n in numbers:
    counts[n] = counts.setdefault(n, 0) + 1
print(counts)

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: