47
\$\begingroup\$

Inspired by We had a unit test once which only failed on Sundays, write a program or function that does nothing but throw an error when it is Sunday, and exit gracefully on any other day.

Rules:

  • No using input or showing output through the usual IO methods, except to print to STDERR or your language's equivalent. You are allowed to print to STDOUT if it's a by-product of your error.
  • A function may return a value on non-Sundays as long as it doesn't print anything
  • Your program may use a Sunday from any timezone, or the local timezone, as long as it is consistent.
  • An error is a something that makes the program terminate abnormally, such as a divide by zero error or using an uninitialised variable. This means that if any code were to be added after the part that errors, it would not be executed on Sunday.
  • You can also use statements that manually create an error, equivalent to Python’s raise.
  • This includes runtime errors, syntax errors and errors while compiling (good luck with that!)
  • On an error there must be some sign that distinguishes it from having no error
  • This is , so the shortest bytecount in each language wins!

I'll have to wait til Sunday to check the answers ;)

\$\endgroup\$
14
  • 2
    \$\begingroup\$ By "write a program or function that does nothing but throw an error on Sunday, and exit gracefully on any other day", do you mean that whenever it is run on sunday it should fail, or do you mean that there should be at least one possibility it fails a sunday. To make it clearer, if it fails only on sunday at 2pm, but not on sunday 3pm, is it fine ? \$\endgroup\$
    – Bromind
    Jan 5 '18 at 13:53
  • 6
    \$\begingroup\$ This would have been even better if Saturday had been used. You could have called it "Saturday Night Error" and even worked in some adjusted song lyrics to the question. \$\endgroup\$
    – Aaron
    Jan 5 '18 at 18:57
  • 3
    \$\begingroup\$ Sundays? How about failing between midnight and 1am? \$\endgroup\$ Jan 5 '18 at 20:25
  • 1
    \$\begingroup\$ It's not clear from your question what constitutes an error. I'm voting to close until this is remedied. \$\endgroup\$
    – Grain Ghost
    Jan 6 '18 at 16:28
  • 4
    \$\begingroup\$ Not really ... How can we distinguish the output of an error from regular output. Is something like print "error" an error? The added paragraph doesn't really clarify anything. \$\endgroup\$
    – Grain Ghost
    Jan 6 '18 at 18:20

52 Answers 52

26
\$\begingroup\$

Bash + coreutils, 15 14 bytes

`date|grep Su`

Try it online!

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

PHP 7, 12 bytes

1%date("w");

On PHP 7 it throws an exception of type DivisionByZero on Sundays. The same happens if it is interpreted using HHVM.

On PHP 5 it displays a warning (on stderr) on Sundays:

PHP Warning:  Division by zero in Command line code on line 1

On any PHP version, it doesn't display anything on the other days of the week.

Run using the CLI:

php -r '1%date("w");'

or try it online!

Two more bytes can be squeezed by stripping the quotes (1%date(w);) but this triggers a notice (that can be suppressed by properly set error_reporting = E_ALL & ~E_NOTICE in php.ini).

\$\endgroup\$
5
  • \$\begingroup\$ I believe you must specify (in the title) that this answer is only and only for PHP7+ and for HHVM, since PHP5.6 and lower exit without any problem. Warnings aren't errors and don't stop the execution of the code. If you do 1%date("w");echo "Alive!", it will stop in PHP7+ and HHVM, but not in all the other versions since PHP 4. \$\endgroup\$ Jan 6 '18 at 12:55
  • \$\begingroup\$ @IsmaelMiguel the question classifies any printing to Standard Error as an error, so a warning's good enough in this case. \$\endgroup\$ Jan 6 '18 at 18:29
  • 1
    \$\begingroup\$ Quoting the question: "An error is a something that makes the program terminate abnormally, such as a divide by zero error or using an uninitialised variable.This means that if any code were to be added after the part that errors, it would not be executed on Sunday. ". This doesn't happen with a warning. \$\endgroup\$ Jan 6 '18 at 18:48
  • \$\begingroup\$ @IsmaelMiguel the paragraph you are quoting was added to the question less than one hour ago. \$\endgroup\$
    – axiac
    Jan 6 '18 at 18:58
  • 1
    \$\begingroup\$ Quoting an older version, the one that was on at the time of the comment that I wrote: "[...] write a program or function that does nothing but throw an error when it is Sunday, and exit gracefully on any other day.". A warning is against this line because PHP will exit gracefully. An error would be a fatal error or a syntax error. Not a warning for dividing by 0. PHP 5.6 and older aren't valid for this challenge. \$\endgroup\$ Jan 7 '18 at 0:49
