-
-
Notifications
You must be signed in to change notification settings - Fork 198
Implement Resistor Colors exercise #808
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
Merged
sshine
merged 1 commit into
exercism:master
from
tejasbubane:implement-resistor-colors-exercise
Mar 12, 2019
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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. |
16 changes: 16 additions & 0 deletions
16
exercises/resistor-colors/examples/success-standard/package.yaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 |
29 changes: 29 additions & 0 deletions
29
exercises/resistor-colors/examples/success-standard/src/ResistorColors.hs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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." |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| resolver: lts-12.4 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 | ||
| } | ||
| ] |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I notice that it would be possible to use http://hackage.haskell.org/package/base-4.12.0.0/docs/Prelude.html#t:Enum here.
I have not yet determined whether it would be a good idea to do so.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I had the same thought.
What you don't get with
deriving Enumis an explicit annotation thatBlackis 0, etc.Making an explicit
instance Enum where ...has the drawback that you need to specify bothtoEnumandfromEnum, even though the exercise only needs the one. There are ways to avoid that, but the first way (using a dynamic lookup table) should be discouraged, and the second way (usingDataandTypeable) is probably a little too complicated to serve as a good example.So I'm content with having a simple
convertfunction.