Skip to content

Commit af670b8

Browse files
committed
elm - bob
1 parent e8ec7aa commit af670b8

File tree

5 files changed

+258
-0
lines changed

5 files changed

+258
-0
lines changed

elm/bob/Bob.elm

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
module Bob exposing (..)
2+
3+
import Regex exposing (contains, regex)
4+
import String exposing (endsWith, toUpper, trim)
5+
6+
7+
yell : String -> Bool
8+
yell str =
9+
toUpper str == str && contains (regex "[a-zA-Z]") str
10+
11+
12+
question : String -> Bool
13+
question str =
14+
endsWith "?" str
15+
16+
17+
nothing : String -> Bool
18+
nothing str =
19+
trim str == ""
20+
21+
22+
hey : String -> String
23+
hey str =
24+
if yell str then
25+
"Whoa, chill out!"
26+
else if question str then
27+
"Sure."
28+
else if nothing str then
29+
"Fine. Be that way!"
30+
else
31+
"Whatever."

elm/bob/README.md

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Bob
2+
3+
Bob is a lackadaisical teenager. In conversation, his responses are very limited.
4+
5+
Bob answers 'Sure.' if you ask him a question.
6+
7+
He answers 'Whoa, chill out!' if you yell at him.
8+
9+
He says 'Fine. Be that way!' if you address him without actually saying
10+
anything.
11+
12+
He answers 'Whatever.' to anything else.
13+
14+
## Elm Installation
15+
16+
Refer to the [Exercism help page](http://exercism.io/languages/elm) for Elm
17+
installation and learning resources.
18+
19+
## Writing the Code
20+
21+
The first time you start an exercise, you'll need to ensure you have the
22+
appropriate dependencies installed.
23+
24+
```bash
25+
$ npm install
26+
```
27+
28+
Execute the tests with:
29+
30+
```bash
31+
$ npm test
32+
```
33+
34+
## Source
35+
36+
Inspired by the 'Deaf Grandma' exercise in Chris Pine's Learn to Program tutorial. [http://pine.fm/LearnToProgram/?Chapter=06](http://pine.fm/LearnToProgram/?Chapter=06)
37+
38+
## Submitting Incomplete Problems
39+
It's possible to submit an incomplete solution so you can see how others have completed the exercise.
40+

elm/bob/Tests.elm

+158
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
port module Main exposing (..)
2+
3+
import Test.Runner.Node exposing (run, TestProgram)
4+
import Json.Encode exposing (Value)
5+
import Test exposing (..)
6+
import Expect
7+
import String
8+
import Char
9+
import Random
10+
import Bob
11+
12+
13+
tests : Test
14+
tests =
15+
describe "Bob"
16+
[ test "stating something" <|
17+
\() ->
18+
Expect.equal "Whatever."
19+
(Bob.hey "Tom-ay-to, tom-aaaah-to.")
20+
, test "shouting" <|
21+
\() ->
22+
Expect.equal
23+
"Whoa, chill out!"
24+
(Bob.hey "WATCH OUT!")
25+
, test "shouting gibberish" <|
26+
\() ->
27+
Expect.equal
28+
"Whoa, chill out!"
29+
(Bob.hey (uppercaseGibberish 10))
30+
, test "asking a question" <|
31+
\() ->
32+
Expect.equal
33+
"Sure."
34+
(Bob.hey "Does this cryogenic chamber make me look fat?")
35+
, test "asking a numeric question" <|
36+
\() ->
37+
Expect.equal
38+
"Sure."
39+
(Bob.hey "You are, what, like 15?")
40+
, test "asking gibberish" <|
41+
\() ->
42+
Expect.equal
43+
"Sure."
44+
(Bob.hey (gibberishQuestion 20))
45+
, test "talking forcefully" <|
46+
\() ->
47+
Expect.equal
48+
"Whatever."
49+
(Bob.hey "Let's go make out behind the gym!")
50+
, test "using acronyms in regular speech" <|
51+
\() ->
52+
Expect.equal
53+
"Whatever."
54+
(Bob.hey "It's OK if you don't want to go to the DMV.")
55+
, test "forceful questions" <|
56+
\() ->
57+
Expect.equal
58+
"Whoa, chill out!"
59+
(Bob.hey "WHAT THE HELL WERE YOU THINKING?")
60+
, test "shouting numbers" <|
61+
\() ->
62+
Expect.equal
63+
"Whoa, chill out!"
64+
(Bob.hey "1, 2, 3 GO!")
65+
, test "only numbers" <|
66+
\() ->
67+
Expect.equal
68+
"Whatever."
69+
(Bob.hey "1, 2, 3")
70+
, test "question with only numbers" <|
71+
\() ->
72+
Expect.equal
73+
"Sure."
74+
(Bob.hey "4?")
75+
, test "shouting with special characters" <|
76+
\() ->
77+
Expect.equal
78+
"Whoa, chill out!"
79+
(Bob.hey "ZOMG THE %^*@#$(*^ ZOMBIES ARE COMING!!11!!1!")
80+
, test "shouting with no exclamation mark" <|
81+
\() ->
82+
Expect.equal
83+
"Whoa, chill out!"
84+
(Bob.hey "I HATE YOU")
85+
, test "statement containing a question mark" <|
86+
\() ->
87+
Expect.equal
88+
"Whatever."
89+
(Bob.hey "Ending with ? means a question.")
90+
, test "prattling on" <|
91+
\() ->
92+
Expect.equal
93+
"Sure."
94+
(Bob.hey "Wait! Hang on. Are you going to be OK?")
95+
, test "silence" <|
96+
\() ->
97+
Expect.equal
98+
"Fine. Be that way!"
99+
(Bob.hey "")
100+
, test "prolonged silence" <|
101+
\() ->
102+
Expect.equal
103+
"Fine. Be that way!"
104+
(Bob.hey " ")
105+
, test "alternate silences" <|
106+
\() ->
107+
Expect.equal
108+
"Fine. Be that way!"
109+
(Bob.hey "\t \n \t ")
110+
, test "on multiple line questions" <|
111+
\() ->
112+
Expect.equal
113+
"Whatever."
114+
(Bob.hey "\nDoes this cryogenic chamber make me look fat?\nno")
115+
]
116+
117+
118+
character : Int -> Int -> Random.Generator Char
119+
character start end =
120+
Random.map Char.fromCode (Random.int start end)
121+
122+
123+
anyCharacter : Random.Generator Char
124+
anyCharacter =
125+
character 32 126
126+
127+
128+
uppercaseCharacter : Random.Generator Char
129+
uppercaseCharacter =
130+
character 65 90
131+
132+
133+
listOfCharacters : Int -> Random.Generator Char -> Random.Generator (List Char)
134+
listOfCharacters length characterList =
135+
Random.list length characterList
136+
137+
138+
gibberish : Int -> Random.Generator Char -> String
139+
gibberish length characterList =
140+
Tuple.first (Random.step (Random.map String.fromList (listOfCharacters length characterList)) (Random.initialSeed 424242))
141+
142+
143+
uppercaseGibberish : Int -> String
144+
uppercaseGibberish length =
145+
gibberish length uppercaseCharacter
146+
147+
148+
gibberishQuestion : Int -> String
149+
gibberishQuestion length =
150+
(gibberish length anyCharacter) ++ "?"
151+
152+
153+
main : TestProgram
154+
main =
155+
run emit tests
156+
157+
158+
port emit : ( String, Value ) -> Cmd msg

elm/bob/elm-package.json

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"version": "3.0.0",
3+
"summary": "Exercism problems in Elm.",
4+
"repository": "https://github.com/exercism/xelm.git",
5+
"license": "BSD3",
6+
"source-directories": [
7+
"."
8+
],
9+
"exposed-modules": [],
10+
"dependencies": {
11+
"elm-lang/core": "5.0.0 <= v < 6.0.0",
12+
"elm-community/elm-test": "3.0.0 <= v < 4.0.0",
13+
"rtfeldman/node-test-runner": "3.0.0 <= v < 4.0.0"
14+
},
15+
"elm-version": "0.18.0 <= v < 0.19.0"
16+
}

elm/bob/package.json

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"description": "Exercism/Elm",
3+
"repository": "https://github.com/exercism/xelm.git",
4+
"license": "MIT",
5+
"scripts": {
6+
"postinstall": "elm-package install -y",
7+
"test": "elm-test Tests.elm"
8+
},
9+
"dependencies": {
10+
"elm": "^0.18.0",
11+
"elm-test": "^0.18.0"
12+
}
13+
}

0 commit comments

Comments
 (0)