10
\$\begingroup\$

Java 8, 69 43 34 bytes

v->1/new java.util.Date().getDay()

-26 bytes thanks to @OlivierGrégoire.
-9 bytes thanks to @Neil.

Explanation:

Try it here.

  • v->{...} (unused Void null parameter) is one byte shorter than ()->{...} (no parameter).
  • new java.util.Date().getDay() will return 0-6 for Sunday-Saturday, so 1/... will give an java.lang.ArithmeticException: / by zero error if the value is 0, which only happens on Sundays.
\$\endgroup\$
5
  • 1
    \$\begingroup\$ v->{int i=1/new java.util.Date().getDay();} (43 bytes). \$\endgroup\$ Jan 5 '18 at 13:33
  • \$\begingroup\$ @OlivierGrégoire Ah, java.util.Date() does have a method to get the day of the week.. And it's even 0 for Sunday.. Not sure how I missed that. :S \$\endgroup\$ Jan 5 '18 at 13:42
  • \$\begingroup\$ Yup, usually the older classes have all the functionality in shorter names. ;-) \$\endgroup\$ Jan 5 '18 at 13:49
  • 1
    \$\begingroup\$ Is this valid for 34 bytes? Try it online! \$\endgroup\$
    – Neil
    Jan 5 '18 at 14:58
  • \$\begingroup\$ @Neil it wasn't valid at the time you posted but became valid less than an hour later. ;-) It was my first idea, though... \$\endgroup\$ Jan 9 '18 at 11:07
9
\$\begingroup\$

PHP, 15 bytes

<?@date(w)?:\n;

Assumes default settings.

Output on Sundays

Fatal error: Undefined constant 'n' on line 1

Try it online!

\$\endgroup\$
1
  • 2
    \$\begingroup\$ Notice that this answer requires PHP 5.3+. An alternative could be ||\n instead of ?:\n. \$\endgroup\$ Jan 6 '18 at 13:03
8
\$\begingroup\$

Python 3, 33 bytes

import time
"Su"in time.ctime()>q

Try it online!

Python 3, 50 bytes

from datetime import*
datetime.now().weekday()>5>q

Try it online!

Saved ~3 bytes thanks to Rod.

\$\endgroup\$
9
  • 1
    \$\begingroup\$ @Rod Why not "Su"in time.ctime()>q (I was editing with this one)? \$\endgroup\$
    – Mr. Xcoder
    Jan 5 '18 at 12:42
  • \$\begingroup\$ I can't quite grasp it. How does it work? \$\endgroup\$
    – pacholik
    Jan 5 '18 at 13:00
  • \$\begingroup\$ @pacholik "Su"in time.ctime() checks if the current day is Sunday. If this is false, the >q part isn't evaluated at all and everything exits smoothly. But if that is true, then the second part of the inequality is evaluated, and since q is not defined, this would throw a NameError. \$\endgroup\$
    – Mr. Xcoder
    Jan 5 '18 at 13:01
  • 1
    \$\begingroup\$ @pacholik Edit: I don't think I am wrong. For efficiency purposes, if the first part is falsy, then Python doesn't even bother to evaluate the last part. I'll have to wait until Sunday to test this, though. (I think) Demonstration. \$\endgroup\$
    – Mr. Xcoder
    Jan 5 '18 at 13:07
  • 3
    \$\begingroup\$ @pacholik Note that Python chains boolean operators, so that is equivalent to ("Su" in time.ctime()) and (time.ctime() > q). \$\endgroup\$
    – DELETE_ME
    Jan 5 '18 at 13:14
7
\$\begingroup\$

Pyth, 8 7 bytes

 l-6.d9

Try it online!

