|
| 1 | +# Grep |
| 2 | + |
| 3 | +Search a file for lines matching a regular expression pattern. Return the line |
| 4 | +number and contents of each matching line. |
| 5 | + |
| 6 | +The Unix [`grep`](http://pubs.opengroup.org/onlinepubs/9699919799/utilities/grep.html) command can be used to search for lines in one or more files |
| 7 | +that match a user-provided search query (known as the *pattern*). |
| 8 | + |
| 9 | +The `grep` command takes three arguments: |
| 10 | + |
| 11 | +1. The pattern used to match lines in a file. |
| 12 | +2. Zero or more flags to customize the matching behavior. |
| 13 | +3. One or more files in which to search for matching lines. |
| 14 | + |
| 15 | +Your task is to implement the `grep` function, which should read the contents |
| 16 | +of the specified files, find the lines that match the specified pattern |
| 17 | +and then output those lines as a single string. Note that the lines should |
| 18 | +be output in the order in which they were found, with the first matching line |
| 19 | +in the first file being output first. |
| 20 | + |
| 21 | +As an example, suppose there is a file named "input.txt" with the following contents: |
| 22 | + |
| 23 | +```text |
| 24 | +hello |
| 25 | +world |
| 26 | +hello again |
| 27 | +``` |
| 28 | + |
| 29 | +If we were to call `grep "hello" input.txt`, the returned string should be: |
| 30 | + |
| 31 | +```text |
| 32 | +hello |
| 33 | +hello again |
| 34 | +``` |
| 35 | + |
| 36 | +### Flags |
| 37 | + |
| 38 | +As said earlier, the `grep` command should also support the following flags: |
| 39 | + |
| 40 | +- `-n` Print the line numbers of each matching line. |
| 41 | +- `-l` Print only the names of files that contain at least one matching line. |
| 42 | +- `-i` Match line using a case-insensitive comparison. |
| 43 | +- `-v` Invert the program -- collect all lines that fail to match the pattern. |
| 44 | +- `-x` Only match entire lines, instead of lines that contain a match. |
| 45 | + |
| 46 | +If we run `grep -n "hello" input.txt`, the `-n` flag will require the matching |
| 47 | +lines to be prefixed with its line number: |
| 48 | + |
| 49 | +```text |
| 50 | +1:hello |
| 51 | +3:hello again |
| 52 | +``` |
| 53 | + |
| 54 | +And if we run `grep -i "HELLO" input.txt`, we'll do a case-insensitive match, |
| 55 | +and the output will be: |
| 56 | + |
| 57 | +```text |
| 58 | +hello |
| 59 | +hello again |
| 60 | +``` |
| 61 | + |
| 62 | +The `grep` command should support multiple flags at once. |
| 63 | + |
| 64 | +For example, running `grep -l -v "hello" file1.txt file2.txt` should |
| 65 | +print the names of files that do not contain the string "hello". |
| 66 | + |
| 67 | +# Java Tips |
| 68 | + |
| 69 | +Since this exercise has difficulty 5 it doesn't come with any starter implementation. |
| 70 | +This is so that you get to practice creating classes and methods which is an important part of programming in Java. |
| 71 | +It does mean that when you first try to run the tests, they won't compile. |
| 72 | +They will give you an error similar to: |
| 73 | +``` |
| 74 | + path-to-exercism-dir\exercism\java\name-of-exercise\src\test\java\ExerciseClassNameTest.java:14: error: cannot find symbol |
| 75 | + ExerciseClassName exerciseClassName = new ExerciseClassName(); |
| 76 | + ^ |
| 77 | + symbol: class ExerciseClassName |
| 78 | + location: class ExerciseClassNameTest |
| 79 | +``` |
| 80 | +This error occurs because the test refers to a class that hasn't been created yet (`ExerciseClassName`). |
| 81 | +To resolve the error you need to add a file matching the class name in the error to the `src/main/java` directory. |
| 82 | +For example, for the error above you would add a file called `ExerciseClassName.java`. |
| 83 | + |
| 84 | +When you try to run the tests again you will get slightly different errors. |
| 85 | +You might get an error similar to: |
| 86 | +``` |
| 87 | + constructor ExerciseClassName in class ExerciseClassName cannot be applied to given types; |
| 88 | + ExerciseClassName exerciseClassName = new ExerciseClassName("some argument"); |
| 89 | + ^ |
| 90 | + required: no arguments |
| 91 | + found: String |
| 92 | + reason: actual and formal argument lists differ in length |
| 93 | +``` |
| 94 | +This error means that you need to add a [constructor](https://docs.oracle.com/javase/tutorial/java/javaOO/constructors.html) to your new class. |
| 95 | +If you don't add a constructor, Java will add a default one for you. |
| 96 | +This default constructor takes no arguments. |
| 97 | +So if the tests expect your class to have a constructor which takes arguments, then you need to create this constructor yourself. |
| 98 | +In the example above you could add: |
| 99 | +``` |
| 100 | +ExerciseClassName(String input) { |
| 101 | +
|
| 102 | +} |
| 103 | +``` |
| 104 | +That should make the error go away, though you might need to add some more code to your constructor to make the test pass! |
| 105 | + |
| 106 | +You might also get an error similar to: |
| 107 | +``` |
| 108 | + error: cannot find symbol |
| 109 | + assertEquals(expectedOutput, exerciseClassName.someMethod()); |
| 110 | + ^ |
| 111 | + symbol: method someMethod() |
| 112 | + location: variable exerciseClassName of type ExerciseClassName |
| 113 | +``` |
| 114 | +This error means that you need to add a method called `someMethod` to your new class. |
| 115 | +In the example above you would add: |
| 116 | +``` |
| 117 | +String someMethod() { |
| 118 | + return ""; |
| 119 | +} |
| 120 | +``` |
| 121 | +Make sure the return type matches what the test is expecting. |
| 122 | +You can find out which return type it should have by looking at the type of object it's being compared to in the tests. |
| 123 | +Or you could set your method to return some random type (e.g. `void`), and run the tests again. |
| 124 | +The new error should tell you which type it's expecting. |
| 125 | + |
| 126 | +After having resolved these errors you should be ready to start making the tests pass! |
| 127 | + |
| 128 | + |
| 129 | +# Running the tests |
| 130 | + |
| 131 | +You can run all the tests for an exercise by entering |
| 132 | + |
| 133 | +```sh |
| 134 | +$ gradle test |
| 135 | +``` |
| 136 | + |
| 137 | +in your terminal. |
| 138 | + |
| 139 | +## Source |
| 140 | + |
| 141 | +Conversation with Nate Foster. [http://www.cs.cornell.edu/Courses/cs3110/2014sp/hw/0/ps0.pdf](http://www.cs.cornell.edu/Courses/cs3110/2014sp/hw/0/ps0.pdf) |
| 142 | + |
| 143 | +## Submitting Incomplete Solutions |
| 144 | + |
| 145 | +It's possible to submit an incomplete solution so you can see how others have completed the exercise. |
0 commit comments