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:
lines
split the complete input, asString
on the line break, resulting in a[String]
(list of strings).tail
is here just to ignore the first line (normally hackerrank tests have a number denoting the count of following elements)yesNo
transalte a boolean to a string YES/NOunlines
joins 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:
words
split the complete input, asString
on any blank character, resulting in a[String]
(list of strings).tail
is here just to ignore the first number (normally hackerrank tests have a number denoting the count of following elements)fmap read
convert each string to a numberfmap show
convert each number back to a stringunwords
joins 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
n
space-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
.
|
|