From f0a4551df2a66b759e7d3045ff77352134750349 Mon Sep 17 00:00:00 2001 From: yenru0 Date: Mon, 13 May 2024 09:44:23 +0900 Subject: [PATCH] delete codeup category and update README.md and complete 2467.py and 27433.py and create new run.py and init_make.py for better testcases management --- .gitignore | 1 + README.md | 14 +-- codeup_C/stdin.txt | 0 codeup_python/stdin.txt | 0 init_make.py | 31 +++++++ run.py | 165 +++++++++++++++++++++++++++++++++ zeta_python/completed/2467.py | 37 ++++++++ zeta_python/completed/27433.py | 10 ++ zeta_python/stdin.txt | 1 + 9 files changed, 252 insertions(+), 7 deletions(-) delete mode 100644 codeup_C/stdin.txt delete mode 100644 codeup_python/stdin.txt create mode 100644 init_make.py create mode 100644 run.py create mode 100644 zeta_python/completed/2467.py create mode 100644 zeta_python/completed/27433.py diff --git a/.gitignore b/.gitignore index 7bf7ab2..51aec33 100644 --- a/.gitignore +++ b/.gitignore @@ -21,3 +21,4 @@ META-INF *.class a.jar +_testcases/* diff --git a/README.md b/README.md index b8bbf1b..cd7b462 100644 --- a/README.md +++ b/README.md @@ -4,11 +4,11 @@ yenru0 code storage 폴더명 `{identifier}_{language}` -`zeta`는 [BOJ](https://www.acmicpc.net/)입니다. +`identifier`의 종류는 다음과 같음. -`codeup`은 [코드업](https://codeup.kr/)입니다. +* `zeta`는 [BOJ](https://www.acmicpc.net/)입니다. -이 뒤에 오는 것들은 작성 언어입니다. 다음은 현재까지 작성된 언어의 예시. +`language`는 작성 언어로 현재까지 작성된 언어 및 예시는 다음과 같음. 작성언어 | 폴더 뒤 | 확장자 :---:|:---:|:---: @@ -18,14 +18,14 @@ yenru0 code storage Kotlin | kotlin | .kt Lua | lua | .lua +std * C++17 -* C99 +* C11 ## completed or incompleted -내가 **납득**되거나 내가 해결한 문제는 `/completed`로 이동됩니다. ~~관짝~~ +내가 **납득**되거나 내가 해결한 문제는 `/completed`로 이동됩니다. -내가 못 풀것 같은 것은 `/incompleted`로 이동됩니다. -다만, 그냥 내가 관심을 안줘서 해결되지 않은 문제는 `/`에 있을 예정입니다. +해결되지 않은 문제는 `/`에 있을 예정입니다. ## 시간 측정 ### python diff --git a/codeup_C/stdin.txt b/codeup_C/stdin.txt deleted file mode 100644 index e69de29..0000000 diff --git a/codeup_python/stdin.txt b/codeup_python/stdin.txt deleted file mode 100644 index e69de29..0000000 diff --git a/init_make.py b/init_make.py new file mode 100644 index 0000000..b72e7e8 --- /dev/null +++ b/init_make.py @@ -0,0 +1,31 @@ +import sys +import os +import glob + +TC_TARGET_DIR = "_testcases" + +DEFAULT_COUNT = 1 + +if __name__ == "__main__": + count = DEFAULT_COUNT + match len(sys.argv): + case 1: + pass + case 2 if sys.argv[1].isdigit() and int(sys.argv[1]) > 0: + count = int(sys.argv[1]) + case _: + print("Invalid arguments.") + sys.exit() + + # default make + if not os.path.exists("./" + TC_TARGET_DIR): + os.mkdir(TC_TARGET_DIR) + + for i in range(1, count + 1): + default_in_path = "./" + TC_TARGET_DIR + f"/{i}.in" + default_out_path = "./" + TC_TARGET_DIR + f"/{i}.out" + with open(default_in_path, "w", encoding="utf-8") as _: + pass + + with open(default_out_path, "w", encoding="utf-8") as _: + pass diff --git a/run.py b/run.py new file mode 100644 index 0000000..31d127e --- /dev/null +++ b/run.py @@ -0,0 +1,165 @@ +import os +import sys +import pathlib +from enum import Enum, unique, auto +from dataclasses import dataclass +import re +import subprocess + +TC_TARGET_DIR = "./_testcases" + + +@dataclass +class ProblemRunType: + name: str + dir: str + runner: str + prefix: str + + +class ProblemRunEnum: + zeta_python: ProblemRunType = ProblemRunType( + name="zeta_python", + dir="./zeta_python", + runner="python {source}", + prefix="py", + ) + zeta_C: ProblemRunType = ProblemRunType( + name="zeta_C", + dir="./zeta_C", + runner="gcc -std=c11 {source} && ./a.out < {input}", + prefix="c", + ) + zeta_cpp: ProblemRunType = ProblemRunType( + name="zeta_cpp", + dir="./zeta_cpp", + runner="g++ -std=c++17 {source} && ./a.out < {input}", + prefix="cpp", + ) + zeta_kotlin: ProblemRunType = ProblemRunType( + name="zeta_kotlin", + dir="./zeta_kotlin", + runner="kotlinc-jvm {source} -include-runtime -d a.jar && java -jar a.jar -Dfile.encoding=UTF-8 < {input}", + prefix="kt", + ) + zeta_lua: ProblemRunType = ProblemRunType( + name="zeta_lua", dir="./zeta_lua", runner="", prefix="lua" + ) + err: ProblemRunType = ProblemRunType(name="err", dir="", runner="", prefix="") + + @staticmethod + def quicklink(link: str) -> ProblemRunType: + match link.lower().replace("-", "").replace("_", ""): + case "zetapython" | "bojpython" | "bojpy" | "zetapy" | "zpy": + return ProblemRunEnum.zeta_python + case "zeta_c" | "zetac" | "boj_c" | "bojc" | "zc": + return ProblemRunEnum.zeta_C + case "zeta_cpp" | "zetacpp" | "boj_cpp" | "bojcpp" | "zcpp": + return ProblemRunEnum.zeta_cpp + case "zeta_lua" | "zetalua" | "boj_lua" | "bojlua" | "zlua": + return ProblemRunEnum.zeta_lua + case "zetakotlin" | "bojkotlin" | "zetakt" | "bojkt" | "zkt": + return ProblemRunEnum.zeta_kotlin + case _: + return ProblemRunEnum.err + + +if __name__ == "__main__": + rtype: ProblemRunType + problem_code: int + cnt: int + verbose_flag: bool = False + match len(sys.argv): + case 3: + rtype = ProblemRunEnum.quicklink(sys.argv[1]) + problem_code = sys.argv[2] if sys.argv[2].isdigit() else None + cnt = 1 + + case 4: + rtype = ProblemRunEnum.quicklink(sys.argv[1]) + problem_code = int(sys.argv[2]) if sys.argv[2].isdigit() else None + if sys.argv[3] == "--verbose": + verbose_flag = True + cnt = 1 + else: + cnt = ( + int(sys.argv[3]) + if sys.argv[3].isdigit() and int(sys.argv[3]) > 0 + else 1 + ) + case 5: + rtype = ProblemRunEnum.quicklink(sys.argv[1]) + problem_code = int(sys.argv[2]) if sys.argv[2].isdigit() else None + + cnt = ( + int(sys.argv[3]) + if sys.argv[3].isdigit() and int(sys.argv[3]) > 0 + else 1 + ) + + if sys.argv[4] == "--verbose": + verbose_flag = True + else: + pass + case _: + print("Invalid argument count.") + sys.exit() + + if rtype is ProblemRunEnum.err: + print(f"Invalid run type: `{sys.argv[1]}`.") + sys.exit() + + problem_code_path = f"{rtype.dir}/{problem_code}.{rtype.prefix}" + if not os.path.exists(problem_code_path): + print(f"Invalid problem code: `{problem_code}`") + sys.exit() + + io_files: list[str] = [ + fpath + for fpath in os.listdir(TC_TARGET_DIR) + if os.path.isfile(os.path.join(TC_TARGET_DIR, fpath)) + ] + + for c in range(1, cnt + 1): + inputs = None + outputs = [] + for name in io_files: + ix = re.match(f"{c}.in", name) + ox = re.match(f"{c}.out[0-9]*", name) + if ix: + inputs = name + elif ox: + outputs.append(name) + if (not inputs) or (not outputs): + print(f"{c:02d}: Pass") + continue + else: + with open(os.path.join(TC_TARGET_DIR, inputs), 'r', encoding='utf-8') as inputf: + stdout = subprocess.run( + rtype.runner.format( + source=problem_code_path, input= os.path.join(TC_TARGET_DIR, inputs) + ).split(), + check=True, + capture_output=True, + text=True, + stdin = inputf + ).stdout + + flag = False + for outputpath in outputs: + with open( + os.path.join(TC_TARGET_DIR, outputpath), "r", encoding="utf-8" + ) as f: + if stdout.strip() == f.read().strip(): + flag = True + break + + print(f"{c:02d}: {flag}") + if verbose_flag: + print("===") + print(stdout) + print("===") + + +### TODO: --profile argument 추가를 위한 command dispatcher 개선 +### TODO: WIP diff --git a/zeta_python/completed/2467.py b/zeta_python/completed/2467.py new file mode 100644 index 0000000..953ac6e --- /dev/null +++ b/zeta_python/completed/2467.py @@ -0,0 +1,37 @@ +def solve_simple(N, S): + _, s = min( + [ + (abs(S[i] + S[j]), sorted((S[i], S[j]))) + for i in range(N) + for j in range(N) + if i != j + ] + ) + return s + + +def solve(N: int, S: list) -> tuple[int, int]: + S.sort() + start = 0 + end = N - 1 + r = 10000000000 + r_pos = None, None + while start < end: + s = S[start] + S[end] + if r > abs(s): + r = abs(s) + r_pos = start, end + if r == 0: + break + if s <= 0: + start += 1 + elif s > 0: + end -= 1 + + return S[r_pos[0]], S[r_pos[1]] + + +if __name__ == "__main__": + N = int(input()) + S = list(map(int, input().split())) + print(*solve(N, S)) diff --git a/zeta_python/completed/27433.py b/zeta_python/completed/27433.py new file mode 100644 index 0000000..a1d3b0c --- /dev/null +++ b/zeta_python/completed/27433.py @@ -0,0 +1,10 @@ +def factorial(n): + if n <= 0: + return 1 + else: + return n * factorial(n - 1) + + +if __name__ == "__main__": + N: int = int(input()) + print(factorial(N)) diff --git a/zeta_python/stdin.txt b/zeta_python/stdin.txt index e69de29..9a03714 100644 --- a/zeta_python/stdin.txt +++ b/zeta_python/stdin.txt @@ -0,0 +1 @@ +10 \ No newline at end of file