uv

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 customize behavior (-n, -l, -i, -v, -x).
8 files (list[str]): List of file paths to search in.
9
10 Returns:
11 str: Matching lines according to the specified 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 try:
38 with open(filename, 'r', encoding='utf-8') as file:
39 lines = file.readlines()
40 except FileNotFoundError:
41 # Edge Case: File not found - skip the file
42 continue
43
44 # Process each line in the file
45 for line_num, line in enumerate(lines, start=1):
46 # Prepare line for comparison
47 compare_line = line.lower().rstrip('\n') if case_insensitive else line.rstrip('\n')
48
49 # Determine if line matches
50 if exact_match:
51 # Edge Case: Exact match flag - check if entire line matches pattern
52 matches = (compare_line == search_pattern)
53 else:
54 # Edge Case: Substring match - check if pattern is in line
55 matches = (search_pattern in compare_line)
56
57 # Apply invert flag
58 if invert_match:
59 # Edge Case: Invert match flag - flip the match result
60 matches = not matches
61
62 # If line matches criteria
63 if matches:
64 # Edge Case: Filenames only flag - collect only the filename
65 if filenames_only:
66 matching_files.add(filename)
67 # No need to process more lines for this file if we only need the filename
68 break
69 else:
70 # Build output line
71 output_line = ""
72 # Edge Case: Multiple files - prepend filename
73 if len(files) > 1:
74 output_line += f"{filename}:"
75 # Edge Case: Line numbers flag - prepend line number
76 if line_numbers:
77 output_line += f"{line_num}:"
78 output_line += line
79 results.append(output_line)
80
81 # Edge Case: Filenames only flag - return only filenames
82 if filenames_only:
83 # Return filenames in the order they were encountered
84 ordered_files = [f for f in files if f in matching_files]
85 return "\n".join(ordered_files) + ("\n" if ordered_files else "")
86
87 # Edge Case: No matches - return empty string
88 return "".join(results)
89
90# Handled Edge Cases: File not found, exact match flag, substring match, invert match flag, filenames only flag, multiple files, line numbers flag, no matches"
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.