9
\$\begingroup\$

This is similar to simplifying fractions, but with Dates!

The input of your program must be of the form mm/dd For example

3/4 //March 4
12/15 //December 15
1/1 // January 1

We assume that the input will be valid such that the months have these numbers of days in them:

January 31
February 28
March 31
April 30
May 31
June 30
July 31
August 31
September 30
October 31
November 30
December 31

Your program's job is to take the assumed valid input, and iteratively (or recursively) simplify the date, and at each iteration (including the 0th), output the date with the full name of the month as written above.

For example:

Given an input of:

12/18

Would output

December 18
June 9
February 3

An input that is already simplified only outputs itself:

11/17

Outputs:

November 17

The month names cannot come from a function in your language. The strings can be obfuscated, computed, however you like, but you cannot use a standard function like GetMonthString(4) or something, you either have to write that function, or find a way to output the month names as described.

I cannot think of any cases where the simplified date produces an illegal date, but if ever you produce an illegal date along the way, output:

Illegal Date

But if you are sure this cannot happen, you do not need to have code covering this case. The dates outputted just always need to be valid according to what was described above (it goes without saying that months and days start at 1).

The algorithm:

At each iteration you divide by the smallest number that divides numerator and denominator.

That is, you find all the numbers such that, dividing both the numerator and denominator by this number produces a new numerator and denominator that are both integers (common factors). Select the smallest one and individually divide the numerator and denominator to produce a new fraction. If the only number you can divide by is 1, then you have simplified as much as possible and you stop.

I hope this is clear.

Any language is allowed. This is Code Golf, shortest code wins!

\$\endgroup\$
  • \$\begingroup\$ question was closed while i was posting an answer. Doh ! \$\endgroup\$ – t-clausen.dk Jun 10 '16 at 13:23
  • \$\begingroup\$ @t-clausen.dk The challenge has been reopened. \$\endgroup\$ – AdmBorkBork Jun 14 '16 at 16:24
  • \$\begingroup\$ Why from 12/18 to 6/9 and not 4/6 (I don't get all the iteration mess ... when I simplify a fraction I got immedialtely the resulting simplified value)? \$\endgroup\$ – edc65 Jun 15 '16 at 10:45
2
\$\begingroup\$

Jelly, 59 bytes

ṣ”/VµÆDf/2ị:@µÐĿị1¦€“£ṢtẒ⁽ẹ½MḊxɲȧėAṅ ɓaṾ¥D¹ṀẏD8÷ṬØ»ṣ⁶¤j€⁶j⁷

Try it online!

How it works

ṣ”/VµÆDf/2ị:@µÐĿị1¦€“...»ṣ⁶¤j€⁶j⁷  Main link. Argument: mm/dd

ṣ”/                                Split at slashes.
   V                               Eval each chunk, yielding [m, d] (integers).
    µ                              Begin a new, monadic chain. Argument: [m, d]
             µÐĿ                   Execute the chain to the left until the results
                                   are no longer unique. Yield the list of all
                                   intermediate results.
     ÆD                              Compute the divisors of each number.
       f/                            Intersect them.
         2ị                          Select the one at index 2. If there is only
                                     one divisor, ị wraps around and selects 1.
           :@                        Divide [m, d] by this common divisor.
                        ¤            Combine the links to the left into a chain.
                 “...»                 Yield the month's name, space-separated.
                      ṣ⁶               Split at spaces.
                €                    For each pair...
             ị                          index into the month's names...
              1¦                        for the first element.
                         j⁶€         Join each pair, separating by spaces.
                            j⁷       Join, separating by linefeeds.
\$\endgroup\$
4
\$\begingroup\$

Pyth - 116 87 bytes

jm++@rR3c."ayÖèÈÒ+J÷z4wëÝ~ñ!Õ¥´{mCØóy|å²z¼qP?ë"\qhd;ed.u/RYNPiFKsMcz\/K

Test Suite.

\$\endgroup\$
  • \$\begingroup\$ Explanation please? :3 \$\endgroup\$ – Downgoat Jun 14 '16 at 16:25
0
\$\begingroup\$

TSQL 296 bytes

Not being allowed to use the standard datename cost me alot of bytes, however to save a few bytes, I used the first 3 characters of the default date description(with the format mon dd yyyy hh:miAM (or PM)) and added the rest of the month name.

Golfed:

use master
DECLARE @ varchar(5) = '12/2'

DECLARE @m int=month('2000/'+@),@d INT=day('2000/'+@)WHILE @m>0BEGIN PRINT left(dateadd(d,@m*29,0),3)+choose(@m,'uary','uary','ch','il','','e','y','ust','tember','ober','ember','ember')+' '+LEFT(@d,2)SELECT @m/=min(n),@d/=min(n)FROM(SELECT number FROM spt_values)x(n)WHERE @m%n+@d%n=0 and n>1 END

Try it online

Ungolfed:

use master
DECLARE @ varchar(5) = '12/2'

DECLARE @m int=month('2000/'+@),@d INT=day('2000/'+@)
WHILE @m>0
BEGIN
PRINT
 left(dateadd(d,@m*29,0),3)
 +choose(@m,'uary','uary','ch','il','','e','y','ust',
  'tember','ober','ember','ember')+' '+LEFT(@d,2)
SELECT @m/=min(n),@d/=min(n)
FROM
(
  SELECT number
  FROM spt_values
)x(n)
WHERE
  @m%n+@d%n=0
  and n>1
END
\$\endgroup\$
  • \$\begingroup\$ Umm... what are the first two lines doing there??? \$\endgroup\$ – Erik the Outgolfer Jun 15 '16 at 14:35
  • \$\begingroup\$ @EʀɪᴋᴛʜᴇGᴏʟғᴇʀ first line is telling which database to use for this script, second line is declaring the input variable. I have not included them in the count as they define where to execute the script and telling what the input variable is \$\endgroup\$ – t-clausen.dk Jun 15 '16 at 16:01
  • \$\begingroup\$ I see a '12/2' on the second line, are you sure you're telling me the truth? \$\endgroup\$ – Erik the Outgolfer Jun 15 '16 at 16:02
  • \$\begingroup\$ @EʀɪᴋᴛʜᴇGᴏʟғᴇʀ I am afraid I don't understand your question \$\endgroup\$ – t-clausen.dk Jun 15 '16 at 16:05
  • \$\begingroup\$ I think you're using a hard-coded date, although I'm not sure if STDIN is supported in SQL and variants... Also, it seems you misspelled September with Septemper. 'temper','ober','ember','ember')+' '+LEFT(@d,2) \$\endgroup\$ – Erik the Outgolfer Jun 15 '16 at 16:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.