Simple algorithm to insert commas (commaize) in 9 languages


This project contains nine different language implementations of a simple algorithm named “commaize” that inserts commas into a number. The languages are: bash, c++, java, javascript, perl, php, python, ruby and tcl. My hope is that it will be a useful reference.

Contents

  1. Introduction
  2. Downloads
  3. Download Files
  4. Installation
  5. Bash Implementation
  6. C++ Implementation
  7. Java Implementation
  8. Javascript Implementation
  9. Perl Implementation
  10. PHP Implementation
  11. Python Implementation
  12. Ruby Implementation
  13. Tcl Implementation
  14. Tested Platforms
  15. License

Introduction

I created it because I sometimes have to work on code written in a variety of languages. Having an example like this is really helpful as a reference for languages that I rarely use (like tcl). Note that because these implementations are meant to act as a reference, they are not optimized.

This project contains the algorithm coded in complete standalone programs. Each program prints a similar list of formatted data in 3 columns. The first column is the test number, the second is the language specific format of the input and the third column is the output. That third column must be identical for all programs. In some cases there is a fourth column to remind me of what the row is for. This is what the output looks like for the python implementation (commaize.py):

Note that there are two compiled languages: c++ and java so there is a Makefile that will run the compilations. You will have to edit it to pick up the correct compiler. When you do you will see that I used gcc-4.8.2 installed locally on my Mac as described here: http://joelinoff.com/blog/?p=1003 because I wanted the latest C++-11 features.

The javascript implementation requires browser rendering so it has an associated commaize.html file.

All of the source code is freely available based on the MIT license. See the LICENSE.txt file for details.

Downloads

All of the implementations are available to download. There are three archive formats: tar.bz2, tar.gz and zip.

File Size Checksum Extract Command
commaize.tar.bz2 13K 60400 tar jxf commaize.tar.bz2
commaize.tar.gz 14K 5113 tar zxf commaize.tar.gz
commaize.zip 21K 54782 unzip commaize.zip

Download Files

The archives contain the following files in a local commaize directory.

File Description
commaize.html HTML file that references commaize.js.
commaize.cc The C++ implementation. Uses some features from C++11.
commaize.java The java (1.6) implementation.
commaize.js The javascript implementation.
commaize.pl The perl (5.12) implementation.
commaize.php The php (5.4) implementation.
commaize.py The python (2.7) implementation.
commaize.rb The ruby (0.9.6, 1.8.7) implementation.
commaize.sh The bash implementation.
commaize.tcl The tcl (8.6) implementation.
golden.txt The golden output for testing.
LICENSE.txt The source code license information.
Makefile Builds and tests the implementations. Here are some interesting targets.

  1. all – check, build and test
  2. build – build the executables for C++ and java
  3. check – check the language compilers/interpreters
  4. clean – clean up
  5. test – run the tests

You will need to modify this file for your environment.

README.txt Information about the project.

Installation

The package is available in 3 formats: bzipped tar format, gzipped tar format and zip format. The following example shows how to download, install and run the project using the bzipped tar format.

Bash Implementation

This is the bash implementation.

C++ Implementation

This is the C++ implementation. It uses C++-11 constructs so it will not work on platforms that do not have a recent C++ compiler. In my case I had to install an updated version of the C++ compiler to handle C++-11 constructs from http://joelinoff.com/blog/?p=1003.

This is how I compiled it.

This is the source code.

Java Implementation

This is the java implementation. You compile it as follows:

Here is the source code.

Javascript Implementation

This is the javascript implementation. It consists of two parts: an HTML wrapper and the javascript code. You can run it in your browser by navigating to the HTML page or you can use a local javascript testing environment like rhino.

When you run it in your browser the output looks like this:

Screen Shot 2014-03-29 at 9.11.19 AM

Here is the HTML source code.

Here is the javascript source code. Note that this implementation deliberately avoids the use of libraries like jquery. For a production system you definitely want to use javascript libraries to make your code easier to read and more maintainable.

Perl Implementation

This is the perl implementation.

PHP Implementation

This is the PHP implementation. This is how you run it.

This is the source code.

Python Implementation

This is the python implementation. Note that I use asserts because they are available natively.

Ruby Implementation

This is the ruby implementation.

Tcl Implementation

This is the tcl implementation.

Tested Platforms

I have tested this on the following platforms. In all cases I had to install an updated versions of the C++ compiler to handle C++-11 constructs from http://joelinoff.com/blog/?p=1003.

Platform Status> Notes
CentOS 5.5 Passed Required installion of g++ 4.8.2
CentOS 5.8 Passed Required installation of g++ 4.8.2
CentOS 6.5 Passed Required installation of g++ 4.8.2
Mac OS X 10.9.2 Passed Required installation of g++ 4.8.2

License

This software is licensed under the MIT open source license.

Copyright (c) 2014 by Joe Linoff

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Enjoy!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.