tobe

Finished
11def grep(pattern: str, flags: str, files: list[str]) -> str:
2 pass
2 # Parse flags
3 line_numbers = '-n' in flags
4 files_only = '-l' in flags
5 case_insensitive = '-i' in flags
6 invert_match = '-v' in flags
7 exact_match = '-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 # Edge Case: Handle files that cannot be read
21 lines = file.readlines()
22 except FileNotFoundError:
23 # Edge Case: Handle files that do not exist
24 continue
25 except IOError:
26 # Edge Case: Handle IO errors when reading files
27 continue
28
29 # Process each line
30 for line_num, line in enumerate(lines, 1):
31 # Prepare line for comparison
32 compare_line = line.lower() if case_insensitive else line
33
34 # Check for match based on exact_match flag
35 if exact_match:
36 is_match = (search_pattern == compare_line.rstrip('\n'))
37 else:
38 is_match = (search_pattern in compare_line)
39
40 # Apply invert flag
41 if invert_match:
42 is_match = not is_match
43
44 # If line matches criteria
45 if is_match:
46 # Edge Case: When -l flag is used, we only need to know which files match
47 if files_only:
48 matched_files.add(filename)
49 break # No need to check more lines in this file
50 else:
51 # Build output line
52 output_line = ""
53 # Add filename prefix if multiple files
54 if len(files) > 1:
55 output_line += f"{filename}:"
56 # Add line number if requested
57 if line_numbers:
58 output_line += f"{line_num}:"
59 # Add the actual line
60 output_line += line
61 results.append(output_line)
62
63 # Edge Case: Handle -l flag output
64 if files_only:
65 # Return filenames in the order they were encountered
66 ordered_files = [f for f in files if f in matched_files]
67 return "\n".join(ordered_files) + ("\n" if ordered_files else "")
68
69 # Edge Case: Handle empty results
70 return "".join(results)
71
72# Handled Edge Cases: files that cannot be read, files that do not exist, IO errors when reading files, -l flag usage, empty results
Test NameStatus
test_multiple_files_no_matches_various_flags
Pass
test_multiple_files_one_match_match_entire_lines_flag
Pass
test_multiple_files_one_match_multiple_flags
Pass
test_multiple_files_one_match_no_flags
Pass
test_multiple_files_one_match_print_file_names_flag
Pass
test_multiple_files_several_matches_case_insensitive_flag
Pass
test_multiple_files_several_matches_file_flag_takes_precedence_over_line_number_flag
Pass
test_multiple_files_several_matches_inverted_and_match_entire_lines_flags
Pass
test_multiple_files_several_matches_inverted_flag
Pass
test_multiple_files_several_matches_no_flags
Pass
test_multiple_files_several_matches_print_line_numbers_flag
Pass
test_one_file_no_matches_various_flags
Pass
test_one_file_one_match_case_insensitive_flag
Pass
test_one_file_one_match_file_flag_takes_precedence_over_line_flag
Pass
test_one_file_one_match_match_entire_lines_flag
Pass
test_one_file_one_match_multiple_flags
Pass
test_one_file_one_match_no_flags
Pass
test_one_file_one_match_print_file_names_flag
Pass
test_one_file_one_match_print_line_numbers_flag
Pass
test_one_file_several_matches_case_insensitive_flag
Pass
test_one_file_several_matches_inverted_and_match_entire_lines_flags
Pass
test_one_file_several_matches_inverted_flag
Pass
test_one_file_several_matches_match_entire_lines_flag
Pass
test_one_file_several_matches_no_flags
Pass
test_one_file_several_matches_print_line_numbers_flag
Pass

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