Message Boards Message Boards


[GOLF] Code-Golfing websites: How old are you on another planet?

Posted 5 years ago
9 Replies
16 Total Likes

NOTE: see the article below about making this website or click on the image to go to the actual website.

enter image description here

Because WL has immense and constantly increasing vocabulary of functions and data (THE only language that has an ocean of built-in curated data), it is easy to come up with fun challenges or pack whole applications in a few lines of code. Below there are 3 examples that compute person's age on other planets in planet's days and years. Starting with very simple code-golfing exercises it is quite fast to build up to a whole beautiful interactive website and I will walk you through this.

Code Golf: 74 characters

Code golf is a fun coding competition with participants aiming for the shortest possible code that implements an algorithm.

Can anyone come up with a shorter version version of the following code?

f=PlanetData;Grid[{f[],(Now-DateObject[{1961, 8, 4}])/f[f[],"OrbitPeriod"]}]

or prettified version:

enter image description here

Here the date is assumed to be someone's birthday (for the curious - who's birthday is it?). The output should be in a nice readable table-like from. According to this little counter I got 74 characters:

    Dynamic[Style["code length = "<>ToString[StringLength[StringDelete[x," "]]],20,Red]],
    InputField[Dynamic[x],String,ContinuousAction->True,FieldHint->"Enter code in InputForm"]

enter image description here


Another challenge actually would be to make a nice tweet out of it for Wolfram Tweet-a-Program (TaP). Interesting thing is not to use just a date, but a famous person name. Can you make this shorter? Code for Tap (note the syntax =[...] as it is the equivalent of CTRL+= in front end) and the actual tweet itself:

f=PlanetData;TableForm[Transpose[{(Now- =[Obama bday])/f[f[],"OrbitPeriod"],f[]}],TableHeadings->{None,{"Obama age","Planet"}}]

enter image description here

Interactive microsite

It is important to remember that due to planet's "OrbitPeriod" property we are considering age in years. We could consider planet's "RotationPeriod" around axis and so count the age in planet's days. Code will still stay so short that it is easy to build a whole microsite from it giving people interface to play with their birthday dates and astronomy facts. Here is a simple function that gets astronomical data and oranges them into a table.


    data=EntityValue["Planet", {"Name","Image","RotationPeriod","OrbitPeriod"}]},



To design user interface via a microsite we will use FormPage function:

       "time" -> {"years" -> 4, "days" -> 3}}, 
         "Title" -> "How old are you on another planet?", 
         "Description" -> "
          Albert Einstein would now be 5 years old on Saturn and vampire-old on Mercury. See his BD below. Try it it yourself.
          Planet rotational period defines days, and its orbital period - years."|>,
       FormTheme -> "Black"
    ][<|"birthday" -> "March 14, 1879", "time" -> 4|>],

Out[]= CloudObject[""]

It gives us a URL if a cloud object where our microsite resides. To have a shorter version we compute:

URLShorten[%[[1]], "howoldplanet"]

and following that URL you will arrive at a simple but stylish microsite shown at the top of this page. Hooray!

9 Replies

Very impressive code! thanks for sharing. I can only make it one character smaller:


by using the @ rather than []...

Thanks, @Sander! I just realized I could easily turn it into tutorial on making microsites. I added some stuff to the post. I hope more folks will be adopting cloud interfaces.

Posted 5 years ago

I'm not the golfing sort, but this is still pretty short:

Dataset[(Now - DateObject[{1961, 8, 4}])/# & /@ 
    PlanetData[PlanetData[], "OrbitPeriod", "EntityAssociation"]]

enter image description here


A very nice try. With my counter it gave exactly 100 characters:

enter image description here

And actually we do not need pure function there and Map, devision is listable:

Dataset[(Now - DateObject[{1961, 8, 4}])/PlanetData[PlanetData[], "OrbitPeriod", "EntityAssociation"]]

Code-Golfing Tweet-a-Program: Martin Luther King's age in years and days on other planets if he was living on them! :-)

AssociationThread[{"yr","days"}->(Now-=[King bday])/#]&/@Dataset[PlanetData[All,{"OrbitPeriod","SolarDay"},"EntityAssociation"]]

Martin Luther King age on other planets

Interestingly, Martin Luther King would be 364 years old on Mercury but only 182 days old since one solar day on Mercury lasts about as long as 176 days on Earth!

Could you please explain the syntax of the URLShorten function that appends the shortened URL with a custom word "howoldplanet" ?

This functionality is available to special accounts, and not to regular users. But you can always generate non-custom random short URL.

Yes, I tried executing the following command but it did not produce correct output.

URLShorten["", "LeibnizsDreamAI"]

But, Thanks for letting me know that its available only for special accounts.

Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
or Discard

Group Abstract Group Abstract