Skip to content
loading...

re: Daily Challenge #137 - Help the Bookseller VIEW POST

FULL DISCUSSION
 

Haskell, quick and dirty.

l = [ "ABART 20", "CDXEF 50", "BKWRK 25", "BTSQZ 89", "DRTYM 60" ]
m = [ 'A', 'B', 'C', 'W' ]

findBooks :: [String] -> [Char] -> [(Char,Int)]
findBooks invInput searchInput =
    (\searchCode -> (,) searchCode $
        foldr (\(invCode, count) runningTotal ->
                  if searchCode == invCode then
                      runningTotal + count
                  else
                      runningTotal
              )
            0
            invList
    ) <$> searchInput
      where
        invList = (\(label:count:_) ->
                        ( head label
                        , read count :: Int
                        )
                  ) <$> words <$> invInput

Output:

*Main> findBooks l m
[('A',20),('B',114),('C',50),('W',0)]
code of conduct - report abuse