Login
You're viewing the code4lib.social public feed.
  • Dec 22, 2025, 9:05 PM

    #Christmas #coding #challenge: write a program that prints the full lyrics to the Twelve Days of Christmas, but be #weird about it.

    Choose whatever programming language you like. Use the language’s idiosyncrasies to your advantage.

    For example, in C you might blend a for-loop with a switch-statement and liberally use case fallthrough. In BASIC you might use computed GOTO. In HTML+CSS you might use counters and selectors to do the numbering. In SQL you might use SQL.

    Go.

    💬 4🔄 3⭐ 0

Replies

  • Dec 24, 2025, 4:14 AM

    Here's my own submission to the 12 Days of Christmas Coding Challenge, in good old XSLT, which is well suited to this kind of processing.

    Invoked: Transform.exe -it:main -xsl:12days.xsl

On the first day of Christmas, my true love sent to me:
a partridge in a pear tree

On the second day of Christmas, my true love sent to me:
two turtledoves
and a partridge in a pear tree

<and so on>
    Source code:

<xsl:stylesheet some boring stylesheets elided>
  <xsl:output method="text"/>

  <ccc:day ordinal="first" gift="a partridge in a pear tree&#10;&#10;"/>
  <ccc:day ordinal="second" gift="two turtledoves&#10;and "/>
  <ccc:day ordinal="third" gift="three french hens&#10;"/>
  <ccc:day ordinal="fourth" gift="four calling birds&#10;"/>
  <ccc:day ordinal="fifth" gift="FIVE GOLD RINGS!&#10;"/>
  <!-- you get the idea -->
  <ccc:day ordinal="twelfth" gift="twelve drummers drumming&#10;"/>

  <xsl:template name="main">
    <xsl:apply-templates select="document('')/*/ccc:day" />
  </xsl:template>

  <xsl:template match="ccc:day">
    <xsl:text>On the </xsl:text>
    <xsl:value-of select="@ordinal"/> 
    <xsl:text> day of Christmas, my true love sent to me:&#10;</xsl:text>
    <xsl:apply-templates select="@gift"/>
    <xsl:apply-templates select="preceding-sibling::ccc:day/@gift">
      <xsl:sort select="position()" data-type="number" order="descending"/>
    </xsl:apply-templates>
  </xsl:template>

</xsl:stylesheet>
    💬 0🔄 0⭐ 0
  • 💬 0🔄 0⭐ 0
  • Dec 23, 2025, 4:52 AM

    @fsvo @futzle Hrm, I dunno. These variable names all make a lot of sense, and where’s the effect system?

    💬 0🔄 0⭐ 0
  • Dec 23, 2025, 5:32 AM

    @futzle Walrus operators in Python are pretty weird, and everybody loves generator expressions, right?:

    data = [
    ("first", "a", "partridge in a pear tree"),
    ("second", "Two", "turtle doves"),
    ("third", "Three", "french hens"),
    ("fourth", "Four", "calling birds"),
    ("fifth", "Five", "golden rings"),
    ("sixth", "Six", "geese a-laying"),
    ("seventh", "Seven", "swans a-swimming"),
    ("eighth", "Eight", "maids a-milking"),
    ("ninth", "Nine", "ladies dancing"),
    ("tenth", "Ten", "lords a-leaping"),
    ("eleventh", "Eleven", "pipers piping"),
    ("twelfth", "Twelve", "drummers drumming"),
    ]
    for x in (
    gift_list := f"On the {ith} day of Christmas, my true love gave to me\n" + f"{count} {gift}\n" + ("" if ith == "first" else "And " + gift_list.split("\n")[-2] + "\n" if ith == "second" else '\n'.join(gift_list.split("\n")[1:]))
    for ith, count, gift in data
    ):
    print(x)

    💬 1🔄 0⭐ 0
  • Dec 23, 2025, 5:33 AM

    @futzle Somewhat cleaner / easier to understand version:

    foo = [
    (gift_list := [f"{count} {gift}"] if ith == "first" else [f"{count} {gift}"] + ["And " + gift_list[-1]] if ith == "second" else [f"{count} {gift}"] + gift_list,
    f"On the {ith} day of Christmas, my true love gave to me\n" + "\n".join(gift_list) + "\n\n")
    for ith, count, gift in data
    ]

    for x in foo:
    print(x[1])

    💬 0🔄 0⭐ 0
  • 💬 1🔄 0⭐ 0