www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Coding Challenges - Dlang or Generic

reply Paul <phshaffer gmail.com> writes:
Greetings Dlang-ers
I was wondering if anyone knew of any coding challenges available 
where the input and output are specified and its left to the 
programmer to find a solution?  Free would be nice but even paid 
services would be worth considering.  I'm taking a D class right 
now and it has little challenges in the lessons where much of the 
work is done for you, but I'm thinking of a site/service that is 
dedicated to these types of challenges (without doing any work 
for you).

I wonder if a site dedicated to Dlang challenges would 'fly'?  It 
would be quite interesting and instructive to see how different 
coders solved a particular coding problem.  Statistics could be 
generated for each solution (e.g. compile time, execution time, 
source code size, executable size,  safe,  nogc, readability of 
source code, helpfulness of comments, etc.).  With D's 
multi-paradigm power it could be fascinating.  I know.  Someone's 
going to say why don't YOU do it:)

regards,
Jan 09 2023
next sibling parent reply "H. S. Teoh" <hsteoh qfbox.info> writes:
On Tue, Jan 10, 2023 at 12:17:18AM +0000, Paul via Digitalmars-d-learn wrote:
 Greetings Dlang-ers
 I was wondering if anyone knew of any coding challenges available
 where the input and output are specified and its left to the
 programmer to find a solution?
Here's a challenge. Given an input year, for example, "2023", write a program that outputs (for the corresponding year): --------------------------------snip--------------------------------- 2023 January February March Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa 1 2 3 4 5 6 7 1 2 3 4 1 2 3 4 8 9 10 11 12 13 14 5 6 7 8 9 10 11 5 6 7 8 9 10 11 15 16 17 18 19 20 21 12 13 14 15 16 17 18 12 13 14 15 16 17 18 22 23 24 25 26 27 28 19 20 21 22 23 24 25 19 20 21 22 23 24 25 29 30 31 26 27 28 26 27 28 29 30 31 April May June Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa 1 1 2 3 4 5 6 1 2 3 2 3 4 5 6 7 8 7 8 9 10 11 12 13 4 5 6 7 8 9 10 9 10 11 12 13 14 15 14 15 16 17 18 19 20 11 12 13 14 15 16 17 16 17 18 19 20 21 22 21 22 23 24 25 26 27 18 19 20 21 22 23 24 23 24 25 26 27 28 29 28 29 30 31 25 26 27 28 29 30 30 July August September Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa 1 1 2 3 4 5 1 2 2 3 4 5 6 7 8 6 7 8 9 10 11 12 3 4 5 6 7 8 9 9 10 11 12 13 14 15 13 14 15 16 17 18 19 10 11 12 13 14 15 16 16 17 18 19 20 21 22 20 21 22 23 24 25 26 17 18 19 20 21 22 23 23 24 25 26 27 28 29 27 28 29 30 31 24 25 26 27 28 29 30 30 31 October November December Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa 1 2 3 4 5 6 7 1 2 3 4 1 2 8 9 10 11 12 13 14 5 6 7 8 9 10 11 3 4 5 6 7 8 9 15 16 17 18 19 20 21 12 13 14 15 16 17 18 10 11 12 13 14 15 16 22 23 24 25 26 27 28 19 20 21 22 23 24 25 17 18 19 20 21 22 23 29 30 31 26 27 28 29 30 24 25 26 27 28 29 30 31 --------------------------------snip--------------------------------- Code will be graded on readability, unittest coverage, and reusability (how many functions have wide applicability outside of this challenge). ;-) T -- A mathematician learns more and more about less and less, until he knows everything about nothing; whereas a philospher learns less and less about more and more, until he knows nothing about everything.
Jan 09 2023
next sibling parent reply matheus <matheus gmail.com> writes:
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.
Jan 09 2023
next sibling parent reply "H. S. Teoh" <hsteoh qfbox.info> writes:
On Tue, Jan 10, 2023 at 03:18:54AM +0000, matheus via Digitalmars-d-learn wrote:
 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:
[...]
 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
[...] Printing it in this format is trivial, and not very interesting. The interest in the challenge is to lay it out like I posted, side-by-side, and to do so in a way that the code is clean, maintainable, and consists of reusable components. That's where the challenge lies. T -- If you think you are too small to make a difference, try sleeping in a closed room with a mosquito. -- Jan van Steenbergen
Jan 09 2023
parent reply matheus <matheus gmail.com> writes:
On Tuesday, 10 January 2023 at 05:21:15 UTC, H. S. Teoh wrote:
 Printing it in this format is trivial, and not very 
 interesting.  The interest in the challenge is to lay it out 
 like I posted, side-by-side,...
Like I said I did it over D online compiler which unfortunately I couldn't validate the output because it "wraps" the text, and the font wasn't monospace. But It just a case of changing the loop in a way to print 3 groups of months.
 ... and to do so in a way that the code is clean, maintainable, 
 and consists of reusable components. ...
