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
  • 1
    \$\begingroup\$ Just reading this makes me feel like it's MMMM \$\endgroup\$
    – Lui
    Commented Jan 14, 2016 at 3:29
  • 7
    \$\begingroup\$ I just think: WTF, and it's weekend! \$\endgroup\$
    – agtoever
    Commented Jan 14, 2016 at 8:03
  • 1
    \$\begingroup\$ STFU! Oh, that does not work... :D \$\endgroup\$
    – flawr
    Commented Jan 14, 2016 at 16:36

4 Answers 4


Retina, 152 88

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



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.

  • 1
    \$\begingroup\$ Dang, I was excited when I finally squished mine below your 152. Can't beat this now XD Very nice guys =) \$\endgroup\$
    – Mwr247
    Commented Jan 14, 2016 at 16:36
  • \$\begingroup\$ T`l``Su\B|\BSa|.*e.*|uTh|o|r saves 3 more bytes. \$\endgroup\$
    – randomra
    Commented Jan 15, 2016 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.


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

  • \$\begingroup\$ You made it a (really long) one liner! \$\endgroup\$
    – TanMath
    Commented Jan 14, 2016 at 5:55
  • \$\begingroup\$ 'Su Mo Tu We Th Fr Sa'.split() is shorter than ['Su','Mo','Tu','We','Th','Fr','Sa'] \$\endgroup\$
    – Sherlock9
    Commented Jan 15, 2016 at 7:36

JavaScript (ES6), 197 bytes



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.

  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
    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


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>


Your Answer

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

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