Explanation

    .d9 # Get the current day of week (0 = Monday, 6 = Sunday)
  -6    # Subtract 6 from the day
 l      # Try to calculate the log base 2 of the result of the previous operation raising a "ValueError: math domain error" on sundays
        # there is an extra space at the start, to supress the output on the other days
\$\endgroup\$
4
  • \$\begingroup\$ This is still invalid. Your output (1.0 is not generated by the error). The OP said explicitly that they don’t allow that. You can fix that by prepending a single space to your code, though \$\endgroup\$
    – Mr. Xcoder
    Jan 5 '18 at 13:37
  • \$\begingroup\$ But you are not allowed to output to STDOUT unless it is Sunday, and you do output to STDOUT. \$\endgroup\$
    – Mr. Xcoder
    Jan 5 '18 at 13:41
  • \$\begingroup\$ @Mr.Xcoder is correct. On non-Sundays nothing should be outputted \$\endgroup\$
    – Jo King
    Jan 5 '18 at 13:42
  • \$\begingroup\$ @JoKing I totally skipped this rule, fixed now \$\endgroup\$
    – Rod
    Jan 5 '18 at 13:44
7
\$\begingroup\$

JavaScript, 23 Bytes

Date().slice(1)>'um'&&k

Full program.

The variable k must not be defined.

JavaScript, 20 bytes by Rick Hitchcock

/Su/.test(Date())&&k

JavaScript, 19 bytes by apsillers

Date().match`Su`&&k
\$\endgroup\$
7
  • \$\begingroup\$ 1) You forgot to include the _=>; without it this is a snippet which are not allowed by default. 2) This outputs false on every other day when it shouldn't output anything. \$\endgroup\$
    – Shaggy
    Jan 5 '18 at 12:58
  • 7
    \$\begingroup\$ 1) It's a full program, which is allowed by the OP. 2) If you run it as a program, there's no output \$\endgroup\$
    – l4m2
    Jan 5 '18 at 13:00
  • 3
    \$\begingroup\$ /Su/.test(Date())&&k for 20 bytes. \$\endgroup\$ Jan 5 '18 at 15:58
  • 1
    \$\begingroup\$ new Date version 25 bytes (new Date/864e5%7|0)-3||p \$\endgroup\$
    – l4m2
    Jan 5 '18 at 16:37
  • 3
    \$\begingroup\$ The solution by @RickHitchcock can be made one shorter by template-tag execution on match instead: Date().match`Su`&&k \$\endgroup\$
    – apsillers
    Jan 5 '18 at 20:53
6
\$\begingroup\$

Ruby, 15 bytes

1/Time.now.wday

wday will return 0 on Sunday causing a ZeroDivisionError: divided by 0 error. For example: 1/Time.new(2018,1,7).wday.

\$\endgroup\$
6
\$\begingroup\$

05AB1E, 45 44 bytes

As 05AB1E doesn't have a built in for getting the day of the week, I've used Zeller's Rule to calculate it.

Prints a newline to stderr in case of a Sunday (observable in the debug view on TIO)

