Platform: Any
Language: Python
Requirements: None

Earlier today, I stumbled across a small programming challenge on rosettacode.org that required a solution in Python. I was intrigued enough to give it a shot and below you will find my approach to “commatizing” numbers in string.

The challenge is simple. Take a string, see if it contains numbers and then format the numbers in a specific way, by clustering the digits and inserting separators like commas, periods, blanks, or blanks, to make them more readable.

In essence, the string
pi=3.14159265358979323846264338327950288419716939937510582097494459231
will be turned into something like
pi=3.14159 26535 89793 23846 26433 83279 50288 41971 69399 37510 58209 74944 59231

The approach needs to be flexible so that clusters can have a variable length and the separator can be defined.

The workings of the code are very simple. I use a regular expression to locate series of digits in the string and then iterate through the resulting match to create a new output string. I am skipping the last match because it represents the End-of-string marker.

Whenever RegEx encounters a non-digit, it will create an empty match, so as I iterate through matches, I check if a match is empty and simply copy the respective letter from the original string.

If a valid digit match is found, I check the length of the series. If it’s shorter than the desired cluster length, it is copied verbatim, while longer series will be turned into a list of clusters of the desired length. They are then joined back together, using the separator. I am doing this with an often-overlooked little Python trick by applying the join() function to the separator string, while providing the list of clusters as a parameter.

The print statements are simple output code to illustrate the usage and the results of the function.

I hope this is something you can use for yourself some time.

Hang loose!

Facebooktwittergoogle_plusredditpinterestlinkedinmail