{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "I\n", "MMMM\n" ] } ], "source": [ "# Write a Python program to convert an integer to a roman numeral.\n", "\n", "class py_solution:\n", " def int_to_Roman(self, num):\n", " val = [\n", " 1000, 900, 500, 400,\n", " 100, 90, 50, 40,\n", " 10, 9, 5, 4,\n", " 1\n", " ]\n", " syb = [\n", " \"M\", \"CM\", \"D\", \"CD\",\n", " \"C\", \"XC\", \"L\", \"XL\",\n", " \"X\", \"IX\", \"V\", \"IV\",\n", " \"I\"\n", " ]\n", " roman_num = ''\n", " i = 0\n", " while num > 0:\n", " for _ in range(num // val[i]):\n", " roman_num += syb[i]\n", " num -= val[i]\n", " i += 1\n", " return roman_num\n", "\n", "\n", "print(py_solution().int_to_Roman(1))\n", "print(py_solution().int_to_Roman(4000))" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.1" } }, "nbformat": 4, "nbformat_minor": 2 }