Skip to content

Instantly share code, notes, and snippets.

@AngelicosPhosphoros
Last active September 10, 2018 22:17
Show Gist options
  • Save AngelicosPhosphoros/a284b2b5393ab0355a64abdb99385a09 to your computer and use it in GitHub Desktop.
Save AngelicosPhosphoros/a284b2b5393ab0355a64abdb99385a09 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Roman numerals"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## I. Roman numerals to decimals\n",
"\n",
"Write a function which receives a Roman numeral written out as a string, and returns an integer representing the decimal form of the input number. "
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"mapping = {\"I\" : 1, \n",
" \"V\" : 5,\n",
" \"X\" : 10,\n",
" \"L\" : 50,\n",
" \"C\" : 100,\n",
" \"D\" : 500,\n",
" \"M\" : 1000}"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"def roman_to_decimal(rom):\n",
" \"\"\"Convert a Roman numeral to decimal.\n",
" \n",
" Parameters\n",
" ----------\n",
" rom : str\n",
" A Roman numeral representing a positive integer.\n",
" \n",
" Returns\n",
" -------\n",
" dec : int\n",
" The result of conversion of `rom` into a decimal system.\n",
" \"\"\"\n",
" last = False\n",
" result = 0\n",
" for char in reversed(rom):\n",
" if last and mapping[char]<last:\n",
" last = mapping[char]\n",
" result -= mapping[char]\n",
" else:\n",
" last = mapping[char]\n",
" result += mapping[char]\n",
" \n",
" return result"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Here are some tests for you to test your code. Your code must pass all of them. You also need to come up with several more tests (your choice)."
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"True\n",
"True\n",
"True\n",
"True\n",
"True\n"
]
}
],
"source": [
"test_pairs = [(\"IX\", 9), (\"XI\", 11), (\"MCCII\", 1202), (\"MMXVIII\", 2018), (\"XLIX\", 49)]\n",
"\n",
"for rom, dec in test_pairs:\n",
" converted = roman_to_decimal(rom)\n",
" print(converted == dec)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## II. Decimal numbers to roman numerals.\n",
"\n",
"The maximum grade for first task (Roman to decimal) is 7 on the 10-point HSE scale. For extra credit, complete the second task: *given a decimal number, convert it to the Roman form*."
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"REVERSE_MAPPING = [\n",
" (1000, \"M\"),\n",
" (900, \"CM\"),\n",
" (500, \"D\"),\n",
" (400, \"CD\"),\n",
" (100, \"C\"),\n",
" (90, \"XC\"),\n",
" (50, \"L\"),\n",
" (40, \"XL\"),\n",
" (10, \"X\"),\n",
" (9, \"IX\"),\n",
" (5, \"V\"),\n",
" (4, \"IV\"),\n",
" (1, \"I\"),\n",
"]\n",
"def decimal_to_roman(dec):\n",
" \"\"\"Convert a decimal to the Roman form.\n",
" \n",
" Parameters\n",
" ----------\n",
" dec : int\n",
" A positive integer number\n",
" \n",
" Returns\n",
" -------\n",
" rom : str\n",
" A string representation of a Roman numeral form of `dec`.\n",
" \"\"\"\n",
" local_copy = int(dec) # maybe we have something nonprimitive\n",
" rom = \"\"\n",
" for rem, latin in REVERSE_MAPPING:\n",
" while rem<=local_copy:\n",
" rom += latin\n",
" local_copy -= rem\n",
" return rom"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You need to come up with test cases to show that your conversion works as expected. \n",
"NB: the conversion is ambiguous in some cases. Any valid conversion is accepted. "
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"True 9 IX IX\n",
"True 11 XI XI\n",
"True 1202 MCCII MCCII\n",
"True 2018 MMXVIII MMXVIII\n",
"True 49 XLIX XLIX\n"
]
}
],
"source": [
"for rom, dec in test_pairs:\n",
" converted = decimal_to_roman(dec)\n",
" print(converted == rom, dec, rom, converted)\n"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"All OK!\n"
]
}
],
"source": [
"failed = False\n",
"for dec in range(100000):\n",
" if dec!=roman_to_decimal(decimal_to_roman(dec)):\n",
" failed = True\n",
" print(\"Failed on %d=%s\", dec, decimal_to_roman(dec))\n",
" \n",
"if not failed:\n",
" print(\"All OK!\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"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.5"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment