Menu Close

Búsqueda en los dígitos de pi

El fichero Digitos_de_pi.txt contiene el número pi con un millón de decimales; es decir,

   3.1415926535897932384626433832 ... 83996346460422090106105779458151

Definir la función

   posicion :: String -> IO (Maybe Int)

tal que (posicion n) es (Just k) si k es la posición de n en la sucesión formada por un millón dígitos decimales del número pi y Nothing si n no ocurre en dicha sucesión. Por ejemplo,

   λ> posicion "15"
   Just 3
   λ> posicion "2017"
   Just 8897
   λ> posicion "022017"
   Just 382052
   λ> posicion "14022017"
   Nothing
   λ> posicion "999999"
   Just 762
   λ> posicion "458151"
   Just 999995

Nota. Se puede comprobar la función mediante The pi-search page o Pi search engine.

Soluciones

import Data.List (isPrefixOf)
 
posicion :: String -> IO (Maybe Int)
posicion ns = do
  ds <- readFile "Digitos_de_pi.txt"
  return (posicionEnLista (drop 2 ds) ns)
 
posicionEnLista :: Eq a => [a] -> [a] -> Maybe Int
posicionEnLista xs ys = aux xs 1
  where aux [] _ = Nothing
        aux (x:xs) n | ys `isPrefixOf` (x:xs) = Just n
                     | otherwise              = aux xs (n+1)
Medio

6 soluciones de “Búsqueda en los dígitos de pi

  1. eliguivil
    posicion :: String -> IO (Maybe Int)
    posicion ys = do
      tres:coma:xs <- readFile "Digitos_de_pi.txt"
      return $ estaEnPosicion 1 ys xs (length ys) 
      where
        estaEnPosicion _ _  [] _ = Nothing
        estaEnPosicion n ys xs l
          | take l xs == ys = Just n
          | otherwise       = estaEnPosicion (n+1) ys (tail xs) l
  2. albcercid
    import System.IO.Unsafe
    import Data.List (isPrefixOf)
     
    posicion :: String -> Maybe Int
    posicion xs = aux xs lista (-1)
       where
         aux _ [] _ = Nothing
         aux (x:xs) (y:ys) n
           | x == y && isPrefixOf xs ys = Just n
           | otherwise                  = aux (x:xs) ys (n+1)
     
    lista :: String
    lista = unsafePerformIO . readFile $ "Digitos_de_pi.txt"
  3. enrnarbej
    import Data.List (isInfixOf, isPrefixOf)
     
    posicion :: String -> IO (Maybe Int)
    posicion xs = do
      a <- readFile "Digitos_de_pi.txt"
      return (if (not . isInfixOf xs) a
              then Nothing
              else buscaP a xs 0)
     
    buscaP :: String -> String -> Int -> Maybe Int
    buscaP a xs n
      | (not . isPrefixOf xs) a = buscaP (tail a) xs (n+1)
      | otherwise               = Just (n-1)
  4. Juanjo Ortega (juaorture)
    import Data.List
     
    posicion :: String -> IO (Maybe Integer)
    posicion xs = do (y:z:ys) <- readFile "Digitos_de_pi.txt"
                     return (aux xs ys 1)
            where aux _  []       n = Nothing
                  aux xs a@(y:ys) n | isPrefixOf xs a = Just n
                                    | otherwise       = aux xs ys (n+1)
  5. Chema Cortés
    import Data.List
     
    posicion :: String -> IO (Maybe Int)
    posicion n = do _:xs <- readFile "Digitos_de_pi.txt"
                    return $ findIndex (n `isPrefixOf`) (tails xs)
  6. cescarde
    digPi = "Digitos_de_pi.txt"
     
    posicion :: String -> IO (Maybe Int)
    posicion xs = do ys <- readFile digPi
                     return $ encuentra xs ys
     
    encuentra :: Eq a => [a] -> [a] -> Maybe Int
    encuentra xs ys | isInfixOf xs ys = Just $ length $ takeWhile (/=xs) (separaEn xs ys)
                    | otherwise = Nothing
     
    separaEn :: [a] -> [a] -> [[a]]
    separaEn xs []  = []
    separaEn xs (y:ys) = take n (y:ys) : separaEn xs ys
             where n = length xs

Escribe tu solución

Este sitio usa Akismet para reducir el spam. Aprende cómo se procesan los datos de tus comentarios.