Skip to content

Commit

Permalink
crypto-square: Check whitespace
Browse files Browse the repository at this point in the history
Fixes exercism#895, the complaint that
the whitespace was being ignored.

The canonical test data itself is fine; the problem was that the test
suite was ignoring whitespace when comparing the actual result to the
expected result.

The decision to ignore whitespace was a originally deliberate decision,
with the reasoning in exercism#259.

However, maintainers' current judgment is that as long as our output is
a String, it's better to keep it as the problem description says, for it
would be against the description if a student emitted an output with a
lot of leading spaces (among others).

If the track changes the output to a [String], then the maintainers
could freely choose whether to require the trailing spaces or forbid
them.
  • Loading branch information
ashaindlin authored and petertseng committed Aug 1, 2021
1 parent 2a29d9a commit 7ac87f4
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,15 @@ import Data.List.Split (chunksOf)

encode :: String -> String
encode = unwords
. justify
. transpose
. (squareSize >>= chunksOf)
. map toLower
. filter isAlphaNum
where
squareSize :: String -> Int
squareSize = ceiling . (sqrt :: Double -> Double) . fromIntegral . length
justify :: [String] -> [String]
justify [] = []
justify (x:xs) = let width = length x in
x : map (\s -> take width (s ++ repeat ' ')) xs
5 changes: 2 additions & 3 deletions exercises/practice/crypto-square/test/Tests.hs
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,12 @@ specs = describe "encode" $ for_ cases test

test Case{..} = describe description $ do

let shouldMatchWords = shouldBe `on` words
shouldMatchString = shouldBe `on` filter (not . isSpace)
let shouldMatchString = shouldBe `on` filter (not . isSpace)
shouldMatchChars = shouldMatchList `on` filter (not . isSpace)

it "normalizes the input" $ encode input `shouldMatchChars` expected
it "reorders the characters" $ encode input `shouldMatchString` expected
it "groups the output" $ encode input `shouldMatchWords` expected
it "groups the output" $ encode input `shouldBe` expected

data Case = Case { description :: String
, input :: String
Expand Down

0 comments on commit 7ac87f4

Please sign in to comment.