Skip to content

Commit 7c76155

Browse files
committed
Refactor conditionals
1 parent 4a8a83e commit 7c76155

3 files changed

Lines changed: 17 additions & 20 deletions

File tree

implement-shell-tools/cat/cat.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
show_lines = args.number
1111
non_blank = args.nonblank
1212

13-
if show_lines == True and non_blank == True:
13+
if show_lines and non_blank:
1414
print("Error: Cannot use -n and -b together. Please use only one flag at a time.")
1515
exit()
1616

@@ -24,15 +24,15 @@
2424
text_list = text.split("\n")
2525
text_list.pop(len(text_list) - 1)
2626

27-
if (show_lines == False and non_blank == False):
27+
if not show_lines and not non_blank:
2828
print("\n".join(text_list))
2929
exit()
3030

3131
for line in text_list:
32-
if non_blank == True and line != "":
32+
if non_blank and line != "":
3333
print(" " + str(i) + " " + line)
3434
i += 1
35-
elif non_blank == False:
35+
elif not non_blank:
3636
print(" " + str(i) + " " + line)
3737
i += 1
3838
else:

implement-shell-tools/ls/ls.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@
1616
contents = []
1717

1818
for f in listdir(path):
19-
if show_hidden == False and f[0] != ".":
19+
if not show_hidden and f[0] != ".":
2020
contents.append(f)
21-
if show_hidden == True:
21+
if show_hidden:
2222
contents.append(f)
2323

2424
contents.sort()
2525

26-
if one_per_line == True:
26+
if one_per_line:
2727
print("\n".join(contents))
2828
else:
2929
print(" ".join(contents))

implement-shell-tools/wc/wc.py

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
w = True
1717
c = True
1818

19-
if args.words == True or args.line == True or args.bytes == True:
19+
if args.words or args.line or args.bytes:
2020
l = args.line
2121
w = args.words
2222
c = args.bytes
@@ -33,18 +33,15 @@
3333
file_data_with_newline[file] = f.read()
3434

3535
def print_helper(line, word, byte, file_name):
36-
text = [" "]
37-
if l == True:
38-
text.append(str(line))
39-
text.append(" ")
40-
if w == True:
41-
text.append(str(word))
42-
text.append(" ")
43-
if c == True:
44-
text.append(str(byte))
45-
text.append(" ")
46-
text.append(file_name)
47-
print("".join(text))
36+
parts = []
37+
if l:
38+
parts.append(str(line))
39+
if w:
40+
parts.append(str(word))
41+
if c:
42+
parts.append(str(byte))
43+
parts.append(file_name)
44+
print(" ".join(parts))
4845

4946
for f in file_data:
5047
word_per_line = 0

0 commit comments

Comments
 (0)