Compare commits

..

No commits in common. "1f84479a803dc3219d11c8082d60133b4531f53f" and "827d2ec034d707384cfc9b2917d76cff3a7e44d0" have entirely different histories.

11 changed files with 30 additions and 64 deletions

2
exercises/ex04/help → exercises/ex04/ex04.py Executable file → Normal file
View File

@ -1,6 +1,4 @@
#!/usr/bin/env python
# Exercise 4
# help arg
# 1. Getting help with -help or -h.
# 2. A least three arguments that are flags, meaning they dont take an extra
# argument but simply putting them on the command line turns something on.

3
exercises/ex05/cat → exercises/ex05/ex05.py Executable file → Normal file
View File

@ -1,6 +1,5 @@
#!/usr/bin/env python
# Exercise 5
# cat command
# 1. cat command
import argparse
from pathlib import Path

3
exercises/ex06/find → exercises/ex06/ex06.py Executable file → Normal file
View File

@ -1,6 +1,5 @@
#!/usr/bin/env python
# Exercise 6
# find command
# 1. find command
import sys
import argparse

3
exercises/ex07/grep → exercises/ex07/ex07.py Executable file → Normal file
View File

@ -1,6 +1,5 @@
#!/usr/bin/env python
# Exercise 7
# grep command
# 1. grep command
import re
import sys

3
exercises/ex08/cut → exercises/ex08/ex08.py Executable file → Normal file
View File

@ -1,6 +1,5 @@
#!/usr/bin/env python
# Exercise 8
# cut command
# 1. cut command
import re
import sys

37
exercises/ex09/sed → exercises/ex09/ex09.py Executable file → Normal file
View File

@ -1,6 +1,5 @@
#!/usr/bin/env python
# Exercise 9
# sed command
# 1. sed command
import re
import sys
@ -8,20 +7,34 @@ import argparse
from pathlib import Path
def parse(expression):
try:
match = re.fullmatch(r's/(.*)/(.*)/(g{0,1})', expression)
count = 0 if match.group(3) == "g" else 1
return {
"rgx": re.compile(match.group(1)),
"rep": match.group(2),
"n": count
}
except Exception:
def check(expression, exp):
error = False
if (
expression[-1] not in "/g"
or exp[0] not in "s"
or len(exp) not in range(3, 5)
or (expression[-1] in "g" and len(exp) != 4)
or (expression[-1] in "/" and len(exp) == 4)
):
error = True
if error:
print(f"sed: expresión «{expression}» inválida")
sys.exit(1)
def parse(expression):
count = 0
exp = list(filter(None, expression.split("/")))
check(expression, exp)
if len(exp) == 3:
count = 1
return {
"rgx": re.compile(exp[1]),
"rep": exp[2],
"n": count,
}
parser = argparse.ArgumentParser()
parser.add_argument("stream", default=sys.stdin, nargs="*")
parser.add_argument(

3
exercises/ex10/sort → exercises/ex10/ex10.py Executable file → Normal file
View File

@ -1,6 +1,5 @@
#!/usr/bin/env python
# Exercise 10
# sort command
# 1. sort command
import sys
import argparse

View File

@ -1 +0,0 @@
../ex08/cut

View File

@ -1 +0,0 @@
../ex09/sed

View File

@ -1 +0,0 @@
../ex10/sort

View File

@ -1,37 +0,0 @@
#!/usr/bin/env python
# Exercise 11
# uniq command
import sys
import argparse
from pathlib import Path
parser = argparse.ArgumentParser()
parser.add_argument("stream", default=sys.stdin, nargs="*")
parser.add_argument("-i", "--ignore-case", action="store_true")
parser.add_argument("-c", "--count", action="store_true")
args = parser.parse_args()
lines = []
counter = None
for stream in args.stream:
if isinstance(args.stream, list):
for line in Path(stream).open().readlines():
lines.append(line.rstrip())
else:
lines.append(stream.strip())
if args.ignore_case:
lines = list(map(lambda l: l.lower(), lines))
if args.count:
counter = {}
for line in lines:
counter.setdefault(line, 0)
counter[line] += 1
lines = sorted(set(lines))
for line in lines:
msg = line if counter is None else f"{counter[line]} {line}"
print(msg)