8
\$\begingroup\$

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"))))))));

Your program should:

  • output full words (STDOUT)
  • not crash if the input is less than 1 or greater than 7
\$\endgroup\$
  • 6
    \$\begingroup\$ 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. \$\endgroup\$ – trichoplax Aug 15 '14 at 15:25
  • 9
    \$\begingroup\$ 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). \$\endgroup\$ – Peter Taylor Aug 15 '14 at 15:31
  • 2
    \$\begingroup\$ 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. \$\endgroup\$ – trichoplax Aug 15 '14 at 15:32
  • 2
    \$\begingroup\$ Note that questions are language agnostic by default - you don't need to specify that it's not restricted to one programming language. \$\endgroup\$ – trichoplax Aug 15 '14 at 15:35
  • 2
    \$\begingroup\$ 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. \$\endgroup\$ – nyuszika7h Aug 17 '14 at 15:07

45 Answers 45

0
\$\begingroup\$

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: 
Read 1 item
[1] "Sunday"
> a=format(Sys.Date()+0:15,"%A");a[grep("^M",a)[1]+0:6][scan()]
1: 4
2: 
Read 1 item
[1] "Thursday"
> a=format(Sys.Date()+0:15,"%A");a[grep("^M",a)[1]+0:6][scan()]
1: 2
2: 
Read 1 item
[1] "Tuesday"
> a=format(Sys.Date()+0:15,"%A");a[grep("^M",a)[1]+0:6][scan()]
1: 8
2: 
Read 1 item
[1] NA
\$\endgroup\$
0
\$\begingroup\$

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.

\$\endgroup\$
0
\$\begingroup\$

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.

\$\endgroup\$
0
\$\begingroup\$

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)+"";}
\$\endgroup\$
0
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ 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). \$\endgroup\$ – nyuszika7h Aug 20 '14 at 10:16
  • \$\begingroup\$ 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 \$\endgroup\$ – Okw Aug 20 '14 at 11:52
0
\$\begingroup\$

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];
\$\endgroup\$
0
\$\begingroup\$

C, 81

Just for fun...

printf("%sday\n","Mon\0Tues\0Wednes\0Thurs\0Fri\0Satur\0Sun"+"  $)06:@"[n&7]-32);
\$\endgroup\$
0
\$\begingroup\$

Java

Uses local language:

return i<1||i>7?"Error":new DateFormatSymbols().getWeekdays()[i];
\$\endgroup\$
  • \$\begingroup\$ You can use only one | instead of || to save one character. Also good idea to create new instance, instead of calling getInstance(). It is shorter. But without specific locale, your output for 1 can be "sunday", not "monday" (locale en_US). \$\endgroup\$ – Tomáš Dvořák Aug 22 '14 at 5:05
0
\$\begingroup\$

C, 117

Also just for fun...

d(n){for(char*p="\177MvT|WpF S\177oxh`a`t\026uze|d;n|eHrrspi\001d\001a\001y\001\n";
*p;p++)-*p++&1<<n-1&&putchar(*p);}

(I realize this isn't an obfuscation challenge. I just wanted to see how far a purely data-driven bit-test loop could be golfed.)


The un-golfed version:

void d(int n)
{
  char *p =
    "\177" "M"   "\166" "T"   "\174" "W"   "\160" "F"   "\040" "S"
    "\177" "o"   "\170" "h"   "\140" "a"   "\140" "t"   "\026" "u"
    "\172" "e"   "\174" "d"   "\073" "n"   "\174" "e"   "\110" "r"
    "\162" "s"   "\160" "i"   "\001" "d"   "\001" "a"   "\001" "y"
    "\001" "\n";

  for (; *p; p++)
    if (-(*(p++)) & (1<<(n-1)))
      putchar(*p);
}

Driver:

int main() { for (int n = 1; n <= 7; n++) d(n); }

Output:

Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
Sunday

How it works:

  • The character string consists of a pairs of bit-flags and output characters.
  • The even indexes are the bit-flags ('\177', 'v', '|', 'p', ' ', etc.) — but they are stored in ones-complement form to minimize the number of nonprintable ASCII values, which are expensive because they have to be encoded in octal.
  • The odd indexes are the output characters ('M', 'T', 'W', 'F', 'S', 'o', 'h', 'a', etc.).
\$\endgroup\$
0
\$\begingroup\$

Batch - 114 Bytes

@cmd/von/c"for %%a in (1Mon 2Tues 3Wednes 4Thur 5Fri 6Satur 7Sun)do @set a=%%a&if "!a:~0,1!"=="%1" echo !a:~1!day"

The first part @cmd/von/c is starting up a new cmd session - it uses the /v switch to enable delayed variable expansion, which saves adding in the lengthy setLocal enableDelayedExpansion. This way you only need to add the @ symbol (to hide output) before the cmd command and in the middle of the for loop, instead of @echo off at the start of the script.

\$\endgroup\$
0
\$\begingroup\$

PHP - 32

Day is given as $i

<?echo date('l',(($i+4)*86400));
\$\endgroup\$
0
\$\begingroup\$

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
...
\$\endgroup\$
  • 1
    \$\begingroup\$ 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... \$\endgroup\$ – Alessandro Pezzato Aug 28 '14 at 17:31
  • \$\begingroup\$ Because 2001-01-01 was Monday, but you can notice that the last number is precedeed by a $, so it's a parameter. \$\endgroup\$ – Alessandro Pezzato Aug 28 '14 at 17:33
0
\$\begingroup\$

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))))
\$\endgroup\$
0
\$\begingroup\$

Javascript - 63

'0Mon0Tues0Wednes0Thurs0Fri0Satur0Sun'.split(0)[prompt()]+'day'
\$\endgroup\$
  • \$\begingroup\$ This doesn't handle invalid input correctly. \$\endgroup\$ – overactor Sep 1 '14 at 15:01
  • \$\begingroup\$ The spec doesn't stipulate invalid input handling, only that the program doesn't crash \$\endgroup\$ – Adam Bowles Sep 2 '14 at 7:38
  • \$\begingroup\$ I stand corrected. \$\endgroup\$ – overactor Sep 2 '14 at 7:46
0
\$\begingroup\$

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.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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