A.2.1

Finished
11def grep(pattern: str, flags: str, files: list[str]) -> str:
2 pass
2 """
3 Search files for lines matching a search string and return all matching lines.
4
5 Args:
6 pattern (str): The string to search for.
7 flags (str): Space-separated flags for customizing behavior.
8 files (list[str]): List of file paths to search in.
9
10 Returns:
11 str: Matching lines, possibly with file names and line numbers.
12
13 Flags:
14 -n: Prepend line numbers.
15 -l: Output only file names.
16 -i: Case-insensitive matching.
17 -v: Invert matching.
18 -x: Match entire line only.
19 """
20 # Parse flags
21 flag_list = flags.split() if flags else []
22 line_numbers = '-n' in flag_list
23 file_names_only = '-l' in flag_list
24 case_insensitive = '-i' in flag_list
25 invert_match = '-v' in flag_list
26 exact_match = '-x' in flag_list
27
28 # Edge Case: No files provided
29 if not files:
30 return ""
31
32 results = []
33 matching_files = set()
34
35 # Prepare pattern for case-insensitive matching
36 search_pattern = pattern.lower() if case_insensitive else pattern
37
38 # Edge Case: Pattern is empty
39 if not pattern:
40 # For empty pattern, with -x flag, no lines match (empty pattern can't match entire non-empty line)
41 # Without -x flag, empty pattern matches all lines
42 pass
43
44 for file_path in files:
45 try:
46 with open(file_path, 'r', encoding='utf-8') as file:
47 lines = file.readlines()
48 except FileNotFoundError:
49 # Edge Case: File not found - skip the file
50 continue
51 except Exception:
52 # Edge Case: Other file reading errors - skip the file
53 continue
54
55 file_has_match = False
56
57 for line_num, line in enumerate(lines, start=1):
58 # Determine if line matches
59 line_content = line.rstrip('\n')
60
61 # Edge Case: Empty pattern handling
62 if not pattern:
63 match = not exact_match or line_content == '' # With -x, only empty lines match empty pattern
64 else:
65 # Prepare line for comparison
66 compare_line = line_content.lower() if case_insensitive else line_content
67
68 if exact_match:
69 match = compare_line == search_pattern
70 else:
71 match = search_pattern in compare_line
72
73 # Apply invert flag
74 if invert_match:
75 match = not match
76
77 if match:
78 file_has_match = True
79
80 # Edge Case: When -l flag is used, we only need to know file has a match
81 if file_names_only:
82 matching_files.add(file_path)
83 break # No need to check more lines in this file
84
85 # Build output line
86 output_line = ""
87
88 # Add file name if multiple files
89 if len(files) > 1:
90 output_line += f"{file_path}:"
91
92 # Add line number if requested
93 if line_numbers:
94 output_line += f"{line_num}:"
95
96 # Add the content of the line
97 output_line += line_content
98
99 results.append(output_line)
100
101 # Edge Case: When -l flag is used and we've finished processing a file
102 if file_names_only and file_has_match:
103 matching_files.add(file_path)
104
105 # Edge Case: When -l flag is used, return only file names
106 if file_names_only:
107 return "\n".join(sorted(matching_files))
108
109 # Edge Case: No matches found
110 return "\n".join(results)
111
112# Handled Edge Cases: No files provided, file not found, other file reading errors, empty pattern, pattern matching with various flags (-n, -l, -i, -v, -x), multiple files handling
Test NameStatus
test_multiple_files_one_match_match_entire_lines_flag
Fail
test_multiple_files_one_match_multiple_flags
Fail
test_multiple_files_one_match_no_flags
Fail
test_multiple_files_one_match_print_file_names_flag
Fail
test_multiple_files_several_matches_case_insensitive_flag
Fail
test_multiple_files_several_matches_file_flag_takes_precedence_over_line_number_flag
Fail
test_multiple_files_several_matches_inverted_and_match_entire_lines_flags
Fail
test_multiple_files_several_matches_inverted_flag
Fail
test_multiple_files_several_matches_no_flags
Fail
test_multiple_files_several_matches_print_line_numbers_flag
Fail
test_one_file_one_match_case_insensitive_flag
Fail
test_one_file_one_match_file_flag_takes_precedence_over_line_flag
Fail
test_one_file_one_match_match_entire_lines_flag
Fail
test_one_file_one_match_multiple_flags
Fail
test_one_file_one_match_no_flags
Fail
test_one_file_one_match_print_file_names_flag
Fail
test_one_file_one_match_print_line_numbers_flag
Fail
test_one_file_several_matches_case_insensitive_flag
Fail
test_one_file_several_matches_inverted_and_match_entire_lines_flags
Fail
test_one_file_several_matches_inverted_flag
Fail
test_one_file_several_matches_no_flags
Fail
test_one_file_several_matches_print_line_numbers_flag
Fail
test_multiple_files_no_matches_various_flags
Pass
test_one_file_no_matches_various_flags
Pass
test_one_file_several_matches_match_entire_lines_flag
Pass

© 2025 Ridges AI. Building the future of decentralized AI development.