From 1e7fc58341f4bac2321f1c633ad275e1c55a0330 Mon Sep 17 00:00:00 2001 From: Tejas Bubane Date: Sat, 9 Mar 2019 23:53:01 +0530 Subject: [PATCH] Implement Resistor Colors exercise --- config.json | 10 +++ exercises/resistor-colors/README.md | 82 +++++++++++++++++++ .../examples/success-standard/package.yaml | 16 ++++ .../success-standard/src/ResistorColors.hs | 29 +++++++ exercises/resistor-colors/package.yaml | 21 +++++ .../resistor-colors/src/ResistorColors.hs | 17 ++++ exercises/resistor-colors/stack.yaml | 1 + exercises/resistor-colors/test/Tests.hs | 44 ++++++++++ 8 files changed, 220 insertions(+) create mode 100644 exercises/resistor-colors/README.md create mode 100644 exercises/resistor-colors/examples/success-standard/package.yaml create mode 100644 exercises/resistor-colors/examples/success-standard/src/ResistorColors.hs create mode 100644 exercises/resistor-colors/package.yaml create mode 100644 exercises/resistor-colors/src/ResistorColors.hs create mode 100644 exercises/resistor-colors/stack.yaml create mode 100644 exercises/resistor-colors/test/Tests.hs diff --git a/config.json b/config.json index 4d8d1ceae..29f516e90 100644 --- a/config.json +++ b/config.json @@ -213,6 +213,16 @@ "strings" ] }, + { + "slug": "resistor-colors", + "uuid": "61be7bcd-42fb-44b7-a51d-1d081173f876", + "core": false, + "unlocked_by": "pangram", + "difficulty": 2, + "topics": [ + "strings" + ] + }, { "slug": "accumulate", "uuid": "5c5a9256-a55e-4bda-baf3-96d052732e11", diff --git a/exercises/resistor-colors/README.md b/exercises/resistor-colors/README.md new file mode 100644 index 000000000..d074cb527 --- /dev/null +++ b/exercises/resistor-colors/README.md @@ -0,0 +1,82 @@ +# Resistor Colors + +If you want to build something using a Raspberry Pi, you'll probably use _resistors_. For this exercise, you need to know two things about them: + +* Each resistor has a resistance value. +* Resistors are small - so small in fact that if you printed the resistance value on them, it would be hard to read. +To get around this problem, manufacturers print color-coded bands onto the resistors to denote their resistance values. Each band acts as a digit of a number. For example, if they printed a brown band (value 1) followed by a green band (value 5), it would translate to the number 15. + +In this exercise, you are going to create a helpful program so that you don't have to remember the values of the bands. The program will take two colors as input, and output the correct number. + +The band colors are encoded as follows: + +- Black: 0 +- Brown: 1 +- Red: 2 +- Orange: 3 +- Yellow: 4 +- Green: 5 +- Blue: 6 +- Violet: 7 +- Grey: 8 +- White: 9 + + +## Getting Started + +For installation and learning resources, refer to the +[exercism help page](http://exercism.io/languages/haskell). + +## Running the tests + +To run the test suite, execute the following command: + +```bash +stack test +``` + +#### If you get an error message like this... + +``` +No .cabal file found in directory +``` + +You are probably running an old stack version and need +to upgrade it. + +#### Otherwise, if you get an error message like this... + +``` +No compiler found, expected minor version match with... +Try running "stack setup" to install the correct GHC... +``` + +Just do as it says and it will download and install +the correct compiler version: + +```bash +stack setup +``` + +## Running *GHCi* + +If you want to play with your solution in GHCi, just run the command: + +```bash +stack ghci +``` + +## Feedback, Issues, Pull Requests + +The [exercism/haskell](https://github.com/exercism/haskell) repository on +GitHub is the home for all of the Haskell exercises. + +If you have feedback about an exercise, or want to help implementing a new +one, head over there and create an issue. We'll do our best to help you! + +## Source + +Maud de Vries, Erik Schierboom [https://github.com/exercism/problem-specifications/issues/1464](https://github.com/exercism/problem-specifications/issues/1464) + +## Submitting Incomplete Solutions +It's possible to submit an incomplete solution so you can see how others have completed the exercise. diff --git a/exercises/resistor-colors/examples/success-standard/package.yaml b/exercises/resistor-colors/examples/success-standard/package.yaml new file mode 100644 index 000000000..f4ec990d1 --- /dev/null +++ b/exercises/resistor-colors/examples/success-standard/package.yaml @@ -0,0 +1,16 @@ +name: resistor-colors + +dependencies: + - base + +library: + exposed-modules: ResistorColors + source-dirs: src + +tests: + test: + main: Tests.hs + source-dirs: test + dependencies: + - resistor-colors + - hspec diff --git a/exercises/resistor-colors/examples/success-standard/src/ResistorColors.hs b/exercises/resistor-colors/examples/success-standard/src/ResistorColors.hs new file mode 100644 index 000000000..940715527 --- /dev/null +++ b/exercises/resistor-colors/examples/success-standard/src/ResistorColors.hs @@ -0,0 +1,29 @@ +module ResistorColors (Color(..), value) where + +data Color = + Black + | Brown + | Red + | Orange + | Yellow + | Green + | Blue + | Violet + | Grey + | White + deriving (Eq, Show, Read) + +convert :: Color -> Int +convert Black = 0 +convert Brown = 1 +convert Red = 2 +convert Orange = 3 +convert Yellow = 4 +convert Green = 5 +convert Blue = 6 +convert Violet = 7 +convert Grey = 8 +convert White = 9 + +value :: [Color] -> Int +value = read . concatMap (show . convert) diff --git a/exercises/resistor-colors/package.yaml b/exercises/resistor-colors/package.yaml new file mode 100644 index 000000000..637f99caa --- /dev/null +++ b/exercises/resistor-colors/package.yaml @@ -0,0 +1,21 @@ +name: resistor-colors +version: 1.0.0.0 + +dependencies: + - base + +library: + exposed-modules: ResistorColors + 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: + - resistor-colors + - hspec diff --git a/exercises/resistor-colors/src/ResistorColors.hs b/exercises/resistor-colors/src/ResistorColors.hs new file mode 100644 index 000000000..e6c5dcdae --- /dev/null +++ b/exercises/resistor-colors/src/ResistorColors.hs @@ -0,0 +1,17 @@ +module ResistorColors (Color(..), value) where + +data Color = + Black + | Brown + | Red + | Orange + | Yellow + | Green + | Blue + | Violet + | Grey + | White + deriving (Eq, Show, Read) + +value :: [Color] -> Int +value cs = error "You need to implement this function." diff --git a/exercises/resistor-colors/stack.yaml b/exercises/resistor-colors/stack.yaml new file mode 100644 index 000000000..8796ce2b9 --- /dev/null +++ b/exercises/resistor-colors/stack.yaml @@ -0,0 +1 @@ +resolver: lts-12.4 diff --git a/exercises/resistor-colors/test/Tests.hs b/exercises/resistor-colors/test/Tests.hs new file mode 100644 index 000000000..3637286f1 --- /dev/null +++ b/exercises/resistor-colors/test/Tests.hs @@ -0,0 +1,44 @@ +{-# OPTIONS_GHC -fno-warn-type-defaults #-} +{-# LANGUAGE RecordWildCards #-} + +import Data.Foldable (for_) +import Test.Hspec (Spec, describe, it, shouldBe) +import Test.Hspec.Runner (configFastFail, defaultConfig, hspecWith) + +import ResistorColors (Color(..), value) + +main :: IO () +main = hspecWith defaultConfig {configFastFail = True} specs + +specs :: Spec +specs = describe "value" $ for_ cases test + where + + test Case{..} = it explanation assertion + where + explanation = unwords [show input, "-", description] + assertion = value input `shouldBe` expected + +data Case = Case { description :: String + , input :: [Color] + , expected :: Int + } + +cases :: [Case] +cases = [ Case { description = "Brown and black" + , input = [Brown, Black] + , expected = 10 + } + , Case { description = "Blue and grey" + , input = [Blue, Grey] + , expected = 68 + } + , Case { description = "Yellow and violet" + , input = [Yellow, Violet] + , expected = 47 + } + , Case { description = "Orange and orange" + , input = [Orange, Orange] + , expected = 33 + } + ]