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
21 changes: 21 additions & 0 deletions implement-shell-tools/cat/cat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import sys

if sys.argv[1] == "-n" or sys.argv[1] == "-b":
filenames = sys.argv[2:]
else:
filenames = sys.argv[1:]
line_number = 1
for filename in filenames:
file = open(filename)
for line in file:
if sys.argv[1] == "-n":
print(line_number, line, end = "")
line_number += 1
elif sys.argv[1]== "-b":
if line.strip() != "":
print(line_number, line, end = "")
line_number += 1
else:
print(line, end="")
else:
print(line, end = "")
42 changes: 42 additions & 0 deletions implement-shell-tools/ls/ls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import sys
import os

show_all = "-a" in sys.argv
one_per_line = "-1" in sys.argv

paths = []

for argument in sys.argv[1:]:
if not argument.startswith("-"):
paths.append(argument)

if not paths:
paths.append(".")

for path in paths:
if os.path.isfile(path):
if len(paths) == 1 or one_per_line:
print(path)
else:
print(path, end = (" "))
else:
if len(paths) > 1:
print()
print()
print(path + ":")

files = sorted(os.listdir(path))

if show_all:
files = ["."] + [".."] + files

for file in files:
if show_all or not file.startswith("."):
if one_per_line:
print(file)
else:
print(file, end=(" "))
if not one_per_line:
print()


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

count_lines = "-l" in sys.argv
count_words = "-w" in sys.argv
count_bytes = "-c" in sys.argv

filenames = []

for argument in sys.argv[1:]:
if not argument.startswith("-"):
filenames.append(argument)

def format_counts(line_count, word_count, byte_count):
output = []

if count_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.

it looks like there is some duplication in the structure of this code. Can you think of a way to simplify it?

output.append(line_count)

if count_words:
output.append(word_count)

if count_bytes:
output.append(byte_count)

if not count_lines and not count_words and not count_bytes:
output.append(line_count)
output.append(word_count)
output.append(byte_count)

formatted_output = []

for number in output:
formatted_output.append(f"{number:3}")

return " ".join(formatted_output)

total_lines = 0
total_words = 0
total_bytes = 0

for filename in filenames:
file = open(filename)

line_count = 0
word_count = 0

for line in file:
line_count += 1
word_count += len(line.split())

byte_count = os.path.getsize(filename)

total_lines += line_count
total_words += word_count
total_bytes += byte_count

print(
format_counts(line_count, word_count, byte_count),
filename,
)

if len(filenames) > 1:
print(
format_counts(total_lines, total_words, total_bytes),
"total",
)
Loading