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): 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 # Prepare pattern for comparison
21 search_pattern = pattern.lower() if case_insensitive else pattern
22
23 # Results storage
24 results = []
25 matching_files = set()
26
27 # Process each file
28 for filename in files:
29 try:
30 with open(filename, 'r', encoding='utf-8') as file:
31 lines = file.readlines()
32
33 # Process each line
34 for i, line in enumerate(lines, start=1):
35 # Prepare line for comparison
36 compare_line = line.lower() if case_insensitive else line
37
38 # Check for match
39 if exact_match:
40 is_match = search_pattern == compare_line.rstrip('\n')
41 else:
42 is_match = search_pattern in compare_line
43
44 # Apply invert flag
45 if invert_match:
46 is_match = not is_match
47
48 # If line matches
49 if is_match:
50 # Edge Case: When -l flag is used, we only need to know which files match
51 if files_only:
52 matching_files.add(filename)
53 break # No need to check more lines in this file
54 else:
55 # Build output line
56 output_line = ""
57 # Edge Case: When multiple files are provided, prepend filename
58 if len(files) > 1:
59 output_line += f"{filename}:"
60 # Edge Case: When -n flag is used, prepend line number
61 if line_numbers:
62 output_line += f"{i}:"
63 output_line += line
64 results.append(output_line)
65 # Edge Case: Handle file not found errors
66 except FileNotFoundError:
67 # According to typical grep behavior, we silently ignore missing files
68 # or we could raise an error - following typical behavior here
69 continue
70
71 # Edge Case: When -l flag is used, return only filenames
72 if files_only:
73 return "\n".join(sorted(list(matching_files)))
74
75 # Edge Case: Handle case where no matches are found
76 if not results:
77 return ""
78
79 # Edge Case: Remove trailing newline if present
80 output = "".join(results)
81 if output.endswith("\n"):
82 output = output[:-1]
83
84 return output
85 # Handled Edge Cases: Empty file list, file not found, no matches found, case sensitivity, exact match, invert match, line numbers, files only flag, multiple files, trailing newlines
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.