patternMinor
Hackerrank, Service Lane in Haskell
Viewed 0 times
hackerranklaneservicehaskell
Problem
Problem statement
Calvin is driving his favorite vehicle on the 101 freeway. He notices that the check engine light of his vehicle is on, and he wants to service it immediately to avoid any risks. Luckily, a service lane runs parallel to the highway. The length of the highway and the service lane is \$N\$ units. The service lane consists of \$N\$ segments of equal length and different width.
Calvin can enter to and exit from any segment. Let's call the entry segment as index \$i\$ and the exit segment as index j. Assume that the exit segment lies after the entry segment (\$i\$≤\$j\$) and 0≤\$i\$. Calvin has to pass through all segments from index \$i\$ to index \$j\$ (both inclusive).
Calvin has three types of vehicles - bike, car, and truck - represented by \$1\$, \$2\$ and \$3\$, respectively. These numbers also denote the width of the vehicle.
You are given an array width of length \$N\$, where \$width[k]\$ represents the width of the kth segment of the service lane. It is guaranteed that while servicing he can pass through at most \$1000\$ segments, including the entry and exit segments.
Given the entry and exit point of Calvin's vehicle in the service lane, output the type of the largest vehicle which can pass through the service lane (including the entry and exit segments).
Input Format
The first line of input contains two integers, \$N\$ and \$T\$, where \$N\$ denotes the length of the freeway and \$T\$ the number of test cases. The next line has \$N\$ space-separated integers which represent the width array.
\$T\$ test cases follow. Each test case contains two integers, \$i\$ and \$j\$, where \$i\$ is the index of the segment through which Calvin enters the service
Calvin is driving his favorite vehicle on the 101 freeway. He notices that the check engine light of his vehicle is on, and he wants to service it immediately to avoid any risks. Luckily, a service lane runs parallel to the highway. The length of the highway and the service lane is \$N\$ units. The service lane consists of \$N\$ segments of equal length and different width.
Calvin can enter to and exit from any segment. Let's call the entry segment as index \$i\$ and the exit segment as index j. Assume that the exit segment lies after the entry segment (\$i\$≤\$j\$) and 0≤\$i\$. Calvin has to pass through all segments from index \$i\$ to index \$j\$ (both inclusive).
Calvin has three types of vehicles - bike, car, and truck - represented by \$1\$, \$2\$ and \$3\$, respectively. These numbers also denote the width of the vehicle.
You are given an array width of length \$N\$, where \$width[k]\$ represents the width of the kth segment of the service lane. It is guaranteed that while servicing he can pass through at most \$1000\$ segments, including the entry and exit segments.
- If \$width[k]=1\$, only the bike can pass through the \$k\$th segment.
- If \$width[k]=2\$, the bike and the car can pass through the \$k\$th segment.
- If \$width[k]=2\$, the bike and the car can pass through the \$k\$th segment.
Given the entry and exit point of Calvin's vehicle in the service lane, output the type of the largest vehicle which can pass through the service lane (including the entry and exit segments).
Input Format
The first line of input contains two integers, \$N\$ and \$T\$, where \$N\$ denotes the length of the freeway and \$T\$ the number of test cases. The next line has \$N\$ space-separated integers which represent the width array.
\$T\$ test cases follow. Each test case contains two integers, \$i\$ and \$j\$, where \$i\$ is the index of the segment through which Calvin enters the service
Solution
What an utterly bizarre problem statement. For one thing bikes don't have check engine lights... I guess this boils down to “find the minimum value within a list subsequence” which you've got nailed.
You have the right idea with
Note that I moved the logic for including the end bound into this function. This makes more sense to me with a new name like
You can clean up
I'd use
You have the right idea with
takePart but there's no need to flip the list turnways. The clue is in the name you picked, take.betweenInclusive :: Int -> Int -> [a] -> [a]
betweenInclusive start end = take (end - start + 1) . drop startNote that I moved the logic for including the end bound into this function. This makes more sense to me with a new name like
betweenInclusive, as it's sort of a utility function on lists. I wouldn't bother with all of the error checking you were doing either, the Constraints section is sufficient to guarantee you'll only receive valid bounds.You can clean up
readTwoInts a bit too, it's odd to mix both (:) and literal lists ([x]) in a single pattern match.readTwoInts :: String -> (Int, Int)
readTwoInts line = (i, j)
where [i, j] = readInts lineI'd use
fmap (`) to reduce the number of intermediate values present in main` as well.main :: IO ()
main = do (_, tests) getLine
width getLine
bounds getLine)
mapM (print . maxVehicleWidth width) boundsCode Snippets
betweenInclusive :: Int -> Int -> [a] -> [a]
betweenInclusive start end = take (end - start + 1) . drop startreadTwoInts :: String -> (Int, Int)
readTwoInts line = (i, j)
where [i, j] = readInts linemain :: IO ()
main = do (_, tests) <- readTwoInts <$> getLine
width <- readInts <$> getLine
bounds <- replicateM tests (readTwoInts <$> getLine)
mapM (print . maxVehicleWidth width) boundsContext
StackExchange Code Review Q#86978, answer score: 3
Revisions (0)
No revisions yet.