Uses NonEmpty to avoid the empty case.
module FindMaxNumber where import Data.List.NonEmpty find_max :: Ord a => NonEmpty a -> a find_max (x:|xs) = foldr (\ a b -> if a > b then a else b) x xs
- module FindMaxNumber where
find_max :: Ord a => [a] -> afind_max [] = error "find_max: Empty list"find_max (x:xs) = foldr (\ a b -> if a > b then a else b) x xs- import Data.List.NonEmpty
- find_max :: Ord a => NonEmpty a -> a
- find_max (x:|xs) = foldr (\ a b -> if a > b then a else b) x xs
module FindMaxNumberSpec where import Prelude hiding (take, iterate, repeat) import Test.Hspec import Test.QuickCheck import Data.List.NonEmpty import FindMaxNumber instance Arbitrary a => Arbitrary (NonEmpty a) where arbitrary = fromList <$> (arbitrary1 `suchThat` (not . null)) spec = do describe "Test find_max" $ do it "find_max [1,2,3,4,5]" $ do find_max (fromList [1..5]) `shouldBe` 5 it "Random tests" $ do property $ \ xs -> if null xs then True else find_max xs == maximum (xs :: NonEmpty Int)
- module FindMaxNumberSpec where
- import Prelude hiding (take, iterate, repeat)
- import Test.Hspec
- import Test.QuickCheck
- import Data.List.NonEmpty
- import FindMaxNumber
- instance Arbitrary a => Arbitrary (NonEmpty a) where
- arbitrary = fromList <$> (arbitrary1 `suchThat` (not . null))
- spec = do
- describe "Test find_max" $ do
- it "find_max [1,2,3,4,5]" $ do
find_max [1, 2, 3, 4, 5] `shouldBe` 5- find_max (fromList [1..5]) `shouldBe` 5
- it "Random tests" $ do
property $ \ xs -> if null xs then True else find_max xs == maximum (xs :: [Int])- property $ \ xs -> if null xs then True else find_max xs == maximum (xs :: NonEmpty Int)
Find maximum element of a list in Haskell
module FindMaxNumber where find_max :: Ord a => [a] -> a find_max = maximum
def find_max(arr):return 0- module FindMaxNumber where
- find_max :: Ord a => [a] -> a
- find_max = maximum
module FindMaxNumberSpec where import Test.Hspec import FindMaxNumber spec = do describe "Test find_max" $ do it "find_max [1,2,3,4,5]" $ do find_max [1, 2, 3, 4, 5] `shouldBe` 5
# TODO: Replace examples and use TDD by writing your own tests# These are some of the methods available:# test.expect(boolean, [optional] message)# test.assert_equals(actual, expected, [optional] message)# test.assert_not_equals(actual, expected, [optional] message)- module FindMaxNumberSpec where
# You can use Test.describe and Test.it to write BDD style test groupingstest.assert_equals(find_max([1, 2, 3, 4, 5]), 5)- import Test.Hspec
- import FindMaxNumber
- spec = do
- describe "Test find_max" $ do
- it "find_max [1,2,3,4,5]" $ do
- find_max [1, 2, 3, 4, 5] `shouldBe` 5
module Scramble where import Data.List (sort) scramble :: String -> String -> Bool scramble a b = sort a == sort b
- module Scramble where
- import Data.List (sort)
- scramble :: String -> String -> Bool
scramble a b = "Scarmble this up"- scramble a b = sort a == sort b
module ScrambleSpec where import Test.Hspec import Scramble spec :: Spec spec = do it "Test Cases" $ do scramble "scramble" "scarmble" `shouldBe` True scramble "scroomble" "scroomble" `shouldBe` True scramble "ScrooMBle" "totally random thing jalkdfjlskfdjlksjdfldjs" `shouldBe` False scramble "qwertyuiop" "QWERTYUIOP" `shouldBe` False
- module ScrambleSpec where
- import Test.Hspec
- import Scramble
- spec :: Spec
- spec = do
- it "Test Cases" $ do
- scramble "scramble" "scarmble" `shouldBe` True
scramble "scroomble" "scroomble" `shouldBe` False- scramble "scroomble" "scroomble" `shouldBe` True
- scramble "ScrooMBle" "totally random thing jalkdfjlskfdjlksjdfldjs" `shouldBe` False
scramble "qwertyuiop" "QWERTYUIOP" `shouldBe False- scramble "qwertyuiop" "QWERTYUIOP" `shouldBe` False
module HelloHaskell where main :: IO () main = putStrLn "Hello Haskell!"
System.Console.WriteLine("Hello, C#!");- module HelloHaskell where
- main :: IO ()
- main = putStrLn "Hello Haskell!"
module HelloHaskellSpec where -- Tests can be written using Hspec http://hspec.github.io/ -- Replace this with your own tests. import Test.Hspec import HelloHaskell spec = runIO HelloHaskell.main >> it "Says hello" True -- the following line is optional for 8.2 main = hspec spec
- module HelloHaskellSpec where
- -- Tests can be written using Hspec http://hspec.github.io/
- -- Replace this with your own tests.
- import Test.Hspec
- import HelloHaskell
- spec = runIO HelloHaskell.main >> it "Says hello" True
- -- the following line is optional for 8.2
- main = hspec spec
Let's test some big primes.
import random def prime(x, miller_rabin_rounds=10): if x <= 1: return False if x == 2: return True if x == 3: return True if x % 2 == 0: return False return miller_rabin(x, miller_rabin_rounds) def factor_out_2(q): r = 0 while q % 2 == 0: r += 1 q //= 2 return r, q def square_and_multiply(x, n, m): y = 1 while n > 1: if n % 2 == 0: x = x**2 % m n //= 2 else: y = x*y % m x = x**2 % m n = (n-1)//2 return x*y % m def miller_rabin(n, k): # Write n = 2^r * d + 1 r, d = factor_out_2(n - 1) for _ in range(k): a = random.randint(2, n-1) x = square_and_multiply(a, d, n) if x == 1 or x == n - 1: continue for _ in range(r-1): x = x**2 % n if x == n - 1: break else: return False return True
def prime(x):counter = 0for i in range(1,x):if x % i is 0:counter += 1if counter is 0:return Trueelse:return False- import random
- def prime(x, miller_rabin_rounds=10):
- if x <= 1: return False
- if x == 2: return True
- if x == 3: return True
- if x % 2 == 0: return False
- return miller_rabin(x, miller_rabin_rounds)
- def factor_out_2(q):
- r = 0
- while q % 2 == 0:
- r += 1
- q //= 2
- return r, q
- def square_and_multiply(x, n, m):
- y = 1
- while n > 1:
- if n % 2 == 0:
- x = x**2 % m
- n //= 2
- else:
- y = x*y % m
- x = x**2 % m
- n = (n-1)//2
- return x*y % m
- def miller_rabin(n, k):
- # Write n = 2^r * d + 1
- r, d = factor_out_2(n - 1)
- for _ in range(k):
- a = random.randint(2, n-1)
- x = square_and_multiply(a, d, n)
- if x == 1 or x == n - 1:
- continue
- for _ in range(r-1):
- x = x**2 % n
- if x == n - 1:
- break
- else:
- return False
- return True
test.assert_equals(prime(0),False) test.assert_equals(prime(1),False) test.assert_equals(prime(2),True) test.assert_equals(prime(3),True) test.assert_equals(prime(9),False) test.assert_equals(prime(31),True) test.assert_equals(prime(42),False) test.assert_equals(prime(12345),False) test.assert_equals(prime(982451653),True) test.assert_equals(prime(982451655),False) # Mersenne primes test.assert_equals(prime(2**2203-1),True) test.assert_equals(prime(2**2205-1),False) test.assert_equals(prime(2**4423-1),True) # Fermat number test.assert_equals(prime(2**2048+1), False)
- test.assert_equals(prime(0),False)
- test.assert_equals(prime(1),False)
- test.assert_equals(prime(2),True)
- test.assert_equals(prime(3),True)
- test.assert_equals(prime(9),False)
- test.assert_equals(prime(31),True)
- test.assert_equals(prime(42),False)
- test.assert_equals(prime(12345),False)
- test.assert_equals(prime(982451653),True)
- test.assert_equals(prime(982451655),False)
- # Mersenne primes
- test.assert_equals(prime(2**2203-1),True)
- test.assert_equals(prime(2**2205-1),False)
- test.assert_equals(prime(2**4423-1),True)
- # Fermat number
- test.assert_equals(prime(2**2048+1), False)