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

Added reverse-string exercise #1191

Merged
merged 4 commits into from
Jan 16, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
12 changes: 12 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,18 @@
"expressions"
]
},
{
"slug": "reverse-string",
"name": "Reverse String",
"uuid": "8ed8f6d7-ee0a-4d61-99b5-c8b86f01c5de",
"practices": [],
"prerequisites": [],
"difficulty": 1,
"topics": [
"strings",
"lists"
]
},
{
"slug": "space-age",
"name": "Space Age",
Expand Down
7 changes: 7 additions & 0 deletions exercises/practice/reverse-string/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Instructions

Reverse a string

For example:
input: "cool"
output: "looc"
23 changes: 23 additions & 0 deletions exercises/practice/reverse-string/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"authors": [
"cdimitroulas"
],
"files": {
"solution": [
"src/ReverseString.hs",
"package.yaml"
],
"test": [
"test/Tests.hs"
],
"example": [
".meta/examples/success-standard/src/ReverseString.hs"
],
"invalidator": [
"stack.yaml"
]
},
"blurb": "Reverse a string.",
"source": "Introductory challenge to reverse an input string",
"source_url": "https://medium.freecodecamp.org/how-to-reverse-a-string-in-javascript-in-3-different-ways-75e4763c68cb"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
name: reverse-string

dependencies:
- base

library:
exposed-modules: ReverseString
source-dirs: src

tests:
test:
main: Tests.hs
source-dirs: test
dependencies:
- reverse-string
- hspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module ReverseString (reverseString) where

reverseString :: String -> String
reverseString input = reverse input
28 changes: 28 additions & 0 deletions exercises/practice/reverse-string/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# This is an auto-generated file.
#
# Regenerating this file via `configlet sync` will:
# - Recreate every `description` key/value pair
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
# - Preserve any other key/value pair
#
# As user-added comments (using the # character) will be removed when this file
# is regenerated, comments can be added via a `comment` key.

[c3b7d806-dced-49ee-8543-933fd1719b1c]
description = "an empty string"

[01ebf55b-bebb-414e-9dec-06f7bb0bee3c]
description = "a word"

[0f7c07e4-efd1-4aaa-a07a-90b49ce0b746]
description = "a capitalized word"

[71854b9c-f200-4469-9f5c-1e8e5eff5614]
description = "a sentence with punctuation"

[1f8ed2f3-56f3-459b-8f3e-6d8d654a1f6c]
description = "a palindrome"

[b9e7dec1-c6df-40bd-9fa3-cd7ded010c4c]
description = "an even-sized word"
21 changes: 21 additions & 0 deletions exercises/practice/reverse-string/package.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: reverse-string
version: 0.1.0.0

dependencies:
- base

library:
exposed-modules: ReverseString
source-dirs: src
ghc-options: -Wall
# dependencies:
# - foo # List here the packages you
# - bar # want to use in your solution.

tests:
test:
main: Tests.hs
source-dirs: test
dependencies:
- reverse-string
- hspec
4 changes: 4 additions & 0 deletions exercises/practice/reverse-string/src/ReverseString.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module ReverseString (reverseString) where

reverseString :: String -> String
cdimitroulas marked this conversation as resolved.
Show resolved Hide resolved
reverseString input = error "You need to implement this funciton."
cdimitroulas marked this conversation as resolved.
Show resolved Hide resolved
1 change: 1 addition & 0 deletions exercises/practice/reverse-string/stack.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
resolver: lts-20.18
12 changes: 12 additions & 0 deletions exercises/practice/reverse-string/stack.yaml.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# This file was autogenerated by Stack.
cdimitroulas marked this conversation as resolved.
Show resolved Hide resolved
# You should not edit this file by hand.
# For more information, please see the documentation at:
# https://docs.haskellstack.org/en/stable/lock_files

packages: []
snapshots:
- completed:
sha256: 9fa4bece7acfac1fc7930c5d6e24606004b09e80aa0e52e9f68b148201008db9
size: 649606
url: https://raw.githubusercontent.com/commercialhaskell/stackage-snapshots/master/lts/20/18.yaml
original: lts-20.18
51 changes: 51 additions & 0 deletions exercises/practice/reverse-string/test/Tests.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
{-# LANGUAGE RecordWildCards #-}

import Data.Foldable (for_)
import Test.Hspec (Spec, describe, it, shouldBe)
import Test.Hspec.Runner (configFailFast, defaultConfig, hspecWith)

import ReverseString (reverseString)

main :: IO ()
main = hspecWith defaultConfig {configFailFast = True} specs

specs :: Spec
specs = describe "reverseString" $ for_ cases test
where

test Case{..} = it explanation assertion
where
explanation = unwords [show input, "-", description]
assertion = reverseString input `shouldBe` expected

data Case = Case { description :: String
, input :: String
, expected :: String
}

cases :: [Case]
cases = [ Case { description = "empty string"
, input = ""
, expected = ""
}
, Case { description = "a word"
, input = "robot"
, expected = "tobor"
}
, Case { description = "a capitalized word"
, input = "Ramen"
, expected = "nemaR"
}
, Case { description = "a sentence with punctuation"
, input = "I am hungry!"
, expected = "!yrgnuh ma I"
}
, Case { description = "a palindrome"
, input = "racecar"
, expected = "racecar"
}
, Case { description = "an even-sized word"
, input = "drawer"
, expected = "reward"
}
]
Loading