10000 added magic square · willdoescode/haskell-leetcode@54fc545 · GitHub
[go: up one dir, main page]

Skip to content

Commit 54fc545

Browse files
committed
added magic square
1 parent 063e0b1 commit 54fc545

File tree

6 files changed

+41
-2
lines changed

6 files changed

+41
-2
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,6 @@
1818
#### Medium
1919

2020
* Sort List
21+
* [Magic Square](https://www.hackerrank.com/challenges/magic-square-forming/problem?h_r=internal-search)
2122

2223
#### Hard

haskell-things.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,4 @@ executable haskell-things
3030
, ToLowerCase
3131
, MinimumIndexSumofTwoLists
3232
, SortList
33+
, MagicSquare

haskell-things.iml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,10 @@
1212
<orderEntry type="sourceFolder" forTests="false" />
1313
<orderEntry type="library" name="base-4.13.0.0" level="project" />
1414
<orderEntry type="library" name="ghc-prim-0.5.3" level="project" />
15-
<orderEntry type="library" name="integer-gmp-1.0.2.0" level="project" />
1615
<orderEntry type="library" name="containers-0.6.2.1" level="project" />
1716
<orderEntry type="library" name="array-0.5.4.0" level="project" />
1817
<orderEntry type="library" name="deepseq-1.4.4.0" level="project" />
18+
<orderEntry type="library" name="base-4.14.1.0" level="project" />
19+
<orderEntry type="library" name="integer-gmp-1.0.3.0" level="project" />
1920
</component>
2021
</module>

src/Main.hs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import SortArrayByParity
1010
import ToLowerCase
1111
import MinimumIndexSumofTwoLists
1212
import SortList
13+
import MagicSquare
1314

1415
main :: IO ()
1516
main = do
@@ -57,3 +58,11 @@ main = do
5758
print $ sortList ([9, 3, 4, 1] :: [Int]) -- cast types for no ide warning
5859
print $ (sortList . reverse . take 10) ([0..] :: [Int]) -- cast types for no ide warning
5960
print $ sortList ['c', 'a', 'f', 'y']
61+
62+
putStrLn "<-- Magic Square -->"
63+
print $ magicSquare [ [4, 9, 2]
64+
, [3, 5, 7]
65+
, [8, 1, 5] ]
66+
print $ magicSquare [ [4, 8, 2]
67+
, [4, 5, 7]
68+
, [6, 1, 6] ]

src/Medium/MagicSquare.hs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
module MagicSquare ( magicSquare ) where
2+
3+
-- https://www.hackerrank.com/challenges/magic-square-forming/problem?h_r=internal-search
4+
5+
import Data.List
6+
7+
type Square = [[Int]]
8+
9+
magic :: Square
10+
magic = [ [8, 1, 6]
11+
, [3, 5, 7]
12+
, [4, 9, 2] ]
13+
14+
rot90 :: Square -> Square
15+
rot90 = map reverse . transpose
16+
17+
refl :: Square -> Square
18+
refl = transpose
19+
20+
allMagic :: [Square]
21+
allMagic = take 4 (iterate rot90 magic) ++ take 4 (iterate rot90 $ refl magic)
22+
23+
distance :: Square -> Square -> Int
24+
distance s1 s2 = sum $ map abs $ zipWith (-) (concat s1) (concat s2)
25+
26+
magicSquare :: Square -> Int
27+
magicSquare s = minimum $ map (distance s) allMagic

src/Medium/SortList.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
module SortList ( sortList ) where
22

33
-- Quick Sort
4-
4+
55
sortList :: (Ord a) => [a] -> [a]
66
sortList (x:xs) = sortList [y | y <- xs, y <= x] ++ [x] ++ sortList [y | y <- xs, y > x]
77
sortList [] = []

0 commit comments

Comments
 (0)
0