# Input a number and return day of week

Not restricted to any one programming language.

What I'm saying is you pass 1 to it and it returns Monday, 7 corresponds with sunday and so on.

I've managed a 146 byte statement in Java to do the same.

return (n<1?"invalid":(n<2?"monday":(n<3?"tuesday":(n<4?"wednesday":(n<5?"thursday":(n<6?"friday":(n<7?"saturday":(n<8?"sunday":"invalid"))))))));


• output full words (STDOUT)
• not crash if the input is less than 1 or greater than 7
• I can't know what other people are downvoting for, but I would expect people to dislike the use of "popularity contest" on a question that is purely a golf question. Generally popularity contest is used for questions that don't have an objective way of deciding a winner. – trichoplax Aug 15 '14 at 15:25
• This question can't agree with itself on what it wants people to write. A statement which finds input in a variable and returns it or a program which writes to stdout? (It's also a bizarre use of popularity-contest which, if it catches on, might do a lot to help those who want to kill the tag completely). – Peter Taylor Aug 15 '14 at 15:31
• You'll learn what works and what makes a good question by asking questions just like you are, and getting feedback. I personally thought this question was too simple to make an interesting challenge, but you have 3 answers already in just half an hour, which shows that it's hard to guess what makes a good challenge until you ask it. With the new reputation you gain from this question you'll soon be able to use the sandbox to get feedback on ideas before you post them. – trichoplax Aug 15 '14 at 15:32
• Note that questions are language agnostic by default - you don't need to specify that it's not restricted to one programming language. – trichoplax Aug 15 '14 at 15:35
• The question says you should output to stdout, but the accepted answer doesn't do so and even assumes the existence of a variable. Also, do we have to output invalid if the input is invalid? The question doesn't state that but the example suggests so. – nyuszika7h Aug 17 '14 at 15:07

# R, 61

a=format(Sys.Date()+0:15,"%A");a[grep("^M",a)[1]+0:6][scan()]


format(Sys.Date()+0:15,"%A") outputs the day of the week of today and the 15 following days. grep("^M",a)[1]+0:6 is the index of the first monday in that sequence and the six following days.

Usage:

> a=format(Sys.Date()+0:15,"%A");a[grep("^M",a)[1]+0:6][scan()]
1: 7
2:
[1] "Sunday"
> a=format(Sys.Date()+0:15,"%A");a[grep("^M",a)[1]+0:6][scan()]
1: 4
2:
[1] "Thursday"
> a=format(Sys.Date()+0:15,"%A");a[grep("^M",a)[1]+0:6][scan()]
1: 2
2:
[1] "Tuesday"
> a=format(Sys.Date()+0:15,"%A");a[grep("^M",a)[1]+0:6][scan()]
1: 8
2:
[1] NA


# Golfscript (50 55)

Expects the input number to be on the stack.

" Mon Tues Wednes Thurs Fri Satur Sun"" "/\="day"+


or

("Mon Tues Wednes Thurs Fri Satur Sun"" "/\="day"+


or (for 51, but it's just fun)

"day Mon Tues Wednes Thurs Fri Satur Sun"" "/.@=\0=


The requirement "not crash" is pretty lenient. This will just print "day" for invalid input – which is not a crash, so it should be allowed.

# CJam - 47 or 45

qi"Sun Mon Tues Wednes Thurs Fri Satur"S/="day"


Try it at http://cjam.aditsu.net/

If I can assume that the number is already on the stack, then the first 2 characters can be removed.

## C# - 28

According to your reference solution, this should be valid.

It assumes the input is stored in the variable i and outputs "Sunday", "Monday", etc for 0, 1, etc respectively.

If i is out of bounds (below 0 or above 7), i will be output. (This is valid because the program doesn't crash)

Console.Write((DayOfWeek)i);


If you actually want a working function, here is one using 48 bytes:

string f(int i){return((DayOfWeek)i)+"";}


# Python - 83 76

from datetime import*
i=input()
if 0<i<8:print date(2013,7,i).strftime("%A")


July 2013 was the first month I found that had monday as its first day. Rest of this is quite self explanatory.

• It's 82 bytes, don't count the trailing newline. You can use from datetime import* and date instead of import datetime as d and d.date (-1 byte), input() instead of int(input) (-5 bytes), and 1,1 instead of 2013,7 (-3 bytes). – nyuszika7h Aug 20 '14 at 10:16
• Thanks for the suggestions! Although "1,1" as "year,month" does not work; ValueError: year=1 is before 1900; the datetime strftime() methods require year >= 1900 – Okw Aug 20 '14 at 11:52

# Java 8, 48 chars

return n>0&n<8?DayOfWeek.of(n).name():"Invalid";


With java 8 comes new enum DayOfWeek, that provides non locale dependent data.

For java 7 and older, this should work good too (76 chars):

return n<1|n>7?"Invalid":DateFormatSymbols.getInstance().getWeekdays()[n+1];


And locale fixed variant (90 chars):

return <1|n>7?"Invalid":DateFormatSymbols.getInstance(Locale.ENGLISH).getWeekdays()[n+1];


Just for fun...

<?echo date('l',(($i+4)*86400));  # bash - 18 date +%A -d01010$1


Save it in a file, e.g. day.sh, then run

./day.sh 1 # output: Monday
./day.sh 2 # output: Tuesday
...

• Try: save it in a file script.sh and run: bash script.s 1, it will output "Monday". bash script.sh 2, it will output "Tuesday" etc... – Alessandro Pezzato Aug 28 '14 at 17:31
• Because 2001-01-01 was Monday, but you can notice that the last number is precedeed by a \$, so it's a parameter. – Alessandro Pezzato Aug 28 '14 at 17:33

## R, 37

The obligatory hack solution with R.

weekdays(as.Date((paste0("1/1/",n))))


where the input is first stored as variable n. Turning this into a named function would be longer:

d=function(n) weekdays(as.Date((paste0("1/1/",n))))


## Javascript - 63

'0Mon0Tues0Wednes0Thurs0Fri0Satur0Sun'.split(0)[prompt()]+'day'

• This doesn't handle invalid input correctly. – overactor Sep 1 '14 at 15:01
• The spec doesn't stipulate invalid input handling, only that the program doesn't crash – Adam Bowles Sep 2 '14 at 7:38
• I stand corrected. – overactor Sep 2 '14 at 7:46

# C# - 30

Since the provided example doesn't actually output, neither does this. Also I'm assuming we don't have to have an error message if the value is below 1 or over 7.

((DayOfWeek)(n%7)).ToString();


For an error message (47):

n<1|n>7?"Error":((DayOfWeek)(n%7)).ToString();


The reason for the %7 is because DayOfWeek begins with Sunday.

Small cheat: DayOfWeek is contained in the System namespace, so if using System; isn't specified elsewhere (usually always is), you'd need to replace DayOfWeek with System.DayOfWeek -- an additional 7 characters.