2
\$\begingroup\$

The goal is to output the number of the months given as input in a compact concatenated form which is still parsable if one knows the construction rules:

If either:

  • January is followed by January, February, November or December; or
  • November is followed by January or February

There must be a separator placed between.
Otherwise there should be no separator.

As such the output may be parsed. For example:

  • March, April and September -> 349
  • January, January and February -> 1-1-2
  • January, January and December -> 1-1-12
  • January, November and February -> 1-11-2
  • November, January and February -> 11-1-2
  • November and December -> 1112

Thus any run of ones is either a run of Novembers, a run of Novembers followed by a December, a run of Novembers followed by an October, or a January followed by an October. This may be parsed by looking to the right of such runs as the resulting string is read from left to right.

Input

A list of months numbers ([1-12]) in any format you want (list, JSON, separated by one or more characters, entered one by one by user, …).

the same month can be present more than once.

Output

The compact concatenated form described above. If a separator is needed, you can freely choose one.

Output examples for January, February and March (1-2-3):

  • 1-23 (chosen for the test cases below)
  • 1 23
  • 1/23
  • 1,23
  • ...

Test cases

[1]       => 1
[1, 2]    => 1-2
[2, 1]    => 21
[12]      => 12
[1, 11]   => 1-11
[11, 1]   => 11-1
[1, 1, 1] => 1-1-1
[2, 11]   => 211
[11, 2]   => 11-2
[1, 2, 3] => 1-23
[11, 11]  => 1111
[1,1,11,1,12,1,11,11,12]     => 1-1-11-1-121-111112
[10,11,12,11,11,10,1,10,1,1] => 1011121111101101-1
[2,12,12,2,1,12,12,1,1,2,12,12,2,11,2,12,1] => 2121221-12121-1-21212211-2121

Rules

\$\endgroup\$
3
  • 1
    \$\begingroup\$ 1111 is extra-ambiguous. Do you mean 1-1-1-1, 1-1-11, 1-11-1, 11-1-1 or 11-11? \$\endgroup\$
    – SteeveDroz
    Oct 27, 2016 at 7:38
  • \$\begingroup\$ I see what you mean, I added a test case. If there are so many 1's, it's hard to know at first, so 1111 should be 11-11 if you mean Nov-Nov. That has been shown in the test case 11-2 (not 112) and added as an extra test case. \$\endgroup\$
    – SteeveDroz
    Oct 27, 2016 at 7:46
  • 2
    \$\begingroup\$ This would be so much easier in base 12... \$\endgroup\$ Oct 27, 2016 at 15:28

2 Answers 2

1
\$\begingroup\$

Python 2.7 96 bytes

Try it out here!

Huge thanks to Jonathan Allan for shortening the answer.

golfed:

b,o=input(),""
for a,n in zip(b,b[1:]):o+=`a`+"-"*(a%10==1and a+n in(2,3,12,13))
print o+`b[-1]`

Explanation:

runs through the input, checks if the current item is a special case and if so handles it, otherwise just concatenates the input, prints the output string.

ungolfed:

input = [2,12,12,2,1,12,12,1,1,2,12,12,2,11,2,12,1]
output = ""
for i in range(len(input) - 1):
    next = input[i + 1]
    item = input[i]
    if item == 1 and (next == 1 or next == 2 or next == 11 or next == 12) or item == 11 and (next == 1 or next == 2):
        output += str(item) + "-"
    else:
        output += str(item)

output += str(input[-1])
    
print output
\$\endgroup\$
8
  • \$\begingroup\$ You need to take input.b,o=input(),"" should do it. \$\endgroup\$ Oct 28, 2016 at 15:15
  • \$\begingroup\$ You can use backticks in place of str(). You can use a zip to construct a,n. The if statement can be used to multiply the "-" (reducing the for to a one liner. The if can be replaced with if a%10==1and a+n in(2,3,12,13). This gets you all the way down to 96 bytes. \$\endgroup\$ Oct 28, 2016 at 15:25
  • \$\begingroup\$ weren't backticks deprecated or not supported after python 2.6 or something like that? \$\endgroup\$ Oct 28, 2016 at 16:09
  • \$\begingroup\$ They were deprecated at 3.0 by PEP 3113, the ideone link I gave is using 2.7.10. \$\endgroup\$ Oct 28, 2016 at 16:24
  • \$\begingroup\$ thanks, I was thrown off by the IDE marking it as invalid, runs just fine. what did you mean by using the zip construct? what to create? \$\endgroup\$ Oct 28, 2016 at 16:28
1
\$\begingroup\$

Jelly, 20 bytes

%⁵=1a+e“£¤€Æ‘⁶ẋ⁸,⁹µ/

TryItOnline! or run all test cases

How?

%⁵=1a+e“£¤€Æ‘⁶ẋ⁸,⁹µ/ - Main link: the list
                   / - reduce
                  µ  - monadic chain separation
%⁵                   - left item mod 10
  =1                 - equal to 1? (Jan or Nov)
    a                - and
     +               - left item plus right item
      e              - is in
       “£¤€Æ‘        - jelly code page index list:  [2,3,12,13]
                           now True (1) for any of:
                               [1,1], [1,2], [1,11], [1,12], [11,1], or [11,2]
             ⁶       - literal space (the separator being used)
              ẋ      - repeated that many times
               ⁸     - left argument
                ,    - pair
                 ⁹   - right argument
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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