#!/usr/bin/env python

# The First M Numbers Classified in Alphabetical Order
# Copyright (c) 2013, 2018 Nick Montfort <nickm@nickm.com>
#
# Copying and distribution of this file, with or without modification, are
# permitted in any medium without royalty provided the copyright notice and
# this notice are preserved. This file is offered as-is, without any warranty.
#
# Updated 31 May 2018, changed "print" for Python 2 & 3 compatibility
# Updated 26 November 2018, substituted a shorter all-permissive license

def romanize(arabic):
    """Return the appropriate roman numeral.
    Should work for numbers in (1, 4999)."""
    numerals = [(1000, 'M'), (500, 'D'), (100, 'C'),
                (50, 'L'), (10, 'X'), (5, 'V'), (1, 'I')]
    smaller = {1000: (100, 'C'), 500: (100, 'C'),
               100: (10, 'X'), 50: (10, 'X'), 10: (1, 'I'),
               5: (1, 'I')}
    roman = ''
    for (value, numeral) in numerals:
        roman += int(arabic / value) * numeral
        arabic -= int(arabic / value) * value
        if value in smaller and arabic >= value - smaller[value][0]:
            roman += smaller[value][1] + numeral
            arabic -= (value - smaller[value][0])
    return roman

nums = []
for i in range(1, 1001):
    nums += [romanize(i)]
nums.sort()
print(' '.join(nums))
