Skip to content

Commit bbcb2e6

Browse files
Add grep exercise (#746)
* Fixes to concept tree * Add Grep exercise with implementation and tests * Implement Grep search functionality with support for flags and file handling * Apply suggestion from @ryanplusplus * Remove stub file from source * Format grep file --------- Co-authored-by: Ryan Hartlage <2488333+ryanplusplus@users.noreply.github.com>
1 parent 2678b2d commit bbcb2e6

12 files changed

Lines changed: 524 additions & 0 deletions

File tree

bin/test-exercises.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ echo $(crystal -v)
77
test_run() {
88
echo "Testing $1"
99
cat "$1/.meta/src/$2.cr" > "${dic}/src/$2.cr"
10+
if [ -d "$1/assets/." ]; then
11+
cp -a "$1/assets/." "./assets/"
12+
fi
1013
spec_file="$1/$(jq -r '.files.test[0]' $1/.meta/config.json)"
1114
cat "${spec_file}" > "${dic}/spec/spec.cr"
1215
sed -i -e 's/pending/it/g' ${dic}/spec/spec.cr

config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1315,6 +1315,14 @@
13151315
"prerequisites": [],
13161316
"difficulty": 4
13171317
},
1318+
{
1319+
"slug": "grep",
1320+
"name": "Grep",
1321+
"uuid": "de1af862-3118-428e-8590-72806a700780",
1322+
"practices": [],
1323+
"prerequisites": [],
1324+
"difficulty": 4
1325+
},
13181326
{
13191327
"slug": "complex-numbers",
13201328
"name": "Complex Numbers",
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Instructions
2+
3+
Search files for lines matching a search string and return all matching lines.
4+
5+
The Unix [`grep`][grep] command searches files for lines that match a regular expression.
6+
Your task is to implement a simplified `grep` command, which supports searching for fixed strings.
7+
8+
The `grep` command takes three arguments:
9+
10+
1. The string to search for.
11+
2. Zero or more flags for customizing the command's behavior.
12+
3. One or more files to search in.
13+
14+
It then reads the contents of the specified files (in the order specified), finds the lines that contain the search string, and finally returns those lines in the order in which they were found.
15+
When searching in multiple files, each matching line is prepended by the file name and a colon (':').
16+
17+
## Flags
18+
19+
The `grep` command supports the following flags:
20+
21+
- `-n` Prepend the line number and a colon (':') to each line in the output, placing the number after the filename (if present).
22+
- `-l` Output only the names of the files that contain at least one matching line.
23+
- `-i` Match using a case-insensitive comparison.
24+
- `-v` Invert the program -- collect all lines that fail to match.
25+
- `-x` Search only for lines where the search string matches the entire line.
26+
27+
[grep]: https://pubs.opengroup.org/onlinepubs/9699919799/utilities/grep.html
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"authors": [
3+
"meatball133"
4+
],
5+
"files": {
6+
"solution": [
7+
"src/grep.cr"
8+
],
9+
"test": [
10+
"spec/grep_spec.cr"
11+
],
12+
"example": [
13+
".meta/src/example.cr"
14+
],
15+
"editor": [
16+
"assets/iliad.txt",
17+
"assets/midsummer-night.txt",
18+
"assets/paradise-lost.txt"
19+
]
20+
},
21+
"blurb": "Search a file for lines matching a regular expression pattern. Return the line number and contents of each matching line.",
22+
"source": "Conversation with Nate Foster.",
23+
"source_url": "https://www.cs.cornell.edu/Courses/cs3110/2014sp/hw/0/ps0.pdf"
24+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
module Grep
2+
def self.search(pattern : String, flags : Array(String), files : Array(String)) : String
3+
# - `-n` Prepend the line number and a colon (':') to each line in the output, placing the number after the filename (if present).
4+
# - `-l` Output only the names of the files that contain at least one matching line.
5+
# - `-i` Match using a case-insensitive comparison.
6+
# - `-v` Invert the program -- collect all lines that fail to match.
7+
# - `-x` Search only for lines where the search string matches the entire line.
8+
9+
results = [] of String
10+
files.each do |file|
11+
File.open("assets/#{file}") do |f|
12+
f.each_line.with_index(1) do |line, line_number|
13+
line_to_check = line.chomp
14+
pattern_to_check = pattern.dup
15+
16+
if flags.includes?("-i")
17+
line_to_check = line_to_check.downcase
18+
pattern_to_check = pattern_to_check.downcase
19+
end
20+
21+
is_match = if flags.includes?("-x")
22+
line_to_check == pattern_to_check
23+
else
24+
line_to_check.includes?(pattern_to_check)
25+
end
26+
27+
is_match = !is_match if flags.includes?("-v")
28+
29+
if is_match
30+
if flags.includes?("-l")
31+
results << file unless results.includes?(file)
32+
break
33+
else
34+
result_line = ""
35+
result_line += "#{file}:" if files.size > 1 && !flags.includes?("-l")
36+
result_line += "#{line_number}:" if flags.includes?("-n")
37+
result_line += line.chomp
38+
results << result_line
39+
end
40+
end
41+
end
42+
end
43+
end
44+
results.join("\n")
45+
end
46+
end
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
require "spec"
2+
require "../src/*"
3+
4+
describe "<%-= to_capitalized(@json["exercise"].to_s) %>" do
5+
<%- @json["cases"].as_a.each do |cases| %>
6+
<%- cases["cases"].as_a.each do |subcases| %>
7+
<%= status()%> "<%-= subcases["description"] %>" do
8+
expected = "<%= subcases["expected"].as_a.join("\n") %>"
9+
pattern = "<%= subcases["input"]["pattern"] %>"
10+
flags = <%= to_s_deep(subcases["input"]["flags"]) %> <%= to_s_deep(subcases["input"]["flags"]) == "[]" ? "of String" : "" %>
11+
files = <%= to_s_deep(subcases["input"]["files"]) %>
12+
13+
<%= to_capitalized(@json["exercise"].to_s) %>.search(pattern, flags, files).should eq(expected)
14+
end
15+
<% end %>
16+
<% end %>
17+
end
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
# This is an auto-generated file.
2+
#
3+
# Regenerating this file via `configlet sync` will:
4+
# - Recreate every `description` key/value pair
5+
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
6+
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
7+
# - Preserve any other key/value pair
8+
#
9+
# As user-added comments (using the # character) will be removed when this file
10+
# is regenerated, comments can be added via a `comment` key.
11+
12+
[9049fdfd-53a7-4480-a390-375203837d09]
13+
description = "Test grepping a single file -> One file, one match, no flags"
14+
15+
[76519cce-98e3-46cd-b287-aac31b1d77d6]
16+
description = "Test grepping a single file -> One file, one match, print line numbers flag"
17+
18+
[af0b6d3c-e0e8-475e-a112-c0fc10a1eb30]
19+
description = "Test grepping a single file -> One file, one match, case-insensitive flag"
20+
21+
[ff7af839-d1b8-4856-a53e-99283579b672]
22+
description = "Test grepping a single file -> One file, one match, print file names flag"
23+
24+
[8625238a-720c-4a16-81f2-924ec8e222cb]
25+
description = "Test grepping a single file -> One file, one match, match entire lines flag"
26+
27+
[2a6266b3-a60f-475c-a5f5-f5008a717d3e]
28+
description = "Test grepping a single file -> One file, one match, multiple flags"
29+
30+
[842222da-32e8-4646-89df-0d38220f77a1]
31+
description = "Test grepping a single file -> One file, several matches, no flags"
32+
33+
[4d84f45f-a1d8-4c2e-a00e-0b292233828c]
34+
description = "Test grepping a single file -> One file, several matches, print line numbers flag"
35+
36+
[0a483b66-315b-45f5-bc85-3ce353a22539]
37+
description = "Test grepping a single file -> One file, several matches, match entire lines flag"
38+
39+
[3d2ca86a-edd7-494c-8938-8eeed1c61cfa]
40+
description = "Test grepping a single file -> One file, several matches, case-insensitive flag"
41+
42+
[1f52001f-f224-4521-9456-11120cad4432]
43+
description = "Test grepping a single file -> One file, several matches, inverted flag"
44+
45+
[7a6ede7f-7dd5-4364-8bf8-0697c53a09fe]
46+
description = "Test grepping a single file -> One file, no matches, various flags"
47+
48+
[3d3dfc23-8f2a-4e34-abd6-7b7d140291dc]
49+
description = "Test grepping a single file -> One file, one match, file flag takes precedence over line flag"
50+
51+
[87b21b24-b788-4d6e-a68b-7afe9ca141fe]
52+
description = "Test grepping a single file -> One file, several matches, inverted and match entire lines flags"
53+
54+
[ba496a23-6149-41c6-a027-28064ed533e5]
55+
description = "Test grepping multiples files at once -> Multiple files, one match, no flags"
56+
57+
[4539bd36-6daa-4bc3-8e45-051f69f5aa95]
58+
description = "Test grepping multiples files at once -> Multiple files, several matches, no flags"
59+
60+
[9fb4cc67-78e2-4761-8e6b-a4b57aba1938]
61+
description = "Test grepping multiples files at once -> Multiple files, several matches, print line numbers flag"
62+
63+
[aeee1ef3-93c7-4cd5-af10-876f8c9ccc73]
64+
description = "Test grepping multiples files at once -> Multiple files, one match, print file names flag"
65+
66+
[d69f3606-7d15-4ddf-89ae-01df198e6b6c]
67+
description = "Test grepping multiples files at once -> Multiple files, several matches, case-insensitive flag"
68+
69+
[82ef739d-6701-4086-b911-007d1a3deb21]
70+
description = "Test grepping multiples files at once -> Multiple files, several matches, inverted flag"
71+
72+
[77b2eb07-2921-4ea0-8971-7636b44f5d29]
73+
description = "Test grepping multiples files at once -> Multiple files, one match, match entire lines flag"
74+
75+
[e53a2842-55bb-4078-9bb5-04ac38929989]
76+
description = "Test grepping multiples files at once -> Multiple files, one match, multiple flags"
77+
78+
[9c4f7f9a-a555-4e32-bb06-4b8f8869b2cb]
79+
description = "Test grepping multiples files at once -> Multiple files, no matches, various flags"
80+
81+
[ba5a540d-bffd-481b-bd0c-d9a30f225e01]
82+
description = "Test grepping multiples files at once -> Multiple files, several matches, file flag takes precedence over line number flag"
83+
84+
[ff406330-2f0b-4b17-9ee4-4b71c31dd6d2]
85+
description = "Test grepping multiples files at once -> Multiple files, several matches, inverted and match entire lines flags"
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
Achilles sing, O Goddess! Peleus' son;
2+
His wrath pernicious, who ten thousand woes
3+
Caused to Achaia's host, sent many a soul
4+
Illustrious into Ades premature,
5+
And Heroes gave (so stood the will of Jove)
6+
To dogs and to all ravening fowls a prey,
7+
When fierce dispute had separated once
8+
The noble Chief Achilles from the son
9+
Of Atreus, Agamemnon, King of men.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
I do entreat your grace to pardon me.
2+
I know not by what power I am made bold,
3+
Nor how it may concern my modesty,
4+
In such a presence here to plead my thoughts;
5+
But I beseech your grace that I may know
6+
The worst that may befall me in this case,
7+
If I refuse to wed Demetrius.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
Of Mans First Disobedience, and the Fruit
2+
Of that Forbidden Tree, whose mortal tast
3+
Brought Death into the World, and all our woe,
4+
With loss of Eden, till one greater Man
5+
Restore us, and regain the blissful Seat,
6+
Sing Heav'nly Muse, that on the secret top
7+
Of Oreb, or of Sinai, didst inspire
8+
That Shepherd, who first taught the chosen Seed

0 commit comments

Comments
 (0)