exercism

Exercism solutions.
git clone git://code.dwrz.net/exercism
Log | Files | Refs

twelve-days.go (1395B)


      1 // Package twelve outputs the lyrics to The Twelve Days of Christmas.
      2 package twelve
      3 
      4 import (
      5 	"fmt"
      6 )
      7 
      8 var ordinals = [13]string{
      9 	"zeroth",
     10 	"first",
     11 	"second",
     12 	"third",
     13 	"fourth",
     14 	"fifth",
     15 	"sixth",
     16 	"seventh",
     17 	"eighth",
     18 	"ninth",
     19 	"tenth",
     20 	"eleventh",
     21 	"twelfth",
     22 }
     23 
     24 var gifts = [13]string{
     25 	"a null pointer",
     26 	"a Partridge in a Pear Tree",
     27 	"two Turtle Doves",
     28 	"three French Hens",
     29 	"four Calling Birds",
     30 	"five Gold Rings",
     31 	"six Geese-a-Laying",
     32 	"seven Swans-a-Swimming",
     33 	"eight Maids-a-Milking",
     34 	"nine Ladies Dancing",
     35 	"ten Lords-a-Leaping",
     36 	"eleven Pipers Piping",
     37 	"twelve Drummers Drumming",
     38 }
     39 
     40 func giftsForDay(day int) string {
     41 	switch day {
     42 	case 0, 1:
     43 		return gifts[day]
     44 	case 2:
     45 		return fmt.Sprintf(
     46 			"%s, and %s",
     47 			gifts[day],
     48 			gifts[day-1],
     49 		)
     50 	default:
     51 		return fmt.Sprintf(
     52 			"%s, %s",
     53 			gifts[day],
     54 			giftsForDay(day-1),
     55 		)
     56 	}
     57 }
     58 
     59 // Verse returns the matching verse for the inputted day.
     60 // Day must be an integer between 1 and 12.
     61 func Verse(day int) string {
     62 	if day < 0 || day > 12 {
     63 		return ""
     64 	}
     65 	return fmt.Sprintf(
     66 		"On the %s day of Christmas my true love gave to me: %s.",
     67 		ordinals[day],
     68 		giftsForDay(day),
     69 	)
     70 }
     71 
     72 // Song returns the text of the song "Twelve Days of Christmas".
     73 // Its verses are separated by newlines.
     74 func Song() (song string) {
     75 	for day := 1; day <= 12; day++ {
     76 		song += fmt.Sprintf("%s\n", Verse(day))
     77 	}
     78 	return
     79 }