couple

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): Flags for customizing the command's behavior.
8 files (list[str]): One or more files to search in.
9
10 Returns:
11 str: Matching lines, possibly with file names and line numbers.
12 """
13 # Parse flags
14 line_numbers = '-n' in flags
15 files_only = '-l' in flags
16 case_insensitive = '-i' in flags
17 invert_match = '-v' in flags
18 exact_match = '-x' in flags
19
20 # Edge Case: Empty pattern
21 if not pattern:
22 if files_only:
23 return ''
24
25 # Edge Case: Empty files list
26 if not files:
27 return ''
28
29 results = []
30 matched_files = set()
31
32 multiple_files = len(files) > 1
33
34 for file_path in files:
35 try:
36 with open(file_path, 'r', encoding='utf-8') as f:
37 lines = f.readlines()
38 except FileNotFoundError:
39 # Edge Case: File not found
40 continue
41
42 # Edge Case: File cannot be read (handled by exception)
43
44 for i, line in enumerate(lines, start=1):
45 # Prepare line and pattern for comparison
46 search_line = line
47 search_pattern = pattern
48
49 if case_insensitive:
50 search_line = search_line.lower()
51 search_pattern = search_pattern.lower()
52
53 # Determine if line matches
54 if exact_match:
55 matches = search_line.rstrip('\n\r') == search_pattern
56 else:
57 matches = search_pattern in search_line
58
59 if invert_match:
60 matches = not matches
61
62 if matches:
63 matched_files.add(file_path)
64
65 if not files_only:
66 output_line = ''
67
68 if multiple_files:
69 output_line += f'{file_path}:'
70
71 if line_numbers:
72 output_line += f'{i}:'
73
74 output_line += line
75 results.append(output_line)
76
77 # Edge Case: No matches found
78 if files_only:
79 return '\n'.join(sorted(matched_files)) + ('\n' if matched_files else '')
80
81 # Edge Case: No matches found
82 return ''.join(results)
83
84# Handled Edge Cases: Empty pattern, Empty files list, File not found, File cannot be read, 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.