{ "cells": [ { "cell_type": "markdown", "id": "1d0c820b-3684-4b17-9824-d53380bd7788", "metadata": {}, "source": [ "作るべき函数\n", "\n", "相対誤差 abs(answer/solution - 1) が 0.005 以下なら正解とする.\n", "\n", "解答用紙生成函数: 辞書を返す\n", "\n", "採点結果生成函数: 辞書と点数を返す" ] }, { "cell_type": "code", "execution_count": 35, "id": "264990ca-c09e-4ee0-bc2f-6a595db6bb52", "metadata": {}, "outputs": [], "source": [ "using TOML, Unicode, PrettyPrinting" ] }, { "cell_type": "code", "execution_count": 188, "id": "88bd5551-54f4-4140-b4c5-8d605958cb17", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1]\n", "[1.1]\n", "x = [123.4, 5]\n", "y = [\"yes\", 5]\n", "z = [[34.5, 199.5], 10]\n", "[1.2]\n", "w = [999, 10]\n", "\n", "[2]\n", "a = [678, 5]\n", "b = [1003, 5]\n", "\n", "[3]\n", "c = [123, 20]\n", "\n", "Dict(\"1\" => Dict(\"1\" => Dict(\"x\" => [123.4, 5],\n", " \"z\" => [[34.5, 199.5], 10],\n", " \"y\" => [\"yes\", 5]),\n", " \"2\" => Dict(\"w\" => [999, 10])),\n", " \"2\" => Dict(\"b\" => [1003, 5], \"a\" => [678, 5]),\n", " \"3\" => Dict(\"c\" => [123, 20]))" ] } ], "source": [ "input = \"\"\"\n", "[1]\n", "[1.1]\n", "x = [123.4, 5]\n", "y = [\"yes\", 5]\n", "z = [[34.5, 199.5], 10]\n", "[1.2]\n", "w = [999, 10]\n", "\n", "[2]\n", "a = [678, 5]\n", "b = [1003, 5]\n", "\n", "[3]\n", "c = [123, 20]\n", "\"\"\"\n", "println(input)\n", "\n", "data = TOML.parse(input)\n", "pprint(data)" ] }, { "cell_type": "code", "execution_count": 142, "id": "779f164f-03cb-4185-ba4f-97be2079a01d", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "name = \"山田太郎\"\n", "id = \"A9BC99999\"\n", "\n", "[1]\n", "[1.1]\n", "x = 123.40001\n", "y = \"yes\"\n", "z = [34.500001, 199.500001]\n", "[1.2]\n", "w = 999\n", "\n", "[2]\n", "a = 678\n", "b = 1003\n", "\n", "Dict(\"name\" => \"山田太郎\",\n", " \"1\" => Dict(\"1\" => Dict(\"x\" => 123.40001,\n", " \"z\" => [34.500001, 199.500001],\n", " \"y\" => \"yes\"),\n", " \"2\" => Dict(\"w\" => 999)),\n", " \"2\" => Dict(\"b\" => 1003, \"a\" => 678),\n", " \"id\" => \"A9BC99999\")" ] } ], "source": [ "input1 = \"\"\"\n", "name = \"山田太郎\"\n", "id = \"A9BC99999\"\n", "\n", "[1]\n", "[1.1]\n", "x = 123.40001\n", "y = \"yes\"\n", "z = [34.500001, 199.500001]\n", "[1.2]\n", "w = 999\n", "\n", "[2]\n", "a = 678\n", "b = 1003\n", "\"\"\"\n", "println(input1)\n", "\n", "data1 = TOML.parse(input1)\n", "pprint(data1)" ] }, { "cell_type": "code", "execution_count": 143, "id": "acab3737-5ace-41a0-946f-2885bd8ae954", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "name = \"山田太郎\"\n", "id = \"A9BC99999\"\n", "\n", "[1]\n", "[1.1]\n", "y = \"Yes\"\n", "z = [34.500001, 199.500001]\n", "[1.2]\n", "w = 990\n", "\n", "[2]\n", "a = 678\n", "b = 1003\n", "\n", "Dict(\"name\" => \"山田太郎\",\n", " \"1\" => Dict(\"1\" => Dict(\"z\" => [34.500001, 199.500001], \"y\" => \"Yes\"),\n", " \"2\" => Dict(\"w\" => 990)),\n", " \"2\" => Dict(\"b\" => 1003, \"a\" => 678),\n", " \"id\" => \"A9BC99999\")" ] } ], "source": [ "input2 = \"\"\"\n", "name = \"山田太郎\"\n", "id = \"A9BC99999\"\n", "\n", "[1]\n", "[1.1]\n", "y = \"Yes\"\n", "z = [34.500001, 199.500001]\n", "[1.2]\n", "w = 990\n", "\n", "[2]\n", "a = 678\n", "b = 1003\n", "\"\"\"\n", "println(input2)\n", "\n", "data2 = TOML.parse(input2)\n", "pprint(data2)" ] }, { "cell_type": "code", "execution_count": 144, "id": "7931498e-6b26-4c03-8a2d-1baebb421fba", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "name = \"山田太郎\"\n", "id = \"A9BC99999\"\n", "\n", "[1]\n", "[1.1]\n", "z = [34.500001, 199.500001]\n", "[1.2]\n", "w = 990\n", "\n", "[2]\n", "a = \"yes\"\n", "b = 1003\n", "\n", "Dict(\"name\" => \"山田太郎\",\n", " \"1\" => Dict(\"1\" => Dict(\"z\" => [34.500001, 199.500001]),\n", " \"2\" => Dict(\"w\" => 990)),\n", " \"2\" => Dict(\"b\" => 1003, \"a\" => \"yes\"),\n", " \"id\" => \"A9BC99999\")" ] } ], "source": [ "input3 = \"\"\"\n", "name = \"山田太郎\"\n", "id = \"A9BC99999\"\n", "\n", "[1]\n", "[1.1]\n", "z = [34.500001, 199.500001]\n", "[1.2]\n", "w = 990\n", "\n", "[2]\n", "a = \"yes\"\n", "b = 1003\n", "\"\"\"\n", "println(input3)\n", "\n", "data3 = TOML.parse(input3)\n", "pprint(data3)" ] }, { "cell_type": "code", "execution_count": 152, "id": "e0a69281-88a2-4bdc-875d-10192bc4e58b", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "saiten (generic function with 4 methods)" ] }, "execution_count": 152, "metadata": {}, "output_type": "execute_result" } ], "source": [ "relerr(x, y) = Inf\n", "relerr(x::Real, y::Real) = abs(x/y - 1)\n", "relerr(x::Vector, y::Vector) = maximum(relerr(x[i], y[i]) for i in eachindex(y))\n", "relerr(x::AbstractString, y::AbstractString) =\n", " Unicode.normalize(x; casefold=true) != Unicode.normalize(y; casefold=true)\n", "nearlyequal(x, y; reltol = 0.005) = relerr(x, y) ≤ reltol\n", "\n", "kukaito(x) = \"\"\n", "kukaito(x::Dict) = Dict()\n", "kukaito(x::AbstractFloat) = NaN\n", "\n", "function saiten(ans, sol::Vector, gokeiten = 0)\n", " score = nearlyequal(ans, sol[1]) * sol[2]\n", " Union{typeof(ans), typeof(score)}[ans, score], gokeiten + score\n", "end\n", "\n", "function saiten(ans::Dict, sol::Dict, gokeiten = 0)\n", " kekka = Dict()\n", " for k in keys(sol)\n", " v, gokeiten = saiten(get(ans, k, kukaito(sol[k])), sol[k], gokeiten)\n", " kekka[k] = v\n", " end\n", " kekka, gokeiten\n", "end" ] }, { "cell_type": "code", "execution_count": 153, "id": "4311f542-4ce1-49c7-8ed3-0a93a06a423a", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(Union{Float64, Int64}[123.1, 10], 10)" ] }, "execution_count": 153, "metadata": {}, "output_type": "execute_result" } ], "source": [ "saiten(123.1, [123, 10], 0)" ] }, { "cell_type": "code", "execution_count": 154, "id": "015d3b90-64d2-448c-9bc6-9346557a2eef", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Dict(\"1\" => Dict(\"1\" => Dict(\"x\" => [123.40001, 5],\n", " \"z\" => [[34.500001, 199.500001], 10],\n", " \"y\" => [\"yes\", 5]),\n", " \"2\" => Dict(\"w\" => [999, 10])),\n", " \"2\" => Dict(\"b\" => [1003, 5], \"a\" => [678, 5]),\n", " \"3\" => Dict(\"c\" => [\"\", 0]))" ] }, { "data": { "text/plain": [ "40" ] }, "execution_count": 154, "metadata": {}, "output_type": "execute_result" } ], "source": [ "kekka1, gokeiten1 = saiten(data1, data)\n", "pprint(kekka1)\n", "gokeiten1" ] }, { "cell_type": "code", "execution_count": 155, "id": "9a860c8d-b969-4d3e-8c05-d8f54053b973", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "[1.1]\n", "x = [123.40001, 5]\n", "y = [\"yes\", 5]\n", "z = [[34.500001, 199.500001], 10]\n", "\n", "[1.2]\n", "w = [999, 10]\n", "[2]\n", "a = [678, 5]\n", "b = [1003, 5]\n", "\n", "[3]\n", "c = [\"\", 0]\n" ] } ], "source": [ "TOML.print(kekka1; sorted=true)" ] }, { "cell_type": "code", "execution_count": 156, "id": "724d9431-d8b5-4680-b801-610e1caaaa6f", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Dict(\"1\" => Dict(\"1\" => Dict(\"x\" => [\"\", 0],\n", " \"z\" => [[34.500001, 199.500001], 10],\n", " \"y\" => [\"Yes\", 5]),\n", " \"2\" => Dict(\"w\" => [990, 0])),\n", " \"2\" => Dict(\"b\" => [1003, 5], \"a\" => [678, 5]),\n", " \"3\" => Dict(\"c\" => [\"\", 0]))" ] }, { "data": { "text/plain": [ "25" ] }, "execution_count": 156, "metadata": {}, "output_type": "execute_result" } ], "source": [ "kekka2, gokeiten2 = saiten(data2, data)\n", "pprint(kekka2)\n", "gokeiten2" ] }, { "cell_type": "code", "execution_count": 157, "id": "c40ea3b8-89b7-4f5e-8f0b-e1d7bf1ef018", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "[1.1]\n", "x = [\"\", 0]\n", "y = [\"Yes\", 5]\n", "z = [[34.500001, 199.500001], 10]\n", "\n", "[1.2]\n", "w = [990, 0]\n", "[2]\n", "a = [678, 5]\n", "b = [1003, 5]\n", "\n", "[3]\n", "c = [\"\", 0]\n" ] } ], "source": [ "TOML.print(kekka2; sorted=true)" ] }, { "cell_type": "code", "execution_count": 158, "id": "d817b945-ff8a-42c1-90f0-bae2f766474b", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Dict(\"1\" => Dict(\"1\" => Dict(\"x\" => [\"\", 0],\n", " \"z\" => [[34.500001, 199.500001], 10],\n", " \"y\" => [\"\", 0]),\n", " \"2\" => Dict(\"w\" => [990, 0])),\n", " \"2\" => Dict(\"b\" => [1003, 5], \"a\" => [\"yes\", 0]),\n", " \"3\" => Dict(\"c\" => [\"\", 0]))" ] }, { "data": { "text/plain": [ "15" ] }, "execution_count": 158, "metadata": {}, "output_type": "execute_result" } ], "source": [ "kekka3, gokeiten3 = saiten(data3, data)\n", "pprint(kekka3)\n", "gokeiten3" ] }, { "cell_type": "code", "execution_count": 159, "id": "424ef849-9fc8-4dd9-8077-15783350d7c5", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "[1.1]\n", "x = [\"\", 0]\n", "y = [\"\", 0]\n", "z = [[34.500001, 199.500001], 10]\n", "\n", "[1.2]\n", "w = [990, 0]\n", "[2]\n", "a = [\"yes\", 0]\n", "b = [1003, 5]\n", "\n", "[3]\n", "c = [\"\", 0]\n" ] } ], "source": [ "TOML.print(kekka3; sorted=true)" ] }, { "cell_type": "code", "execution_count": 160, "id": "078996c5-e73b-4d77-8e91-2470f507384f", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Dict(\"name\" => \"山田太郎\",\n", " \"1\" => Dict(\"1\" => Dict(\"z\" => [34.500001, 199.500001]),\n", " \"2\" => Dict(\"w\" => 990)),\n", " \"2\" => Dict(\"b\" => 1003, \"a\" => \"yes\"),\n", " \"id\" => \"A9BC99999\")" ] } ], "source": [ "pprint(data3)" ] }, { "cell_type": "code", "execution_count": 182, "id": "10e5866e-bab6-4cc3-86dd-ae36b92bdabc", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "make_mohankaito (generic function with 1 method)" ] }, "execution_count": 182, "metadata": {}, "output_type": "execute_result" } ], "source": [ "gijikaito(x::AbstractFloat) = 46.49373\n", "gijikaito(x::Integer) = 4649373\n", "gijikaito(x::AbstractString) = \"yes or no\"\n", "gijikaito(x::Vector) = [gijikaito(x) for x in x]\n", "\n", "make_kaitoyoshi(sol::Vector; gijikaitofunc=gijikaito) = gijikaitofunc(sol[1])\n", "\n", "function make_kaitoyoshi(sol::Dict; gijikaitofunc=gijikaito)\n", " kaitoyoshi = Dict()\n", " for k in keys(sol)\n", " v = make_kaitoyoshi(sol[k]; gijikaitofunc)\n", " kaitoyoshi[k] = v\n", " end\n", " kaitoyoshi\n", "end\n", "\n", "make_mohankaito(sol::Dict) = make_kaitoyoshi(sol; gijikaitofunc=identity)" ] }, { "cell_type": "code", "execution_count": 183, "id": "53fe51d9-4f41-4759-8652-b75e34fc1566", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "[1.1]\n", "x = 123.4\n", "z = [34.5, 199.5]\n", "y = \"yes\"\n", "\n", "[1.2]\n", "w = 999\n", "[2]\n", "b = 1003\n", "a = 678\n", "\n", "[3]\n", "c = 123\n" ] } ], "source": [ "mohankaito = make_mohankaito(data)\n", "TOML.print(mohankaito)" ] }, { "cell_type": "code", "execution_count": 187, "id": "cb4b40d1-2169-4605-8868-6ad1144004e3", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "[1.1]\n", "x = [123.4, 5]\n", "z = [[34.5, 199.5], 10]\n", "y = [\"yes\", 5]\n", "\n", "[1.2]\n", "w = [999, 10]\n", "[2]\n", "b = [1003, 5]\n", "a = [678, 5]\n", "\n", "[3]\n", "c = [123, 20]\n" ] } ], "source": [ "TOML.print(data)" ] }, { "cell_type": "code", "execution_count": 174, "id": "9f2ac3a1-39f1-46d1-8529-f0b8da8c6631", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Dict(\"1\" => Dict(\"1\" => Dict(\"x\" => 46.49373,\n", " \"z\" => [46.49373, 46.49373],\n", " \"y\" => \"yes or no\"),\n", " \"2\" => Dict(\"w\" => 4649373)),\n", " \"2\" => Dict(\"b\" => 4649373, \"a\" => 4649373),\n", " \"3\" => Dict(\"c\" => 4649373))" ] } ], "source": [ "kaitoyoshi = make_kaitoyoshi(data)\n", "pprint(kaitoyoshi)" ] }, { "cell_type": "code", "execution_count": 175, "id": "f4cf629c-66a2-4f78-90dc-ca98e9cb58f3", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "[1.1]\n", "x = 46.49373\n", "y = \"yes or no\"\n", "z = [46.49373, 46.49373]\n", "\n", "[1.2]\n", "w = 4649373\n", "[2]\n", "a = 4649373\n", "b = 4649373\n", "\n", "[3]\n", "c = 4649373\n" ] } ], "source": [ "TOML.print(kaitoyoshi; sorted=true)" ] }, { "cell_type": "code", "execution_count": 176, "id": "30f8598c-2bc3-445c-a54d-3746d0c608ce", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "[1.1]\n", "x = [123.4, 5]\n", "y = [\"yes\", 5]\n", "z = [[34.5, 199.5], 10]\n", "\n", "[1.2]\n", "w = [999, 10]\n", "[2]\n", "a = [678, 5]\n", "b = [1003, 5]\n", "\n", "[3]\n", "c = [123, 20]\n" ] } ], "source": [ "TOML.print(data; sorted=true)" ] }, { "cell_type": "code", "execution_count": 172, "id": "79c2c757-75fa-48f9-9bce-947df5a2317b", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Dict(\"1\" => Dict(\"1\" => Dict(\"x\" => [46.49373, 0],\n", " \"z\" => [[46.49373, 46.49373], 0],\n", " \"y\" => [\"yes or no\", 0]),\n", " \"2\" => Dict(\"w\" => [4649373, 0])),\n", " \"2\" => Dict(\"b\" => [4649373, 0], \"a\" => [4649373, 0]),\n", " \"3\" => Dict(\"c\" => [4649373, 0]))\n", "0\n" ] } ], "source": [ "pprintln.(saiten(kaitoyoshi, data));" ] }, { "cell_type": "code", "execution_count": null, "id": "cc724687-1e2c-4687-8450-bafa85ca5241", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Julia 1.7.3", "language": "julia", "name": "julia-1.7" }, "language_info": { "file_extension": ".jl", "mimetype": "application/julia", "name": "julia", "version": "1.7.3" } }, "nbformat": 4, "nbformat_minor": 5 }