Initial upload

This commit is contained in:
2022-08-24 14:28:45 +02:00
parent c67653ddee
commit 57bc7b0289
370 changed files with 18479 additions and 0 deletions

View File

@@ -0,0 +1 @@
{"track":"julia","exercise":"trinary","id":"a33190c993fa4f9c917b0c33141e98a3","url":"https://exercism.io/my/solutions/a33190c993fa4f9c917b0c33141e98a3","handle":"halfdan","is_requester":true,"auto_approve":false}

32
julia/trinary/README.md Normal file
View File

@@ -0,0 +1,32 @@
# Trinary
Convert a trinary number, represented as a string (e.g. '102012'), to its
decimal equivalent using first principles.
The program should consider strings specifying an invalid trinary as the
value 0.
Trinary numbers contain three symbols: 0, 1, and 2.
The last place in a trinary number is the 1's place. The second to last
is the 3's place, the third to last is the 9's place, etc.
```shell
# "102012"
1 0 2 0 1 2 # the number
1*3^5 + 0*3^4 + 2*3^3 + 0*3^2 + 1*3^1 + 2*3^0 # the value
243 + 0 + 54 + 0 + 3 + 2 = 302
```
If your language provides a method in the standard library to perform the
conversion, pretend it doesn't exist and implement it yourself.
## Source
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-)
## Version compatibility
This exercise has been tested on Julia versions >=1.0.
## Submitting Incomplete Solutions
It's possible to submit an incomplete solution so you can see how others have completed the exercise.

47
julia/trinary/runtests.jl Normal file
View File

@@ -0,0 +1,47 @@
using Test
include("trinary.jl")
@testset "trinary 1 is decimal 1" begin
@test trinary_to_decimal("1") == 1
end
@testset "trinary 2 is decimal 2" begin
@test trinary_to_decimal("2") == 2
end
@testset "trinary 10 is decimal 3" begin
@test trinary_to_decimal("10") == 3
end
@testset "trinary 11 is decimal 4" begin
@test trinary_to_decimal("11") == 4
end
@testset "trinary 100 is decimal 9" begin
@test trinary_to_decimal("100") == 9
end
@testset "trinary 112 is decimal 14" begin
@test trinary_to_decimal("112") == 14
end
@testset "trinary 222 is decimal 26" begin
@test trinary_to_decimal("222") == 26
end
@testset "trinary 1122000120 is decimal 32091" begin
@test trinary_to_decimal("1122000120") == 32091
end
@testset "invalid trinary digits returns 0" begin
@test trinary_to_decimal("1234") == 0
end
@testset "invalid word as input returns 0" begin
@test trinary_to_decimal("carrot") == 0
end
@testset "invalid numbers with letters as input returns 0" begin
@test trinary_to_decimal("0a1b2c") == 0
end

View File

@@ -0,0 +1,68 @@
{
"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
}

6
julia/trinary/trinary.jl Normal file
View File

@@ -0,0 +1,6 @@
function trinary_to_decimal(str)
all(x -> x ('0', '1', '2'), str) || return 0
number = parse(Int, str)
digs = digits(number)
sum([k*3^(i-1) for (i,k) in enumerate(digs)])
end