trackler 2.0.8.12 → 2.0.8.13
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/common/exercises/hamming/canonical-data.json +121 -104
- data/lib/trackler/version.rb +1 -1
- data/tracks/delphi/.gitignore +40 -2
- data/tracks/delphi/config.json +8 -0
- data/tracks/delphi/exercises/allergies/{AllergyTests.dpr → Allergies.dpr} +1 -1
- data/tracks/delphi/exercises/allergies/uAllergyTests.pas +1 -1
- data/tracks/delphi/exercises/bank-account/{BankAccountTests.dpr → BankAccount.dpr} +1 -1
- data/tracks/delphi/exercises/bank-account/uBankAccountTests.pas +1 -1
- data/tracks/delphi/exercises/beer-song/{BeerSongTests.dpr → BeerSong.dpr} +1 -1
- data/tracks/delphi/exercises/beer-song/uBeerSongTests.pas +7 -6
- data/tracks/delphi/exercises/binary-search/{BinarySearchTest.dpr → BinarySearch.dpr} +1 -1
- data/tracks/delphi/exercises/binary-search/uBinarySearchTest.pas +9 -8
- data/tracks/delphi/exercises/bob/uBobTests.pas +18 -17
- data/tracks/delphi/exercises/book-store/{BookStoreTests.dpr → BookStore.dpr} +1 -1
- data/tracks/delphi/exercises/book-store/uBookStoreTests.pas +12 -11
- data/tracks/delphi/exercises/bowling/{BowlingTests.dpr → Bowling.dpr} +1 -1
- data/tracks/delphi/exercises/bowling/uBowlingTests.pas +24 -23
- data/tracks/delphi/exercises/circular-buffer/{CircularBufferTests.dpr → CircularBuffer.dpr} +1 -1
- data/tracks/delphi/exercises/circular-buffer/uCircularBufferTests.pas +1 -1
- data/tracks/delphi/exercises/clock/{ClockTest.dpr → Clock.dpr} +1 -1
- data/tracks/delphi/exercises/clock/uClockTest.pas +13 -5
- data/tracks/delphi/exercises/etl/{ETLtests.dpr → ETL.dpr} +1 -1
- data/tracks/delphi/exercises/etl/uETLtests.pas +1 -1
- data/tracks/delphi/exercises/grains/{GrainsTests.dpr → Grains.dpr} +1 -1
- data/tracks/delphi/exercises/grains/uGrainsTests.pas +11 -10
- data/tracks/delphi/exercises/hamming/uHammingTests.pas +1 -0
- data/tracks/delphi/exercises/leap/{LeapTest.dpr → Leap.dpr} +1 -1
- data/tracks/delphi/exercises/leap/uLeapTests.pas +1 -0
- data/tracks/delphi/exercises/nucleotide-count/{NucleotideCountTest.dpr → NucleotideCount.dpr} +1 -1
- data/tracks/delphi/exercises/nucleotide-count/uNucleotideCountTest.pas +9 -8
- data/tracks/delphi/exercises/perfect-numbers/{PerfectNumbersTest.dpr → PerfectNumbers.dpr} +1 -1
- data/tracks/delphi/exercises/perfect-numbers/uPerfectNumbersTest.pas +71 -20
- data/tracks/delphi/exercises/phone-number/{PhoneNumberTests.dpr → PhoneNumber.dpr} +1 -1
- data/tracks/delphi/exercises/phone-number/uPhoneNumberTests.pas +1 -0
- data/tracks/delphi/exercises/poker/uPokerTest.pas +1 -0
- data/tracks/delphi/exercises/raindrops/raindrops.dpr +60 -0
- data/tracks/delphi/exercises/raindrops/uRaindropsExample.pas +27 -0
- data/tracks/delphi/exercises/raindrops/uRaindropsTest.pas +180 -0
- data/tracks/delphi/exercises/rna-transcription/{TestRnaTranscription.dpr → RnaTranscription.dpr} +1 -1
- data/tracks/delphi/exercises/rna-transcription/uTestRnaTranscription.pas +8 -7
- data/tracks/delphi/exercises/saddle-points/{TestSaddlePoints.dpr → SaddlePoints.dpr} +1 -1
- data/tracks/delphi/exercises/saddle-points/uSaddlePointsTests.pas +6 -5
- data/tracks/go/config.json +12 -4
- data/tracks/go/exercises/accumulate/accumulate_test.go +2 -4
- data/tracks/go/exercises/luhn/cases_test.go +76 -0
- data/tracks/go/exercises/luhn/example.go +26 -26
- data/tracks/go/exercises/luhn/example_gen.go +50 -0
- data/tracks/go/exercises/luhn/luhn_test.go +1 -14
- data/tracks/go/exercises/perfect-numbers/perfect_numbers_test.go +14 -13
- data/tracks/java/exercises/rna-transcription/src/example/java/RnaTranscription.java +1 -1
- data/tracks/java/exercises/rna-transcription/src/main/java/RnaTranscription.java +5 -0
- data/tracks/java/exercises/rna-transcription/src/test/java/RnaTranscriptionTest.java +14 -7
- data/tracks/julia/.travis.yml +1 -0
- data/tracks/lisp/docs/ABOUT.md +10 -7
- data/tracks/lisp/docs/INSTALLATION.md +12 -4
- data/tracks/lisp/docs/LEARNING.md +11 -5
- data/tracks/lisp/docs/RESOURCES.md +18 -9
- data/tracks/lisp/docs/TESTS.md +4 -0
- data/tracks/scala/exercises/simple-linked-list/src/test/scala/SimpleLinkedListTest.scala +9 -1
- data/tracks/typescript/config.json +6 -0
- data/tracks/typescript/exercises/food-chain/food-chain.example.ts +54 -0
- data/tracks/typescript/exercises/food-chain/food-chain.test.ts +163 -0
- data/tracks/typescript/exercises/food-chain/package.json +37 -0
- data/tracks/typescript/exercises/food-chain/tsconfig.json +21 -0
- data/tracks/typescript/exercises/food-chain/tslint.json +127 -0
- data/tracks/typescript/exercises/food-chain/yarn.lock +2697 -0
- metadata +30 -19
- data/tracks/java/exercises/rna-transcription/src/main/java/.keep +0 -0
@@ -1,40 +1,40 @@
|
|
1
1
|
package luhn
|
2
2
|
|
3
|
-
|
3
|
+
import "strings"
|
4
4
|
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
if len(d) == 0 {
|
5
|
+
const testVersion = 2
|
6
|
+
|
7
|
+
func Valid(id string) bool {
|
8
|
+
|
9
|
+
if len(strings.TrimSpace(id)) == 1 {
|
11
10
|
return false
|
12
11
|
}
|
13
|
-
last := len(d) - 1
|
14
|
-
return (check(d[:last])+d[last])%10 == 0
|
15
|
-
}
|
16
12
|
|
17
|
-
|
18
|
-
|
19
|
-
for _, r := range
|
20
|
-
if r
|
21
|
-
|
13
|
+
d := make([]int, 0, len(id))
|
14
|
+
|
15
|
+
for _, r := range id {
|
16
|
+
if r == ' ' {
|
17
|
+
continue
|
18
|
+
}
|
19
|
+
if r < '0' || r > '9' {
|
20
|
+
return false
|
22
21
|
}
|
22
|
+
d = append(d, int(r-'0'))
|
23
23
|
}
|
24
|
-
|
24
|
+
|
25
|
+
return sum(d)%10 == 0
|
25
26
|
}
|
26
27
|
|
27
|
-
func
|
28
|
-
for i :=
|
29
|
-
|
30
|
-
if
|
31
|
-
x
|
28
|
+
func sum(d []int) (s int) {
|
29
|
+
for i, x := range d {
|
30
|
+
j := len(d) - i
|
31
|
+
if j%2 == 0 {
|
32
|
+
x *= 2
|
33
|
+
if x > 9 {
|
34
|
+
x -= 9
|
35
|
+
}
|
32
36
|
}
|
33
|
-
d[i] = x
|
34
|
-
}
|
35
|
-
s := 0
|
36
|
-
for _, x := range d {
|
37
37
|
s += x
|
38
38
|
}
|
39
|
-
return
|
39
|
+
return
|
40
40
|
}
|
@@ -0,0 +1,50 @@
|
|
1
|
+
// +build ignore
|
2
|
+
|
3
|
+
package main
|
4
|
+
|
5
|
+
import (
|
6
|
+
"log"
|
7
|
+
"text/template"
|
8
|
+
|
9
|
+
"../../gen"
|
10
|
+
)
|
11
|
+
|
12
|
+
func main() {
|
13
|
+
t, err := template.New("").Parse(tmpl)
|
14
|
+
if err != nil {
|
15
|
+
log.Fatal(err)
|
16
|
+
}
|
17
|
+
var j js
|
18
|
+
if err := gen.Gen("luhn", &j, t); err != nil {
|
19
|
+
log.Fatal(err)
|
20
|
+
}
|
21
|
+
}
|
22
|
+
|
23
|
+
// The JSON structure we expect to be able to unmarshal into
|
24
|
+
type js struct {
|
25
|
+
Valid []struct {
|
26
|
+
Description string
|
27
|
+
Input string
|
28
|
+
Expected bool
|
29
|
+
}
|
30
|
+
}
|
31
|
+
|
32
|
+
// template applied to above data structure generates the Go test cases
|
33
|
+
var tmpl = `package luhn
|
34
|
+
|
35
|
+
// Source: {{.Ori}}
|
36
|
+
{{if .Commit}}// Commit: {{.Commit}}
|
37
|
+
{{end}}
|
38
|
+
|
39
|
+
var testCases = []struct {
|
40
|
+
description string
|
41
|
+
input string
|
42
|
+
ok bool
|
43
|
+
}{
|
44
|
+
{{range .J.Valid}}{
|
45
|
+
{{printf "%q" .Description}},
|
46
|
+
{{printf "%q" .Input}},
|
47
|
+
{{.Expected}},
|
48
|
+
},
|
49
|
+
{{end}}
|
50
|
+
}`
|
@@ -2,20 +2,7 @@ package luhn
|
|
2
2
|
|
3
3
|
import "testing"
|
4
4
|
|
5
|
-
const targetTestVersion =
|
6
|
-
|
7
|
-
var testCases = []struct {
|
8
|
-
input string
|
9
|
-
description string
|
10
|
-
ok bool
|
11
|
-
}{
|
12
|
-
{"1", "single digit strings can not be valid", false},
|
13
|
-
{"0", "a single zero is invalid", false},
|
14
|
-
{"046 454 286", "valid Canadian SIN", true},
|
15
|
-
{"046 454 287", "invalid Canadian SIN", false},
|
16
|
-
{"8273 1232 7352 0569", "invalid credit card", false},
|
17
|
-
{"827a 1232 7352 0569", "strings that contain non-digits are not valid", false},
|
18
|
-
}
|
5
|
+
const targetTestVersion = 2
|
19
6
|
|
20
7
|
func TestTestVersion(t *testing.T) {
|
21
8
|
if testVersion != targetTestVersion {
|
@@ -4,6 +4,19 @@ import "testing"
|
|
4
4
|
|
5
5
|
var _ error = ErrOnlyPositive
|
6
6
|
|
7
|
+
var classificationTestCases = []struct {
|
8
|
+
input uint64
|
9
|
+
expected Classification
|
10
|
+
}{
|
11
|
+
{1, ClassificationDeficient},
|
12
|
+
{13, ClassificationDeficient},
|
13
|
+
{12, ClassificationAbundant},
|
14
|
+
{6, ClassificationPerfect},
|
15
|
+
{28, ClassificationPerfect},
|
16
|
+
{496, ClassificationPerfect},
|
17
|
+
{8128, ClassificationPerfect},
|
18
|
+
}
|
19
|
+
|
7
20
|
const targetTestVersion = 1
|
8
21
|
|
9
22
|
func TestTestVersion(t *testing.T) {
|
@@ -19,19 +32,7 @@ func TestGivesPositiveRequiredError(t *testing.T) {
|
|
19
32
|
}
|
20
33
|
|
21
34
|
func TestClassifiesCorrectly(t *testing.T) {
|
22
|
-
|
23
|
-
input uint64
|
24
|
-
expected Classification
|
25
|
-
}{
|
26
|
-
{1, ClassificationDeficient},
|
27
|
-
{13, ClassificationDeficient},
|
28
|
-
{12, ClassificationAbundant},
|
29
|
-
{6, ClassificationPerfect},
|
30
|
-
{28, ClassificationPerfect},
|
31
|
-
{496, ClassificationPerfect},
|
32
|
-
{8128, ClassificationPerfect},
|
33
|
-
}
|
34
|
-
for _, c := range cases {
|
35
|
+
for _, c := range classificationTestCases {
|
35
36
|
if cat, err := Classify(c.input); err != nil {
|
36
37
|
t.Errorf("%d: Expected no error but got %s", c.input, err)
|
37
38
|
} else if cat != c.expected {
|
@@ -1,42 +1,49 @@
|
|
1
1
|
import org.junit.Assert;
|
2
|
-
import org.junit.
|
2
|
+
import org.junit.Before;
|
3
3
|
import org.junit.Ignore;
|
4
|
+
import org.junit.Test;
|
4
5
|
|
5
6
|
public class RnaTranscriptionTest {
|
6
7
|
|
8
|
+
private RnaTranscription rnaTranscription;
|
9
|
+
|
10
|
+
@Before
|
11
|
+
public void setUp() {
|
12
|
+
rnaTranscription = new RnaTranscription();
|
13
|
+
}
|
7
14
|
|
8
15
|
@Test
|
9
16
|
public void testRnaTranscriptionOfEmptyDnaIsEmptyRna() {
|
10
|
-
Assert.assertEquals("",
|
17
|
+
Assert.assertEquals("", rnaTranscription.ofDna(""));
|
11
18
|
}
|
12
19
|
|
13
20
|
@Ignore
|
14
21
|
@Test
|
15
22
|
public void testRnaTranscriptionOfCytosineIsGuanine() {
|
16
|
-
Assert.assertEquals("G",
|
23
|
+
Assert.assertEquals("G", rnaTranscription.ofDna("C"));
|
17
24
|
}
|
18
25
|
|
19
26
|
@Ignore
|
20
27
|
@Test
|
21
28
|
public void testRnaTranscriptionOfGuanineIsCytosine() {
|
22
|
-
Assert.assertEquals("C",
|
29
|
+
Assert.assertEquals("C", rnaTranscription.ofDna("G"));
|
23
30
|
}
|
24
31
|
|
25
32
|
@Ignore
|
26
33
|
@Test
|
27
34
|
public void testRnaTranscriptionOfThymineIsAdenine() {
|
28
|
-
Assert.assertEquals("A",
|
35
|
+
Assert.assertEquals("A", rnaTranscription.ofDna("T"));
|
29
36
|
}
|
30
37
|
|
31
38
|
@Ignore
|
32
39
|
@Test
|
33
40
|
public void testRnaTranscriptionOfAdenineIsUracil() {
|
34
|
-
Assert.assertEquals("U",
|
41
|
+
Assert.assertEquals("U", rnaTranscription.ofDna("A"));
|
35
42
|
}
|
36
43
|
|
37
44
|
@Ignore
|
38
45
|
@Test
|
39
46
|
public void testRnaTranscription() {
|
40
|
-
Assert.assertEquals("UGCACCAGAAUU",
|
47
|
+
Assert.assertEquals("UGCACCAGAAUU", rnaTranscription.ofDna("ACGTGGTCTTAA"));
|
41
48
|
}
|
42
49
|
}
|
data/tracks/julia/.travis.yml
CHANGED
data/tracks/lisp/docs/ABOUT.md
CHANGED
@@ -1,9 +1,12 @@
|
|
1
|
-
Common Lisp is an offshoot of the long-running family of Lisp
|
2
|
-
It's a multi-paradigm programming language that
|
3
|
-
according to your
|
1
|
+
Common Lisp is an offshoot of the long-running family of Lisp
|
2
|
+
programming languages. It's a multi-paradigm programming language that
|
3
|
+
allows you to choose the approach and paradigm according to your
|
4
|
+
application domain.
|
4
5
|
|
5
|
-
Common Lisp has fast prototyping capabilities, and exceptional support
|
6
|
-
It also boasts a macro system which
|
7
|
-
|
6
|
+
Common Lisp has fast prototyping capabilities, and exceptional support
|
7
|
+
for object oriented programming. It also boasts a macro system which
|
8
|
+
allows you to adapt the language to your needs, and a run-time
|
9
|
+
environment which allows modification of running applications.
|
8
10
|
|
9
|
-
To learn more about Common Lisp, take a look
|
11
|
+
To learn more about Common Lisp, take a look
|
12
|
+
at [the Common Lisp homepage](https://common-lisp.net/).
|
@@ -1,4 +1,6 @@
|
|
1
|
-
Install a lisp implementation such as SBCL
|
1
|
+
Install a lisp implementation such as [SBCL](http://www.sbcl.org/)
|
2
|
+
or [CLisp](http://clisp.org/). Both can be installed via Homebrew on
|
3
|
+
Mac OS X.
|
2
4
|
|
3
5
|
```bash
|
4
6
|
brew install sbcl
|
@@ -6,7 +8,11 @@ brew install sbcl
|
|
6
8
|
brew install clisp
|
7
9
|
```
|
8
10
|
|
9
|
-
|
11
|
+
(See their homepages for instructions for installing on other
|
12
|
+
platforms.)
|
13
|
+
|
14
|
+
Then install [QuickLisp](http://www.quicklisp.org/beta/#installation)
|
15
|
+
and download
|
10
16
|
[http://beta.quicklisp.org/quicklisp.lisp](http://beta.quicklisp.org/quicklisp.lisp).
|
11
17
|
|
12
18
|
```bash
|
@@ -19,7 +25,8 @@ Load it into your running Lisp implementation, and type:
|
|
19
25
|
(quicklisp-quickstart:install)
|
20
26
|
```
|
21
27
|
|
22
|
-
To add the startup code to your init file so that the next time you
|
28
|
+
To add the startup code to your init file so that the next time you
|
29
|
+
start the lisp interpreter QuickLisp will be loaded, type:
|
23
30
|
|
24
31
|
```lisp
|
25
32
|
(ql:add-to-init-file)
|
@@ -31,4 +38,5 @@ Optionally load lisp-unit:
|
|
31
38
|
(ql:quickload "lisp-unit")
|
32
39
|
```
|
33
40
|
|
34
|
-
This will be done the first time you run any exercises tests. But you
|
41
|
+
This will be done the first time you run any exercises tests. But you
|
42
|
+
can do it now if you want.
|
@@ -2,8 +2,14 @@ Exercism provides exercises and feedback but can be difficult to jump
|
|
2
2
|
into for those learning Common Lisp for the first time. These
|
3
3
|
resources can help you get started:
|
4
4
|
|
5
|
-
* [Cliki.net Getting Start Started Page](http://cliki.net/Getting%20Started),
|
6
|
-
|
7
|
-
* [
|
8
|
-
|
9
|
-
* [
|
5
|
+
* [Cliki.net Getting Start Started Page](http://cliki.net/Getting%20Started),
|
6
|
+
by various contributors
|
7
|
+
* [Wikibooks book of Common Lisp, First steps](http://en.wikibooks.org/wiki/Common_Lisp/First_steps),
|
8
|
+
by various contributors
|
9
|
+
* [Learn X in Y minutes: where X = Common Lisp](http://learnxinyminutes.com/docs/common-lisp/) by
|
10
|
+
Paul Nathan
|
11
|
+
* [Northwestern University EECS 325 Readings](http://www.cs.northwestern.edu/academics/courses/325/readings/readings.php),
|
12
|
+
by Chris Riesbeck
|
13
|
+
* [The Common Lisp Cookbook](http://lispcookbook.github.io/cl-cookbook/),
|
14
|
+
by various contributors
|
15
|
+
([legacy SF site](http://cl-cookbook.sourceforge.net/))
|
@@ -3,10 +3,14 @@ one of the learning section.
|
|
3
3
|
|
4
4
|
### Important resources
|
5
5
|
|
6
|
-
* [The Common Lisp HyperSpec](http://www.lispworks.com/documentation/common-lisp.html),
|
7
|
-
|
8
|
-
* [Common Lisp
|
9
|
-
|
6
|
+
* [The Common Lisp HyperSpec](http://www.lispworks.com/documentation/common-lisp.html),
|
7
|
+
by X3J13 committee. The Common Lisp Standard
|
8
|
+
* [Common Lisp the Language, 2nd Edition](http://www.cs.cmu.edu/Groups/AI/html/cltl/cltl2.html),
|
9
|
+
by Guy Steele
|
10
|
+
* [Common Lisp Document Repository](http://cdr.eurolisp.org/) a
|
11
|
+
collection of emergent and de-facto standards
|
12
|
+
* [Common-Lisp.net](http://common-lisp.net/), project hosting and
|
13
|
+
gateway
|
10
14
|
* [The Common Lisp Wiki](http://www.cliki.net/)
|
11
15
|
|
12
16
|
### Free books
|
@@ -14,16 +18,21 @@ one of the learning section.
|
|
14
18
|
There are many other great books you can find links to. These are the
|
15
19
|
most popularly linked introductory works.
|
16
20
|
|
17
|
-
* [Practical Common Lisp](http://www.gigamonkeys.com/book/), by Peter
|
18
|
-
|
19
|
-
* [Common Lisp:
|
20
|
-
|
21
|
+
* [Practical Common Lisp](http://www.gigamonkeys.com/book/), by Peter
|
22
|
+
Seibel
|
23
|
+
* [Common Lisp: A Gentle Introduction to Symbolic Computation](http://www-cgi.cs.cmu.edu/afs/cs.cmu.edu/user/dst/www/LispBook/index.html) (PDF),
|
24
|
+
by David S. Touretzky
|
25
|
+
* [Common Lisp: An Interactive Approach](http://www.cse.buffalo.edu/~shapiro/Commonlisp/) (PDF),
|
26
|
+
by Stuart C. Shapiro
|
27
|
+
* [Successful Lisp: How to Understand and Use Common Lisp](http://psg.com/~dlamkins/sl/cover.html),
|
28
|
+
by David B. Lamkins
|
21
29
|
|
22
30
|
Following these are two more advanced books, but beginners shouldn't
|
23
31
|
be afraid of them, they're quite accessible and "classics".
|
24
32
|
|
25
33
|
* [On Lisp](http://www.paulgraham.com/onlisptext.html), by Paul Graham
|
26
|
-
* [Structure and Interpretation of Computer Programs](http://mitpress.mit.edu/sicp/),
|
34
|
+
* [Structure and Interpretation of Computer Programs](http://mitpress.mit.edu/sicp/),
|
35
|
+
by Abelson, Sussman, and Sussman
|
27
36
|
|
28
37
|
The last is actually on a related Lisp dialect, Scheme, but it's very
|
29
38
|
helpful book, generally. If you find you prefer Scheme, it's an
|
data/tracks/lisp/docs/TESTS.md
CHANGED
@@ -1,3 +1,7 @@
|
|
1
|
+
Start up your Lisp implementation in the directory of the exercise you
|
2
|
+
are working on (or change the current directory for an already running
|
3
|
+
Lisp implementation to that directory).
|
4
|
+
|
1
5
|
Load the test file into your running Lisp implementation, for example,
|
2
6
|
`(load "point-mutations-test")`. This will run the tests the first
|
3
7
|
time automatically. After that you can run the test suite in the REPL
|
@@ -61,7 +61,7 @@ class SimpleLinkedListTest extends FlatSpec with Matchers with GeneratorDrivenPr
|
|
61
61
|
reversed.next.next.next.next.value should be (2)
|
62
62
|
reversed.next.next.next.next.next.value should be (1)
|
63
63
|
}
|
64
|
-
|
64
|
+
|
65
65
|
it should "handle arbitrary list fromSeq toSeq" in {
|
66
66
|
pending
|
67
67
|
forAll { seq: Seq[Int] =>
|
@@ -99,6 +99,14 @@ class SimpleLinkedListTest extends FlatSpec with Matchers with GeneratorDrivenPr
|
|
99
99
|
}
|
100
100
|
}
|
101
101
|
|
102
|
+
it should "return original arbitrary list from added list elements" in {
|
103
|
+
pending
|
104
|
+
forAll { xs: Seq[Int] =>
|
105
|
+
val list = xs.foldLeft(SimpleLinkedList[Int]())(_.add(_))
|
106
|
+
assert(list.toSeq == xs)
|
107
|
+
}
|
108
|
+
}
|
109
|
+
|
102
110
|
it should "handle arbitrary generics" in {
|
103
111
|
pending
|
104
112
|
forAll { xs: Seq[String] =>
|
@@ -0,0 +1,54 @@
|
|
1
|
+
const animals = ["fly", "spider", "bird", "cat", "dog", "goat", "cow", "horse"]
|
2
|
+
|
3
|
+
export default class FoodChain {
|
4
|
+
|
5
|
+
public static verses(start = 1, end = 8): string {
|
6
|
+
if (end < start) {throw new Error("end should be smaller than the start")}
|
7
|
+
let verses = ""
|
8
|
+
|
9
|
+
for ( ; start <= end; start += 1) {
|
10
|
+
verses += FoodChain.verse(start)
|
11
|
+
if (start !== end) {
|
12
|
+
verses += "\n"
|
13
|
+
}
|
14
|
+
}
|
15
|
+
return verses
|
16
|
+
}
|
17
|
+
|
18
|
+
public static verse(num: number): string {
|
19
|
+
let result = ""
|
20
|
+
|
21
|
+
let index = num - 1
|
22
|
+
result += `I know an old lady who swallowed a ${animals[index]}.\n`
|
23
|
+
|
24
|
+
switch (num) {
|
25
|
+
case 2: result += "It wriggled and jiggled and tickled inside her.\n"
|
26
|
+
break
|
27
|
+
case 3: result += "How absurd to swallow a bird!\n"
|
28
|
+
break
|
29
|
+
case 4: result += "Imagine that, to swallow a cat!\n"
|
30
|
+
break
|
31
|
+
case 5: result += "What a hog, to swallow a dog!\n"
|
32
|
+
break
|
33
|
+
case 6: result += "Just opened her throat and swallowed a goat!\n"
|
34
|
+
break
|
35
|
+
case 7: result += "I don't know how she swallowed a cow!\n"
|
36
|
+
break
|
37
|
+
case 8: result += "She's dead, of course!\n"
|
38
|
+
return result
|
39
|
+
default: break
|
40
|
+
}
|
41
|
+
|
42
|
+
while ( index >= 1 ) {
|
43
|
+
result += `She swallowed the ${animals[index]} to catch the ${animals[index - 1]}`
|
44
|
+
if ( index === 2 ) {
|
45
|
+
result += " that wriggled and jiggled and tickled inside her"
|
46
|
+
}
|
47
|
+
result += ".\n"
|
48
|
+
index -= 1
|
49
|
+
}
|
50
|
+
result += "I don't know why she swallowed the fly. Perhaps she'll die."
|
51
|
+
|
52
|
+
return result + "\n"
|
53
|
+
}
|
54
|
+
}
|