Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
6 changes: 6 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
"custom-set",
"wordy",
"palindrome-products",
"twelve-days",
"sublist"
],
"exercises": [
Expand Down Expand Up @@ -307,6 +308,11 @@
"difficulty": 1,
"topics": []
},
{
"slug": "twelve-days",
"difficulty": 1,
"topics": []
},
{
"slug": "sublist",
"difficulty": 1,
Expand Down
2 changes: 2 additions & 0 deletions exercises/settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,5 @@ include 'triangle'
include 'trinary'
include 'word-count'
include 'wordy'
include 'twelve-days'

17 changes: 17 additions & 0 deletions exercises/twelve-days/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
apply plugin: "java"
apply plugin: "eclipse"
apply plugin: "idea"

repositories {
mavenCentral()
}

dependencies {
testCompile "junit:junit:4.12"
}
test {
testLogging {
exceptionFormat = 'full'
events = ["passed", "failed", "skipped"]
}
}
46 changes: 46 additions & 0 deletions exercises/twelve-days/src/example/java/TwelveDays.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
public class TwelveDays {
private static String[] days = new String[] {"first", "second", "third", "fourth", "fifth", "sixth", "seventh",
"eighth", "ninth", "tenth", "eleventh", "twelfth"};
private static String[] gifts = new String[] {
"a Partridge in a Pear Tree.",
"two Turtle Doves, ",
"three French Hens, ",
"four Calling Birds, ",
"five Gold Rings, ",
"six Geese-a-Laying, ",
"seven Swans-a-Swimming, ",
"eight Maids-a-Milking, ",
"nine Ladies Dancing, ",
"ten Lords-a-Leaping, ",
"eleven Pipers Piping, ",
"twelve Drummers Drumming, "
};

public static String verse(int verseNumber) {
return constructVerse(verseNumber);
}

public static String verses(int verseNumberStart, int verseNumberEnd) {
StringBuilder stringBuilder = new StringBuilder(constructVerse(verseNumberStart));
for (int i = verseNumberStart + 1; i <= verseNumberEnd; i++) {
stringBuilder.append("\n");
stringBuilder.append(constructVerse(i));
}
return stringBuilder.toString();
}

public static String sing() {
return verses(1, 12);
}

private static String constructVerse(int verseNumber) {
StringBuilder stringBuilder = new StringBuilder("On the " + days[verseNumber - 1]
+ " day of Christmas my true love gave to me, ");
for (int i = verseNumber; i > 0; i--) {
if (verseNumber != 1 && i == 1) stringBuilder.append("and ");
stringBuilder.append(gifts[i - 1]);
}
stringBuilder.append("\n");
return stringBuilder.toString();
}
}
Empty file.
172 changes: 172 additions & 0 deletions exercises/twelve-days/src/test/java/TwelveDaysTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
import org.junit.Ignore;
import org.junit.Test;

import static org.junit.Assert.assertEquals;

public class TwelveDaysTest {

@Test
public void testVerseOne() {
String expectedVerseOne = "On the first day of Christmas my true love gave to me, " +
"a Partridge in a Pear Tree.\n";
assertEquals(expectedVerseOne, TwelveDays.verse(1));
}

@Ignore
@Test
public void testVerseTwo() {
String expectedVerseTwo = "On the second day of Christmas my true love gave to me, two Turtle Doves, " +
"and a Partridge in a Pear Tree.\n";
assertEquals(expectedVerseTwo, TwelveDays.verse(2));
}

@Ignore
@Test
public void testVerseThree() {
String expectedVerseThree = "On the third day of Christmas my true love gave to me, three French Hens, " +
"two Turtle Doves, and a Partridge in a Pear Tree.\n";
assertEquals(expectedVerseThree, TwelveDays.verse(3));
}

@Ignore
@Test
public void testVerseFour() {
String expectedVerseFour = "On the fourth day of Christmas my true love gave to me, four Calling Birds, " +
"three French Hens, two Turtle Doves, and a Partridge in a Pear Tree.\n";
assertEquals(expectedVerseFour, TwelveDays.verse(4));
}

@Ignore
@Test
public void testVerseFive() {
String expectedVerseFive = "On the fifth day of Christmas my true love gave to me, five Gold Rings, " +
"four Calling Birds, three French Hens, two Turtle Doves, and a Partridge in a Pear Tree.\n";
assertEquals(expectedVerseFive, TwelveDays.verse(5));
}

@Ignore
@Test
public void testVerseSix() {
String expectedVerseSix = "On the sixth day of Christmas my true love gave to me, six Geese-a-Laying, " +
"five Gold Rings, four Calling Birds, three French Hens, two Turtle Doves, " +
"and a Partridge in a Pear Tree.\n";
assertEquals(expectedVerseSix, TwelveDays.verse(6));
}

@Ignore
@Test
public void testVerseSeven() {
String expectedVerseSeven = "On the seventh day of Christmas my true love gave to me, " +
"seven Swans-a-Swimming, six Geese-a-Laying, five Gold Rings, four Calling Birds, three French Hens, " +
"two Turtle Doves, and a Partridge in a Pear Tree.\n";
assertEquals(expectedVerseSeven, TwelveDays.verse(7));
}

@Ignore
@Test
public void testVerseEight() {
String expectedVerseEight = "On the eighth day of Christmas my true love gave to me, eight Maids-a-Milking," +
" seven Swans-a-Swimming, six Geese-a-Laying, five Gold Rings, four Calling Birds, " +
"three French Hens, two Turtle Doves, and a Partridge in a Pear Tree.\n";
assertEquals(expectedVerseEight, TwelveDays.verse(8));
}

@Ignore
@Test
public void testVerseNine() {
String expectedVerseNine = "On the ninth day of Christmas my true love gave to me, nine Ladies Dancing, " +
"eight Maids-a-Milking, seven Swans-a-Swimming, six Geese-a-Laying, five Gold Rings, " +
"four Calling Birds, three French Hens, two Turtle Doves, and a Partridge in a Pear Tree.\n";
assertEquals(expectedVerseNine, TwelveDays.verse(9));
}

@Ignore
@Test
public void testVerseTen() {
String expectedVerseTen = "On the tenth day of Christmas my true love gave to me, ten Lords-a-Leaping, " +
"nine Ladies Dancing, eight Maids-a-Milking, seven Swans-a-Swimming, six Geese-a-Laying, " +
"five Gold Rings, four Calling Birds, three French Hens, two Turtle Doves, " +
"and a Partridge in a Pear Tree.\n";
assertEquals(expectedVerseTen, TwelveDays.verse(10));
}

@Ignore
@Test
public void testVerseEleven() {
String expectedVerseEleven = "On the eleventh day of Christmas my true love gave to me, " +
"eleven Pipers Piping, ten Lords-a-Leaping, nine Ladies Dancing, eight Maids-a-Milking, " +
"seven Swans-a-Swimming, six Geese-a-Laying, five Gold Rings, four Calling Birds, " +
"three French Hens, two Turtle Doves, and a Partridge in a Pear Tree.\n";
assertEquals(expectedVerseEleven, TwelveDays.verse(11));
}

@Ignore
@Test
public void testVerseTwelve() {
String expectedVerseTwelve = "On the twelfth day of Christmas my true love gave to me, " +
"twelve Drummers Drumming, eleven Pipers Piping, ten Lords-a-Leaping, nine Ladies Dancing, " +
"eight Maids-a-Milking, seven Swans-a-Swimming, six Geese-a-Laying, five Gold Rings, " +
"four Calling Birds, three French Hens, two Turtle Doves, and a Partridge in a Pear Tree.\n";

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, is the final newline an intrinsic part of these verses? In my mind that's a verse separator, so I'd not necessarily expect it to be included when requesting an individual verse. I do think the double newline separators make sense when printing out more than one verse though. What do you think, @FridaTveit?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't have the newline there originally, but I changed it when I saw that the exercise in other languages had the newline at the end of every verse. I think it's because if you copy a line of the song from the description, you get the newline as well.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, that makes sense r.e. the description of the problem. Thanks!

assertEquals(expectedVerseTwelve, TwelveDays.verse(12));
}

@Ignore
@Test
public void testMultipleVerses() {
String expectedVerseOneToThree = "On the first day of Christmas my true love gave to me, " +
"a Partridge in a Pear Tree.\n\n" +
"On the second day of Christmas my true love gave to me, two Turtle Doves, " +
"and a Partridge in a Pear Tree.\n\n" +
"On the third day of Christmas my true love gave to me, three French Hens, two Turtle Doves, " +
"and a Partridge in a Pear Tree.\n";
assertEquals(expectedVerseOneToThree, TwelveDays.verses(1, 3));
}

@Ignore
@Test
public void testSingWholeSong() {
String expectedSong = "On the first day of Christmas my true love gave to me, a Partridge in a Pear Tree.\n" +
"\n" +
"On the second day of Christmas my true love gave to me, two Turtle Doves, " +
"and a Partridge in a Pear Tree.\n" +
"\n" +
"On the third day of Christmas my true love gave to me, three French Hens, two Turtle Doves, " +
"and a Partridge in a Pear Tree.\n" +
"\n" +
"On the fourth day of Christmas my true love gave to me, four Calling Birds, three French Hens, " +
"two Turtle Doves, and a Partridge in a Pear Tree.\n" +
"\n" +
"On the fifth day of Christmas my true love gave to me, five Gold Rings, four Calling Birds, " +
"three French Hens, two Turtle Doves, and a Partridge in a Pear Tree.\n" +
"\n" +
"On the sixth day of Christmas my true love gave to me, six Geese-a-Laying, five Gold Rings, " +
"four Calling Birds, three French Hens, two Turtle Doves, and a Partridge in a Pear Tree.\n" +
"\n" +
"On the seventh day of Christmas my true love gave to me, seven Swans-a-Swimming, " +
"six Geese-a-Laying, five Gold Rings, four Calling Birds, three French Hens, two Turtle Doves, " +
"and a Partridge in a Pear Tree.\n" +
"\n" +
"On the eighth day of Christmas my true love gave to me, eight Maids-a-Milking, " +
"seven Swans-a-Swimming, six Geese-a-Laying, five Gold Rings, four Calling Birds, " +
"three French Hens, two Turtle Doves, and a Partridge in a Pear Tree.\n" +
"\n" +
"On the ninth day of Christmas my true love gave to me, nine Ladies Dancing, eight Maids-a-Milking," +
" seven Swans-a-Swimming, six Geese-a-Laying, five Gold Rings, four Calling Birds, " +
"three French Hens, two Turtle Doves, and a Partridge in a Pear Tree.\n" +
"\n" +
"On the tenth day of Christmas my true love gave to me, ten Lords-a-Leaping, nine Ladies Dancing," +
" eight Maids-a-Milking, seven Swans-a-Swimming, six Geese-a-Laying, five Gold Rings, " +
"four Calling Birds, three French Hens, two Turtle Doves, and a Partridge in a Pear Tree.\n" +
"\n" +
"On the eleventh day of Christmas my true love gave to me, eleven Pipers Piping, " +
"ten Lords-a-Leaping, nine Ladies Dancing, eight Maids-a-Milking, seven Swans-a-Swimming, " +
"six Geese-a-Laying, five Gold Rings, four Calling Birds, three French Hens, two Turtle Doves, " +
"and a Partridge in a Pear Tree.\n" +
"\n" +
"On the twelfth day of Christmas my true love gave to me, twelve Drummers Drumming, " +
"eleven Pipers Piping, ten Lords-a-Leaping, nine Ladies Dancing, eight Maids-a-Milking, " +
"seven Swans-a-Swimming, six Geese-a-Laying, five Gold Rings, four Calling Birds, " +
"three French Hens, two Turtle Doves, and a Partridge in a Pear Tree.\n";

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems like a "gotcha" that this test string ends in a single newline, but the test string for testMultipleVerses ends in a double newline. I could imagine that this might confuse implementors a little bit! Do you think it would be possible to make them consistent (as above, I would lean towards no trailing newlines in either case, but I think consistency of some kind here is more important than the actual choice we make)?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's no double newline in this one, because there's no double newline at the end of the song in the description. Personally, I don't think the multiple verses should end in a double newline. However, in the implementation of twelve days in other languages the multiple verses end in a double newline. So I'm happy to change the multiple verses to end with a single newline if you're happy with my implementation not being exactly the same as in other languages. I wasn't sure how important it was to be consistent with other implementations...

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would say; let's break with the other tracks here for the sake of consistency in our own track. I'll add a further note below in response to @jtigger's comments regarding cross-track checks :)

assertEquals(expectedSong, TwelveDays.sing());
}
}