Хламидомонада / Говнокод #20068 Ссылка на оригинал

0

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  16. 16
  17. 17
  18. 18
  19. 19
  20. 20
  21. 21
  22. 22
  23. 23
  24. 24
  25. 25
  26. 26
  27. 27
  28. 28
  29. 29
  30. 30
  31. 31
  32. 32
  33. 33
  34. 34
  35. 35
  36. 36
  37. 37
  38. 38
  39. 39
  40. 40
  41. 41
  42. 42
  43. 43
  44. 44
  45. 45
  46. 46
  47. 47
  48. 48
  49. 49
  50. 50
  51. 51
  52. 52
  53. 53
  54. 54
  55. 55
  56. 56
  57. 57
  58. 58
  59. 59
data TTree k v =
  TNode {
    _key :: !k
  , _val :: !(Maybe v)
  , _eq :: !(TTree k v)
  , _left :: !(TTree k v)
  , _right :: !(TTree k v)
  , _height :: !Int
  }
  | TTNil
  deriving (Show, Generic)
instance (Binary k, Binary v) => Binary (TTree k v)

insertWith' :: (Ord k)
            => (v -> v -> v) -- ^ Conflict resolution function
            -> [k]           -- ^ Key
            -> Int           -- ^ Length of the key
            -> v             -- ^ Value
            -> TTree k v     -- ^ Tree
            -> TTree k v
