Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cabal Updates with Related Clean-up #3

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Created by https://www.gitignore.io/api/haskell

### Haskell ###
dist
dist-*
cabal-dev
*.o
*.hi
*.chi
*.chs.h
*.dyn_o
*.dyn_hi
.hpc
.hsenv
.cabal-sandbox/
cabal.sandbox.config
*.prof
*.aux
*.hp
*.eventlog
.stack-work/
cabal.project.local
.HTF/

# End of https://www.gitignore.io/api/haskell
5 changes: 5 additions & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Revision history for CtCI-6th-Edition-Haskell

## 0.1.0.0 -- YYYY-mm-dd

* First version. Released on an unsuspecting world.
121 changes: 121 additions & 0 deletions CtCI-6th-Edition-Haskell.cabal
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
name: CtCI-6th-Edition-Haskell
version: 0.1.0.0
synopsis: Haskell Solutions for Cracking the Coding Interview 6th Edition

-- description:

homepage: https://github.com/careercup/CtCI-6th-Edition-Haskell
bug-reports: https://github.com/careercup/CtCI-6th-Edition-Haskell/issues
-- license:
license-file: LICENSE
author: Variuos Contributors
maintainer: NA
-- copyright:
-- category:
build-type: Simple
cabal-version: >= 1.10
tested-with: GHC == 8.0.*

extra-source-files:
ChangeLog.md
, LICENSE
, README.md
, Setup.hs

source-repository head
type: git
location: https://github.com/careercup/CtCI-6th-Edition-Haskell

library
default-language: Haskell2010

ghc-options: -Wall -fwarn-tabs -fwarn-monomorphism-restriction
-fwarn-unused-do-bind

hs-source-dirs:
src

exposed-modules:
Introduction.VI.Example01

other-modules:

build-depends:
base == 4.9.*

other-extensions:

test-suite CtCI-6th-Edition-Haskell-Tests
default-language: Haskell2010
type: exitcode-stdio-1.0
main-is: Spec.hs

ghc-options: -Wall -fwarn-tabs -fwarn-monomorphism-restriction
-fwarn-unused-do-bind

hs-source-dirs:
src
, test

other-modules:
Introduction.VI.Example01
, Introduction.VI.Example01Spec

build-tool-depends:
hspec-discover:hspec-discover == 2.4.*

build-depends:
base == 4.9.*
, hspec == 2.4.*
, QuickCheck == 2.9.*

other-extensions:

test-suite tests
default-language: Haskell2010
type: exitcode-stdio-1.0
main-is: Main.hs

ghc-options: -Wall -fwarn-tabs -fwarn-monomorphism-restriction
-fwarn-unused-do-bind

hs-source-dirs:
src
, src/chapter-1
, src/chapter-2
, src/chapter-3

other-modules:
-- Ch1
-- , Ch2
-- , Ch3
-- , Data.Queue
-- , Data.Stack
-- , find-cycle
-- , intersection
-- , is-palindrome
-- , is-permutation
-- , is-rotation
-- , is-unique
-- , kth-last
-- , one-away
-- , palindrome-permutation
-- , partition
-- , Questions.MinStack
-- , remove-dups
-- , remove-node
-- , rotate-matrix
-- , string-compression
-- , sum-lists
-- , Test.MinStack
-- , Test.Queue
-- , Test.Stack
-- , url-encode
-- , zero-matrix

build-depends:
base
, hspec
, QuickCheck

other-extensions:
83 changes: 0 additions & 83 deletions haskell-solutions.cabal

This file was deleted.

14 changes: 14 additions & 0 deletions src/Introduction/VI/Example01.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{-|
Module : Introduction.VI.Example01
Description : Introduction VI (Big O) Example 1 on Page 40
-}
module Introduction.VI.Example01 where

import Prelude hiding (sum)

sum :: Int -> Int
sum n | n <= 0 = 0
| otherwise = n + sum (n - 1)

main :: IO ()
main = print $ sum 4
2 changes: 0 additions & 2 deletions src/Main.hs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
module Main where

import Test.Hspec
import Test.QuickCheck
import Ch1
import Ch2
import Ch3
Expand Down
8 changes: 3 additions & 5 deletions src/chapter-1/Ch1.hs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
module Ch1 where

import Test.QuickCheck
import Test.Hspec

