diff --git a/exercises/ex09/ex09b.py b/exercises/ex09/ex09b.py new file mode 100644 index 0000000..5e3a5a6 --- /dev/null +++ b/exercises/ex09/ex09b.py @@ -0,0 +1,21 @@ +""" +Project: Mad Libs +Page 230 +""" + +import re +import sys +from pathlib import Path + +keywords = ["ADJECTIVE", "NOUN", "ADVERB", "VERB"] +infile = Path(sys.argv[1]) +outfile = infile.parent / f"{infile.stem}.res{infile.suffix}" +text = infile.read_text() +matches = re.finditer(r'(' + r'|'.join(keywords) + r')', text) + +for match in matches: + name = match.group() + res = input(f"Enter {name.lower()}:\n") + text = text.replace(name, res, 1) +print(text) +outfile.write_text(text) diff --git a/exercises/ex09/ex09c.py b/exercises/ex09/ex09c.py new file mode 100644 index 0000000..7609d22 --- /dev/null +++ b/exercises/ex09/ex09c.py @@ -0,0 +1,25 @@ +""" +Project: Regex Search +Page 230 +""" + +import re +import sys +from pathlib import Path + +directory = Path(sys.argv[1]) +keys = "|".join(sys.argv[2:]) +regex = re.compile(f"({keys})") +result = [] + +for path in directory.glob("**/*.txt"): + text = open(path, "r").readlines() + show_header = True + for i, line in enumerate(text): + if re.match(regex, line): + if show_header: + result.append(f"\n{path}") + show_header = False + result.append(f"{i + 1}\t{line.strip()}") + +print("\n".join(result).strip()) diff --git a/exercises/ex09/madlibs.res.txt b/exercises/ex09/madlibs.res.txt new file mode 100644 index 0000000..7eabc10 --- /dev/null +++ b/exercises/ex09/madlibs.res.txt @@ -0,0 +1,2 @@ +The red panda walked to the duck and then sleep. A nearby sheep was +unaffected by these events. diff --git a/exercises/ex09/madlibs.txt b/exercises/ex09/madlibs.txt new file mode 100644 index 0000000..e931d8e --- /dev/null +++ b/exercises/ex09/madlibs.txt @@ -0,0 +1,2 @@ +The ADJECTIVE panda walked to the NOUN and then VERB. A nearby NOUN was +unaffected by these events. diff --git a/exercises/ex09/sample1.txt b/exercises/ex09/sample1.txt new file mode 100644 index 0000000..8581ce7 --- /dev/null +++ b/exercises/ex09/sample1.txt @@ -0,0 +1,3 @@ +Write a program that opens all .txt files in a folder and searches for any +line that matches a user-supplied regular expression. The results should +be printed to the screen. diff --git a/exercises/ex09/sample2.txt b/exercises/ex09/sample2.txt new file mode 100644 index 0000000..5031f0c --- /dev/null +++ b/exercises/ex09/sample2.txt @@ -0,0 +1,11 @@ +1. What is a relative path relative to? +2. What does an absolute path start with? +3. What does Path('C:/Users') / 'Al' evaluate to on Windows? +4. What does 'C:/Users' / 'Al' evaluate to on Windows? +5. What do the os.getcwd() and os.chdir() functions do? +6. What are the . and .. folders? +7. In C:\bacon\eggs\spam.txt, which part is the dir name, and which part is the base name? +8. What are the three “mode” arguments that can be passed to the open() function? +9. What happens if an existing file is opened in write mode? +10. What is the difference between the read() and readlines() methods? +11. What data structure does a shelf value resemble?