Coding Challenges - Dlang or Generic

matheus matheus at gmail.com
Tue Jan 10 03:18:54 UTC 2023


On Tuesday, 10 January 2023 at 01:22:33 UTC, H. S. Teoh wrote:
> ...
>
> Here's a challenge.  Given an input year, for example, "2023", 
> write a program that outputs (for the corresponding year):
> ...

The layout isn't like yours, I wrote this using a D Online 
compiler and I'm very sleepy right now:

import std.stdio, std.string, std.conv, std.datetime;

bool validDate(int y,int m,int d){
    try{
        Date(y,m,d);
        return true;
    }catch(Exception e){}
    return false;
}

void main(){
     int[string] dow;
     int i, y = 2023, m = 1, d = 1;

     for(i=1;i<8;++i){
         dow[to!string(Date(1899,1,i).dayOfWeek)]=i;
     }

     for(m=1;m<13;++m){
         
write("\n\n\t\t",capitalize(to!string(Date(y,m,1).month)),"\n");

         for(i=1;i<8;++i){
         	write(to!string(Date(1899,1,i).dayOfWeek)[0..2], " ");
         }

         writeln();
         int c = dow[to!string(Date(y,m,1).dayOfWeek)];

         for(i=1;i<c;++i){ write("   "); }

     	for(d=0;d<32;++d){
     	    if(c%8==0){c=1;writeln();}
     	    ++c;
             if(validDate(y,m,d+1)){
                 writef("%2s ",d+1);
             }
         }
     }
}

Prints:


	Jan
su mo tu we th fr sa
  1  2  3  4  5  6  7
  8  9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31

	Feb
su mo tu we th fr sa
           1  2  3  4
  5  6  7  8  9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28

	Mar
su mo tu we th fr sa
           1  2  3  4
  5  6  7  8  9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31

	Apr
su mo tu we th fr sa
                    1
  2  3  4  5  6  7  8
  9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30

	May
su mo tu we th fr sa
     1  2  3  4  5  6
  7  8  9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31

	Jun
su mo tu we th fr sa
              1  2  3
  4  5  6  7  8  9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30


	Jul
su mo tu we th fr sa
                    1
  2  3  4  5  6  7  8
  9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31

	Aug
su mo tu we th fr sa
        1  2  3  4  5
  6  7  8  9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31

	Sep
su mo tu we th fr sa
                 1  2
  3  4  5  6  7  8  9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30


	Oct
su mo tu we th fr sa
  1  2  3  4  5  6  7
  8  9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31

	Nov
su mo tu we th fr sa
           1  2  3  4
  5  6  7  8  9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30

	Dec
su mo tu we th fr sa
                 1  2
  3  4  5  6  7  8  9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31

Matheus.


More information about the Digitalmars-d-learn mailing list