Skip to content

Commit

Permalink
feat: abc363
Browse files Browse the repository at this point in the history
  • Loading branch information
xhiroga committed Jul 21, 2024
1 parent 567723b commit e3f622f
Show file tree
Hide file tree
Showing 3 changed files with 368 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"def piling_up() -> int:\n",
" rate = int(input())\n",
" return 100 - int(rate % 100)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"77\n"
]
}
],
"source": [
"from unittest.mock import patch\n",
"\n",
"with patch(\n",
" \"builtins.input\",\n",
" side_effect=\"\"\"\n",
"123\n",
"\"\"\".strip().splitlines(),\n",
"):\n",
" expected = 77\n",
" actual = piling_up()\n",
" print(actual)\n",
" assert expected == actual, print(f\"{expected=}, {actual=}\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": ".venv",
"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.12.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"def japanese_cursed_doll() -> int:\n",
" n, t, p = map(int, input().split())\n",
" ls = sorted(list(map(int, input().split())), reverse=True)\n",
" return max(t - ls[p-1], 0)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"7\n"
]
}
],
"source": [
"from unittest.mock import patch\n",
"\n",
"with patch(\n",
" \"builtins.input\",\n",
" side_effect=\"\"\"\n",
"5 10 3\n",
"3 11 1 6 2\n",
"\"\"\".strip().splitlines(),\n",
"):\n",
" expected = 7\n",
" actual = japanese_cursed_doll()\n",
" print(actual)\n",
" assert expected == actual, print(f\"{expected=}, {actual=}\")"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0\n"
]
}
],
"source": [
"from unittest.mock import patch\n",
"\n",
"with patch(\n",
" \"builtins.input\",\n",
" side_effect=\"\"\"\n",
"2 5 2\n",
"10 10\n",
"\"\"\".strip().splitlines(),\n",
"):\n",
" expected = 0\n",
" actual = japanese_cursed_doll()\n",
" print(actual)\n",
" assert expected == actual, print(f\"{expected=}, {actual=}\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": ".venv",
"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.12.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"env: PYTHON_LOGLEVEL=DEBUG\n"
]
}
],
"source": [
"%env PYTHON_LOGLEVEL=DEBUG"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"from logging import getLogger, StreamHandler, WARNING\n",
"import os\n",
"\n",
"PYTHON_LOGLEVEL = os.environ.get(\"PYTHON_LOGLEVEL\", WARNING)\n",
"logger = getLogger(__name__)\n",
"if not logger.hasHandlers():\n",
" handler = StreamHandler()\n",
" handler.setLevel(PYTHON_LOGLEVEL)\n",
" logger.setLevel(PYTHON_LOGLEVEL)\n",
" logger.addHandler(handler)\n",
" logger.propagate = False"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"outputs": [],
"source": [
"from collections import defaultdict\n",
"from math import factorial\n",
"from unittest.mock import MagicMock\n",
"\n",
"\n",
"try:\n",
" logger # type: ignore\n",
"except NameError:\n",
" logger = MagicMock()\n",
"\n",
"\n",
"def avoid_k_palindrome_2() -> int:\n",
" n, k = map(int, input().split())\n",
" s = input()\n",
"\n",
" # 以降、回文の片側を翼と呼ぶ\n",
" 翼長 = k //2\n",
"\n",
" 辞書: defaultdict[str, int] = defaultdict(lambda: 0)\n",
" for char in s:\n",
" 辞書[char] += 1\n",
" 翼候補 = {key: value for key, value in 辞書.items() if 2 <=value}\n",
" 一般文字 = {key: value for key, value in 辞書.items() if value <= 1}\n",
" logger.debug(f\"{翼候補=}, {一般文字=}\")\n",
"\n",
" 回文の有無を問わない全通り = factorial(n)\n",
" for value in 辞書.values():\n",
" 回文の有無を問わない全通り //= factorial(value)\n",
" logger.debug(f\"{回文の有無を問わない全通り=}\")\n",
"\n",
" # 回文が作れない場合は全通りを返す\n",
" if sum(翼候補.values()) // 2 < 翼長:\n",
" logger.debug(f\"{sum(翼候補.values()) // 2 < 翼長=}, {回文の有無を問わない全通り=}\")\n",
" return 回文の有無を問わない全通り\n",
"\n",
" # 回文が作れる場合、文字列の全通りから、回文を含む文字数全通りを引く\n",
" def 与えられた文字列を翼の部分として含む文字列のパターン数(選ばれた翼候補: str, 残っている翼候補: dict[str, int]) -> int:\n",
" logger.debug(f\"{選ばれた翼候補=}, {残っている翼候補=}\")\n",
"\n",
" 定員 = len(選ばれた翼候補) == 翼長\n",
" if 定員:\n",
" # ある回文を含む文字列の全通りは、その回文を1文字と見なした文字列の全通りに等しい\n",
" # 回文が奇数文字の場合でも、全通りの数を数えるだけなら、偶数文字の場合と同様に数えられる\n",
" 選んだ回文を含む文字列の全通り = 1\n",
" 回文の両翼を合わせて1文字と見なした文字数 = len(一般文字.keys()) + sum(残っている翼候補.values()) + 1\n",
" for i in range(1, 回文の両翼を合わせて1文字と見なした文字数 + 1):\n",
" 選んだ回文を含む文字列の全通り *= i\n",
" # 組合せなので重複する文字数で割る。\n",
" for value in 残っている翼候補.values():\n",
" 選んだ回文を含む文字列の全通り //= value\n",
"\n",
" # このアプローチは失敗する!!!\"yzyzx\"のように、1文に2つ以上の回文が含まれる可能性があるため。\n",
" logger.debug(f\"{定員=}, {選ばれた翼候補=}, {残っている翼候補=}, {選んだ回文を含む文字列の全通り=}, {回文の両翼を合わせて1文字と見なした文字数=}\")\n",
" return 選んだ回文を含む文字列の全通り\n",
" \n",
" else:\n",
" 選んだ回文を含む文字列の全通り = 0\n",
" for 次に選ぶ翼候補 in 残っている翼候補.keys():\n",
" 次も残っている翼候補 = {\n",
" key: value for key, value in 残っている翼候補.items() if key != 次に選ぶ翼候補\n",
" }\n",
" 次に選ぶ翼候補の残り文字数 = 残っている翼候補[次に選ぶ翼候補] - 2\n",
" if 2 <= 次に選ぶ翼候補の残り文字数:\n",
" 次も残っている翼候補[次に選ぶ翼候補] = 次に選ぶ翼候補の残り文字数\n",
" 選んだ回文を含む文字列の全通り += 与えられた文字列を翼の部分として含む文字列のパターン数(選ばれた翼候補 + 次に選ぶ翼候補, 次も残っている翼候補)\n",
" \n",
" logger.debug(f\"{定員=}, {選ばれた翼候補=}, {残っている翼候補=}, {選んだ回文を含む文字列の全通り=}\")\n",
" return 選んだ回文を含む文字列の全通り\n",
"\n",
" 回文を含む文字列の全通り = 与えられた文字列を翼の部分として含む文字列のパターン数(\"\", 翼候補)\n",
" return 回文の有無を問わない全通り - 回文を含む文字列の全通り"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"翼候補={'z': 2, 'y': 2}, 一般文字={'x': 1}\n",
"回文の有無を問わない全通り=30\n",
"選ばれた翼候補='', 残っている翼候補={'z': 2, 'y': 2}\n",
"選ばれた翼候補='z', 残っている翼候補={'y': 2}\n",
"定員=True, 選ばれた翼候補='z', 残っている翼候補={'y': 2}, 選んだ回文を含む文字列の全通り=12, 回文の両翼を合わせて1文字と見なした文字数=4\n",
"選ばれた翼候補='y', 残っている翼候補={'z': 2}\n",
"定員=True, 選ばれた翼候補='y', 残っている翼候補={'z': 2}, 選んだ回文を含む文字列の全通り=12, 回文の両翼を合わせて1文字と見なした文字数=4\n",
"定員=False, 選ばれた翼候補='', 残っている翼候補={'z': 2, 'y': 2}, 選んだ回文を含む文字列の全通り=24\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"6\n",
"expected=16, actual=6\n"
]
},
{
"ename": "AssertionError",
"evalue": "None",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mAssertionError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[0;32mIn[24], line 13\u001b[0m\n\u001b[1;32m 11\u001b[0m actual \u001b[38;5;241m=\u001b[39m avoid_k_palindrome_2()\n\u001b[1;32m 12\u001b[0m \u001b[38;5;28mprint\u001b[39m(actual)\n\u001b[0;32m---> 13\u001b[0m \u001b[38;5;28;01massert\u001b[39;00m expected \u001b[38;5;241m==\u001b[39m actual, \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;132;01m{\u001b[39;00mexpected\u001b[38;5;132;01m=}\u001b[39;00m\u001b[38;5;124m, \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mactual\u001b[38;5;132;01m=}\u001b[39;00m\u001b[38;5;124m\"\u001b[39m)\n",
"\u001b[0;31mAssertionError\u001b[0m: None"
]
}
],
"source": [
"from unittest.mock import patch\n",
"\n",
"with patch(\n",
" \"builtins.input\",\n",
" side_effect=\"\"\"\n",
"5 3\n",
"zzyyx\n",
"\"\"\".strip().splitlines(),\n",
"):\n",
" expected = 16\n",
" actual = avoid_k_palindrome_2()\n",
" print(actual)\n",
" assert expected == actual, print(f\"{expected=}, {actual=}\")"
]
}
],
"metadata": {
"kernelspec": {
"display_name": ".venv",
"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.12.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}

0 comments on commit e3f622f

Please sign in to comment.