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
30 changes: 30 additions & 0 deletions implement-shell-tools/cat/cat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import argparse
parser = argparse.ArgumentParser(
prog="my-cat",
description="Simple cat clone with -n and -b options",
)

counterNumber = 1

Choose a reason for hiding this comment

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

In python the usual code style is to use snake_case.

parser.add_argument("-n", action="store_true", help="number all lines")

Choose a reason for hiding this comment

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

cat -n sample-files/1.txt, cat.py sample-files/ *.txt (including -n option) and cat -b sample-files/3.txt all show slightly different result from yours.

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()

for file_path in args.path:
with open(file_path, "r") as f:
content = f.read()
arrText = content.split("\n")

Choose a reason for hiding this comment

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

There is a method in python to read file into an array of lines directly.


if args.b:
for i in range(len(arrText )):

Choose a reason for hiding this comment

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

You can potentially use enumerate here.

if arrText[i].strip() != "":
print(counterNumber,arrText[i])
counterNumber += 1

elif args.n:
for i in range(len(arrText )):

Choose a reason for hiding this comment

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

You can format your code with tools like ruff.

print(counterNumber, arrText[i])
counterNumber += 1
else:
print(content)

Choose a reason for hiding this comment

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

Could you please wrap you logic into a method and run it using if __name__ == "__main__": approach? Ideally the program should not contain global vars, and having methods improves readability.

39 changes: 39 additions & 0 deletions implement-shell-tools/ls/ls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import argparse
import os

def show_unhidden_files(listDir):

Choose a reason for hiding this comment

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

Can these two functions be merged into one using extra method param?

for file in listDir:
if not file.startswith('.'):
print(file)
def show_all_files(listDir):
for file in listDir:
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="use a long listing format")

Choose a reason for hiding this comment

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

Looks like the order of file for ls and your command is slightly different, is it possible to make them appear in the same order?

Choose a reason for hiding this comment

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

What do you mean by long listing format here?

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!="":
if args.a and args.one:
show_all_files(listDir)
elif args.a:
show_all_files(listDir)
elif args.one:
show_unhidden_files(listDir)

else:
show_unhidden_files(listDir)

66 changes: 66 additions & 0 deletions implement-shell-tools/wc/wc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
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",
)
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()

if not args.l and not args.w and not args.c:

Choose a reason for hiding this comment

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

Can you rework this code to have less duplication, by using methods?

for file_path in args.path:
with open(file_path, "r") as f:
content = f.read()
arrText = content.split("\n")
lineCounter += len(arrText)
arrWords = content.split()
wordCounter += len(arrWords)
charCounter += len(content)
print("Line count:", lineCounter,"lines")
print("Word count:", wordCounter,"words")
print("Character count:", charCounter,"characters")
elif args.l and args.w:
for file_path in args.path:
with open(file_path, "r") as f:
content = f.read()
arrText = content.split("\n")
lineCounter += len(arrText)
arrWords = content.split()
wordCounter += len(arrWords)
print("Line count:", lineCounter,"lines")
print("Word count:", wordCounter,"words")
elif args.l and args.c:
for file_path in args.path:
with open(file_path, "r") as f:
content = f.read()
arrText = content.split("\n")
lineCounter += len(arrText)
charCounter += len(content)
print("Line count:", lineCounter,"lines")
print("Character count:", charCounter,"characters")
elif args.l:
for file_path in args.path:
with open(file_path, "r") as f:
content = f.read()
arrText = content.split("\n")
lineCounter += len(arrText)
print("Line count:", lineCounter,"lines")

elif args.w:
for file_path in args.path:
with open(file_path, "r") as f:
content = f.read()
arrText = content.split()
wordCounter += len(arrText)
print("Word count:", wordCounter,"words")
elif args.c:
for file_path in args.path:
with open(file_path, "r") as f:
content = f.read()
charCounter += len(content)
print("Character count:", charCounter,"characters")