exercism/julia/trinary/trinary.ipynb
2022-08-24 14:28:45 +02:00

69 lines
4.5 KiB
Plaintext

{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": ["# Trinary\n", "\n", "Convert a trinary number, represented as a string (e.g. '102012'), to its\n", "decimal equivalent using first principles.\n", "\n", "The program should consider strings specifying an invalid trinary as the\n", "value 0.\n", "\n", "Trinary numbers contain three symbols: 0, 1, and 2.\n", "\n", "The last place in a trinary number is the 1's place. The second to last\n", "is the 3's place, the third to last is the 9's place, etc.\n", "\n", "```shell\n", "# \"102012\"\n", " 1 0 2 0 1 2 # the number\n", "1*3^5 + 0*3^4 + 2*3^3 + 0*3^2 + 1*3^1 + 2*3^0 # the value\n", " 243 + 0 + 54 + 0 + 3 + 2 = 302\n", "```\n", "\n", "If your language provides a method in the standard library to perform the\n", "conversion, pretend it doesn't exist and implement it yourself.\n", "\n", "## Source\n", "\n", "All of Computer Science [http://www.wolframalpha.com/input/?i=binary&a=*C.binary-_*MathWorld-](http://www.wolframalpha.com/input/?i=binary&a=*C.binary-_*MathWorld-)\n", "\n", "## Version compatibility\n", "This exercise has been tested on Julia versions >=1.0.\n", "\n", "## Submitting Incomplete Solutions\n", "It's possible to submit an incomplete solution so you can see how others have completed the exercise."]
},
{
"cell_type": "markdown",
"metadata": {},
"source": ["## Your solution"]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": ["# submit\n", "function trinary_to_decimal(str)\n", "\n", "end"]
},
{
"cell_type": "markdown",
"metadata": {},
"source": ["## Test suite"]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": ["using Test\n", "\n", "# include(\"trinary.jl\")\n", "\n", "@testset \"trinary 1 is decimal 1\" begin\n", " @test trinary_to_decimal(\"1\") == 1\n", "end\n", "\n", "@testset \"trinary 2 is decimal 2\" begin\n", " @test trinary_to_decimal(\"2\") == 2\n", "end\n", "\n", "@testset \"trinary 10 is decimal 3\" begin\n", " @test trinary_to_decimal(\"10\") == 3\n", "end\n", "\n", "@testset \"trinary 11 is decimal 4\" begin\n", " @test trinary_to_decimal(\"11\") == 4\n", "end\n", "\n", "@testset \"trinary 100 is decimal 9\" begin\n", " @test trinary_to_decimal(\"100\") == 9\n", "end\n", "\n", "@testset \"trinary 112 is decimal 14\" begin\n", " @test trinary_to_decimal(\"112\") == 14\n", "end\n", "\n", "@testset \"trinary 222 is decimal 26\" begin\n", " @test trinary_to_decimal(\"222\") == 26\n", "end\n", "\n", "@testset \"trinary 1122000120 is decimal 32091\" begin\n", " @test trinary_to_decimal(\"1122000120\") == 32091\n", "end\n", "\n", "@testset \"invalid trinary digits returns 0\" begin\n", " @test trinary_to_decimal(\"1234\") == 0\n", "end\n", "\n", "@testset \"invalid word as input returns 0\" begin\n", " @test trinary_to_decimal(\"carrot\") == 0\n", "end\n", "\n", "@testset \"invalid numbers with letters as input returns 0\" begin\n", " @test trinary_to_decimal(\"0a1b2c\") == 0\n", "end"]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Prepare submission\n",
"To submit your exercise, you need to save your solution in a file called `trinary.jl` before using the CLI.\n",
"You can either create it manually or use the following functions, which will automatically write every notebook cell that starts with `# submit` to the file `trinary.jl`.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# using Pkg; Pkg.add(\"Exercism\")\n",
"# using Exercism\n",
"# Exercism.create_submission(\"trinary\")"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Julia 1.3.0",
"language": "julia",
"name": "julia-1.3"
},
"language_info": {
"file_extension": ".jl",
"mimetype": "application/julia",
"name": "julia",
"version": "1.3.0"
}
},
"nbformat": 4,
"nbformat_minor": 2
}