2
\$\begingroup\$

I have solved the designer pdf question with haskell. The code works. I am posting the code here to know how I could have done it better.

  • First line contains the weight of each alphabet
  • Second line contains the word.

Sample input

1 3 1 3 1 4 1 3 2 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 7
zaba

Output

28

Explanation
each character would take 1 space and multiply it with the max weight
4 characters * 1 space * 7 weight = 28
'z' has the max weight of 7.

Code

import Data.List
import Data.Maybe

getintval::(Maybe Int) -> Int
getintval Nothing = 1
getintval (Just x) = x

solve'::[Char]->[(Char, Int)] -> Int
solve' ch lst = k $  map getintval $ map (\x -> finder' x) ch
  where
  k::[Int] -> Int
  k fb = (*) (length ch)   $ foldl (\acc x -> max acc x) 1 fb
  finder'::Char -> Maybe Int
  finder' g =  case i of
            Just(x1,x2) -> Just x2
            Nothing -> Just 1
            where i = find(\(val1, val2) -> val1 == g) lst


solve::[Char] -> [Char] -> Int
solve wght val = solve' val rec
  where
  rec::[(Char, Int)]
  rec = zipWith (\x y -> (x, y)) ['a'..'z'] word1
  word1::[Int]
  word1 = map(read::String->Int) $ words wght

main::IO()
main = do
  weight <- getLine
  pdfstr <- getLine
  putStr . show $ solve weight pdfstr
\$\endgroup\$
2
  • \$\begingroup\$ Welcome to Code Review. I have been hesitating to use a block quote for the problem description as it is not even close to a verbatim quote. At least for titling, revisit How do I ask a Good Questio? \$\endgroup\$
    – greybeard
    Commented Nov 1, 2019 at 6:08
  • \$\begingroup\$ A concern not really addressed by the nice answer from Gurkenglas: the initial hackerRank problem is quite explicitly about character heights (as in the on-screen vertical dimension). However, your source code is about weight which is confusing. For example, the first argument of solve could be called heightsLine or maybe hLine for brevity, not weight orwght . In a similar fashion, it is unusual to use ch (first argument of solve') for an input word. Normally ch would stand for a single CHaracter. \$\endgroup\$
    – jpmarinier
    Commented Nov 3, 2019 at 20:57

1 Answer 1

3
\$\begingroup\$

getintval is just fromMaybe 1. finder always produces Just - you probably meant to map Nothing to Nothing. finder' is just lookup. foldl (\acc x -> max acc x) 1 is just maximum (so long as ch is never empty). k can be inlined.

solve' :: [Char] -> [(Char, Int)] -> Int
solve' ch lst = (*length ch) $ maximum $ map (fromMaybe 1 . (`lookup` lst)) ch

zipWith (\x y -> (x, y)) is just zip. read's type can be deduced. rec and word1 can be inlined.

solve :: [Char] -> [Char] -> Int
solve wght val = solve' val $ zip ['a'..'z'] $ map read $ words wght

mapMaybe throws away invalid characters. We can flatten the call tree by letting main assemble the pieces.

solve :: [Char] -> [(Char, Int)] -> Int
solve ch lst = (*length ch) $ maximum $ mapMaybe (`lookup` lst) ch

parse :: [Char] -> [(Char, Int)]
parse = zip ['a'..'z'] . map read . words

main :: IO ()
main = do
  weight <- getLine
  pdfstr <- getLine
  print $ solve pdfstr $ parse weight 
\$\endgroup\$
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.