From 329ff4b0c15f232dd7e130de5940afc2b84a34cb Mon Sep 17 00:00:00 2001 From: mirabellemorah Date: Mon, 10 Aug 2026 04:44:06 +0100 Subject: [PATCH 1/2] implementing cat.py --- implement-shell-tools/cat/cat.py | 49 ++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 implement-shell-tools/cat/cat.py diff --git a/implement-shell-tools/cat/cat.py b/implement-shell-tools/cat/cat.py new file mode 100644 index 000000000..c276700f7 --- /dev/null +++ b/implement-shell-tools/cat/cat.py @@ -0,0 +1,49 @@ +import argparse + +parser = argparse.ArgumentParser( + prog="check-for-cat", + description="Implement my own version of cat", +) + +parser.add_argument("paths", nargs="+", help="The file paths to process") +parser.add_argument("-n", action="store_true", help="Number lines") +parser.add_argument("-b", action="store_true", help="Number non-blank lines") + +args = parser.parse_args() + +paths = args.paths +show_n = args.n +show_b = args.b + +line_number = 1 + +for path in paths: + with open(path, "r", encoding="utf-8") as f: + content = f.read() + + ends_with_newline = content.endswith("\n") + lines = content.split("\n") + if ends_with_newline: + lines.pop() + + new_lines = [] + for line in lines: + if show_b: + if line == "": + new_lines.append(line) + else: + numbered = str(line_number).rjust(6, " ") + "\t" + line + line_number += 1 + new_lines.append(numbered) + elif show_n: + numbered = str(line_number).rjust(6, " ") + "\t" + line + line_number += 1 + new_lines.append(numbered) + else: + new_lines.append(line) + lines = new_lines + + output = "\n".join(lines) + if ends_with_newline: + output += "\n" + print(output, end="") \ No newline at end of file From df8505db6f68d2fc1291418aed249621f559a369 Mon Sep 17 00:00:00 2001 From: mirabellemorah Date: Mon, 10 Aug 2026 05:13:19 +0100 Subject: [PATCH 2/2] cat-adjusted --- implement-shell-tools/cat/cat.py | 2 +- implement-shell-tools/ls/ls.py | 43 ++++++++++++++++++++ implement-shell-tools/wc/wc.py | 70 ++++++++++++++++++++++++++++++++ 3 files changed, 114 insertions(+), 1 deletion(-) create mode 100644 implement-shell-tools/ls/ls.py create mode 100644 implement-shell-tools/wc/wc.py diff --git a/implement-shell-tools/cat/cat.py b/implement-shell-tools/cat/cat.py index c276700f7..ebc866f1e 100644 --- a/implement-shell-tools/cat/cat.py +++ b/implement-shell-tools/cat/cat.py @@ -2,7 +2,7 @@ parser = argparse.ArgumentParser( prog="check-for-cat", - description="Implement my own version of cat", + description="Implement my own version of cat in python", ) parser.add_argument("paths", nargs="+", help="The file paths to process") diff --git a/implement-shell-tools/ls/ls.py b/implement-shell-tools/ls/ls.py new file mode 100644 index 000000000..e8506ac6c --- /dev/null +++ b/implement-shell-tools/ls/ls.py @@ -0,0 +1,43 @@ +import argparse +import os +import stat + +parser = argparse.ArgumentParser( + prog="check-for-ls", + description="Implement my own version of ls", +) + +parser.add_argument("paths", nargs="*", help="The file paths to process") +parser.add_argument("-1", "--one", action="store_true", help="This lists one file per line") +parser.add_argument("-a", action="store_true", help="This shows all files") + +args = parser.parse_args() + +show_one_line = args.one +show_all_files = args.a + +paths = args.paths +file_path = paths +if len(file_path) == 0: + file_path = ["."] + +for target in file_path: + info = os.stat(target) + + if stat.S_ISDIR(info.st_mode): + show_files = os.listdir(target) + + if show_all_files: + show_files = [".", "..", *show_files] + else: + show_files = [name for name in show_files if not name.startswith(".")] + + show_files.sort() + else: + show_files = [target] + + if show_one_line: + for file in show_files: + print(file) + else: + print(" ".join(show_files)) \ No newline at end of file diff --git a/implement-shell-tools/wc/wc.py b/implement-shell-tools/wc/wc.py new file mode 100644 index 000000000..d4c8aa5b5 --- /dev/null +++ b/implement-shell-tools/wc/wc.py @@ -0,0 +1,70 @@ +import argparse +import re + +parser = argparse.ArgumentParser( + prog="check-for-wc", + description="Implement my own version of wc", +) + +parser.add_argument("paths", nargs="+", help="The file paths to process") +parser.add_argument("-l", action="store_true", help="Counts the total number of lines") +parser.add_argument("-c", action="store_true", help="Counts the total number of characters") +parser.add_argument("-w", action="store_true", help="Counts the total number of words") + +args = parser.parse_args() + +paths = args.paths +show_lines = args.l +show_words = args.w +show_char = args.c + +no_flags_given = not show_lines and not show_words and not show_char + +columns = [] + +for path in paths: + with open(path, "r", encoding="utf-8") as f: + content = f.read() + + line_count = content.count("\n") + + word_count = len([word for word in re.split(r"\s+", content) if word != ""]) + + char_count = len(content.encode("utf-8")) + + columns.append({ + "path": path, + "lines": line_count, + "words": word_count, + "char": char_count, + }) + +if len(columns) > 1: + total_lines = 0 + total_words = 0 + total_char = 0 + + for result in columns: + total_lines += result["lines"] + total_words += result["words"] + total_char += result["char"] + + columns.append({ + "path": "total", + "lines": total_lines, + "words": total_words, + "char": total_char, + }) + +for result in columns: + line = "" + if no_flags_given or show_lines: + line += str(result["lines"]).rjust(6, " ") + if no_flags_given or show_words: + line += str(result["words"]).rjust(6, " ") + if no_flags_given or show_char: + line += str(result["char"]).rjust(6, " ") + + line += " " + result["path"] + + print(line) \ No newline at end of file