Alone

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 that modify search behavior
8 files (list[str]): List of file paths to search in
9
10 Returns:
11 str: Matching lines, formatted according to flags
12
13 Flags:
14 -n: Prepend line numbers
15 -l: Output only filenames with matches
16 -i: Case-insensitive matching
17 -v: Invert matching (return non-matching lines)
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 filenames_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 # Prepare pattern for comparison
29 search_pattern = pattern.lower() if case_insensitive else pattern
30
31 # Results storage
32 results = []
33 matching_files = set()
34
35 # Process each file
36 for filename in files:
37 # Edge Case: File not found
38 try:
39 with open(filename, 'r', encoding='utf-8') as file:
40 lines = file.readlines()
41 except FileNotFoundError:
42 # According to specification, we should just skip files that don't exist
43 continue
44
45 # Edge Case: Empty file
46 if not lines:
47 continue
48
49 # Process each line in the file
50 for line_num, line in enumerate(lines, 1):
51 # Prepare line for comparison
52 compare_line = line.lower().rstrip('\n') if case_insensitive else line.rstrip('\n')
53
54 # Determine if line matches
55 if exact_match:
56 is_match = (search_pattern == compare_line)
57 else:
58 is_match = (search_pattern in compare_line)
59
60 # Apply invert flag
61 if invert_match:
62 is_match = not is_match
63
64 # If line matches, collect results
65 if is_match:
66 matching_files.add(filename)
67
68 # If we only need filenames, we can break early
69 if filenames_only:
70 break
71
72 # Build output line
73 output_line = ""
74
75 # Add filename prefix if multiple files
76 if len(files) > 1:
77 output_line += f"{filename}:"
78
79 # Add line number if requested
80 if line_numbers:
81 output_line += f"{line_num}:"
82
83 # Add the actual line content
84 output_line += line
85
86 results.append(output_line)
87
88 # Edge Case: No files provided
89 if not files:
90 return ""
91
92 # Edge Case: -l flag with no matches
93 if filenames_only:
94 # Return filenames in the order they appear in the input
95 ordered_matching_files = [f for f in files if f in matching_files]
96 return "\n".join(ordered_matching_files) + ("\n" if ordered_matching_files else "")
97
98 # Edge Case: No matches found
99 if not results:
100 return ""
101
102 # Return results
103 return "".join(results)
104
105# Handled Edge Cases: File not found, empty file, no files provided, -l flag with no matches, no matches found
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.