Skip to main content
aligned title, marked problem description as originating elsewhere
Source Link
Stephen Rauch
  • 4.2k
  • 12
  • 23
  • 36

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
```

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
```

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
aligned title, marked problem description as originating elsewhere
Source Link

Suggestion on the hackerRank solution about alphabets Compute text area given letter heights and weighta text

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
  • First line contains the weight of each alphabet
  • Second line contains the word.

Sample input

explanation each character would take 1 space and multiply it with the max weight

3 1 3 1 4 characters * 1 space *3 2 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 7 weight = 28 'z' has the max weight of 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
```

Suggestion on the hackerRank solution about alphabets and weight

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
```

Compute text area given letter heights and a text

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
```
Source Link
presci
  • 249
  • 1
  • 5

Suggestion on the hackerRank solution about alphabets and weight

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
```