Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions implement-shell-tools/cat/cat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import argparse


def run(args):
for file_path in args.path:
counter_number = 1

with open(file_path, "r") as f:
lines = f.readlines()

if args.b:
for line in lines:
if line.strip() != "":
print(counter_number, line, end="")
counter_number += 1
else:
print(line, end="")

elif args.n:
for line in lines:
print(counter_number, line, end="")
counter_number += 1

else:
for line in lines:
print(line, end="")


def main():
parser = argparse.ArgumentParser(
prog="my-cat",
description="Simple cat clone with -n and -b options",
)

parser.add_argument("-n", action="store_true", help="number all lines")
parser.add_argument("-b", action="store_true", help="The character to search for")
parser.add_argument("path", nargs="+", help="The file to search")

args = parser.parse_args()
run(args)


if __name__ == "__main__":
main()
31 changes: 31 additions & 0 deletions implement-shell-tools/ls/ls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import argparse
import os


def show_files(files, show_hidden):
for file in files:
if show_hidden:
print(file)
else:
if not file.startswith("."):
print(file)


parser = argparse.ArgumentParser(
prog="my-ls",
description="Simple ls clone with -a and -l options",
)

parser.add_argument("-a", action="store_true", help="include hidden files")
parser.add_argument(
"-1", dest="one", action="store_true", help="list one entry per line"
)
parser.add_argument("path", nargs="?", default=".", help="The directory to list")
args = parser.parse_args()


Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, formatting could be improved with ruff here or other formatters :)

fn = args.path
listDir = os.listdir(fn)

if fn != "":
show_files(listDir, args.a)
55 changes: 55 additions & 0 deletions implement-shell-tools/wc/wc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import argparse

parser = argparse.ArgumentParser(

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please enasure your output is the same as the one suggested for examples in the readme (it is ok if whitespaces are different in your output):

It must act the same as `wc` would, if run from the directory containing this README.md file, for the following command lines:

* `wc sample-files/*`
* `wc -l sample-files/3.txt`
* `wc -w sample-files/3.txt`
* `wc -c sample-files/3.txt`
* `wc -l sample-files/*`

prog="my-wc",
description="Simple wc clone with -l and -w options",
)




def read_file(file_path):
with open(file_path, "r") as file:
return file.read()


def count_text(text):
line_count = text.count("\n")
word_count = len(text.split())
char_count = len(text)
return line_count, word_count, char_count




lineCounter = 0
wordCounter = 0
charCounter = 0

parser.add_argument("-l", action="store_true", help="count lines")
parser.add_argument("-w", action="store_true", help="count words")
parser.add_argument("-c", action="store_true", help="count characters")
parser.add_argument("path", nargs="+", default=".", help="The file to count")
args = parser.parse_args()



for path in args.path:
text = read_file(path)
lines, words, chars = count_text(text)

lineCounter += lines
wordCounter += words
charCounter += chars

if args.l:
print(lines, path)
elif args.w:
print(words, path)
elif args.c:
print(chars, path)
else:
print(lines, words, chars, path)

if len(args.path) > 1 and not args.l and not args.w and not args.c:
print(lineCounter, wordCounter, charCounter, "total")