My dog ate my calendar, and now my days are all mixed up. I tried putting it back together, but I keep mixing up the days of the week! I need some help putting my calendar back together, with the days in the correct order.
And since I need my calendar put together as fast as possible, don't waste my time by sending me superfluous bytes. The fewer bytes I have to read, the better!
Input
The days of the week, in any order. Input can be taken as a list of strings, or a space separated string, or any reasonable way of representing 7 strings (one for each day of the week).
The strings themselves are all capitalized, as weekdays should be, so the exact strings are:
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
Sunday
Output
The days of the week, in sorted order (Monday - Sunday, because of course we adhere to ISO 8601). Output can be as a list of strings, or printed with some delimiter.
Disclaimer
Note that this is a kolmogorov-complexity challenge, with the added benefit of being able to use the input to shorten your code. You are not required to use the input if you don't want to. You are also free to use any approach, from a builtin datetime library to hard-coding the output.
Examples
To see example input and output, you can consult this python script.
-
8\$\begingroup\$ I hope you've learnt your lesson from this: never leave dogs with calendars. \$\endgroup\$lyxal– lyxal ♦2020年04月28日 06:50:00 +00:00Commented Apr 28, 2020 at 6:50
-
\$\begingroup\$ any delimiter allowed? \$\endgroup\$Helena– Helena2020年04月28日 18:00:25 +00:00Commented Apr 28, 2020 at 18:00
-
\$\begingroup\$ @Helena I'd say any delimiter within reason. I'd prefer space, comma, or newline, but if the language you're using has another default separator, or you'll be able to knock off a few bytes, go for it. Though I will specify that it has to be the same separator between all output words. \$\endgroup\$maxb– maxb2020年04月29日 06:08:02 +00:00Commented Apr 29, 2020 at 6:08
-
2\$\begingroup\$ I hope you have learnt your lesson from this: it is a dog-eat-calendar world. \$\endgroup\$Christian Gibbons– Christian Gibbons2020年04月29日 20:16:32 +00:00Commented Apr 29, 2020 at 20:16
-
1\$\begingroup\$ @ASCII-only The program should work for any of the \7ドル!\$ different ways to order the days of the week. I used a random shuffle in my example code just to show that the output order is independent of the input order. \$\endgroup\$maxb– maxb2020年05月08日 17:09:25 +00:00Commented May 8, 2020 at 17:09
37 Answers 37
Pyth, 7 bytes
o%CN258
Convert each string to a number via treating its ASCII codes as a base 256 number, then take that mod 258, and sort. This gives the mapping
['Monday', 49]
['Tuesday', 75]
['Wednesday', 89]
['Thursday', 99]
['Friday', 103]
['Saturday', 125]
['Sunday', 211]
Same length but less fun is
.P1314S
The 1314th permutation of the sorted input in lexicographic order.
-
3\$\begingroup\$ How did you find that? \$\endgroup\$Adám– Adám2020年04月28日 07:16:33 +00:00Commented Apr 28, 2020 at 7:16
-
6\$\begingroup\$ @Adam I just searched the first 1000 moduluses and got very lucky. On average, it should take over 5040 (7!) moduluses to find one that gives a specific ordering, but this one appears quite early. \$\endgroup\$izzyg– izzyg2020年04月28日 07:20:52 +00:00Commented Apr 28, 2020 at 7:20
-
5\$\begingroup\$ If you ask people "how did you find that", they probably just brute-forced. \$\endgroup\$user92069– user920692020年04月28日 07:23:21 +00:00Commented Apr 28, 2020 at 7:23
-
1\$\begingroup\$ I wanted to wait a bit before posting my own solution, but you had the exact same idea that I had (and you did it with 7 bytes instead of 8). Good solution! \$\endgroup\$maxb– maxb2020年04月28日 07:27:04 +00:00Commented Apr 28, 2020 at 7:27
JavaScript (Node.js), (削除) 37 36 (削除ここまで) 35 bytes
This should also work in Chrome and Edge (Chromium)
Returns a list of strings.
a=>a.sort().sort(_=>-(a=a*595|5)%7)
How?
We first sort the input array in lexicographical order. Whatever the input is, we get:
Friday, Monday, Saturday, Sunday, Thursday, Tuesday, Wednesday
We then invoke sort() a second time with a callback function that, while ignoring its input, generates a sequence of positive and negative values in such a way that the underlying sorting algorithm (insertion sort) is tricked into putting the array in the desired order.
Below is a summary of all steps. Note that because of the bitwise OR, the value stored in \$a\$ is always coerced to a signed 32-bit integer (and so are the 3rd and 4th columns in this table).
A | B | previous a | -(a*595|5) | mod 7 | new order
-----+-----+-------------+-------------+-------+-----------------------------------
Fri | Mon | NaN | -5 | -5 | Mon Fri Sat Sun Thu Tue Wed
Mon | Sat | 5 | -2975 | 0 | unchanged
Fri | Sat | 2975 | -1770125 | 0 | unchanged
Fri | Sun | 1770125 | -1053224375 | 0 | unchanged
Sat | Sun | 1053224375 | 396722091 | 3 | unchanged
Sat | Thu | -396722091 | -173557135 | -3 | Mon Fri Thu Sat Sun Tue Wed
Fri | Thu | 173557135 | -187280221 | -2 | Mon Thu Fri Sat Sun Tue Wed
Mon | Thu | 187280221 | 237418201 | 6 | unchanged
Fri | Tue | -237418201 | -470091173 | -6 | Mon Thu Tue Fri Sat Sun Wed
Thu | Tue | 470091173 | -531373695 | -6 | Mon Tue Thu Fri Sat Sun Wed
Mon | Tue | 531373695 | 1660231379 | 2 | unchanged
Fri | Wed | -1660231379 | -4807575 | -3 | Mon Tue Thu Wed Fri Sat Sun
Tue | Wed | 4807575 | 1434460171 | 4 | unchanged
Thu | Wed | -1434460171 | -1194690159 | -5 | Mon Tue Wed Thu Fri Sat Sun
-
2\$\begingroup\$ This relies on Node's specific sorting algorithm, so it's not generic ES6. \$\endgroup\$Neil– Neil2020年04月28日 07:46:50 +00:00Commented Apr 28, 2020 at 7:46
-
1\$\begingroup\$ Wait, you're not using either argument. So whacky. \$\endgroup\$Steve Bennett– Steve Bennett2020年04月28日 08:20:24 +00:00Commented Apr 28, 2020 at 8:20
-
1\$\begingroup\$ @SteveBennett Ah, you're right. I've added a note to clarify. \$\endgroup\$Arnauld– Arnauld2020年04月28日 08:45:01 +00:00Commented Apr 28, 2020 at 8:45
-
1\$\begingroup\$ @Arnauld Not sure what you mean by all major JS engines, as I get Wednesday, Monday, Thursday, Tuesday, Friday, Saturday, Sunday in Safari. \$\endgroup\$Neil– Neil2020年04月28日 09:35:07 +00:00Commented Apr 28, 2020 at 9:35
-
1\$\begingroup\$ Yeah, that's much better, thanks. \$\endgroup\$Neil– Neil2020年04月28日 09:52:18 +00:00Commented Apr 28, 2020 at 9:52
Pyke, 2 bytes
Pyke has some weird constant built-ins (a link to the Stack Exchange API, the lengths of months as numbers, the names of the days of the week and so on).
~C
Doesn't take input. Try it online!
-
\$\begingroup\$ I don't think anyone is going to beat this \$\endgroup\$maxb– maxb2020年04月28日 08:24:27 +00:00Commented Apr 28, 2020 at 8:24
-
\$\begingroup\$ I won't be surprised if there is some language I forgot about with a 1-byter for this. \$\endgroup\$the default.– the default.2020年04月28日 08:25:01 +00:00Commented Apr 28, 2020 at 8:25
JavaScript, 37 characters
e=>[...'1564023'].map(a=>e.sort()[a])
JavaScript, 38 characters
e=>[1,5,6,4,0,2,3].map(a=>e.sort()[a])
Javascript, 39 characters
e=>e.map((a,i)=>e.sort()['1564023'[i]])
Javascript, 45 characters
e=>e.map((a,i)=>e.sort()[[1,5,6,4,0,2,3][i]])
Javascript, 51 characters
e=>(v=e.sort(),v.map((a,i)=>v[[1,5,6,4,0,2,3][i]]))
Javascript, 52 characters
e=>(v=e.sort(),[v[1],v[5],v[6],v[4],v[0],v[2],v[3]])
Javascript, 61 characters
I tried a couple of things, but they were fractionally longer than:
z=>'Monday Tuesday Wednesday Thursday Friday Saturday Sunday'
-
\$\begingroup\$ The 54 is 52 because the
z=shouldn't have been included in the count. \$\endgroup\$Neil– Neil2020年04月28日 10:00:07 +00:00Commented Apr 28, 2020 at 10:00 -
\$\begingroup\$ I wonder if it would be possible to do
e=>e.map((_,n)=>e.sort()[some_magic_with(n)])instead, but that seems unlikely. ¯\_(ツ)_/¯ \$\endgroup\$Arnauld– Arnauld2020年04月28日 10:37:49 +00:00Commented Apr 28, 2020 at 10:37 -
\$\begingroup\$ Yeah, I pondered something along those lines. There's about 8 characters to play with (assuming the magic is going to contain
nand we want 36 characters or fewer total). \$\endgroup\$Steve Bennett– Steve Bennett2020年04月28日 11:03:27 +00:00Commented Apr 28, 2020 at 11:03
APL (Dyalog Unicode) 18.0 beta, 19 bytes
Full program, taking no input.
'Dddd'(1200⌶)⍳7
Returns a list of strings:
┌──────┬───────┬─────────┬────────┬──────┬────────┬──────┐
│Monday│Tuesday│Wednesday│Thursday│Friday│Saturday│Sunday│
└──────┴───────┴─────────┴────────┴──────┴────────┴──────┘
⍳7 Integers 1...7, representing the dates Jan 1–7, 1900
(1200⌶) Format Date-time ("12:00") as follows:
'Dddd' long Day name
-
1\$\begingroup\$ I get a domain error when trying the code online:
line(1,0) : error AC0008: error (DOMAIN ERROR) executing line "'Dddd'(1200⌶)⍳7"\$\endgroup\$maxb– maxb2020年04月28日 06:39:55 +00:00Commented Apr 28, 2020 at 6:39 -
\$\begingroup\$ @maxb I forgot to remove the TIO link as TIO is on 17.1. Works locally by me. I'll add a link to the docs instead. \$\endgroup\$Adám– Adám2020年04月28日 06:45:36 +00:00Commented Apr 28, 2020 at 6:45
-
3\$\begingroup\$ "The Nineteenth Byte" \$\endgroup\$user92069– user920692020年04月28日 07:10:23 +00:00Commented Apr 28, 2020 at 7:10
Python 3, (削除) 40 (削除ここまで) 38 bytes
-2 bytes thanks to xnor
from calendar import*;print(*day_name)
Python 2, (削除) 44 (削除ここまで) 43 bytes
lambda d:sorted(d,key=lambda x:~hash(x)%72)
-
1\$\begingroup\$ The perfect built-in! I think
from calendar import*;print(*day_name)would be allowed. \$\endgroup\$xnor– xnor2020年04月28日 07:38:50 +00:00Commented Apr 28, 2020 at 7:38 -
\$\begingroup\$ By the way, I've been looking for a solution similar to your sorting one, but with an object method for the key so as to avoid writing a lambda. I had hopes for
"...".stripor"...".translate, but it seems none of those can work. Perhaps you have a better idea with this. \$\endgroup\$xnor– xnor2020年04月28日 07:43:58 +00:00Commented Apr 28, 2020 at 7:43 -
\$\begingroup\$ Thanks for the improvement, I clearly missed that! As for my 2nd answer, I will definitely try to shave off bytes if I can. As you said, the lambda takes up quite a few bytes. \$\endgroup\$dingledooper– dingledooper2020年04月28日 07:48:36 +00:00Commented Apr 28, 2020 at 7:48
Java (JDK), 27 bytes
java.time.DayOfWeek::values
Doing this because the challenge input is very structured, but the output is not structured at all.
No built-ins, 49 bytes
l->l.sort((a,b)->~a[0]*a[4]%-473-~b[0]*b[4]%-473)
APL (Dyalog Unicode), 21 bytes
Anonymous prefix lambda. Port of isaacg's Pyth solution — go upvote that!
{⍵[⍋258|256⊥ ̈⎕UCS ̈⍵]}
{...} "dfn"; argument is ⍵:
⍵[...] reorder the argument into the following order:
⎕UCS ̈⍵ Universal Character Set code points of each string
256⊥ ̈ evaluate each in base-256
258| the division remainder when divided by 258
⍋ grade (permutation that would sort it)
-
1\$\begingroup\$ You posted 3 different answers for APL... \$\endgroup\$user92069– user920692020年04月28日 07:24:31 +00:00Commented Apr 28, 2020 at 7:24
-
3\$\begingroup\$ @petStorm Sure, why not? The are completely different in their approaches. \$\endgroup\$Adám– Adám2020年04月28日 07:24:56 +00:00Commented Apr 28, 2020 at 7:24
05AB1E, 6 bytes
Port of isaacg's base conversion answer.
Σ1ö29%
Explanation
Σ Filter the input by this function:
1ö Base-convert it from 256
1 Constant 256
Ì Add 2 (= 258)
% Modulo by this number
05AB1E, 6 bytes
-1 byte thanks to Expired Data
{œŽ5dè
05AB1E, 21 bytes
Σ"TuWeThFrSaSu"2ôåāsÏ
Explanation
Σ Sort by the output of this function.
"TuWeThFrSaSu"2ô Split every item of that by length of 2.
å Contains?
āsÏ Find all truthy indices of that.
-
\$\begingroup\$ You can get 6 with the other approach too \$\endgroup\$Expired Data– Expired Data2020年04月28日 09:24:21 +00:00Commented Apr 28, 2020 at 9:24
-
\$\begingroup\$
{œŽ5dèwas exactly the same solution I've found o-o \$\endgroup\$Command Master– Command Master2020年04月28日 09:31:24 +00:00Commented Apr 28, 2020 at 9:31 -
1\$\begingroup\$ Using
öinstead ofβalso gives 6 bytes, and doesn't require a footer to pretty-print. \$\endgroup\$Grimmy– Grimmy2020年04月28日 10:15:33 +00:00Commented Apr 28, 2020 at 10:15 -
1\$\begingroup\$ You can remove the footer in that second approach of @ExpiredData by replacing the
'in the input-list with": try it online. Also, there is an \$n^{th}\$ permutations builtin.I, which is faster than generating all permutations first before indexing:{Ž5d.I(although it's irrelevant for the byte-count). \$\endgroup\$Kevin Cruijssen– Kevin Cruijssen2020年04月28日 13:43:27 +00:00Commented Apr 28, 2020 at 13:43
-
\$\begingroup\$ I'm confused. Care to explain? \$\endgroup\$Adám– Adám2020年04月28日 06:54:21 +00:00Commented Apr 28, 2020 at 6:54
-
\$\begingroup\$ @Adám DayName works like this:
DayName[date]. So I found another way by using integers. Unfortunately the order didn't work for Friday.... \$\endgroup\$ZaMoC– ZaMoC2020年04月28日 06:58:06 +00:00Commented Apr 28, 2020 at 6:58 -
\$\begingroup\$ How comes the integers are not representing dates in order? \$\endgroup\$Adám– Adám2020年04月28日 06:58:42 +00:00Commented Apr 28, 2020 at 6:58
-
\$\begingroup\$ I really don't know... range[7] returns
{Monday, Tuesday, Wednesday, Thursday, Saturday, Sunday, Monday}. This is not supposed to work with integers. It's an abuse \$\endgroup\$ZaMoC– ZaMoC2020年04月28日 07:01:37 +00:00Commented Apr 28, 2020 at 7:01 -
\$\begingroup\$ Not really an abuse. The
dateformat thatDayNametakes can be{year,month,day}(or even have hours/minutes/seconds); or it can be{year,month}in which case the day is assumed to be 1 if needed; or it can even be{year}in which case the month and day are assumed to be 1 if needed. So this code is calculating the day of the week for 1 January 198, 1 January 199, ... through 1 January 204. I believe it's doing so assuming (wrongly) that our current Gregorian calendar extends back indefinitely. ... \$\endgroup\$Greg Martin– Greg Martin2020年04月28日 16:44:37 +00:00Commented Apr 28, 2020 at 16:44
APL (Dyalog Unicode), 35 bytes
Anonymous prefix lambda. This one actually sorts its argument and doesn't use any "cheating" built-ins.
{⍵[⍋((∊∘⎕A⊂⊢)'MoTuWeThFrSa')⍳2↑ ̈⍵]}
{...} "dfn"; argument is ⍵:
⍵[...] reorder the argument into the following order:
2↑ ̈ take the first two letters from each input day name
(...)⍳ find the index in the following list (missing items become 1 beyond last index)
(...)'MoTuWeThFrSa' apply the following tacit function to this string:
⊢ the argument
⊂ split on
∊∘⎕A membership of the uppercase Alphabet
⍋ grade (permutation that would sort it)
C (gcc) -m32, 50 bytes
main(i){for(;puts(nl_langinfo(131079+i%7))-i++;);}
Explanation
Simply put, nl_langinfo() is a useful function which returns a particular string given an argument. It just turns out that the argument to pass for obtaining weekday names is 131079 ... 131086. One other thing is that we must add the flag -m32, which is explained nicely in this answer.
-
\$\begingroup\$ I was really confused by the
-i++part, does it work becauseSundayhappens to be 6 letters (and the 7th day of the week)? EDIT: Innl'_langinfoSunday is the first day of the week, but youri%7seems to handle that. \$\endgroup\$maxb– maxb2020年04月29日 09:06:53 +00:00Commented Apr 29, 2020 at 9:06 -
1\$\begingroup\$ The
ivariable starts to 1, and is incremented after every iteration. That means we actually start at131080instead of131079. This is on purpose, since as you said,Sundayis the first day of the week when usingnl_langinfo. On the last iteration, wheniequals 7, the number becomes131079because of the modulo, printingSunday. The reason it terminates after outputtingSundayis becauseputsreturns the length of the string (including newline), soSundayreturns 7. It also happens thatiequals 7, so subtracting them exits the loop. \$\endgroup\$dingledooper– dingledooper2020年04月29日 15:47:35 +00:00Commented Apr 29, 2020 at 15:47
Python 2, (削除) 45 (削除ここまで) 43 bytes
-2 bytes thanks to @xnor!
lambda l:map(sorted(l).pop,[1,4,4,3,0,0,0])
Sort by normal string comparison first, then look up the correct permutation.
Python 3, 58 bytes
lambda l:sorted(l,key=lambda s:"TuWeThFrSaSu".find(s[:2]))
Use the first 2 letters of each day to look up the order.
-
2\$\begingroup\$ Nice that you skip
Mo, which results in-1for Monday. \$\endgroup\$maxb– maxb2020年04月28日 07:05:39 +00:00Commented Apr 28, 2020 at 7:05 -
1\$\begingroup\$ Nice idea with the sorting! Here's a small byte save, though it needs Python 2 since Python 3 produces a map object unless you unpack. \$\endgroup\$xnor– xnor2020年04月28日 07:29:10 +00:00Commented Apr 28, 2020 at 7:29
-
\$\begingroup\$ @xnor Thank you! I was looking at
__getitem__, but totally forgot aboutpop. \$\endgroup\$Surculose Sputum– Surculose Sputum2020年04月28日 07:33:02 +00:00Commented Apr 28, 2020 at 7:33
Factor, 20 bytes
day-names 1 rotate .
Factor has a built-in sequence for the days of the week, but is starts with Sunday - that's why I need to rotate the items to the right.
T-SQL, 49 bytes
SELECT DATENAME(w,RANK()OVER(ORDER BY d)-1)FROM t
Input is taken as a pre-existing table t with varchar column d, per our IO standards.
My code doesn't actually use the values in the input table in any way, so the input order doesn't matter (nor do the actual strings, they just have to be distinct). Instead, it uses the fact that it has 7 rows, along with the RANK() function, to generate the numbers 1 through 7.
After subtracting 1, these numbers are implicitly cast as dates (0=Mon Jan 1, 0001, 6=Sun Jan 7, 0001), and the DATENAME function returns the corresponding day of the week.
R, 30 bytes
Assuming an English locale
weekdays(as.Date("1/1/1")+0:6)
January 1st of the year 1 is a Monday (according to R).
-
\$\begingroup\$ Technically, this only works if your computer is set to a locale such as
en_GBwhere the day names are the same as the question's. I think you are allowed to assume any reasonable locale you like for these sorts of challenges so it's not an invalid answer, but you should perhaps say at the top that you are assuming an English locale. \$\endgroup\$JDL– JDL2020年04月29日 09:41:20 +00:00Commented Apr 29, 2020 at 9:41 -
\$\begingroup\$ @JDL Thanks, never thought about that \$\endgroup\$Bart-Jan van Rossum– Bart-Jan van Rossum2020年04月29日 12:35:17 +00:00Commented Apr 29, 2020 at 12:35
MathGolf, 8 bytes
á{$ûÞ♠$%
Explanation
á{ sort by the output given from a code block
$ convert to ordinal (base 256)
ûÞ♠ push "Þ♠"
$ convert to ordinal (gives 1791)
% modulo
I had the exact same idea as isaacg's Pyth answer, but in MathGolf I had to use another modulo. I tried every number up to 1000000, and noticed that the ordinal strings for each weekday ended up in the correct order for sorting when taking them modulo 1791.
-
\$\begingroup\$ Tried to find a shorter way to generate
1791, but can't find one unfortunately. I did found an equal-bytes alternative, though:○しろまる♠~+(2048+~256). \$\endgroup\$Kevin Cruijssen– Kevin Cruijssen2020年04月28日 13:56:29 +00:00Commented Apr 28, 2020 at 13:56 -
\$\begingroup\$ @KevinCruijssen I thought about modifying the input string (e.g. "Monday") or its ordinal, in order to change the 1791 into another nicer number (hopefully a 1-byte builtin), but I didn't investigate further there. Best case scenario is that it saves 2 bytes, which would be tied with 05AB1E. \$\endgroup\$maxb– maxb2020年04月28日 14:16:19 +00:00Commented Apr 28, 2020 at 14:16
-
\$\begingroup\$ You mean like removing the first character of the string before the
$or something along those lines? \$\endgroup\$Kevin Cruijssen– Kevin Cruijssen2020年04月28日 14:17:37 +00:00Commented Apr 28, 2020 at 14:17 -
\$\begingroup\$ @KevinCruijssen Yes, something along those lines. It could also be squaring the number after the
$, or something similar. But there were so many operators to try that I gave up before I started. \$\endgroup\$maxb– maxb2020年04月28日 14:19:25 +00:00Commented Apr 28, 2020 at 14:19
Kotlin , 58 bytes
DayOfWeek.values().map{it.name.toLowerCase().capitalize()}
Perl 5, (削除) 33 (削除ここまで) 29 bytes
print+(sort<>)[1,5,6,4,0,2,3]
Try it online! Just another sort/permute answer. Edit: Saved 4 bytes thanks to @Xcali.
-
\$\begingroup\$ Shaved 4 bytes: Try it online! \$\endgroup\$Xcali– Xcali2020年04月28日 20:22:00 +00:00Commented Apr 28, 2020 at 20:22
-
\$\begingroup\$ @Xcali Thanks! I don't really know Perl, I was just putting in parentheses until it stopped erroring... \$\endgroup\$Neil– Neil2020年04月28日 20:57:47 +00:00Commented Apr 28, 2020 at 20:57
-
\$\begingroup\$ I just found another issue there. Thursday and Friday are reversed in your output. \$\endgroup\$Xcali– Xcali2020年04月28日 21:12:59 +00:00Commented Apr 28, 2020 at 21:12
-
\$\begingroup\$ @Xcali Thanks for spotting my typo... (I got the sequence of digits correct in my other answer at least.) \$\endgroup\$Neil– Neil2020年04月28日 21:49:50 +00:00Commented Apr 28, 2020 at 21:49
Retina 0.8.2, (削除) 32 (削除ここまで) (削除) 29 (削除ここまで) (削除) 27 (削除ここまで) 26 bytes
Tu
13
T`MWT`E
O`
T`d`MTWuT
Approach: (削除) Replace the first letter of the days with Replace the A, B, C, etc. so we can sort them lexicographically. (削除ここまで)M in Monday with a 0, the T in Tuesday with a 1, the W in Wednesday with a 2, and the T in Thursday with 4. To help golf off a byte, also replace the u in Tuesday with 3
Tu
13
T`MWT`E
Perform the replacements described above (The E inserts even numbers 02468)
O`
Sort lexicographically. At this point, our list of words looks like this:
0onday
13esday
2ednesday
4hursday
Friday
Saturday
Sunday
Notice that Friday, Saturday, and Sunday are already conveniently in alphabetical order.
T`d`MTWuT
Undo the transformations we performed above (The d inserts the range 0-9)
Powershell, 23 bytes
1..6+0|%{[DayOfWeek]$_}
Convert integers to weekday, but bump array by one because .net prefers Sundays.
37 bytes sorting
($args|%{[DayOfWeek]$_}|sort)[1..6+0]
input is string list to args.
-
1
PHP, 39 bytes
for(;$i<7;)echo" ".jddayofweek($i++,1);
Works in Windows but not in TIO, gotta find how to activate the extension.. takes no input.
We could save 4 bytes with an empty delimiter (which is "some delimiter" the question doesn't say a non-empty delimiter) but I'm not that nasty..
EDIT: version that works universally for 1 byte more
PHP, 40 bytes
for(;$i<7;)echo date("l ",1e6+$i++*8e4);
C (gcc) Little Endian Byte Order, (削除) 123 (削除ここまで) \$\cdots\$ (削除) 85 (削除ここまで) 67 bytes
c(int**a,int**b){a=**a%274%79-**b%274%79;}f(int*s){qsort(s,7,8,c);}
Saved a whopping 18 bytes - and made it shorter than just printing days - thanks to ceilingcat!!!
Function f takes a list of strings as input and sorts it.
How
Reads the first four characters as a 32-bit int, \$i\$, and then calculates \$((i\mod{274})\mod{79})\$:
Monday -> 5
Tuesday -> 7
Wednesday -> 11
Thursday -> 23
Friday -> 47
Saturday -> 59
Sunday -> 61
Then uses qsort to sort the array.
Previous solution
C (gcc), (削除) 72 (削除ここまで) 70 bytes
Saved 2 bytes thanks to gastropner!!!
f(){puts("Monday Tuesday Wednesday Thursday Friday Saturday Sunday");}
Just prints the days of the week.
(削除) Unfortunately (削除ここまで) Fortunately this (削除) is (削除ここまで) isn't shorter than sorting the input! All thanks to ceilingcat!! :)))
-
\$\begingroup\$ You could save 2 bytes by using
puts()instead ofprintf(). \$\endgroup\$gastropner– gastropner2020年04月30日 01:05:56 +00:00Commented Apr 30, 2020 at 1:05 -
\$\begingroup\$ @gastropner Nice one - thanks! :-) \$\endgroup\$Noodle9– Noodle92020年04月30日 06:07:18 +00:00Commented Apr 30, 2020 at 6:07
-
\$\begingroup\$ Nice solution, but it isn't ISO 8601 compliant unfortunately. The week should start with Monday for this challenge. \$\endgroup\$maxb– maxb2020年04月28日 07:00:43 +00:00Commented Apr 28, 2020 at 7:00
-
\$\begingroup\$ Fixed. Thanks for pointing it out. \$\endgroup\$Mitchell Spector– Mitchell Spector2020年04月28日 07:03:14 +00:00Commented Apr 28, 2020 at 7:03
Keg, 15 bytes
"jnsDt[rƳm(7)dQ7⅍
You'd think this was some sort of fancy sorting algorithm, but no. It's simply a compressed string.
-
2\$\begingroup\$ Meh, I've got 22 in Jelly:
"¡²2GỌḌ¹Ẹṙṙ,ßṃþ-wẈÇUx»\$\endgroup\$Adám– Adám2020年04月28日 07:04:32 +00:00Commented Apr 28, 2020 at 7:04
Retina, 40 bytes
^
MTuWThFSaSu
,6L$s`(.+)(?=.*(1円\w+))
2ドル
Try it online! Link shuffles input in header. Explanation:
^
MTuWThFSaSu
Insert unique day name abbreviations.
,6L$s`(.+)(?=.*(1円\w+))
2ドル
Find the first seven duplicate substrings, and output the word containing the duplicate, thus unabbreviating the names.
Charcoal, (削除) 29 (削除ここまで) 25 bytes
≔E7SθW−θυ⊞υ⌊ιE1564023§υIι
Try it online! Link is to verbose version of code. Explanation:
≔E7Sθ
Input the seven days.
W−θυ⊞υ⌊ι
Sort the days lexicographically.
E1564023§υIι
Apply the permutation to the array.
Explore related questions
See similar questions with these tags.