insertWith' f k1@(k:kt) h v t =
  case t of
    TTNil ->
      insertWith' f k1 h v $ TNode {
          _key = k
        , _eq = TTNil
        , _left = TTNil
        , _right = TTNil
        , _val = Nothing
        , _height = h
        }
    node@TNode{_key=k0, _height=h0, _val=v0, _eq=eq0, _left=left0, _right=right0} ->
      case compare k0 k of
        EQ | null kt ->
               node {
                 _val = Just $ maybe v (flip f $ v) v0
               }
           | True ->
               node {
                 _eq = insertWith' f kt (h-1) v eq0
               , _height = max h h0
               }
        GT ->
           node {
             _left = insertWith' f k1 h v left0
           , _height = max h h0
           }
        LT ->
           node {
             _right = insertWith' f k1 h v right0
           , _height = max h h0
           }
{-# SPECIALIZE insertWith' :: (v -> v -> v) 
                           -> [Char] 
                           -> Int
                           -> v 
                           -> TTree Char v
                           -> TTree Char v
  #-}

а почему бы не использовать несбалансированное тернанрое дерево для индекса
вроде ничего стра
Out of memory: Kill process 2987 (govno) score 265 or sacrifice child

CHayT CHayT, (Updated )

Комментарии (18, +18)

Хламидомонада / Говнокод #20004 Ссылка на оригинал

0

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
import qualified Data.ByteString.Lazy.Char8 as BS

readLineOfInts :: BS.ByteString -> [Int]
readLineOfInts str = case BS.readInt str of
          Nothing -> []
          Just (i, rest) -> i : if BS.null rest then [] else readLineOfInts (BS.tail rest)

То чувство, когда map read . words не проходит по таймауту.

Yuuri Yuuri, (Updated )

Комментарии (20, +20)

Хламидомонада / Говнокод #19959 Ссылка на оригинал

0

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
instance Applicative (Sample m) where
  pure a = Val a

  Val{_unVal=f} <*> a = fmap f a
  Fork{..} <*> a = Fork {
      _next = \x -> (_next x) <*> a
    , ..
    }
  Zero <*> _ = Zero
  Random{..} <*> a = Random { -- Crazy-ass weirdo haskeller, why did you define instance Random for ->?!!
      _next = \x -> (_next x) <*> a
    , ..
    }

CHayT CHayT, (Updated )

Комментарии (6, +6)

Хламидомонада / Говнокод #19672 Ссылка на оригинал

0

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  16. 16
  17. 17
  18. 18
  19. 19
  20. 20
  21. 21
  22. 22
  23. 23
  24. 24
  25. 25
  26. 26
  27. 27
  28. 28
  29. 29
  30. 30
  31. 31
  32. 32
  33. 33
  34. 34
  35. 35
  36. 36
  37. 37
  38. 38
  39. 39
  40. 40
  41. 41
  42. 42
  43. 43
  44. 44
  45. 45
  46. 46
  47. 47
  48. 48
  49. 49
  50. 50
  51. 51
  52. 52
  53. 53
  54. 54
  55. 55
  56. 56
  57. 57
  58. 58
  59. 59
  60. 60
  61. 61
  62. 62
  63. 63
  64. 64
  65. 65
  66. 66
  67. 67
  68. 68
  69. 69
  70. 70
  71. 71
  72. 72
  73. 73
  74. 74
  75. 75
  76. 76
  77. 77
  78. 78
  79. 79
  80. 80
  81. 81
  82. 82
  83. 83
  84. 84
  85. 85
  86. 86
  87. 87
  88. 88
  89. 89
  90. 90
  91. 91
  92. 92
  93. 93
  94. 94
  95. 95
  96. 96
  97. 97
  98. 98
module Control.Monad.Warning (
   errorToWarning
 , errorsToWarnings
 , justW
 , rightW
 , WarningT(..)
 , MonadWarning(..)
 , module Control.Monad.Except
 , module Control.Monad.Writer
 )
 where

import Control.Applicative
import Control.Monad.Except
import Control.Monad.Writer
import Control.Monad.Reader
import Data.Monoid

newtype WarningT w e m a = WarningT { runWarningT ∷ w → m (w, Either e a) }

class (Monad m) ⇒ MonadWarning w e m | m → w e where
  warning ∷ w → m ()
  throwW  ∷ e → m a
  catchW  ∷ m a → (e → m a) → m a

instance (Functor m) ⇒ Functor (WarningT w e m) where
  fmap f a = WarningT $ \w → let f' (w', a') = (w', (fmap f) a')
                             in fmap f' $ runWarningT a w

instance (Applicative m) ⇒ Applicative (WarningT w e m) where
  pure a = WarningT $ \w → pure (w, Right a)
  f <*> a = WarningT $ \w → runWarningT f w 
       (w', f') = 
       (w'', a') = runWarningT a w'
     in case (f', a') of
       (Right f'', Right a'') → undefined --runWarningT (f'' a'') w''
       (Left l, _) → pure (w', Left l)
       (_, Left l) → pure (w'', Left l)

instance (Monad m, Monoid w) ⇒ Monad (WarningT w e m) where
  return a = WarningT $ \w → return (w, Right a)
  
  a >>= b = WarningT $ \w → do
    (w', e) ← runWarningT a w
    case e of
     Right r → runWarningT (b r) w'
     Left l → return (w', Left l)

  fail = WarningT . fail

instance (Monad m, Monoid w) ⇒ MonadWarning w e (WarningT w e m) where
  warning w' = WarningT $ \w → return (w `mappend` w', Right ())

  throwW e = WarningT $ \w → return (w, Left e)
  
  catchW a f = WarningT $ \w → do
    (w', e) ← runWarningT a w
    case e of
     Right e' → return (w', Right e')
     Left e' → runWarningT (f e') w'

instance (MonadWarning w e m) ⇒ MonadError e m where
  throwError = throwW
  catchError = catchW

instance (Monoid w, MonadWarning w e m) ⇒ MonadWriter w m where
  tell = warning

instance MonadTrans (WarningT w e) where
  lift a = WarningT $ \w → do
    a' ← a
    return $ (w, Right a')

instance (Monoid w, MonadIO m) ⇒ MonadIO (WarningT w e m) where
  liftIO = lift . liftIO

instance (MonadReader r m, Monoid w) ⇒ MonadReader r (WarningT w e m) where
  ask = lift ask
  -- TODO: Check and test it.
  local f a = WarningT $ \w → local f $ runWarningT a w
  
errorToWarning ∷ (Monoid w, MonadWarning w e m) ⇒ (e → w) → (e → m a) → m a → m a
errorToWarning f g a = catchW a (\e → warning (f e) >> g e)

errorsToWarnings ∷ (Monoid w, MonadWarning w e m) ⇒ (e → w) → [m a] → m [a]
errorsToWarnings f = foldl go (return [])
  where go r a = errorToWarning f (const r) $ do
          a' ← a
          r' ← r
          return $ a' : r'--
          
justW ∷ (MonadWarning w e m) ⇒ e → Maybe a → m a
justW _ (Just x) = return x
justW e Nothing  = throwW e

rightW ∷ (MonadWarning w e m) ⇒ (e' → e) → Either e' a → m a
rightW _ (Right x) = return x
rightW f (Left e) = throwW (f e)

выкладываю перед выпиливанием этого говна

CHayT CHayT, (Updated )

Комментарии (329, +329)

Хламидомонада / Говнокод #19670 Ссылка на оригинал

0

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  16. 16
  17. 17
  18. 18
  19. 19
  20. 20
  21. 21
  22. 22
  23. 23
  24. 24
  25. 25
  26. 26
  27. 27
  28. 28
  29. 29
  30. 30
  31. 31
  32. 32
  33. 33
  34. 34
  35. 35
  36. 36
  37. 37
  38. 38
  39. 39
  40. 40
  41. 41
  42. 42
  43. 43
  44. 44
  45. 45
  46. 46
  47. 47
  48. 48
  49. 49
  50. 50
  51. 51
  52. 52
  53. 53
  54. 54
  55. 55
  56. 56
  57. 57
  58. 58
  59. 59
  60. 60
  61. 61
  62. 62
  63. 63
  64. 64
  65. 65
  66. 66
  67. 67
  68. 68
  69. 69
  70. 70
  71. 71
  72. 72
  73. 73
  74. 74
  75. 75
  76. 76
  77. 77
  78. 78
  79. 79
  80. 80
  81. 81
  82. 82
{-# LANGUAGE FlexibleInstances #-}
module Connect where 

import Data.List

data Color = Black | White deriving (Show, Eq)

data Start = Begin | End deriving (Show, Eq)

data Tree a = Node a [Tree a] deriving  (Eq)

instance Show (Tree (Int,Int)) where
    show = showTree 0

showTree :: Int -> Tree (Int,Int) -> String
showTree n (Node a s) = show a ++ "\n" ++ replicate n ' ' ++ concatMap (showTree (n+1)) s

elemTree :: Eq a => a -> Tree a -> Bool
elemTree e (Node a []) = if a == e then True else False
elemTree e (Node a s) = if a == e then True else any (e `elemTree`) s 
    
resultFor :: [String] -> Maybe Color
resultFor = check

charToColor :: Char -> Color
charToColor 'X' = Black
charToColor 'O' = White
charToColor _ = error "Bad data!"

check :: [[Char]] -> Maybe Color
check s = if (null iob || null ioe) && (null ixb || null ixe) 
            then Nothing
            else let whb = any (\t -> any (`elemTree` t) ioe) $ map (go White Begin s []) iob 
                     whe = any (\t -> any (`elemTree` t) iob) $ map (go White End s []) ioe
                     blb = any (\t -> any (`elemTree` t) ixe) $ map (go Black Begin s []) ixb
                     ble = any (\t -> any (`elemTree` t) ixb) $ map (go Black End s []) ixe
                 in if whb || whe then Just White else if blb || ble then Just Black else Nothing
    where 
        iob = map (\y -> (0,y)) $ elemIndices 'O' (s !! 0)
        ioe = map (\y -> (length s,y)) $ elemIndices 'O' (last s)
        ixb = map (\x -> (x,0)) $ elemIndices 'X' (map head s)
        ixe = map (\x -> (x,length (s!!0))) $ elemIndices 'X' (map last s)

search :: Foldable t =>
          Color
          -> [[Char]] -> t (Int, Int) -> (Int, Int) -> Maybe [(Int, Int)]
search color arr from (cx, cy) = (\x -> if null x then Nothing else Just x) $ map fst $ filter snd $ concatMap 
    (\x -> map 
        (\y -> testCell color arr from (cx, cy) (x,y)) 
        (filter (\yy -> yy >= 0 && yy < length (arr!!0)) [cy-1, cy, cy+1])) 
    (filter (\xx -> xx >= 0 && xx < length arr) [cx-1,cx,cx+1])

testCell :: Foldable t =>
            Color
            -> [[Char]]
            -> t (Int, Int)
            -> (Int, Int)
            -> (Int, Int)
            -> ((Int, Int), Bool)
testCell color arr from (cx, cy) (x,y)
    |x == cx && y == cy = ((x,y),False)
    |cx - x == 1 && cy - y == 1 = ((x,y),False)
    |x - cx == 1 && y - cy == 1 = ((x,y),False)
    |(x,y) `elem` from = ((x,y),False) 
    |arr !! x !! y /= '.' && color == charToColor (arr !! x !! y) = ((x,y),True)
    |otherwise = ((x,y),False) 

go :: Color
      -> Start
      -> [[Char]]
      -> [(Int, Int)]
      -> (Int, Int)
      -> Tree (Int, Int)
go c s arr from (x,y) 
    |(c,s) == (White, Begin) && x == length arr - 1 = Node (x,y) []
    |(c,s) == (White, End) && x == 0 = Node (x,y) []
    |(c,s) == (Black, Begin) && y == length (arr !! 0) -1 = Node (x,y) []
    |(c,s) == (Black, End) && y == 0 = Node (x,y) []
    |otherwise = let f = search c arr from (x,y)
                 in case f of 
                         Nothing -> Node (x,y) []
                         Just r -> Node (x,y) $ map (go c s arr ((x,y):from)) r

Abbath Abbath, (Updated )

Комментарии (94, +94)

Хламидомонада / Говнокод #18739 Ссылка на оригинал

0

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
'(haskell-ask-also-kill-buffers nil)
 '(haskell-interactive-popup-errors nil)
 '(haskell-process-suggest-add-package nil)
 '(haskell-process-suggest-language-pragmas nil)
 '(haskell-process-suggest-no-warn-orphans nil)
 '(haskell-process-suggest-overloaded-strings nil)

не говнокод, но говнофичи
дёрнуло меня обновить haskell-mode
я вас скажу, это просто ад и Израль, глад и мор, и семь казней египетских

CHayT CHayT, (Updated )

Комментарии (71, +71)

Хламидомонада / Говнокод #17746 Ссылка на оригинал

0

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  16. 16
  17. 17
  18. 18
  19. 19
  20. 20
  21. 21
  22. 22
  23. 23
  24. 24
  25. 25
  26. 26
  27. 27
  28. 28
  29. 29
  30. 30
  31. 31
  32. 32
  33. 33
  34. 34
  35. 35
  36. 36
  37. 37
  38. 38
  39. 39
  40. 40
  41. 41
  42. 42
  43. 43
  44. 44
  45. 45
  46. 46
  47. 47
  48. 48
  49. 49
  50. 50
  51. 51
  52. 52
  53. 53
  54. 54
  55. 55
  56. 56
--Поиск минимальной выпуклой оболочки
import Data.List; import Data.Ord
--общие функции и типы
data Point = P{x::Float,y::Float}
	deriving (Show,Eq) 
	
getRotate a b c = baX * cbY - baY * cbX
	where baX = x b - x a; baY = y b - y a;
		  cbX = x c - x b; cbY = y c - y b;
		 
sortFunc a b c 
	|k < 0  = LT
	|k == 0 = compare (long a c) (long a b) 
	|k > 0  = GT
		where k = getRotate a b c

long a b = (x b - x a)*(x b - x a) + (y b - y a)*(y b - y a)
		
getLeftPoint = minimumBy (comparing x)
--Джарвис
getMBOJarvis l = mboJ fp l fp
	where fp = getLeftPoint l		
		
mboJ current list fp 
	|getRotate current next fp > 0   = []
	|True                            = current : mboJ next listWOC fp
		where listWOC = filter ((/=)current) list;
			  next    = minimumBy (sortFunc current) listWOC;
--Грехем			
getMBOGragam = tail.throwGraham.sortGraham 

sortGraham list = fp:sortBy (sortFunc fp) list
	where  fp = getLeftPoint list
		   
throwGraham (f:s:t) = mboG (s:f:[]) t
		   
mboG <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="dfb9ac9f">[email protected]</a>(f:s:st) <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="80f3eec0">[email protected]</a>(h:t)
	|sortFunc s f h == GT = mboG (s:st) sn
	|True                 = mboG(h:fs) t
	
mboG <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d0b6a390">[email protected]</a>(f:st) <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="76051836">[email protected]</a>(h:t)   = mboG(h:fs) t
	
mboG l [] = l
--тесты		     
testList1 = [P 0 (-1), P (-1) 0, P 0 1,P 1 0,P (-0.5) (-0.5),P 0.5 (-0.5),P (-0.5) 0.5,P 0.5 0.5,P 0 0]
		  
testList2 = [P 0 0, P 1 0, P 0 1,P 2 0,P 1 1,P 0 2,P 2 1,P 1 2,P 2 2]
	
	
testJ1  = mapM_ print $ getMBOJarvis testList1		
		
testG1  = mapM_ print $ getMBOGragam testList1

testJ2  = mapM_ print $ getMBOJarvis testList2		
		
testG2  = mapM_ print $ getMBOGragam testList2

Haskell
[сарказм]
Как я могу идти против моды - не заливать этих французских лаб и не выпивать чаю?

Выкладываю, что бы порадовать своего кота Барсика. Барсик, покойся с миром.

А спонсор этого говна - компания "Потролль препода". "Потролль препода" - пиши лабы на хаскелле
[/сарказм]

kegdan kegdan, (Updated )

Комментарии (60, +60)

Хламидомонада / Говнокод #16405 Ссылка на оригинал

0

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  16. 16
  17. 17
  18. 18
  19. 19
  20. 20
  21. 21
  22. 22
  23. 23
  24. 24
  25. 25
map_of_enemy :: [[Int]] -> [[Int]]
map_of_enemy [] = []
map_of_enemy list = (iniciar (0) (0) (list))

iniciar :: Int -> Int -> [[Int]] -> [[Int]]
iniciar a b list = if(a == (length list)-1) then [rango a 0 list]
               else [rango a 0 list]++[(iniciar (a+1) 0 list)]

rango :: Int -> Int -> [[Int]] -> [Int]
rango a b list = if (b==(length list)-1 && (((list!!a!!0)==(list!!b!!0)) &&     ((list!!a!!1)==(list!!b!!1)) && ((list!!a!!2)==(list!!b!!2))))
             then [0]
             else if (b==(length list)-1 && (((list!!a!!0)/=(list!!b!!0)) || ((list!!a!!1)/=(list!!b!!1)) || ((list!!a!!2)/=(list!!b!!2))))
             then (rango2 a (list!!b) list)
             else if (((list!!a!!0)==(list!!b!!0)) && ((list!!a!!1)==(list!!b!!1)) && ((list!!a!!2)==(list!!b!!2)))
             then [0]++(rango a (b+1) list)
             else (rango2 a (list!!b) list)++(rango a (b+1) list)

rango2 :: Int -> [Int] -> [[Int]] -> [Int]
rango2 a b list = if ((verif [(list!!a!!0)+(list!!a!!2),(list!!a!!1)+(list!!a!!2)] [(list!!a!!0)-(list!!a!!2),(list!!a!!1)-(list!!a!!2)] (b))) then [1]
              else [0]

verif a b c = if (((c!!0) < (a!!0)) && ((c!!0) > (b!!0)) && ((c!!1) < (a!!1)) && ((c!!1) > (b!!1))) then True
          else if (((c!!0) < (a!!0)) && ((c!!0) == (b!!0)) && ((c!!1) < (a!!1)) && ((c!!1) == (b!!1))) then True
          else if (((c!!0) == (a!!0)) && ((c!!0) > (b!!0)) && ((c!!1) == (a!!1)) && ((c!!1) > (b!!1))) then True
          else False

Haskell
OMG mode on

kegdan kegdan, (Updated )

Комментарии (17, +17)