Message Boards Message Boards

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

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:

Framed[Column[{
    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

Tweet-a-Program

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.

hoap[bd_,u_]:=Module[

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

    period=(Now-bd)/data[[All,u]];
    imgs=ConformImages[data[[All,2]],400];

    Style[
    Grid[{
       data[[;;4,1]],period[[;;4]],imgs[[;;4]],
       data[[-4;;,1]],period[[-4;;]],imgs[[-4;;]]},
    Alignment->Left],
    FontSize->20,Red]  
]

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

CloudDeploy[
    FormPage[
       {"birthday"->"Date",
       "time" -> {"years" -> 4, "days" -> 3}}, 
       hoap[#birthday,#time]&,
       AppearanceRules-><|
         "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|>],
"howoldplanet",Permissions->"Public"]

Out[]= CloudObject["https://www.wolframcloud.com/objects/user-3c5d3268-040e-45d5-8ac1-25476e7870da/howoldplanet"]

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

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

https://wolfr.am/howoldplanet

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

Attachments:
POSTED BY: Vitaliy Kaurov
9 Replies

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

URLShorten["http://blog.stephenwolfram.com/2016/10/computational-law-symbolic-discourse-and-the-ai-constitution", "LeibnizsDreamAI"]

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

POSTED BY: Manjunath Babu

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

POSTED BY: Vitaliy Kaurov

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

POSTED BY: Manjunath Babu

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!

POSTED BY: Bernat Espigulé

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

Dataset[(Now - DateObject[{1961, 8, 4}])/PlanetData[PlanetData[], "OrbitPeriod", "EntityAssociation"]]
POSTED BY: Vitaliy Kaurov

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

enter image description here

POSTED BY: Vitaliy Kaurov
Posted 8 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

POSTED BY: J. M.

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 BY: Vitaliy Kaurov

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

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

by using the @ rather than []...

POSTED BY: Sander Huisman
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract