Skip to content

Commit 2ee678b

Browse files
committed
cat, la and wc changes done, removed other sprint work
1 parent cb9b93b commit 2ee678b

File tree

4 files changed

+49
-88
lines changed

4 files changed

+49
-88
lines changed

implement-shell-tools/cat/cat.py

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,39 +6,42 @@ def cleanInput(listOfFiles):
66

77
for file in listOfFiles:
88
grabbedText = Path(file).read_text(encoding="utf-8")
9-
splitLines = grabbedText.split("\n")
9+
splitLines = grabbedText.splitlines()
1010
cleanLinesArr.extend(splitLines)
1111

1212
return cleanLinesArr
1313

14-
args = sys.argv[1:]
15-
flag = None
16-
restIsFiles = []
17-
18-
if len(args) > 0 and args[0] and args[0][0] == "-":
19-
flag = args[0]
20-
restIsFiles = args[1:]
21-
else:
22-
flag = None
23-
restIsFiles = args
24-
2514
def takeSpecifiedAction(cleanLinesArr, flag):
2615
countingOnlyFullLines = 1
2716

28-
for file in cleanLinesArr:
17+
if flag not in (None, "-n", "-b"):
18+
print("incorrect flag")
19+
return
20+
21+
for line in cleanLinesArr:
2922
if not flag:
30-
print(file)
23+
print(line)
3124
elif flag == "-n":
32-
print(f"{countingOnlyFullLines} {file}")
25+
print(f"{countingOnlyFullLines} {line}")
3326
countingOnlyFullLines += 1
3427
elif flag == "-b":
35-
if file == "":
28+
if line == "":
3629
print("")
3730
else:
38-
print(f"{countingOnlyFullLines} {file}")
31+
print(f"{countingOnlyFullLines} {line}")
3932
countingOnlyFullLines += 1
40-
else:
41-
print("incorrect flag")
33+
34+
35+
args = sys.argv[1:]
36+
flag = None
37+
restIsFiles = []
38+
39+
if len(args) > 0 and args[0] and args[0][0] == "-":
40+
flag = args[0]
41+
restIsFiles = args[1:]
42+
else:
43+
flag = None
44+
restIsFiles = args
4245

4346
lines = cleanInput(restIsFiles)
4447
takeSpecifiedAction(lines, flag)

implement-shell-tools/ls/ls.py

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,30 @@ def showAllFilesInDir(directory):
1010
print(eachFile)
1111

1212
# `ls -1 sample-files`
13-
def showVisibleInSampleFiles():
14-
listOfFiles = os.listdir("sample-files")
13+
def showVisibleInSampleFiles(directory):
14+
listOfFiles = os.listdir(directory)
1515

1616
for eachFile in listOfFiles:
1717
if eachFile[0] != ".":
1818
print(eachFile)
1919

2020
# `ls -1 -a sample-files`
21-
def showAllInSampleFiles():
22-
listOfFiles = os.listdir("sample-files")
21+
def showAllInSampleFiles(directory):
22+
listOfFiles = os.listdir(directory)
2323

2424
for eachFile in listOfFiles:
2525
print(eachFile)
2626

2727
argv = sys.argv[1:]
2828

29-
if "-a" in argv:
30-
showAllInSampleFiles()
31-
elif "sample-files" in argv:
32-
showVisibleInSampleFiles()
33-
else:
34-
showAllFilesInDir(".")
29+
show_all = "-a" in argv
30+
31+
directories = [arg for arg in argv if arg != "-a"]
32+
if not directories:
33+
directories = ["."]
34+
35+
for directory in directories:
36+
if show_all:
37+
showAllInSampleFiles(directory)
38+
else:
39+
showVisibleInSampleFiles(directory)

implement-shell-tools/wc/wc.py

Lines changed: 12 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,26 @@
11
import sys
22
from pathlib import Path
33

4-
def calculateCounts(inputFiles):
5-
return {
6-
"lines": len(inputFiles.split("\n")) - 1,
7-
"words": len(inputFiles.split()),
8-
"bytes": len(inputFiles),
9-
}
10-
11-
# * `wc -l sample-files/3.txt`
12-
# * `wc -l sample-files/*`
13-
def countLines(listOfFiles):
14-
for file in listOfFiles:
15-
content = Path(file).read_text(encoding="utf-8")
16-
17-
counts = calculateCounts(content)
18-
print(f"{counts['lines']} {file}")
19-
20-
# * `wc -w sample-files/3.txt`
21-
def countWords(listOfFiles):
22-
for file in listOfFiles:
23-
content = Path(file).read_text(encoding="utf-8")
24-
25-
# const wordsCounted = content.split(" ").filter(word => word !== "").length;
26-
counts = calculateCounts(content)
27-
print(f"{counts['words']} {file}")
28-
29-
# * `wc -c sample-files/3.txt`
30-
def countBytes(listOfFiles):
31-
for file in listOfFiles:
32-
content = Path(file).read_text(encoding="utf-8")
33-
counts = calculateCounts(content)
34-
print(f"{counts['bytes']} {file}")
35-
36-
# * `wc sample-files/*`
37-
def countAll(listOfFiles):
4+
def countAll(listOfFiles, flag=None):
385
for file in listOfFiles:
396
content = Path(file).read_text(encoding="utf-8")
40-
counts = calculateCounts(content)
41-
print(f"{counts['lines']} {counts['words']} {counts['bytes']} {file}")
7+
if flag == "-l":
8+
print(f"{len(content.splitlines())} {file}")
9+
elif flag == "-w":
10+
print(f"{len(content.split())} {file}")
11+
elif flag == "-c":
12+
print(f"{len(content)} {file}")
13+
else:
14+
print(f"{len(content.splitlines())} {len(content.split())} {len(content)} {file}")
4215

4316
argv = sys.argv[1:]
4417
files = [arg for arg in argv if not arg.startswith("-")]
4518

4619
if "-l" in argv:
47-
countLines(files)
20+
countAll(files, "-l")
4821
elif "-w" in argv:
49-
countWords(files)
22+
countAll(files, "-w")
5023
elif "-c" in argv:
51-
countBytes(files)
24+
countAll(files, "-c")
5225
else:
5326
countAll(files)

number-systems/README.md

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,61 +5,41 @@ Do not convert any binary numbers to decimal when solving a question unless the
55
The goal of these exercises is for you to gain an intuition for binary numbers. Using tools to solve the problems defeats the point.
66

77
Convert the decimal number 14 to binary.
8-
Answer:1110
98

109
Convert the binary number 101101 to decimal:
11-
Answer:45
1210

1311
Which is larger: 1000 or 0111?
14-
Answer:1000
1512

1613
Which is larger: 00100 or 01011?
17-
Answer:01011
1814

1915
What is 10101 + 01010?
20-
Answer:11111
2116

2217
What is 10001 + 10001?
23-
Answer:100010
2418

2519
What's the largest number you can store with 4 bits, if you want to be able to represent the number 0?
26-
Answer:1111 so 15
2720

2821
How many bits would you need in order to store the numbers between 0 and 255 inclusive?
29-
Answer:8 as 2 **8 = 256 0-255
3022

3123
How many bits would you need in order to store the numbers between 0 and 3 inclusive?
32-
Answer: 2 **2 0 1 2 3
3324

3425
How many bits would you need in order to store the numbers between 0 and 1000 inclusive?
35-
Answer:2** 10 = 1024 so 10
3626

3727
How can you test if a binary number is a power of two (e.g. 1, 2, 4, 8, 16, ...)?
38-
Answer: number /2 until it reaches 1, check remainder at each division
3928

4029
Convert the decimal number 14 to hex.
41-
Answer: E
4230

4331
Convert the decimal number 386 to hex.
44-
Answer:0x182 863/16 =24 %2 24/16=1 %8
4532

4633
Convert the hex number 386 to decimal.
47-
Answer:902
4834

4935
Convert the hex number B to decimal.
50-
Answer:11
5136

5237
If reading the byte 0x21 as a number, what decimal number would it mean?
53-
Answer: 33
5438

5539
If reading the byte 0x21 as an ASCII character, what character would it mean?
56-
Answer:!
5740

5841
If reading the byte 0x21 as a greyscale colour, as described in "Approaches for Representing Colors and Images", what colour would it mean?
59-
Answer:33 intensity so grey dark
6042

6143
If reading the bytes 0xAA00FF as an RGB colour, as described in "Approaches for Representing Colors and Images", what colour would it mean?
62-
Answer:RGB(170, 0, 255)
6344

6445
If reading the bytes 0xAA00FF as a sequence of three one-byte decimal numbers, what decimal numbers would they be?
65-
Answer: 170, 0, 255

0 commit comments

Comments
 (0)