Talking about modularity and reusable components, I really think it depends, because if that's was really the case, then I would think of using the OS functions to move the cursor around (If console/terminal) to print a given month in some location or side by side until reach some horizontal limit (Terminal), then it could be 1 column only (Like I did), 2, 3 and maybe 4 columns if the terminal/resolution permit, and for that I would use ARSD. :] But I think this would be too much for this kind of thing writing on online compiler.
 ... That's where the challenge lies.
To be honest when I saw your proposal, I really thought that the real challenge would be to write my own algo to handle the date, and I was pretty sure after posting above, you would say that, but not about the layout or printing in groups. =] Matheus.
Jan 10 2023
parent reply drug007 <drug2004 bk.ru> writes:
10.01.2023 13:57, matheus пишет:
 On Tuesday, 10 January 2023 at 05:21:15 UTC, H. S. Teoh wrote:
 Printing it in this format is trivial, and not very interesting.  The 
 interest in the challenge is to lay it out like I posted, 
 side-by-side,...
Like I said I did it over D online compiler which unfortunately I couldn't validate the output because it "wraps" the text, and the font wasn't monospace. But It just a case of changing the loop in a way to print 3 groups of months.
 ... and to do so in a way that the code is clean, maintainable, and 
 consists of reusable components. ...
Talking about modularity and reusable components, I really think it depends, because if that's was really the case, then I would think of using the OS functions to move the cursor around (If console/terminal) to print a given month in some location or side by side until reach some horizontal limit (Terminal), then it could be 1 column only (Like I did), 2, 3 and maybe 4 columns if the terminal/resolution permit, and for that I would use ARSD. :] But I think this would be too much for this kind of thing writing on online compiler.
 ... That's where the challenge lies.
