Programming Puzzles & Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Given an input of a list of days of the week, output the shortest sorted representation of the list.

The format of the input is a string consisting of one or more of the two-character substrings Su (Sunday), Mo (Monday), Tu (etc.), We, Th, Fr, and Sa. The input may not necessarily be given in sorted order.

To convert the input to the output format,

  • Sort the input by day of week, starting from Sunday (ex. ThMoSaSuFrTuWe -> SuMoTuWeThFrSa).

  • Reduce the abbreviations down to one letter if it leaves no ambiguity. For example, SuMoTuWe should become SMTW because the first S couldn't possible be Saturday, as that would make the output unsorted (same for the T). However, ThFrSa should become ThFS, as both Tuesday and Thursday come before Friday and reducing it down to TFS would create ambiguity.

  • If the output is now MTWTF, output D instead (which stands for "weekdays"). Similarly, SS should become E for weekend. Finally, SMTWTFS should become A for all days.

The input and output must both be a single string.

Since this is , the shortest code in bytes wins.

Test cases:

In              Out    | In              Out
-----------------------|--------------------
SuTu            STu    | SuTuWe          STW
SuTuSa          STuS   | SuWeTh          SWT
TuThSa          TTS    | TuThSu          STT
Su              Su     | Sa              Sa
WeTh            WT     | FrTh            ThF
WeTu            TW     | FrTu            TuF
FrWeMo          MWF    | SaWeSu          SWS
ThTu            TT     | We              W
ThTuMoFrWe      D      | SaSu            E
SuMoWeTuThFr    SMTWTF | ThMoSaSuFrTuWe  A
share|improve this question
    
Just reading this makes me feel like it's MMMM – Lui Jan 14 at 3:29
6  
I just think: WTF, and it's weekend! – agtoever Jan 14 at 8:03
    
STFU! Oh, that does not work... :D – flawr Jan 14 at 16:36
up vote 6 down vote accepted

Retina, 152 88

Massively golfed with @Martin's and @randomra's help! Thanks both of you!

^
SuMoTuWeThFrSa
([A-Z].)(?!.*\1)

T`l``Su\B|\BSa|o|r|u?We.?.?|uTh
^MTWTF$
D
SS
E
.{7}
A

Try it online. A couple of the lines start with m` with this online interpreter link. This is so the program works with multiple input lines (to run all tests in one shot). However, multiple input lines is not a requirement, so these are not included above or in my score.

share|improve this answer
1  
Dang, I was excited when I finally squished mine below your 152. Can't beat this now XD Very nice guys =) – Mwr247 Jan 14 at 16:36
    
T`l``Su\B|\BSa|.*e.*|uTh|o|r saves 3 more bytes. – randomra Jan 15 at 17:47

JavaScript (ES7), 187 178 168 157 143 bytes

x=>({SMTWTFS:'A',SS:'E',MTWTF:'D'}[x=[for(a of'Su M Tu W Th F Sa'.split` `)if(x.match(a))x.match({S:/.../,T:/W|T.*T/}[b=a[0]])?b:a].join``]||x)

The regex tests helped make quick work of the special day rules, and while less than ideal, the object map does it's work. I'm certain I can squeeze a few more out of this though.

share|improve this answer

Python 3, 321 Bytes

def w(n,a=lambda b,c,d:b.replace(c[0],d).replace(c[1],d)):d=''.join([[o[0],o][o[0]in'ST']for o in['Su','Mo','Tu','We','Th','Fr','Sa']if o in[n[i:i+2]for i in range(0,len(n),2)]]);d=[d,a(d,['Tu','Th'],'T')][('W'in d)+('TuT'in d)];l=len(d);d=[d,a(d,['Su','Sa'],'S')][l>2];return[[[d,'A'][l>8],'E'][d=='SS'],'D'][d=='MTWTF']

Test on ideone

share|improve this answer
    
You made it a (really long) one liner! – TanMath Jan 14 at 5:55
    
'Su Mo Tu We Th Fr Sa'.split() is shorter than ['Su','Mo','Tu','We','Th','Fr','Sa'] – Sherlock9 Jan 15 at 7:36

JavaScript (ES6), 197 bytes

s=>eval(`n=0;d="SuMoTuWeThFrSa";s.match(/../g).map(t=>n|=1<<d.search(t)/2);o="";for(i=0;i<7;i++)n&1<<i?o+=d.substr(i*2,n<2|n==64|(!(n&8|(n&20)>19)&&i==2|i==4)?2:1):0;n-127?n-62?n-65?o:"E":"D":"A"`)

Explanation

Encodes each day as a bit and stores the input as a number n. Bit 0 = Sunday ... bit 6 = Saturday. This allows the ambiguity rule checking code to be a lot shorter due to the bit-wise operations and being able to compare the entire combination with a number that is always less than 128.

s=>
  eval(`                   // eval enables the for loop without {} or return
    n=0;                   // n = input encoded as a number
    d="SuMoTuWeThFrSa";    // d = day strings
    s.match(/../g).map(t=> // for each day string t in the input
      n|=1<<d.search(t)/2  // set the bit in n that corresponds to the day
    );
    o="";                  // o = output string
    for(i=0;i<7;i++)       // for each day i from Sunday to Monday
      n&1<<i?              // if the day was in the input
        o+=d.substr(i*2,   // add the day string to the output
          n<2              // Sunday by itself is ambiguous
          |n==64           // Saturday by itself is ambiguous
          |(!(n&8          // Without Wednesday...
            |(n&20)>19     // ...or both Tuesday and Thursday,
            )&&i==2|i==4)  // ...Tuesday or Thursday are ambiguous
          ?2:1             // 2 characters if ambiguous, else 1
        )
      :0;
    n-127?                 // n == 127 = All days (A)
      n-62?                // n == 62 = All week days (D)
        n-65?              // n == 65 = All weekend days (E)
          o                // else output the constructed string
        :"E"
      :"D"
    :"A"
  `)

Test

var solution = s=>eval(`n=0;d="SuMoTuWeThFrSa";s.match(/../g).map(t=>n|=1<<d.search(t)/2);o="";for(i=0;i<7;i++)n&1<<i?o+=d.substr(i*2,n<2|n==64|(!(n&8|(n&20)>19)&&i==2|i==4)?2:1):0;n-127?n-62?n-65?o:"E":"D":"A"`)
<input type="text" id="input" value="FrTh" />
<button onclick="result.textContent=solution(input.value)">Go</button>
<pre id="result"></pre>

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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