#! /usr/bin/env python # -*- coding: utf-8 -*- # Time-stamp: "2007-10-18 00:27:04 sceptik" # Author: Alex A. Smirnov # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License as # published by the Free Software Foundation; either version 2, or (at # your option) any later version. # # This program is distributed in the hope that it will be useful, but # WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU # General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write the Free Software # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA # 02110-1301 USA from string import join from math import modf class RomanOutOfBounds(Exception): pass class RomanValueError(Exception): pass alternate_numeric = {10: 'A', 11: 'B', 12: 'C', 13: 'D', 14: 'E', 15: 'F', 16: 'G', 17: 'H', 18: 'I', 19: 'J', 20: 'K', 21: 'L', 22: 'M', 23: 'N', 24: 'O', 25: 'P', 26: 'Q', 27: 'R', 28: 'S', 29: 'T', 30: 'U', 31: 'V', 32: 'W', 33: 'X', 34: 'Y', 35: 'Z', 36: 'a', 37: 'b', 38: 'c', 39: 'd', 40: 'e', 41: 'f', 42: 'g', 43: 'h', 44: 'i', 45: 'j', 46: 'k', 47: 'l', 48: 'm', 49: 'n', 50: 'o', 51: 'p', 52: 'q', 53: 'r', 54: 's', 55: 't', 56: 'u', 57: 'v', 58: 'w', 59: 'x', 60: 'y', 61: 'z', 62 : '+', 63 : '/'} ralternate_numeric = dict( zip(alternate_numeric.values(), alternate_numeric.keys()) ) roman_fundamental = (('M', 1000), ('CM', 900), ('D', 500), ('CD', 400), ('C', 100), ('XC', 90), ('L', 50), ('XL', 40), ('X', 10), ('IX', 9), ('V', 5), ('IV', 4), ('I', 1)) decimal_binary_eq = {'0' : '0000', '1' : '0001', '2' : '0010', '3' : '0011', '4' : '0100', '5' : '0101', '6' : '0110', '7' : '0111', '8' : '1000', '9' : '1001', '.' : '.'} rdecimal_binary_eq = dict( zip(decimal_binary_eq.values(), decimal_binary_eq.keys()) ) def dec2roman(number): """ Dec -> Roman """ if (number != int(number)): raise RomanValueError(number) if not (0 < number < 4000): raise RomanOutOfBounds(number) result = '' for roman, value in roman_fundamental: while number >= value: number -= value result += roman return result def roman2dec(number): """ Roman -> Dec """ # TODO: добавить проверки simple_roman = filter(lambda item: len(item[0]) == 1, roman_fundamental) complex_roman = filter(lambda item: len(item[0]) > 1, roman_fundamental) result = 0 for romans in [complex_roman, simple_roman]: for roman, value in romans: count = number.count(roman) if not count: continue number = number.replace(roman, '') result += count * value return result def dec2any(number, radix, accuracy=15): """ Dec -> Any """ if not number: return 0 result = '' fracp, intp = modf(number) number = int(intp) while number: division = number % radix number /= radix result += (alternate_numeric[division] if radix > 10 and division >= 10 else str(division)) result = result[::-1] if fracp: result += ('.' if result else '0.') while fracp and accuracy: accuracy -= 1 fracp = fracp * radix fracp, intp = modf(fracp) result += (alternate_numeric[int(intp)] if radix > 10 and intp >= 10 else str(int(intp))) return result def any2dec(number, radix): """ Any -> Dec """ power, result = 0, 0 dotpos = number.find('.') if dotpos > 0: power = -len(number[dotpos + 1:]) for position in number[::-1]: if position == '.': continue try: value = ralternate_numeric[position] except KeyError: value = int(position) result += value * (radix ** power) power += 1 return result def dec2bindec(number): """ Decimal -> Binary-Decimal """ result = '' for position in str(number): result += decimal_binary_eq[position] return result def bindec2dec(number): """ Binary-Decimal -> Decimal """ if not number: return 0 parts = str(number).split('.') if len(parts) > 1: parts[1] += ((4 - len(parts[1]) % 4) * '0' if len(parts[1]) % 4 else '') parts[0] = ((4 - len(parts[0]) % 4) * '0' if len(parts[0]) % 4 else '') + parts[0] results = [] for part in parts: result = '' for state in xrange(0, len(part) / 4): result += rdecimal_binary_eq[part[state * 4:(state + 1) * 4]] results += [result] return join(results, '.') def any2any(number, fradix, tradix): return dec2any(any2dec(number, int(fradix)), int(tradix))