To be honest when I saw your proposal, I really thought that the real challenge would be to write my own algo to handle the date, and I was pretty sure after posting above, you would say that, but not about the layout or printing in groups. =] Matheus.
[To clarify the situation](https://wiki.dlang.org/Component_programming_with_ranges) (H S Teoh is the author of this article)
Jan 10 2023
next sibling parent drug007 <drug2004 bk.ru> writes:
10.01.2023 14:23, drug007 пишет:
 10.01.2023 13:57, matheus пишет:
 On Tuesday, 10 January 2023 at 05:21:15 UTC, H. S. Teoh wrote:
 Printing it in this format is trivial, and not very interesting.  The 
 interest in the challenge is to lay it out like I posted, 
 side-by-side,...
Like I said I did it over D online compiler which unfortunately I couldn't validate the output because it "wraps" the text, and the font wasn't monospace. But It just a case of changing the loop in a way to print 3 groups of months.
 ... and to do so in a way that the code is clean, maintainable, and 
 consists of reusable components. ...
Talking about modularity and reusable components, I really think it depends, because if that's was really the case, then I would think of using the OS functions to move the cursor around (If console/terminal) to print a given month in some location or side by side until reach some horizontal limit (Terminal), then it could be 1 column only (Like I did), 2, 3 and maybe 4 columns if the terminal/resolution permit, and for that I would use ARSD. :] But I think this would be too much for this kind of thing writing on online compiler.
 ... That's where the challenge lies.
To be honest when I saw your proposal, I really thought that the real challenge would be to write my own algo to handle the date, and I was pretty sure after posting above, you would say that, but not about the layout or printing in groups. =] Matheus.
[To clarify the situation](https://wiki.dlang.org/Component_programming_with_ranges) (H S Teoh is the author of this article)
Also I'd like to add that this article inspired Eric Niebler to write ranges for C++. Now C++ has already 3rd version of ranges and they are still not used in general.
Jan 10 2023
prev sibling parent matheus <matheus gmail.com> writes:
On Tuesday, 10 January 2023 at 11:23:15 UTC, drug007 wrote:
 10.01.2023 13:57, matheus пишет:
 ...
 [To clarify the 
 situation](https://wiki.dlang.org/Component_programming_with_ranges)
 (H S Teoh is the author of this article)
Hmm very interesting (I'm at work and I just gave it a glimpse). But since I'm a C programmer who just play with D as hobby I would need more time to digest it. I'll try to read it later. Matheus.
Jan 10 2023
prev sibling parent reply Salih Dincer <salihdb hotmail.com> writes:
On Tuesday, 10 January 2023 at 03:18:54 UTC, matheus wrote:
 The layout isn't like yours, I wrote this using a D Online 
 compiler and I'm very sleepy right now:
 ```d
 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);
             }
         }
     }
 }
 ```
You don't need validDate. Because there is daysInMonth: ```d writeln(); const date = Date(y, m, 1); int c = dow[to!string(date.dayOfWeek)]; for(i = 1; i < c; ++i) write(" "); for(d = 0; d < 32; ++d) { if(++c % 9 == 0) { writeln(); c = 2; } if(d < date.daysInMonth) writef("%2s ", d + 1); } ``` SDB 79
Jan 09 2023
parent reply matheus <matheus gmail.com> writes:
On Tuesday, 10 January 2023 at 07:38:31 UTC, Salih Dincer wrote:
 On Tuesday, 10 January 2023 at 03:18:54 UTC, matheus wrote:
 ...`
 You don't need validDate.  Because there is daysInMonth:
 ...
That's really better. thanks for the info. Matheus.
Jan 10 2023
parent Salih Dincer <salihdb hotmail.com> writes:
On Tuesday, 10 January 2023 at 10:58:37 UTC, matheus wrote:
 On Tuesday, 10 January 2023 at 07:38:31 UTC, Salih Dincer wrote:
 You don't need validDate.  Because there is daysInMonth:
That's really better. thanks for the info.
Actually, I should thank you. When I first wrote these codes, I couldn't shorten them that much. Because I've done fun things like this. I think you will like EnumPrint(): ```d auto enumPrint(T)(size_t len = 0) { int[string] result; int i = 1; for(T x = T.min; x <= T.max; x++) { result[len ? x.to!string[0..len] : x.to!string] = i++; } return result; } enumPrint!DayOfWeek(3).writeln; } ``` Thank you... SDB 79
Jan 10 2023
prev sibling next sibling parent bachmeier <no spam.net> writes:
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):

 --------------------------------snip---------------------------------
                               2023
        January              February                March
  Su Mo Tu We Th Fr Sa  Su Mo Tu We Th Fr Sa  Su Mo Tu We Th Fr 
 Sa
   1  2  3  4  5  6  7            1  2  3  4            1  2  3  
 4
   8  9 10 11 12 13 14   5  6  7  8  9 10 11   5  6  7  8  9 10 
 11
  15 16 17 18 19 20 21  12 13 14 15 16 17 18  12 13 14 15 16 17 
 18
  22 23 24 25 26 27 28  19 20 21 22 23 24 25  19 20 21 22 23 24 
 25
  29 30 31              26 27 28              26 27 28 29 30 31

         April                  May                  June
  Su Mo Tu We Th Fr Sa  Su Mo Tu We Th Fr Sa  Su Mo Tu We Th Fr 
 Sa
                     1      1  2  3  4  5  6               1  2  
 3
   2  3  4  5  6  7  8   7  8  9 10 11 12 13   4  5  6  7  8  9 
 10
   9 10 11 12 13 14 15  14 15 16 17 18 19 20  11 12 13 14 15 16 
 17
  16 17 18 19 20 21 22  21 22 23 24 25 26 27  18 19 20 21 22 23 
 24
  23 24 25 26 27 28 29  28 29 30 31           25 26 27 28 29 30
  30

         July                 August               September
  Su Mo Tu We Th Fr Sa  Su Mo Tu We Th Fr Sa  Su Mo Tu We Th Fr 
 Sa
                     1         1  2  3  4  5                  1  
 2
   2  3  4  5  6  7  8   6  7  8  9 10 11 12   3  4  5  6  7  8  
 9
   9 10 11 12 13 14 15  13 14 15 16 17 18 19  10 11 12 13 14 15 
 16
  16 17 18 19 20 21 22  20 21 22 23 24 25 26  17 18 19 20 21 22 
 23
  23 24 25 26 27 28 29  27 28 29 30 31        24 25 26 27 28 29 
 30
  30 31

        October              November              December
  Su Mo Tu We Th Fr Sa  Su Mo Tu We Th Fr Sa  Su Mo Tu We Th Fr 
 Sa
   1  2  3  4  5  6  7            1  2  3  4                  1  
 2
   8  9 10 11 12 13 14   5  6  7  8  9 10 11   3  4  5  6  7  8  
 9
  15 16 17 18 19 20 21  12 13 14 15 16 17 18  10 11 12 13 14 15 
 16
  22 23 24 25 26 27 28  19 20 21 22 23 24 25  17 18 19 20 21 22 
 23
  29 30 31              26 27 28 29 30        24 25 26 27 28 29 
 30
                                              31
 --------------------------------snip---------------------------------

 Code will be graded on readability, unittest coverage, and 
 reusability (how many functions have wide applicability outside 
 of this challenge).

 ;-)


 T
``` import std.process, std.stdio; void main() { writeln(executeShell("cal -y 2023").output); } ```
Jan 10 2023
prev sibling next sibling parent reply Paul <phshaffer gmail.com> writes:
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):
...
 Code will be graded on readability, unittest coverage, and 
 reusability (how many functions have wide applicability outside 
 of this challenge).

 ;-)


 T
I think you must have done a blog post or tutorial or something, Teoh, because I've seen this before. Don't let this go to your head :), but I was blown away by the presentation and solution! BTW where is it posted?
Jan 10 2023
parent matheus <matheus gmail.com> writes:
On Tuesday, 10 January 2023 at 22:10:57 UTC, Paul wrote:
 ...
 I think you must have done a blog post or tutorial or 
 something, Teoh, because I've seen this before.  Don't let this 
 go to your head :), but I was blown away by the presentation 
 and solution!  BTW where is it posted?
ITT: https://forum.dlang.org/post/tpjhr3$2ilc$1 digitalmars.com drug007 wrote:
[To clarify the 
situation](https://wiki.dlang.org/Component_programming_with_ranges)
(H S Teoh is the author of this article)
Matheus.
Jan 10 2023
prev sibling parent reply Salih Dincer <salihdb hotmail.com> writes:
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):
Now, I wrote a nested class using range and copying from Matheus' code. Of course not as comprehensive as [your dcal](https://github.com/quickfur/dcal/blob/master/dcal.d). I like this one and even thought of a new challenge! ```d void main() { import std.stdio, std.string : cp = capitalize; import std.range, std.conv : to; with(new MyCalendar(2023, 12)) { const title = dowToStr!2; foreach(month; range.take(6)) { const mName = date.month.to!string; year.write(" "); // current year mName.cp.writefln!"%s:"; // name of the month title.writefln!"%-(%s %)"; // days of week month.writeln; // formatted days } } class MyCalendar { import std.array, std.datetime, std.conv : to; import std.string : capitalize; Range range; Date date; int year, month; this(int year, int month) { this.month = month; this.year = year; this.date = Date(year, month, 1); this.range = new Range(); } class Range { enum empty = false; string front() { import std.format : fw = formattedWrite; auto res = appender!string; int day, dow = date.dayOfWeek; res.fw("%s", replicated(dow * 3)); // skip days for(day = 1; day <= date.daysInMonth; ++day) { if(++dow % 7) res.fw("%2s ", day); else res.fw("%2s\n", day); } if(dow % 7) res.put("\n"); return res.data; } void popFront() { month++; if(month > 12) { ++year; month = 1; } date = Date(year, month, 1); } } auto dowToStr(size_t len = 0)() { alias E = DayOfWeek; string[] result; for(E x = E.min; x <= E.max; x++) { result ~= len ? x.to!string[0..len] : x.to!string; } return result; } } ``` SDB 79
Jan 12 2023
parent reply matheus <matheus gmail.com> writes:
On Thursday, 12 January 2023 at 19:06:49 UTC, Salih Dincer wrote:
 ...

 Now, I wrote a nested class using range and copying from 
 Matheus' code. Of course not as comprehensive as [your 
 dcal](https://github.com/quickfur/dcal/blob/master/dcal.d). I 
 like this one and even thought of a new challenge!

 ...
Hi Salih, Unfortunately it's not working for me:
 .d(79): Error: found `End of File` when expecting `}` following 
 compound statement
So I think it's missing a "}" in main, and after I added that, it gives:
 .d(46): Error: undefined identifier `replicated`, did you mean 
 template `replicate(S)(S s, size_t n) if (isDynamicArray!S)`?
Matheus.
Jan 13 2023
parent reply Salih Dincer <salihdb hotmail.com> writes:
On Friday, 13 January 2023 at 18:59:01 UTC, matheus wrote:
 Unfortunately it's not working for me
Yeah, it was an old development version. I also implemented another version the same day: * [Nested Class](https://forum.dlang.org/thread/vkjhkftvyprsivozyuyf forum.dlang.org) * [Only One Struct](https://forum.dlang.org/post/thxvuddjimgswalzojgm forum.dlang.org) SDB 79 Sory...
Jan 13 2023
parent reply matheus <matheus gmail.com> writes:
On Friday, 13 January 2023 at 21:12:17 UTC, Salih Dincer wrote:
 On Friday, 13 January 2023 at 18:59:01 UTC, matheus wrote:
 Unfortunately it's not working for me
Yeah, it was an old development version. I also implemented another version the same day: * [Nested Class](https://forum.dlang.org/thread/vkjhkftvyprsivozyuyf forum.dlang.org) * [Only One Struct](https://forum.dlang.org/post/thxvuddjimgswalzojgm forum.dlang.org) SDB 79 Sory...
No problem. =] Question: Have you compared the timings between this way (With ranges) and a normal way (Without ranges)? I'm using D Online Compiler from different platforms but unfortunately I can't loop too much because they don't accept, but anyway a normal version runs almost twice faster than this version (With ranges). If you like I could send or post here a version without ranges to try out. Matheus.
Jan 17 2023
next sibling parent reply Siarhei Siamashka <siarhei.siamashka gmail.com> writes:
On Tuesday, 17 January 2023 at 21:50:06 UTC, matheus wrote:
 Question: Have you compared the timings between this way (With 
 ranges) and a normal way (Without ranges)?
If you are intensively using ranges, UFCS or the other convenient high level language features, then the compiler choice does matter a lot. And only LDC compiler is able to produce fast binaries from such source code. GDC compiler has severe performance problems with inlining, unless LTO is enabled. And it also allocates closures on stack. This may or may not be fixed in the future, but today I can't recommend GDC if you really care about performance. DMD compiler uses an outdated code generation backend from Digital Mars C++ and will never be able to produce fast binaries. It prioritizes fast compilation speed over everything else.
 I'm using D Online Compiler from different platforms but 
 unfortunately I can't loop too much because they don't accept, 
 but anyway a normal version runs almost twice faster than this 
 version (With ranges).
Can you try to run the following diagnostics program on this D Online Compiler platform? https://gist.github.com/ssvb/5c926ed9bc755900fdaac3b71a0f7cfd
 If you like I could send or post here a version without ranges 
 to try out.
Please post it. This is interesting.
Jan 17 2023
next sibling parent "H. S. Teoh" <hsteoh qfbox.info> writes:
On Tue, Jan 17, 2023 at 11:08:19PM +0000, Siarhei Siamashka via
Digitalmars-d-learn wrote:
 On Tuesday, 17 January 2023 at 21:50:06 UTC, matheus wrote:
 Question: Have you compared the timings between this way (With
 ranges) and a normal way (Without ranges)?
If you are intensively using ranges, UFCS or the other convenient high level language features, then the compiler choice does matter a lot. And only LDC compiler is able to produce fast binaries from such source code. GDC compiler has severe performance problems with inlining, unless LTO is enabled. And it also allocates closures on stack. This may or may not be fixed in the future, but today I can't recommend GDC if you really care about performance.
Interesting, I didn't know GDC has issues with inlining. I thought it was more-or-less on par with LDC in terms of the quality of code generation. Do you have a concrete example of this problem?
 DMD compiler uses an outdated code generation backend from Digital
 Mars C++ and will never be able to produce fast binaries. It
 prioritizes fast compilation speed over everything else.
[...] For anything performance related, I wouldn't even consider DMD. For all the 10+ years I've been using D, it has consistently produced executables that run about 20-30% slower than those produced by LDC or GDC, sometimes even up to 40%. For script-like programs or interactive apps that don't care about performance, DMD is fine for convenience and fast compile turnaround times. But as soon as performance matters, DMD is not even on my radar. T -- Heuristics are bug-ridden by definition. If they didn't have bugs, they'd be algorithms.
Jan 17 2023
prev sibling parent reply matheus <matheus gmail.com> writes:
On Tuesday, 17 January 2023 at 23:08:19 UTC, Siarhei Siamashka 
wrote:
 On Tuesday, 17 January 2023 at 21:50:06 UTC, matheus wrote:
 Question: Have you compared the timings between this way (With 
 ranges) and a normal way (Without ranges)?
If you are intensively using ranges, UFCS or the other convenient high level language features, then the compiler choice does matter a lot. And only LDC compiler is able to produce fast binaries from such source code. ...
I ran in two sites: https://onecompiler.com/d and then https://godbolt.org/, with the latter I set LDC with -O2. My version (Source in the end) ran about 2x faster than the version with ranges.
...

 I'm using D Online Compiler from different platforms but 
 unfortunately I can't loop too much because they don't accept, 
 but anyway a normal version runs almost twice faster than this 
 version (With ranges).
Can you try to run the following diagnostics program on this D Online Compiler platform? https://gist.github.com/ssvb/5c926ed9bc755900fdaac3b71a0f7cfd
https://onecompiler.com/d/3yv7t9ap9 Gives me: HelloWorld.d(43): Error: undefined identifier `volatileStore` HelloWorld.d(44): Error: undefined identifier `volatileLoad`
 If you like I could send or post here a version without ranges 
 to try out.
Please post it. This is interesting.
Like I said above I ran my version against the one with ranges (From: https://github.com/quickfur/dcal/blob/master/dcal.d), and I modified to just print the calendar for some year. With godbolt.org LDC -O2 my version ran 2x faster, and here is the source: import std.stdio, std.string, std.conv, std.range, std.datetime; import std.datetime.stopwatch : benchmark, StopWatch; void draw_months(int year ,int month, int columns){ int i; if(month>12){return;} auto columspace = " ".replicate(columns); write("\n"); for(i=0;i<columns;++i){ if(month+i>12){break;} write(" " ~ " ".replicate(11*(i>0)) ~ columspace,capitalize(to!string(Date(year,month+i,1).month))); } write("\n"); auto m = new string[][](columns); for(int j=0;j<columns;++j){ if(month+j>12){return;} for(i=1;i<8;++i){ write(to!string(Date(1899,1,i).dayOfWeek)[0..2], " "); } int c = DW[to!string(Date(year,month+j,1).dayOfWeek)]; auto dm = Date(2023,month+j,1).daysInMonth; for(i=0;i<c;++i){ m[j] ~= " "; } for(i=1;i<dm+1;++i){ m[j] ~= i.to!string; } for(;i<36;++i){ m[j] ~= " "; } write(columspace); } //write("\n\n",m[0].length,"\n"); writeln(); for(int k=0;k<5;++k){ for(int j=0;j<columns;++j){ for(i=k*7;i<(k+1)*7;++i){ writef("%2s ",m[j][i]); } write(columspace); } writeln(); } } void draw_year(int year, int columns){ int steps = 12/columns; for(int i=0;i<steps;++i){ draw_months(2023,(i*columns)+1,columns); } } int[string] DW; void main(){ int[string] dow; int i, y = 2023, m = 1, d = 1; auto sw = StopWatch(); sw.start(); for(i=1;i<8;++i){ DW[to!string(Date(1899,1,i).dayOfWeek)]=i-1; } for(i=0;i<10;++i){ draw_year(2023,3); } sw.stop(); writeln(sw.peek.total!"usecs"); } Matheus.
Jan 17 2023
parent reply Siarhei Siamashka <siarhei.siamashka gmail.com> writes:
On Tuesday, 17 January 2023 at 23:27:03 UTC, matheus wrote:
 I ran in two sites: https://onecompiler.com/d and then 
 https://godbolt.org/, with the latter I set LDC with -O2.

 My version (Source in the end) ran about 2x faster than the 
 version with ranges.
Well, the use of ranges is not the only difference.
 Can you try to run the following diagnostics program on this D 
 Online Compiler platform? 
 https://gist.github.com/ssvb/5c926ed9bc755900fdaac3b71a0f7cfd
https://onecompiler.com/d/3yv7t9ap9 Gives me: HelloWorld.d(43): Error: undefined identifier `volatileStore` HelloWorld.d(44): Error: undefined identifier `volatileLoad`
The volatileStore/volatileLoad functions used to be in `core.bitop` in the old versions of D compilers, but then moved to `core.volatile`. This kind of reshuffling is annoying, because supporting multiple versions of D compilers becomes unnecessarily difficult. After importing `core.volatile` in https://onecompiler.com/d/3yv7u9v7c now it prints: Detected compiler: DMD Performance warning: '-O' option was not used! Performance warning: '-release' option was not used! Performance warning: DMD generates much slower code than GDC or LDC! And this is a systematic problem with various online D compilers. Some of them don't bother to enable optimizations. Which isn't bad by itself, but makes it unsuitable for running benchmarks.
Jan 17 2023
parent reply matheus <matheus gmail.com> writes:
On Wednesday, 18 January 2023 at 01:05:58 UTC, Siarhei Siamashka 
wrote:
 On Tuesday, 17 January 2023 at 23:27:03 UTC, matheus wrote:
 I ran in two sites: https://onecompiler.com/d and then 
 https://godbolt.org/, with the latter I set LDC with -O2.

 My version (Source in the end) ran about 2x faster than the 
 version with ranges.
Well, the use of ranges is not the only difference. ...
What are the other differences? Just remember what I said above:
 I ran my version against the one with ranges (From: 
 https://github.com/quickfur/dcal/blob/master/dcal.d), and I 
 modified to just print the calendar for some year.
Again I ran both versions with/without ranges only generating and printing the months. Matheus.
Jan 18 2023
parent Siarhei Siamashka <siarhei.siamashka gmail.com> writes:
On Wednesday, 18 January 2023 at 11:10:01 UTC, matheus wrote:
 On Wednesday, 18 January 2023 at 01:05:58 UTC, Siarhei 
 Siamashka wrote:
 On Tuesday, 17 January 2023 at 23:27:03 UTC, matheus wrote:
 I ran in two sites: https://onecompiler.com/d and then 
 https://godbolt.org/, with the latter I set LDC with -O2.

 My version (Source in the end) ran about 2x faster than the 
 version with ranges.
Well, the use of ranges is not the only difference. ...
What are the other differences?
Can't you easily see them yourself? For example, the https://github.com/quickfur/dcal/blob/master/dcal.d implementation uses https://dlang.org/library/std/format/format.html but I don't see it in your code. And this is not the only difference. Your code even doesn't produce the same output.
 Just remember what I said above:

 I ran my version against the one with ranges (From: 
 https://github.com/quickfur/dcal/blob/master/dcal.d), and I 
 modified to just print the calendar for some year.
Again I ran both versions with/without ranges only generating and printing the months.
You are comparing two completely different implementations. If you wanted to specifically measure the overhead of using ranges, then you could just remove them without changing how the std.datetime api calls are done and how strings are formatted.
Jan 18 2023
prev sibling parent reply Salih Dincer <salihdb hotmail.com> writes:
On Tuesday, 17 January 2023 at 21:50:06 UTC, matheus wrote:
 Have you compared the timings between this way (With ranges) 
 and a normal way (Without ranges)?
Of course it is possible to speed it up. However, even as it is, it is enough to see the power of intervals. I would argue that you'll get fast results even with DMD Compiler! ```d import std.stdio; import std.datetime.date; void main() { import std.datetime.stopwatch : benchmark, StopWatch; auto sw = StopWatch(); #line 1 sw.start(); enum xTest = 1200; auto title = enumToStr!DayOfWeek(2); auto date = Date(2023, 12, 1); auto range = MyCalendar(date); for(size_t test; test < xTest; range.popFront, ++test) { range.writeln; // month and year title.writefln!"%-(%s %)"; // days of week range.front.writeln; // formatted days } sw.stop(); writeln(sw.peek.total!"usecs"); } struct MyCalendar { import std.array : appender, replicate; import std.format : format, formattedWrite; import std.string : capitalize; Date date; enum empty = false; string front() { auto res = appender!string; int day, dow = date.dayOfWeek; // skip days: res.formattedWrite("%s", " ".replicate(dow * 3)); for(day = 1; day <= date.daysInMonth; ++day) { res.formattedWrite("%2s ", day); if(++dow % 7 == 0) res.put("\n"); } if(dow % 7) res.put("\n"); return res.data; } void popFront() { date.roll!"months"(1); } string toString() const { const currentYear = date.year; // Current Year const currentMonth = date.month; // Name of the month const monthAndYear = format("%s/%s:", currentMonth, currentYear); return capitalize(monthAndYear); } } auto enumToStr(E)(size_t len = 0) { import std.conv : to; string[] result; for(E x = E.min; x <= E.max; x++) { result ~= len ? x.to!string[0..len] : x.to!string; } return result; } ``` SDB 79
Jan 17 2023
parent reply matheus <matheus gmail.com> writes:
On Wednesday, 18 January 2023 at 04:51:11 UTC, Salih Dincer wrote:
 On Tuesday, 17 January 2023 at 21:50:06 UTC, matheus wrote:
 Have you compared the timings between this way (With ranges) 
 and a normal way (Without ranges)?
Of course it is possible to speed it up. However, even as it is, it is enough to see the power of intervals. I would argue that you'll get fast results even with DMD Compiler! ```d ...
Well I'll need to check this, because your version is slightly different than mine and this one (https://github.com/quickfur/dcal/blob/master/dcal.d), It prints only one column. I'll change mine to do this and compare, I can't do this right now because I'm at work. But unfortunately I can't use xTest = 1200 on these online compilers, only 10 or they will stop. Could you please run your version vs mine (Changing to print one column, line 70: draw_year(2023,1); ) and tell us the result? Matheus.
Jan 18 2023
parent Salih Dincer <salihdb hotmail.com> writes:
On Wednesday, 18 January 2023 at 11:20:11 UTC, matheus wrote:
 Could you please run your version vs mine (Changing to print 
 one column, line 70: draw_year(2023,1); ) and tell us the 
 result?
When implementing MyCalendar(), it was not designed to output multiple columns. Therefore, we cannot make a direct comparison. But the code you designed is also very nice. Can you let me do a little magic touch using contract programming? ```d void draw_year(int columns)(int year) in(imported!"std.numeric".lcm(12, columns) == 12, "Permited columns < 4, 6 and 12") { foreach(i; 0..12 / columns) year.draw_months(i * columns + 1, columns); } unittest { //foreach(_; 0..10) 2023.draw_year!12; // 12 x 1 2023.draw_year!6; // 6 x 2 2023.draw_year!4; // 4 x 3 2023.draw_year!3; // 3 x 4 2023.draw_year!2; // 2 x 6 2023.draw_year!1; // 1 x 12 } ``` SDB 79
Jan 18 2023
prev sibling next sibling parent reply =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
On 1/9/23 16:17, Paul wrote:

 coding challenges
Perhaps the following two? https://rosettacode.org/ https://adventofcode.com/ Ali
Jan 09 2023
parent reply Paul <phshaffer gmail.com> writes:
On Tuesday, 10 January 2023 at 01:31:28 UTC, Ali Çehreli wrote:
 On 1/9/23 16:17, Paul wrote:

 coding challenges
Perhaps the following two? https://rosettacode.org/ https://adventofcode.com/ Ali
Excellent. Thanks.
Jan 10 2023
parent reply =?UTF-8?Q?Christian_K=c3=b6stlin?= <christian.koestlin gmail.com> writes:
On 10.01.23 23:30, Paul wrote:
 On Tuesday, 10 January 2023 at 01:31:28 UTC, Ali Çehreli wrote:
 On 1/9/23 16:17, Paul wrote:

 coding challenges
Perhaps the following two?   https://rosettacode.org/   https://adventofcode.com/ Ali
Excellent.  Thanks.
For this years advent-of-code Steven Schveighoffer (https://github.com /schveiguy/adventofcode/tree/master/2022) has a complete set of dlang solutions. Kind regards, Christian
Jan 12 2023
parent Paul <phshaffer gmail.com> writes:
On Thursday, 12 January 2023 at 20:28:26 UTC, Christian Köstlin 
wrote:

 For this years advent-of-code Steven Schveighoffer 
 (https://github.com
 /schveiguy/adventofcode/tree/master/2022) has a complete set of 
 dlang
 solutions.

 Kind regards,
 Christian
Very helpful. Thanks Christian.
Jan 12 2023
prev sibling next sibling parent reply Siarhei Siamashka <siarhei.siamashka gmail.com> writes:
On Tuesday, 10 January 2023 at 00:17:18 UTC, Paul wrote:
 I was wondering if anyone knew of any coding challenges 
 available where the input and output are specified and its left 
 to the programmer to find a solution?  Free would be nice but 
 even paid services would be worth considering.  I'm taking a D 
 class right now and it has little challenges in the lessons 
 where much of the work is done for you, but I'm thinking of a 
 site/service that is dedicated to these types of challenges 
 (without doing any work for you).
What kind of D class is that? Are you learning D language in a school or university? Or is it some kind of online language course? I can recommend the https://atcoder.jp site and their beginner contests. At least the first two tasks of each beginner contest are relatively easily solvable by anyone without any kind of special knowledge or training. The next beginner contest is [scheduled in 5 days](https://atcoder.jp/contests/abc285). The previous beginner contest was [here](https://atcoder.jp/contests/abc284) and the accepted solutions in D language can be found [here](https://atcoder.jp/contests/abc284/submissions?f.LanguageName=D&f.Status=AC&f.Ta k=&f.User=&page=1). You can try to solve any tasks from the previous contests yourself as a practice without any time limit and without any stress of being ranked. Additionally, there are "heuristic" contests and they are very fun in their own way. You can try https://atcoder.jp/contests/ahc015/tasks/ahc015_a as an example, which essentially boils down to implementing an AI for a simple game. Implementing some sort of a trivial solution is easy. For example, always tilting the box in the same direction is a valid solution. But it won't score a lot of points. Doing something smarter will result in a better score. And again, the already existing solutions in D language are also available [here](https://atcoder.jp/contests/ahc015/submissions?f.Task=&f.LanguageName=D&f.Status=AC&f.User=).
Jan 09 2023
parent Paul <phshaffer gmail.com> writes:
On Tuesday, 10 January 2023 at 06:45:40 UTC, Siarhei Siamashka 
wrote:
...
 What kind of D class is that? Are you learning D language in a 
 school or university? Or is it some kind of online language 
 course?
... I don't know if there are rules about sharing links and such but its a site called https://www.educative.io/learn. There are two D courses. I just finished the second class. The business model on 'Educative' is by subscription with maybe one week or month free trial. So you can get as many certificates as you can complete during your subscription. So I don't know how much weight the certificates carry but I have two D course certificates.
Jan 10 2023
prev sibling next sibling parent thebluepandabear <therealbluepandabear protonmail.com> writes:
 I know.  Someone's going to say why don't YOU do it:)

 regards,
The official book on D by Ali has many coding challenges. There isn't any need to create a website for D coding challenges or incorporate it into an existing website since D has under 1% of the market share.
Jan 09 2023
prev sibling next sibling parent monkyyy <crazymonkyyy gmail.com> writes:
On Tuesday, 10 January 2023 at 00:17:18 UTC, Paul wrote:
  I know.  Someone's going to say why don't YOU do it:)
https://github.com/crazymonkyyy/dingbats I could use contributors
Jan 10 2023
prev sibling next sibling parent reply =?UTF-8?Q?Christian_K=c3=b6stlin?= <christian.koestlin gmail.com> writes:
On 10.01.23 01:17, Paul wrote:
There is also https://exercism.org/tracks/d with some tasks for dlang.

Kind regards,
Christian
Jan 10 2023
parent reply monkyyy <crazymonkyyy gmail.com> writes:
On Tuesday, 10 January 2023 at 19:10:09 UTC, Christian Köstlin 
wrote:
 On 10.01.23 01:17, Paul wrote:
 There is also https://exercism.org/tracks/d with some tasks for 
 dlang.

 Kind regards,
 Christian
Its all converted code; worthless
Jan 10 2023
parent =?UTF-8?Q?Christian_K=c3=b6stlin?= <christian.koestlin gmail.com> writes:
On 10.01.23 23:22, monkyyy wrote:
 On Tuesday, 10 January 2023 at 19:10:09 UTC, Christian Köstlin wrote:
 On 10.01.23 01:17, Paul wrote:
 There is also https://exercism.org/tracks/d with some tasks for dlang.

 Kind regards,
 Christian
Its all converted code; worthless
I was not aware, that the question was searching for solutions in dlang. At least exorcism gives a skeleton for a dlang project (including unittest) to solve the task. Kind regards, Christian
Jan 12 2023
prev sibling parent TheZipCreator <thezipcreator protonmail.com> writes:
On Tuesday, 10 January 2023 at 00:17:18 UTC, Paul wrote:
 Greetings Dlang-ers
 I was wondering if anyone knew of any coding challenges 
 available where the input and output are specified and its left 
 to the programmer to find a solution?  Free would be nice but 
 even paid services would be worth considering.  I'm taking a D 
 class right now and it has little challenges in the lessons 
 where much of the work is done for you, but I'm thinking of a 
 site/service that is dedicated to these types of challenges 
 (without doing any work for you).

 [...]
while not specifically D-related, the code golf stack exchange is good place to find challenges (even if you're not golfing).
Jan 14 2023