from preloaded import describe, it, test with describe("oppression"): with it('should raise exceptions inside the decorated function'): 1 / 0 with it('should merrily continue on'): 1 / 0 with it('fixes allow_raise'): test.expect(False, allow_raise=True) test.expect(False, allow_raise=True) with it("disallows bare it"): 1 / 0
import codewars_test as testimport threadingimport queue- from preloaded import describe, it, test
class it:def __init__(self, title):self.title = titledef __enter__(self, *_whatever):# I'm sure there's a more suitable sync primitive, but, hey, queues.self.wait = queue.Queue()entered = queue.Queue()self.exited = queue.Queue()def hodor():@test.it(self.title)def impostor():entered.put(())self.wait.get()self.exited.put(())threading.Thread(target=hodor).start()entered.get()def __exit__(self, *_whatever):self.wait.put(())self.exited.get()# same as `it`, copy pasted out of lazinessclass describe:def __init__(self, title):self.title = titledef __enter__(self, *_whatever):# I'm sure there's a more suitable sync primitive, but, hey, queues.self.wait = queue.Queue()entered = queue.Queue()self.exited = queue.Queue()def hodor():@test.describe(self.title)def impostor():entered.put(())self.wait.get()self.exited.put(())threading.Thread(target=hodor).start()entered.get()def __exit__(self, *_whatever):self.wait.put(())self.exited.get()- with describe("oppression"):
- with it('should raise exceptions inside the decorated function'):
- 1 / 0
- with it('should merrily continue on'):
- 1 / 0
- with it('fixes allow_raise'):
- test.expect(False, allow_raise=True)
- test.expect(False, allow_raise=True)
def taste():return 'black'with describe("Help! I'm being oppressed"):with describe("Once upon a time ..."):with it("should taste like purple"):test.assert_equals(taste(), "purple")with it("should taste like black"):test.assert_equals(taste(), "black")with describe("Should things exist?"):with it("yes."):test.expect(True)with it("nah."):test.expect(False)- with it("disallows bare it"):
- 1 / 0
...
import codewars_test as test
import threading
import queue
class it:
def __init__(self, title):
self.title = title
def __enter__(self, *_whatever):
# I'm sure there's a more suitable sync primitive, but, hey, queues.
self.wait = queue.Queue()
entered = queue.Queue()
self.exited = queue.Queue()
def hodor():
@test.it(self.title)
def impostor():
entered.put(())
self.wait.get()
self.exited.put(())
threading.Thread(target=hodor).start()
entered.get()
def __exit__(self, *_whatever):
self.wait.put(())
self.exited.get()
# same as `it`, copy pasted out of laziness
class describe:
def __init__(self, title):
self.title = title
def __enter__(self, *_whatever):
# I'm sure there's a more suitable sync primitive, but, hey, queues.
self.wait = queue.Queue()
entered = queue.Queue()
self.exited = queue.Queue()
def hodor():
@test.describe(self.title)
def impostor():
entered.put(())
self.wait.get()
self.exited.put(())
threading.Thread(target=hodor).start()
entered.get()
def __exit__(self, *_whatever):
self.wait.put(())
self.exited.get()
def taste():
return 'black'
with describe("Help! I'm being oppressed"):
with describe("Once upon a time ..."):
with it("should taste like purple"):
test.assert_equals(taste(), "purple")
with it("should taste like black"):
test.assert_equals(taste(), "black")
with describe("Should things exist?"):
with it("yes."):
test.expect(True)
with it("nah."):
test.expect(False)
{-# OPTIONS_GHC -O1 #-}
module LongestPath (longestPath) where
{-
import Data.Vector.Unboxed as Vector (fromList,(!),(!?))
import Data.List (maximumBy,elemIndex)
import Data.Maybe (fromMaybe)
import Data.Monoid ((<>))
import Data.Function (on)
import Data.Ord (Down(Down))
memo :: (Enum a) => (a -> b) -> (a -> b)
memo fn = (map fn [ toEnum 0 .. ] !!) . fromEnum
longestPath :: String -> String
longestPath s = maximumBy order $ ("" :) $ zipWith ( \ i c -> if c == '\n' then "" else getLongestPath i ) [0..] s where
width = fromMaybe (length s) $ elemIndex '\n' s
order = (compare `on` length) <> (compare `on` Down)
v = fromList s
getLongestPath = memo $ \ i ->
v ! i : (maximumBy order $ ("" :)
$ map getLongestPath
$ filter ( \ j -> Just (v ! i) < v !? j )
$ [ i-width-2, i-width-1, i-width
, i-1 , i+1
, i+width , i+width+1, i+width+2
]
)
-}
import Control.Monad
import Data.Function ((&))
import Data.Ix (inRange)
import Data.List.Split (chunksOf)
import Data.Maybe
import Data.Vector (Vector)
import qualified Data.Vector as V
minimumOn :: (Ord b) => (a -> b) -> [a] -> a
minimumOn f [] = error "Data.List.Extra.minimumOn: empty list"
minimumOn f (x:xs) = g x (f x) xs
where
g v mv [] = v
g v mv (x:xs) | mx < mv = g x mx xs
| otherwise = g v mv xs
where mx = f x
longestPath :: String -> String
longestPath = solve . lines
maximumCell :: [(Int, [Char])] -> (Int, [Char])
maximumCell = minimumOn (\(n, s) -> (-n, s))
solve :: [[Char]] -> [Char]
solve input = knot & V.toList & V.concat & V.toList & maximumCell & snd
where
grid = V.fromList $ V.fromList <$> input
height = length input
width = length (head input)
knot = V.fromList $ do
row <- [0 .. height - 1]
pure $ V.fromList $ seqLenAt grid knot row <$> [0 .. width - 1]
around :: [(Int, Int)]
around = (,) <$> [-1 .. 1] <*> [-1 .. 1] & filter (/= (0, 0))
seqLenAt
:: Vector (Vector Char)
-> Vector (Vector (Int, [Char]))
-> Int
-> Int
-> (Int, [Char])
seqLenAt grid knot row col = maximumCell candidates
where
hereCh = (grid V.! row) V.! col
candidates = (1, [hereCh]) : do
(dx, dy) <- around
let (ty, tx) = (row + dy, col + dx)
thereCh <- maybeToList $ (V.!? ty) >=> (V.!? tx) $ grid
guard $ hereCh < thereCh
let (n, s) = (knot V.! ty) V.! tx
pure (n + 1, hereCh : s)
{-# OPTIONS_GHC -O1 #-}
module LongestPathSpec (spec) where
import LongestPath (longestPath)
import Test.Hspec
import Test.QuickCheck
import Data.Vector.Unboxed as Vector (fromList,(!),(!?))
import Data.List (maximumBy,elemIndex)
import Data.Maybe (fromMaybe)
import Data.Monoid ((<>))
import Data.Function (on)
import Data.Ord (Down(Down))
spec :: Spec
spec = do
it "example tests" $ do
longestPath "mda\n\
\xfc\n\
\gxx\n" `shouldBe` "acdfgx"
longestPath "bja\n\
\def\n\
\ghi\n" `shouldBe` "bdefhi"
longestPath "/o'xk4^%6N\n\
\xZ-(CKd:}N\n\
\#LoYiI.o(2\n\
\Qu+$oBE[oe\n\
\RSr&Y|O'*Q\n\
\ypmJ9th[&G\n\
\*XKq,{&/Q_\n\
\44>S6=6{jR\n\
\&)2KgPBlAF\n\
\39rG:2ixUV\n" `shouldBe` "$&+LQRSmpru"
it "fixed tests" $ do
longestPath "" `shouldBe` ""
longestPath "\n" `shouldBe` ""
longestPath "\n\
\\n" `shouldBe` ""
longestPath "a\n" `shouldBe` "a"
longestPath "ab\n" `shouldBe` "ab"
longestPath "a\n\
\b\n" `shouldBe` "ab"
longestPath "zzzz\n\
\zzzz\n" `shouldBe` "z"
it "random tests" $ do
withMaxSuccess 100 $ forAll (genGrid (10,20)) $ \ s -> do
-- print s
-- print $ ( length $ refLongestPath s, refLongestPath s )
longestPath s `shouldBe` refLongestPath s
it "random tests" $ do
withMaxSuccess 300 $ forAll (genGrid (50,80)) $ \ s -> do
-- print s
-- print $ ( length $ refLongestPath s, refLongestPath s )
longestPath s `shouldBe` refLongestPath s
genGrid :: (Int,Int) -> Gen String
genGrid range = do
width <- choose range
height <- choose range
fmap unlines $ vectorOf height $ vectorOf width $ elements [' '..'~']
memo :: (Enum a) => (a -> b) -> (a -> b)
memo fn = (map fn [ toEnum 0 .. ] !!) . fromEnum
refLongestPath :: String -> String
refLongestPath s = longestPath s
refLongestPath s = maximumBy order $ ("" :) $ zipWith ( \ i c -> if c == '\n' then "" else getLongestPath i ) [0..] s where
width = fromMaybe (length s) $ elemIndex '\n' s
order = (compare `on` length) <> (compare `on` Down)
v = fromList s
getLongestPath = memo $ \ i ->
v ! i : (maximumBy order $ ("" :)
$ map getLongestPath
$ filter ( \ j -> Just (v ! i) < v !? j )
$ [ i-width-2, i-width-1, i-width
, i-1 , i+1
, i+width , i+width+1, i+width+2
]
)