joseph

Finished
11def grep(pattern: str, flags: str, files: list[str]) -> str:
2 pass
2 # Parse flags - handle both individual flags and combined flags
3 line_numbers = '-n' in flags or 'n' in flags
4 files_only = '-l' in flags or 'l' in flags
5 case_insensitive = '-i' in flags or 'i' in flags
6 invert_match = '-v' in flags or 'v' in flags
7 exact_match = '-x' in flags or 'x' in flags
8
9 # Prepare pattern for comparison
10 search_pattern = pattern.lower() if case_insensitive else pattern
11
12 # Results storage
13 results = []
14 matched_files = set()
15
16 # Process each file
17 for filename in files:
18 try:
19 with open(filename, 'r', encoding='utf-8') as file:
20 lines = file.readlines()
21 except FileNotFoundError:
22 # Edge Case: File not found - skip the file
23 continue
24
25 # Process each line in the file
26 for line_num, line in enumerate(lines, start=1):
27 # Prepare line for comparison (remove trailing newline for comparison)
28 line_content = line.rstrip('\n')
29 compare_line = line_content.lower() if case_insensitive else line_content
30
31 # Check for match based on exact_match flag
32 if exact_match:
33 is_match = (search_pattern == compare_line)
34 else:
35 is_match = (search_pattern in compare_line)
36
37 # Apply invert flag
38 if invert_match:
39 is_match = not is_match
40
41 # If line matches criteria
42 if is_match:
43 matched_files.add(filename)
44
45 # If we only need file names, we can break early
46 if files_only:
47 break
48
49 # Format output line
50 output_line = ""
51
52 # Add filename prefix if multiple files
53 if len(files) > 1:
54 output_line += f"{filename}:"
55
56 # Add line number if requested
57 if line_numbers:
58 output_line += f"{line_num}:"
59
60 # Add the actual line content (with its original newline)
61 output_line += line
62
63 results.append(output_line)
64
65 # If we only need file names and we found a match, we can stop processing this file
66 if files_only and filename in matched_files:
67 continue
68
69 # If files_only flag is set, return only the file names
70 if files_only:
71 # Edge Case: No files matched the pattern - return empty string
72 if not matched_files:
73 return ""
74 return "\n".join(sorted(list(matched_files)))
75
76 # Edge Case: No lines matched the pattern - return empty string
77 # Edge Case: Empty file list - return empty string
78 if not results:
79 return ""
80
81 # Join results with newlines but don't add a trailing newline
82 return "".join(results[:-1]) + results[-1].rstrip('\n') if results else ""
83
84# Handled Edge Cases: File not found, No files matched the pattern, No lines matched the pattern, Empty file list
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.