žežf11+14%Ì13*5÷žgžf3‹-т%D4÷žgт÷©4÷®·(O7%i.ǝ

Try it online!

Explanation

The general formula used is
DoW = d + [(13*(m+1))/5] + y + [y/4] + [c/4] - 2*c
Where DoW=day of week, d=day, m=month, y=last 2 digits of year, c=century and and expression in brackets ([]) is rounded down.

Each month used in the formula correspond to a number, where Jan=13,Feb=14,Mar=3,...,Dec=12
As we have the current month in the more common format Jan=1,...,Dec=12 we convert the month using the formula
m = (m0 + 11) % 14 + 1

As a biproduct of March being the first month, January and February belong to the previous year, so the calculation for determining y becomes
y = (year - (m0 < 3)) % 100

The final value for DoW we get is an int where 0=Sat,1=Sun,...,6=Fri.
Now we can explicitly throw an error if the result is true.

\$\endgroup\$
1
  • 12
    \$\begingroup\$ 05AB1E losing to Java? Everything I know is a lie \$\endgroup\$ Jan 5 '18 at 16:40
6
\$\begingroup\$

Haskell + Data.Dates, 55 bytes

import Data.Dates
succ.dateWeekDay<$>getCurrentDateTime

Try it online!

This uses the fact that Sunday is the last day of the week. dateWeekDay returns the day of the week as a WeekDay type, which is simply defined as

data WeekDay = Monday | Tuesday | Wednesday | Thursday | Friday | Saturday | Sunday

WeekDay is an instance of Enum, thus we can use succ and pred to get the successor or predecessor of a weekday, e.g. succ Monday yields Tuesday.

However, Sunday is the last enum entry, so calling succ Sunday results in the following error:

fail_on_sunday.hs: succ{WeekDay}: tried to take `succ' of last tag in enumeration
CallStack (from HasCallStack):
  error, called at .\Data\Dates.hs:56:34 in dates-0.2.2.1-6YwCvjmBci55IfacFLnAPe:Data.Dates

Edit 1: Thanks to nimi for -3 bytes!
Edit 2: -11 bytes now that functions are allowed.


Full program: 88 81 74 69 66 bytes

import Data.Dates
main=pure$!succ.dateWeekDay<$>getCurrentDateTime

Try it online!

pure is needed to lift the resulting WeekDay back into the IO Monad. However, Haskell sees that the value is not output in any way by the program, so lazy as it is, the expression is not evaluated, so even on Sundays the program would not fail. This is why $! is needed, which forces the evaluation even if Haskell would normally not evaluate the expression.


Previous approach with Data.Time: 127 124 bytes

import Data.Time.Clock
import Data.Time.Calendar.WeekDate
c(_,_,d)|d<7=d
main=getCurrentTime>>=(pure$!).c.toWeekDate.utctDay

Try it online! These are some impressive imports. Change d<7 to e.g. d/=5 to test failure on a Friday. Fails with the following exception: Non-exhaustive patterns in function c.

\$\endgroup\$
5
  • 1
    \$\begingroup\$ main=pure$!succ.dateWeekDay<$>getCurrentDateTime. And, as functions are allowed, you can drop the main=. \$\endgroup\$
    – nimi
    Jan 5 '18 at 15:01
  • \$\begingroup\$ @nimi Thanks! I'm not sure about the function though, because of the No using input or showing output through the usual IO methods rule. As far as I see, using a function would result in an output for non-Sundays, even though it is wrapped in an IO-action. \$\endgroup\$
    – Laikoni
    Jan 5 '18 at 15:42
  • \$\begingroup\$ Maybe you're right, but on the other hand a full program has an exit code, which is also a standard method. \$\endgroup\$
    – nimi
    Jan 5 '18 at 15:46
  • \$\begingroup\$ ... the challenge rules now allow functions to return values on non-Sundays as long as they don't print. \$\endgroup\$
    – nimi
    Jan 5 '18 at 15:51
  • \$\begingroup\$ @nimi Thanks for the notice. \$\endgroup\$
    – Laikoni
    Jan 6 '18 at 20:12
6
\$\begingroup\$

Perl 5, 13 bytes

1/(gmtime)[6]

Try it online!

Ported @biketire's answerj

removed 3 bytes with @mik's reminder

\$\endgroup\$
1
  • 1
    \$\begingroup\$ gmtime instead of localtime will also meet the rules, and is 3 bytes shorter \$\endgroup\$
    – mik
    Jan 6 '18 at 15:28
5
\$\begingroup\$

TI-Basic 84+, 23 bytes

getDate
0/(1-dayOfWk(Ans(1),Ans(2),Ans(3

Needs date & time commands, which are 84+ and higher only.

\$\endgroup\$
5
\$\begingroup\$

C, 35, 34 27 bytes

f(n){n/=time(0)/86400%7^3;}

-7 bytes with thanks to @MartinEnder and @Dennis

Try it online!

\$\endgroup\$
1
  • 3
    \$\begingroup\$ Welcome to PPCG! \$\endgroup\$ Jan 5 '18 at 16:28
5
\$\begingroup\$

VBA / VBScript, 22 20 bytes

Saved 2 bytes thanks to Taylor Scott.

a=1/(Weekday(Now)-1)

This should be run in the Immediate Window. Weekday() returns 1 (Sunday) through 7 (Saturday) so this creates a divide by zero error on Sunday. Otherwise, no output.

Error Message

\$\endgroup\$
6
  • \$\begingroup\$ You beat me to an answer by an hour - 19 Bytes: ?1/(Weekday(Now)-1) \$\endgroup\$ Jan 5 '18 at 15:23
  • 1
    \$\begingroup\$ @TaylorScott I forgot that Now is valid without the () in VBA but I can't use print because I think all output is disallowed unless it errors out. No using input or showing output through the usual IO methods, except to print to STDERR. Still, saved 2 bytes. \$\endgroup\$ Jan 5 '18 at 15:40
  • 1
    \$\begingroup\$ I had just written these exact 20 bytes as a VBScript solution, and then I thought I'd look to see if there was an existing VB-style language already submitted and here it is. So, this works for VBScript and probably other VB-style languages as well. \$\endgroup\$
    – user44863
    Jan 8 '18 at 16:51
  • \$\begingroup\$ @EngineerToast I think you should mark this as a polyglot with VBScript \$\endgroup\$ Jan 11 '18 at 14:25
  • \$\begingroup\$ @TaylorScott I haven't done that before. Did I do it right? \$\endgroup\$ Jan 11 '18 at 15:32
4
\$\begingroup\$

R, 31 bytes 30 bytes

if(format(Sys.Date(),'%u')>6)a

Try it online!

No output on non-Sundays, Error: object 'a' not found on Sundays.

format(Sys.Date(),'%u') was the shortest way I could find to get weekday, it outputs a character-class number for day of week, with 7 for Sundays. We can compare to a numeric 7, and if true attempt to use an undefined object.

Saved a byte thanks to Giuseppe!

\$\endgroup\$
1
  • \$\begingroup\$ >6 is a byte shorter. \$\endgroup\$
    – Giuseppe
    Jan 8 '18 at 1:19
3
\$\begingroup\$

jq, 42 characters

(39 characters code + 3 characters command line option)

now|strftime("%w")|strptime("%d")|empty

Just trying a different approach here: parse week day number (0..6) as month day number (1..31).

Sample run:

bash-4.4$ TZ=UTC faketime 2018-01-06 jq -n 'now|strftime("%w")|strptime("%d")|empty'

bash-4.4$ TZ=UTC faketime 2018-01-07 jq -n 'now|strftime("%w")|strptime("%d")|empty'
jq: error (at <unknown>): date "0" does not match format "%d"

Note that jq only handles UTC dates.

Try it online!

\$\endgroup\$
3
\$\begingroup\$

VBA 18 bytes

This relies on the inbuilt function date() returning a day number that remainders 1 if divided by 7, so may be OS and/or CPU specific.

a=1/(date mod 7-1)

It runs in the VBA project Immediate window.

\$\endgroup\$
4
  • 2
    \$\begingroup\$ Welcome to PPCG! In general, an explanation and a link to an online compiler/interpreter is appreciated. \$\endgroup\$ Jan 7 '18 at 21:51
  • \$\begingroup\$ @tfbninja - ok updating \$\endgroup\$
    – JohnRC
    Jan 7 '18 at 21:51
  • \$\begingroup\$ and, if necessary, instructions for how to use, e.g. function call or variable \$\endgroup\$ Jan 7 '18 at 21:53
  • \$\begingroup\$ Nice golfing, was just about to post this myself after seeing the other answer! \$\endgroup\$
    – Greedo
    Jan 8 '18 at 0:12
3
\$\begingroup\$

MATL, 12 bytes

vZ'8XOs309>)

The error produced on Sundays is:

  • Interpreter running on Octave:

    MATL run-time error: The following Octave error refers to statement number 9:  )
    ---
    array(1): out of bound 0
    
  • Interpreter running on Matlab:

    MATL run-time error: The following MATLAB error refers to statement number 9:  )
    ---
    Index exceeds matrix dimensions
    

To invert behaviour (error on any day except on Sundays), add ~ after >.

Try it Online!

Explanation

This exploits the fact that

  • indexing into an empty array with the logical index false is valid (and the result is an empty array, which produces no output); whereas

  • indexing with true causes an error because the array lacks a first entry.

Commented code:

v       % Concatenate stack. Gives empty array
Z'      % Push current date and time as a number
8XO     % Convert to date string with format 8: gives 'Mon', 'Tue' etc
s       % Sum of ASCII codes. Gives 310 for 'Sun', and less for others
309>    % Greater than 309? Gives true for 'Sun', false for others
)       % Index into the empty array
        % Implicit display. Empty arrays are not displayed (not even newline)
\$\endgroup\$
3
\$\begingroup\$

Vyxal O, 25 bytes

kðtD4/‟₀/N‟:400/kτṠ7%2<[←

Try it Online!

Vyxal doesn't have a weekday function, so I made one myself.

\$\endgroup\$
3
\$\begingroup\$

Zsh -G, 14 bytes

>`date`
dd Su*

Attempt This Online!

  • date: get the date in the format Sun 28 Mar 07:55:54 BST 2021
  • >: create a file named according to each of the words in the output
  • dd: "copy and convert" - here it basically does nothing
    • this command expects no arguments
  • Su*: search for a file starting with Su
    • if a file matching that exists (which happens when it is Sunday), then it is passed to dd
      • since dd expects no arguments, this produces an error
    • with the -G option: if no file matches (i.e. it is not Sunday), don't error as usual

This produces a bit of extra junk output to STDERR (not an error though) which may or may not be allowed; the question isn't really clear. Here's an alternative answer which doesn't:

Zsh -G, 18 bytes

>`date`
mv <-> Su*

Attempt This Online!

<-> matches any file whose name is a number, of which there are two: the day of the month, and the year.

When there is no Su* file, this is renames the day of the month to the year (overwriting the year), which works fine.

When Sun exists, it tries to move the day of the month and the year into the directory Sun, which fails because Sun is a file, not a directory.

\$\endgroup\$
2
  • \$\begingroup\$ I'm confused, is this not the opposite of the challenge, since it fails when it is not sunday? \$\endgroup\$ Oct 12 at 4:02
  • 1
    \$\begingroup\$ @thejonymyster fixed \$\endgroup\$
    – pxeger
    Oct 12 at 7:21
2
\$\begingroup\$

APL (Dyalog), 23 bytes

⎕CY'dfns'
o←÷7|⌊days⎕TS

Try it online!

\$\endgroup\$
2
\$\begingroup\$

Ocaml, 46 bytes

open Unix
let()=1/(gmtime(time())).tm_wday;()

and in the ocaml REPL, we can achieve better by removing the let and the final :():

$ open Unix;;1/(gmtime(time())).tm_wday;;<CR>

which is 41 bytes (incuding 1 byte for the carriage return).

\$\endgroup\$
4
  • 1
    \$\begingroup\$ Welcome to PPCG! \$\endgroup\$
    – Laikoni
    Jan 5 '18 at 14:46
  • 1
    \$\begingroup\$ Is it possible to make this code work on Try it online? \$\endgroup\$
    – Laikoni
    Jan 5 '18 at 14:52
  • \$\begingroup\$ Mmh... the compile command is ocamlopt unix.cmxa <file>, I don't know how to give compile option on tio. I'll investigate this evening \$\endgroup\$
    – Bromind
    Jan 5 '18 at 15:04
  • \$\begingroup\$ The tio uses a (simili) REPL interpreter, so you should use the 2nd possibility. However, it doesn't seem to have the Unix library (or don't allow access to it, for any reason) \$\endgroup\$
    – Bromind
    Jan 5 '18 at 15:08
2
\$\begingroup\$

SAS, 36 bytes

%put %eval(1/(1-%index(&sysday,Su)))
\$\endgroup\$
2
\$\begingroup\$

Q, 20 Bytes

if[1=.z.d mod 7;'e]

.z.d returns the current date. mod does the modulo of the current date, which returns an int. If the date is a sunday, .z.d mod 7 returns 1. If 1=1, (on sunday), and error is raised using the ' operator For brevity the error is just the e character.

\$\endgroup\$
1
  • 3
    \$\begingroup\$ Welcome to PPCG! \$\endgroup\$ Jan 5 '18 at 16:29
2
\$\begingroup\$

Julia 0.6, 32 bytes

Thanks to @Dennis for pointing out < saves a byte over !=.

@assert Dates.dayofweek(now())<7

Try it online!

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

C# (.NET Core), 55 54 48 bytes

Try it online!

Saved 1 byte thanks to Shaggy

Saved 5 byte thanks to Emigna

Saved 1 byte thanks to Kevin Cruijssen

_=>{var k=1/(int)System.DateTime.Now.DayOfWeek;}

Lucky that Sunday is indexed 0 in enum or else it would've needed to be (System.DayOfWeek)7

\$\endgroup\$
4
  • 1
    \$\begingroup\$ 54 bytes? \$\endgroup\$
    – Shaggy
    Jan 5 '18 at 12:56
  • 3
    \$\begingroup\$ Do you get using System for free in C#? If so I think you could do ()=>{var k=1/(int)DateTime.Now.DayOfWeek;} for 42. Otherwise 49 with the explicit System. \$\endgroup\$
    – Emigna
    Jan 5 '18 at 13:18
  • 3
    \$\begingroup\$ @Emigna beat me to it; ()=>{var k=1/(int)System.DateTime.Now.DayOfWeek;} is shorter. And you can save one more byte by using an empty unused parameter instead of no parameter (i.e. v->{...} instead of ()->{...}) \$\endgroup\$ Jan 5 '18 at 13:38
  • 1
    \$\begingroup\$ 40 bytes: ()=>1/(int)System.DateTime.Now.DayOfWeek. Try it here. \$\endgroup\$
    – Ayb4btu
    Jan 8 '18 at 23:52
2
\$\begingroup\$

Groovy, 16 bytes

1/new Date().day

Try it online!

\$\endgroup\$
2
  • \$\begingroup\$ Welcome to PPCG! \$\endgroup\$ Jan 6 '18 at 14:59
  • \$\begingroup\$ Thanks! Also, thanks for editing! I was not sure which online playground to choose. Now I know. :) \$\endgroup\$ Jan 6 '18 at 19:23
2
\$\begingroup\$

C,  68  55 bytes

Thanks to @Ken Y-N for saving 13 bytes!

#import<time.h>
f(n){time(&n);n/=gmtime(&n)->tm_wday;;}

Try it online!

\$\endgroup\$
2
  • \$\begingroup\$ Drop the intermediate d to get n/=gmtime(&n)->tm_wday; for 54 bytes (but I'm not sure I like all these compiler warnings...) \$\endgroup\$
    – Ken Y-N
    Jan 8 '18 at 5:59
  • \$\begingroup\$ @KenY-N Thanks! \$\endgroup\$
    – Steadybox
    Jan 8 '18 at 11:05
2
\$\begingroup\$

Zsh, 15 bytes

${(%):-%(w._.)}

Try it online!

Prompt sequences can be really crazy sometimes...

${(%):-%(w._.)}
${(%)         }   # expand as prompt sequence
     :-           # ${var:-fallback}, but without the var
       %( . .)    # Prompt ternary
         w        # If DoW matches given number (implied 0, which is Sunday)
          ._      # Then substitute _
            .     # Else substitute nothing

Since _ is not a command, it fails on Sunday.

\$\endgroup\$
2
\$\begingroup\$

Excel, 24

Closing quote and paren not counted toward final score, as Excel will autocorrect both of those. Tested in Excel 2016.

=IF(MOD(TODAY()-1,7)^0,"")

I think this is different from the other submission enough to warrant another answer.

How it works:

  • I've rolled the error into the conditional check. As it turns out, TODAY() mod 7 also gives the weekday. If it's a Sunday, this means that MOD(TODAY()-1,7) is 0, and 0^0 is an error in Excel.
    • Of course, I could also have divided 1 by the MOD() value or used +6 instead of -1.
  • If it's not Sunday, the MOD(...,7) will be non-zero, which when raised to 0, returns 1, a truthy value. This makes the IF return an empty string (our "nothing").
  • The if statement, therefore, cannot evaluate to FALSE, because it errors or returns nothing.

Alternative

Here's one that works just as well, but uses a Name error instead:

=IF(MOD(TODAY(),7)=1,A,"")
\$\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.