Repository navigation
twelve-days: add to track #239
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
Changes from all commits
839dce2
4a8dd6c
9b92ac5
c11a53f
4576cb0
2746deb
c7f102d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -52,3 +52,5 @@ include 'triangle' | |
| include 'trinary' | ||
| include 'word-count' | ||
| include 'wordy' | ||
| include 'twelve-days' | ||
|
|
||
| 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"] | ||
| } | ||
| } |
| 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(); | ||
| } | ||
| } |
| 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"; | ||
| 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"; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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...
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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()); | ||
| } | ||
| } | ||
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.
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?
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 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.
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.
Ah, that makes sense r.e. the description of the problem. Thanks!