DEV Community

Discussion on: Daily Challenge #137 - Help the Bookseller

Collapse
 
charukiewicz profile image
Christian • Edited

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)]