[haskell-br] HackerRank

Jean Lopes hawu.bnu at gmail.com
Sun Jan 4 19:42:45 UTC 2015


problema: https://www.hackerrank.com/challenges/find-digits
import Data.Char
import Control.Monad

intDigits :: Integer -> [Int]
intDigits n = map (\x -> read [x] :: Int) (show n)

charDigits :: [Int] -> [Char]
charDigits = map (\x -> intToDigit x)

digits :: Integer -> [Char]
digits = charDigits . intDigits

divide :: Integer -> Char -> Bool
divide _ '0' = False
divide n c = isMultiple
    where digit = toInteger $ digitToInt c
          nModDigit = n `mod` digit
          isMultiple = nModDigit == 0

findDigit :: Integer -> Int
findDigit n = foldl (\a b -> a + (if div b then 1 else 0)) 0 list
    where div = divide n
          list = digits n

getStrings :: Integer -> [IO String]
getStrings n
    | n <= 0 = []
    | otherwise = getLine : getStrings (n - 1)

getIntegers :: [String] -> [Integer]
getIntegers [] = []
getIntegers (x:xs) = readInteger x : getIntegers xs

readInteger :: String -> Integer
readInteger = read

main = do
    qtd <- getLine
    valStr <- sequence $ getStrings (readInteger qtd)
    let valores = getIntegers valStr
        digitsFound = map (findDigit) valores
    mapM (print) digitsFound


Em 4 de janeiro de 2015 17:41, Jean Lopes <hawu.bnu at gmail.com> escreveu:

> Legal esse site, finalmente consegui resolver o primeiro de lógica em
> Haskell!
> tive bastante trabalho com a parte de IO (não sabia nada ainda, agora sei
> quase nada kk)
> tive que pesquisar também sobre como transformar um número em string..
> pelo que vi da pra melhorar bastante ainda! alguem tem dicas ?? segue o
> código
>
> Em 29 de dezembro de 2014 21:14, Luis Fernando Milano Oliveira <
> luismilanooliveira at gmail.com> escreveu:
>
> Legal, Sérgio!
>> Eu não conhecia não, valeu pela dica!
>>
>> [ ] s
>>
>> Em 29 de dezembro de 2014 18:19, Sergio costa <skosta at gmail.com>
>> escreveu:
>>
>> sim, por isso achei legal :) quase não passava em estrutura de dados,
>>> rs...
>>>
>>> Em 29 de dezembro de 2014 17:02, Rafael Barros Félix <
>>> rafaelbfs1 at gmail.com> escreveu:
>>>
>>> sim
>>>> um entrevistador da Inglaterra até me mandou tarefas desse site. E eu
>>>> descobri que eu sei bem menos do haskell do que eu achava
>>>>
>>>> Em 29 de dezembro de 2014 16:26, Sergio costa <skosta at gmail.com>
>>>> escreveu:
>>>>
>>>>> Prezados
>>>>>
>>>>> Conhecem este site ? Tem diversos desafios em programação funcional.
>>>>>
>>>>> https://www.hackerrank.com/
>>>>>
>>>>> abs
>>>>> --
>>>>> Prof. Sérgio Souza Costa
>>>>> https://sites.google.com/site/skosta/
>>>>>
>>>>> _______________________________________________
>>>>> haskell-br mailing list
>>>>> haskell-br at haskell.org
>>>>> http://www.haskell.org/mailman/listinfo/haskell-br
>>>>>
>>>>>
>>>>
>>>> _______________________________________________
>>>> haskell-br mailing list
>>>> haskell-br at haskell.org
>>>> http://www.haskell.org/mailman/listinfo/haskell-br
>>>>
>>>>
>>>
>>>
>>> --
>>> Prof. Sérgio Souza Costa
>>> https://sites.google.com/site/skosta/
>>>
>>> _______________________________________________
>>> haskell-br mailing list
>>> haskell-br at haskell.org
>>> http://www.haskell.org/mailman/listinfo/haskell-br
>>>
>>>
>>
>> _______________________________________________
>> haskell-br mailing list
>> haskell-br at haskell.org
>> http://www.haskell.org/mailman/listinfo/haskell-br
>>
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/haskell-br/attachments/20150104/62ad147e/attachment.html>


More information about the haskell-br mailing list