Skip to content
Open
Show file tree
Hide file tree
Changes from 3 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
57 changes: 57 additions & 0 deletions implement-shell-tools/cat/cat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# in built module
import sys

args = sys.argv[1:]

flag = ""
file_paths = []


# Check if the first argument is a flag like -n or -b
if args and args[0].startswith("-"):
flag = args[0]
file_paths = args[1:]
else:
file_paths = args


# Read all files and combine their contents into one string
content = ""

for file in file_paths:
with open(file, "r") as f:
content += f.read()


# Split the file content into separate lines
lines = content.splitlines()


# Handle -n flag: add a number to every line
if flag == "-n":
new_lines = []

for index, line in enumerate(lines):
new_lines.append(f"{index + 1} {line}")

lines = new_lines


# Handle -b flag: number only non-empty lines
if flag == "-b":
line_number = 1
new_lines = []

for line in lines:
# Keep empty lines without adding numbers
if line.strip() == "":
new_lines.append(line)

# Add a number only to lines that contain text
else:
new_lines.append(f"{line_number} {line}")
line_number += 1

lines = new_lines

sys.stdout.write("\n".join(lines))
48 changes: 48 additions & 0 deletions implement-shell-tools/ls/ls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# Access command-line arguments from the terminal
import sys

# Work with the operating system (files and directories)
import os

# Get command-line arguments
args = sys.argv[1:]

flags = []
paths = []


# Separate flags and paths
for arg in args:
if arg.startswith("-"):
flags.append(arg)
else:
paths.append(arg)


# Use the current directory if no path is provided
if not paths:
paths = ["."]


# Process each path
for path in paths:

# If the path is a file, print its name
if os.path.isfile(path):
print(path)

# If the path is a directory, get its contents
elif os.path.isdir(path):
contents = os.listdir(path)
if "-a" not in flags:
contents=[
file for file in contents
if not file.startswith(".")
]
if "-1" in flags:
print("\n".join(contents))
else:
print(" ".join(contents))
# Handle invalid paths
else:
print(f"No such file or directory: {path}")
74 changes: 74 additions & 0 deletions implement-shell-tools/wc/wc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import sys

args = sys.argv[1:]

flags = []
paths = []

# Separate flags and paths
for arg in args:
if arg.startswith("-"):
flags.append(arg)
else:
paths.append(arg)


total_lines = 0
total_words = 0
total_chars = 0


for file in paths:

# Read file as bytes
with open(file, "rb") as f:
content = f.read()

# Count
lines = content.count(b"\n")
words = len(content.split())
chars = len(content)

# Output for this file
output = []

if len(flags) == 0:
output = [lines, words, chars]

else:
if "-l" in flags:
output.append(lines)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

If you run this with multiple files, how does the output look? What change could make it neater?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Thanks. I’ve updated the output formatting to make the results neater when using multiple files.


if "-w" in flags:
output.append(words)

if "-c" in flags:
output.append(chars)

print(*output, file)

# Add to totals
total_lines += lines
total_words += words
total_chars += chars


# Print total only when multiple files
if len(paths) > 1:

total_output = []

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

What do you think about the output code here? Can you spot any duplication across the file?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Thanks. I removed the duplicated output logic by creating a get_output() function.


if len(flags) == 0:
total_output = [total_lines, total_words, total_chars]

else:
if "-l" in flags:
total_output.append(total_lines)

if "-w" in flags:
total_output.append(total_words)

if "-c" in flags:
total_output.append(total_chars)

print(*total_output, "total")
Loading