The interact function
The interact function takes a function of type String -> String as its argument.
The entire input from the standard input device is passed to this function as
its argument, and the resulting string is output on the standard output device.
Predicate on strings
Example: Balanced Brackets
Input:
|
|
Output:
|
|
The first line contains a single integer n, the number of strings.
Each of the next n lines contains a single string s.
In this example:
linessplit the complete input, asStringon the line break, resulting in a[String](list of strings).tailis here just to ignore the first line (normally hackerrank tests have a number denoting the count of following elements)yesNotransalte a boolean to a string YES/NOunlinesjoins the resulting list of string, placing a line break between the strings.
|
|
List of numbers
Example: Closest Numbers
Input:
|
|
Output:
|
|
We are provided with a list of numbers and our algorithm must produce another list of numbers (not necessarily of the same length).
In this example:
wordssplit the complete input, asStringon any blank character, resulting in a[String](list of strings).tailis here just to ignore the first number (normally hackerrank tests have a number denoting the count of following elements)fmap readconvert each string to a numberfmap showconvert each number back to a stringunwordsjoins the resulting list of string, placing a space between the strings.
|
|
T test cases, each test case has 2 lines
The first line contains t, the number of test cases.
The next t pairs of lines each represent a test case.
- The first line contains
n, the number of elements in the arrayarr. - The second line contains
nspace-separated integers
We can ignore the first line (we will read to the end of file) and the lines
with n. So we can just read the lines with the actual array and split by spaces.
We can even ignore odd lines, this is why filter fst, where fst is
alternately True and False.
|
|