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

resistor-color-duo: Change to value :: (Color, Color) -> Int #865

Merged
merged 4 commits into from
Nov 9, 2019
Merged
Show file tree
Hide file tree
Changes from 2 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
10 changes: 10 additions & 0 deletions exercises/resistor-color-duo/.meta/hints.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
## Hints

You need to implement the function

```haskell
value :: (Color, Color) -> Int
```

that only accepts two colors and produces the resistor's numeric value
component of its resistance.
12 changes: 12 additions & 0 deletions exercises/resistor-color-duo/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,18 @@ brown-green should return 15
brown-green-violet should return 15 too, ignoring the third color.


## Hints

You need to implement the function

```haskell
value :: (Color, Color) -> Int
```

that only accepts two colors and produces the resistor's numeric value
component of its resistance.



## Getting Started

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,19 @@ data Color =
| Violet
| Grey
| White
deriving (Eq, Show, Read)
deriving (Eq, Show)

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
value1 :: Color -> Int
value1 Black = 0
value1 Brown = 1
value1 Red = 2
value1 Orange = 3
value1 Yellow = 4
value1 Green = 5
value1 Blue = 6
value1 Violet = 7
value1 Grey = 8
value1 White = 9

value :: [Color] -> Int
value = read . concatMap (show . convert) . take 2
value :: (Color, Color) -> Int
value (a, b) = 10 * value1 a + value1 b
2 changes: 1 addition & 1 deletion exercises/resistor-color-duo/package.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: resistor-color-duo
version: 2.1.0.2
version: 2.1.0.3

dependencies:
- base
Expand Down
6 changes: 3 additions & 3 deletions exercises/resistor-color-duo/src/ResistorColors.hs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ data Color =
| Violet
| Grey
| White
deriving (Eq, Show, Read)
deriving (Eq, Show)

value :: [Color] -> Int
value cs = error "You need to implement this function."
value :: (Color, Color) -> Int
value (a, b) = error "You need to implement this function."
14 changes: 5 additions & 9 deletions exercises/resistor-color-duo/test/Tests.hs
Original file line number Diff line number Diff line change
Expand Up @@ -20,29 +20,25 @@ specs = describe "value" $ for_ cases test
assertion = value input `shouldBe` expected

data Case = Case { description :: String
, input :: [Color]
, input :: (Color, Color)
, expected :: Int
}

cases :: [Case]
cases = [ Case { description = "Brown and black"
, input = [Brown, Black]
, input = (Brown, Black)
, expected = 10
}
, Case { description = "Blue and grey"
, input = [Blue, Grey]
, input = (Blue, Grey)
, expected = 68
}
, Case { description = "Yellow and violet"
, input = [Yellow, Violet]
, input = (Yellow, Violet)
, expected = 47
}
, Case { description = "Orange and orange"
, input = [Orange, Orange]
, input = (Orange, Orange)
, expected = 33
}
, Case { description = "Ignore additional colors"
, input = [Green, Brown, Orange]
, expected = 51
}
]