Skip to content

Commit

Permalink
Solve 'Orthogonal Vectors' kata
Browse files Browse the repository at this point in the history
  • Loading branch information
borisskert committed Nov 1, 2024
1 parent 15172d0 commit b3171fc
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/Orthogonal.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module Orthogonal where

-- https://www.codewars.com/kata/53670c5867e9f2222f000225/train/haskell

isOrthogonal :: [Int] -> [Int] -> Bool
isOrthogonal xs ys = (== 0) $ dotProduct xs ys

dotProduct :: [Int] -> [Int] -> Int
dotProduct [] [] = 0
dotProduct (x : xs) (y : ys) = x * y + dotProduct xs ys



-- #againwhatlearned
-- use `zipWith`
-- dotProduct = (sum .) . zipWith (*)
28 changes: 28 additions & 0 deletions test/OrthogonalSpec.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
module OrthogonalSpec where

import Orthogonal (isOrthogonal)
import Test.HUnit
import Test.Hspec

runTest :: ([Int], [Int], Bool) -> Expectation
runTest (xs, ys, expected) =
assertEqual (unwords [show xs, show ys]) expected $
isOrthogonal xs ys

spec :: Spec
spec = do
describe "Fixed Tests" $ do
it "Works for some example tests" $ do
mapM_
runTest
[ ([1, 2], [2, 1], False),
([1, -2], [2, 1], True),
([7, 8], [7, -6], False),
([-13, -26], [-8, 4], True),
([1, 2, 3], [0, -3, 2], True),
([3, 4, 5], [6, 7, -8], False),
([3, -4, -5], [-4, -3, 0], True),
([1, -2, 3, -4], [-4, 3, 2, -1], True),
([2, 4, 5, 6, 7], [-14, -12, 0, 8, 4], True),
([5, 10, 1, 20, 2], [-2, -20, -1, 10, 5], False)
]

0 comments on commit b3171fc

Please sign in to comment.