Skip to content

Commit

Permalink
Solve 'Geometry Basics: Circle Area in 2D' kata
Browse files Browse the repository at this point in the history
  • Loading branch information
borisskert committed Oct 16, 2024
1 parent de093f8 commit 142753c
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 10 deletions.
4 changes: 2 additions & 2 deletions src/Distance.hs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
module Distance (distance) where
module Distance (distance, Point(..)) where

-- https://www.codewars.com/kata/58dced7b702b805b200000be/train/haskell

import Preloaded (Point(..))
data Point = Point {x :: Double, y :: Double} deriving (Show)

distance :: Point -> Point -> Double
distance p1 p2 = sqrt ((x p1 - x p2) ^ 2 + (y p1 - y p2) ^ 2)
Expand Down
8 changes: 8 additions & 0 deletions src/Geometry.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module Geometry (circleArea) where

import Preloaded (Point(..), Circle(..))

-- https://www.codewars.com/kata/58e3f824a33b52c1dc0001c0/train/haskell

circleArea :: Circle -> Double
circleArea (Circle _ r) = r * r * pi
12 changes: 10 additions & 2 deletions src/Preloaded.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import Data.List.Split (divvy)

data Shape
= Rectangle {width, length :: Double}
| Circle {radius :: Double}
| Circle1 {radius1 :: Double}
deriving (Eq, Show)

data RPSLS = Rock | Paper | Scissors | Lizard | Spock deriving (Show, Eq)
Expand All @@ -30,4 +30,12 @@ isWaveSorted xs

data Tree = Node {val :: Int, left, right :: Maybe Tree}

data Point = Point {x :: Double, y :: Double} deriving (Show)
data Point = Point
{ xValue :: Double
, yValue :: Double
} deriving (Show, Eq)

data Circle = Circle
{ center :: Point
, radius :: Double
} deriving (Show, Eq)
2 changes: 1 addition & 1 deletion src/SortByArea.hs
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ sortByArea = sortOn toArea

toArea :: Shape -> Double
toArea (Rectangle a b) = a * b
toArea (Circle r) = r * r * pi
toArea (Circle1 r) = r * r * pi
3 changes: 1 addition & 2 deletions test/DistanceSpec.hs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
module DistanceSpec (spec) where

import Preloaded (Point(..))
import Distance (distance)
import Distance (distance, Point(..))
import Test.Hspec
import Control.Monad (unless)

Expand Down
18 changes: 18 additions & 0 deletions test/GeometrySpec.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module GeometrySpec where

import Geometry (circleArea)
import Preloaded
import Test.Hspec
import Test.Hspec.Codewars

spec :: Spec
spec = do
describe "Fixed Tests" $ do
it "circleArea $ Circle (Point 10 10) 30" $ do
circleArea (Circle (Point 10 10) 30) `shouldBeApprox` 2827.4333882308138
it "circleArea $ Circle (Point 25 (-70)) 30" $ do
circleArea (Circle (Point 25 (-70)) 30) `shouldBeApprox` 2827.4333882308138
it "circleArea $ Circle (Point (-15) 5) 0" $ do
circleArea (Circle (Point (-15) 5) 0) `shouldBeApprox` 0
it "circleArea $ Circle (Point (-15) 5) 12.5" $ do
circleArea (Circle (Point (-15) 5) 12.5) `shouldBeApprox` 490.8738521234052
6 changes: 3 additions & 3 deletions test/SortByAreaSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import Test.Hspec
spec :: Spec
spec = do
it "example tests" $ do
sortByArea [ Rectangle 4.23 6.43, Circle 1.23, Circle 3.444, Rectangle 1.342 3.212 ]
`shouldBe` [ Rectangle 1.342 3.212, Circle 1.23, Rectangle 4.23 6.43, Circle 3.444 ]
sortByArea [ Rectangle 2 5, Circle 6 ] `shouldBe` [ Rectangle 2 5, Circle 6 ]
sortByArea [ Rectangle 4.23 6.43, Circle1 1.23, Circle1 3.444, Rectangle 1.342 3.212 ]
`shouldBe` [ Rectangle 1.342 3.212, Circle1 1.23, Rectangle 4.23 6.43, Circle1 3.444 ]
sortByArea [ Rectangle 2 5, Circle1 6 ] `shouldBe` [ Rectangle 2 5, Circle1 6 ]
sortByArea [] `shouldBe` []

0 comments on commit 142753c

Please sign in to comment.