ch1 :: IO ()
ch1 = hspec $ do
describe "_______________________Chapter 1 tests_______________________" $ do
it "should have tests" $ do
True
ch1 = hspec $
describe "_______________________Chapter 1 tests_______________________" $
it "should have tests" True
4 changes: 2 additions & 2 deletions src/chapter-1/one-away.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ oneAway a b = a `elem` candidates
where candidates = deletes ++ transposes ++ replaces ++ inserts
strlen = 1 + length b
splits = zipWith splitAt [0..strlen] (replicate strlen b)
deletes = [x ++ (tail y) | (x, y) <- splits, length y > 0]
deletes = [x ++ tail y | (x, y) <- splits, not (null y)]
transposes = [x ++ head t : head y : tail t | (x, y) <- splits, length y > 1, let t = tail y]
replaces = [x ++ c : (tail y) | (x, y) <- splits, c <- ['a'..'z'], length y > 0]
replaces = [x ++ c : tail y | (x, y) <- splits, c <- ['a'..'z'], not (null y)]
inserts = [x ++ c : y | (x, y) <- splits, c <- ['a'..'z']]
2 changes: 1 addition & 1 deletion src/chapter-1/zero-matrix.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ zeroMatrix :: [[Integer]] -> [[Integer]]
zeroMatrix x = [[zero i j v | (j, v) <- zip [0..] row] | (i, row) <- zip [0..] x]
where zeroRows = zeroIndices x
zeroCols = zeroIndices $ transpose x
zeroIndices y = map fst $ filter snd $ zip [0..] (map (any (== 0)) y)
zeroIndices y = map fst $ filter snd $ zip [0..] (map (elem 0) y)
zero row col val | row `elem` zeroRows = 0
| col `elem` zeroCols = 0
| otherwise = val
8 changes: 3 additions & 5 deletions src/chapter-2/Ch2.hs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
module Ch2 where

import Test.QuickCheck
import Test.Hspec

ch2 :: IO ()
ch2 = hspec $ do
describe "_______________________Chapter 2 tests_______________________" $ do
it "should have tests" $ do
True
ch2 = hspec $
describe "_______________________Chapter 2 tests_______________________" $
it "should have tests" True

3 changes: 1 addition & 2 deletions src/chapter-3/Ch3.hs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
module Ch3 where

import Test.QuickCheck
import Test.Hspec
import Test.Stack
import Test.Queue
import Test.MinStack

ch3 :: IO ()
ch3 = hspec $ do
ch3 = hspec $
describe "_______________________Chapter 3 tests_______________________" $ do
testStack
testQueue
Expand Down
12 changes: 6 additions & 6 deletions src/chapter-3/Data/Queue.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ instance Eq a => Eq (Queue a) where
(==) a b = let a' = reorder a
b' = reorder b
in checkEq a' b'
where checkEq (Queue xs ys) (Queue xs' ys') = ys == ys'
where checkEq (Queue _ ys) (Queue _ ys') = ys == ys'

mkQueue :: a -> Queue a
mkQueue a = Queue [a] []
Expand All @@ -16,18 +16,18 @@ enqueue a (Queue xs ys) = Queue (a:xs) ys

dequeue :: Queue a -> Queue a
dequeue q = case reorder q of
(Queue xs (y:ys)) -> Queue xs ys
otherwise -> reorder q
(Queue xs (_:ys)) -> Queue xs ys
_ -> reorder q

peek :: Queue a -> Maybe a
peek a = case reorder a of
(Queue _ (y:ys)) -> Just y
otherwise -> Nothing
(Queue _ (y:_)) -> Just y
_ -> Nothing

isEmpty :: Queue a -> Bool
isEmpty (Queue [] []) = True
isEmpty (Queue _ _ ) = False

reorder :: Queue a -> Queue a
reorder q@(Queue xs (y:ys)) = q
reorder (Queue xs []) = Queue [] (reverse xs)
reorder q = q
8 changes: 4 additions & 4 deletions src/chapter-3/Data/Stack.hs
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
module Data.Stack where

data Stack a = Stack [a]
newtype Stack a = Stack [a]
deriving (Eq, Show)

push :: a -> Stack a -> Stack a
push a (Stack xs) = Stack (a:xs)

pop :: Stack a -> Stack a
pop (Stack (x:xs)) = Stack xs
pop (Stack (_:xs)) = Stack xs
pop (Stack []) = Stack []

peek :: Stack a -> Maybe a
peek (Stack (x:xs)) = Just x
peek (Stack (x:_)) = Just x
peek (Stack []) = Nothing

isEmpty :: Stack a -> Bool
isEmpty (Stack []) = True
isEmpty (Stack xs) = False
isEmpty (Stack _) = False

mkStack :: a -> Stack a
mkStack a = Stack [a]
Expand Down
Loading