Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. The Lounge
  3. Programming Quiz

Programming Quiz

Scheduled Pinned Locked Moved The Lounge
csharpjavascriptcomdata-structuresquestion
29 Posts 19 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Marc Clifton

    OK, reformatted to protect the guilty. Write a function in your favorite language to create a "day view" in an array for a month, like 11/2019. So for that month/year, the array should look like this, where the day numbers not in the month are null or undefined:

                             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

    My solution in C#, which uses an extension method:

    int?[] days = new int?[6 * 7];
    var dow = (int)new DateTime(2019, 11, 1).DayOfWeek;
    Enumerable.Range(1, DateTime.DaysInMonth(2019, 11)).ForEachWithIndex((idx, n) => days[dow + idx] = n);

    And the output function:

    days.ForEachWithIndex((idx, n) => Console.Write(((idx % 7 == 0) ? "\r\n" : "") + (n?.ToString()?.PadLeft(5) ?? " ")));

    Which of course generates a leading CRLF, but oh well, that wasn't specifically indicated not to do so in the spec. :laugh: For the curious, this came up in a conversation with a coworker.

    Latest Articles:
    16 Days: A TypeScript application from concept to implementation

    L Offline
    L Offline
    Lost User
    wrote on last edited by
    #3

    Ok, completely not familiar with this stuff, but would ((idx % 7 == 0) && (idx > 0)) ? not prevent from a leading space? :-O

    It does not solve my Problem, but it answers my question

    M 1 Reply Last reply
    0
    • M Marc Clifton

      OK, reformatted to protect the guilty. Write a function in your favorite language to create a "day view" in an array for a month, like 11/2019. So for that month/year, the array should look like this, where the day numbers not in the month are null or undefined:

                               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

      My solution in C#, which uses an extension method:

      int?[] days = new int?[6 * 7];
      var dow = (int)new DateTime(2019, 11, 1).DayOfWeek;
      Enumerable.Range(1, DateTime.DaysInMonth(2019, 11)).ForEachWithIndex((idx, n) => days[dow + idx] = n);

      And the output function:

      days.ForEachWithIndex((idx, n) => Console.Write(((idx % 7 == 0) ? "\r\n" : "") + (n?.ToString()?.PadLeft(5) ?? " ")));

      Which of course generates a leading CRLF, but oh well, that wasn't specifically indicated not to do so in the spec. :laugh: For the curious, this came up in a conversation with a coworker.

      Latest Articles:
      16 Days: A TypeScript application from concept to implementation

      Richard DeemingR Offline
      Richard DeemingR Offline
      Richard Deeming
      wrote on last edited by
      #4

      Now make it work for cultures whose week doesn't start on Sunday. ;P


      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

      W M A M F 5 Replies Last reply
      0
      • Richard DeemingR Richard Deeming

        Now make it work for cultures whose week doesn't start on Sunday. ;P


        "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

        W Offline
        W Offline
        W Balboos GHB
        wrote on last edited by
        #5

        Richard Deeming wrote:

        cultures whose week doesn't start on Sunday.

        That's easy - NUKE 'EM !

        Ravings en masse^

        "The difference between genius and stupidity is that genius has its limits." - Albert Einstein

        "If you are searching for perfection in others, then you seek disappointment. If you seek perfection in yourself, then you will find failure." - Balboos HaGadol Mar 2010

        F 1 Reply Last reply
        0
        • L Lost User

          Ok, completely not familiar with this stuff, but would ((idx % 7 == 0) && (idx > 0)) ? not prevent from a leading space? :-O

          It does not solve my Problem, but it answers my question

          M Offline
          M Offline
          Marc Clifton
          wrote on last edited by
          #6

          0x01AA wrote:

          && (idx > 0))

          Exactly what I was thinking, I just figured I'd show you what I'd come up with, warts and all. ;)

          Latest Articles:
          16 Days: A TypeScript application from concept to implementation

          1 Reply Last reply
          0
          • Richard DeemingR Richard Deeming

            Now make it work for cultures whose week doesn't start on Sunday. ;P


            "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

            M Offline
            M Offline
            Marc Clifton
            wrote on last edited by
            #7

            Richard Deeming wrote:

            Now make it work for cultures whose week doesn't start on Sunday.

            I wonder if there are any cultures that don't have a 7 day week? Or even the concept of a week?

            Latest Articles:
            16 Days: A TypeScript application from concept to implementation

            Richard DeemingR U 2 Replies Last reply
            0
            • R RossMW

              Is this your homework, sonny.

              A Fine is a Tax for doing something wrong A Tax is a Fine for doing something good.

              M Offline
              M Offline
              Marc Clifton
              wrote on last edited by
              #8

              RossMW wrote:

              Is this your homework, sonny.

              Urgentz! Help PLZ!!! ;)

              Latest Articles:
              16 Days: A TypeScript application from concept to implementation

              1 Reply Last reply
              0
              • M Marc Clifton

                Richard Deeming wrote:

                Now make it work for cultures whose week doesn't start on Sunday.

                I wonder if there are any cultures that don't have a 7 day week? Or even the concept of a week?

                Latest Articles:
                16 Days: A TypeScript application from concept to implementation

                Richard DeemingR Offline
                Richard DeemingR Offline
                Richard Deeming
                wrote on last edited by
                #9

                Is the 7 day week universal? : NoStupidQuestions[^]


                "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

                1 Reply Last reply
                0
                • M Marc Clifton

                  OK, reformatted to protect the guilty. Write a function in your favorite language to create a "day view" in an array for a month, like 11/2019. So for that month/year, the array should look like this, where the day numbers not in the month are null or undefined:

                                           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

                  My solution in C#, which uses an extension method:

                  int?[] days = new int?[6 * 7];
                  var dow = (int)new DateTime(2019, 11, 1).DayOfWeek;
                  Enumerable.Range(1, DateTime.DaysInMonth(2019, 11)).ForEachWithIndex((idx, n) => days[dow + idx] = n);

                  And the output function:

                  days.ForEachWithIndex((idx, n) => Console.Write(((idx % 7 == 0) ? "\r\n" : "") + (n?.ToString()?.PadLeft(5) ?? " ")));

                  Which of course generates a leading CRLF, but oh well, that wasn't specifically indicated not to do so in the spec. :laugh: For the curious, this came up in a conversation with a coworker.

                  Latest Articles:
                  16 Days: A TypeScript application from concept to implementation

                  P Offline
                  P Offline
                  PIEBALDconsult
                  wrote on last edited by
                  #10

                  Pfft... I've been using an MS Word template with a macro to create calendars since the mid-90s. With weeks beginning on Monday as per ISO and ISO week numbers as well.

                  1 Reply Last reply
                  0
                  • Richard DeemingR Richard Deeming

                    Now make it work for cultures whose week doesn't start on Sunday. ;P


                    "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                    A Offline
                    A Offline
                    AFell2
                    wrote on last edited by
                    #11

                    Took this a step further and extended the code, and it should work with culture:

                    void Main()
                    {
                    StringBuilder builder = new StringBuilder();

                    Enumerable.Range(1, 12)
                        .ToList()
                        .ForEach(e => PopulateDayOfMonth(2021, e, builder));
                        
                    Console.WriteLine(builder.ToString().Trim());
                    

                    }

                    static void PopulateDayOfMonth(int year, int month, StringBuilder builder)
                    {
                    builder.AppendLine();
                    int space = 6;
                    int dashLine = 1 + (space * 7);

                    IEnumerable days = Enumerable.Range(1, DateTime.DaysInMonth(year, month))
                        .Select(d => new DateTime(year, month, d));
                    
                    builder.AppendLine(days.First().ToString("MMMMM"));
                    builder.Append('-', dashLine);
                    builder.AppendLine();
                    builder.AppendLine($"|{string.Join("|", Enum.GetNames(typeof(DayOfWeek)).Select(d => $" {d.Substring(0, 3)} "))}|" );
                    builder.Append('-', dashLine);
                    builder.AppendLine();
                    DayOfWeek dow = days.First().DayOfWeek;
                    
                    while (dow != 0)
                    {
                        builder.Append(' ', space);
                        dow--;
                    }
                    
                    bool first = true;
                    
                    foreach (var d in days)
                    {
                        if (!first && d.DayOfWeek == 0)
                        {
                            builder.AppendLine("|");
                            builder.Append('-', dashLine);
                            builder.AppendLine();
                        }
                    
                        builder.Append($"|{d.Day.ToString().PadLeft(space - 2)} ");
                        first = false;
                    }
                    
                    builder.AppendLine("|");
                    builder.Append('-', dashLine);
                    builder.AppendLine();
                    

                    }

                    With the following output:

                    January

                    | Sun | Mon | Tue | Wed | Thu | Fri | Sat |

                                              |   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 |

                    February

                    | Sun | Mon | Tue | Wed | Thu | Fri | Sat |

                      |   1 |   2 |   3 |   4 |   5 |   6 |
                    

                    | 7 | 8 | 9 | 10 | 11 | 12 | 13 |

                    B Richard DeemingR 2 Replies Last reply
                    0
                    • Richard DeemingR Richard Deeming

                      Now make it work for cultures whose week doesn't start on Sunday. ;P


                      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                      M Offline
                      M Offline
                      Member 11005478
                      wrote on last edited by
                      #12

                      Sunday is part of the weekend. That's a clue as to the correct day that a week starts on

                      Z 1 Reply Last reply
                      0
                      • A AFell2

                        Took this a step further and extended the code, and it should work with culture:

                        void Main()
                        {
                        StringBuilder builder = new StringBuilder();

                        Enumerable.Range(1, 12)
                            .ToList()
                            .ForEach(e => PopulateDayOfMonth(2021, e, builder));
                            
                        Console.WriteLine(builder.ToString().Trim());
                        

                        }

                        static void PopulateDayOfMonth(int year, int month, StringBuilder builder)
                        {
                        builder.AppendLine();
                        int space = 6;
                        int dashLine = 1 + (space * 7);

                        IEnumerable days = Enumerable.Range(1, DateTime.DaysInMonth(year, month))
                            .Select(d => new DateTime(year, month, d));
                        
                        builder.AppendLine(days.First().ToString("MMMMM"));
                        builder.Append('-', dashLine);
                        builder.AppendLine();
                        builder.AppendLine($"|{string.Join("|", Enum.GetNames(typeof(DayOfWeek)).Select(d => $" {d.Substring(0, 3)} "))}|" );
                        builder.Append('-', dashLine);
                        builder.AppendLine();
                        DayOfWeek dow = days.First().DayOfWeek;
                        
                        while (dow != 0)
                        {
                            builder.Append(' ', space);
                            dow--;
                        }
                        
                        bool first = true;
                        
                        foreach (var d in days)
                        {
                            if (!first && d.DayOfWeek == 0)
                            {
                                builder.AppendLine("|");
                                builder.Append('-', dashLine);
                                builder.AppendLine();
                            }
                        
                            builder.Append($"|{d.Day.ToString().PadLeft(space - 2)} ");
                            first = false;
                        }
                        
                        builder.AppendLine("|");
                        builder.Append('-', dashLine);
                        builder.AppendLine();
                        

                        }

                        With the following output:

                        January

                        | Sun | Mon | Tue | Wed | Thu | Fri | Sat |

                                                  |   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 |

                        February

                        | Sun | Mon | Tue | Wed | Thu | Fri | Sat |

                          |   1 |   2 |   3 |   4 |   5 |   6 |
                        

                        | 7 | 8 | 9 | 10 | 11 | 12 | 13 |

                        B Offline
                        B Offline
                        BillWoodruff
                        wrote on last edited by
                        #13

                        :thumbsup:

                        «One day it will have to be officially admitted that what we have christened reality is an even greater illusion than the world of dreams.» Salvador Dali

                        1 Reply Last reply
                        0
                        • M Marc Clifton

                          OK, reformatted to protect the guilty. Write a function in your favorite language to create a "day view" in an array for a month, like 11/2019. So for that month/year, the array should look like this, where the day numbers not in the month are null or undefined:

                                                   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

                          My solution in C#, which uses an extension method:

                          int?[] days = new int?[6 * 7];
                          var dow = (int)new DateTime(2019, 11, 1).DayOfWeek;
                          Enumerable.Range(1, DateTime.DaysInMonth(2019, 11)).ForEachWithIndex((idx, n) => days[dow + idx] = n);

                          And the output function:

                          days.ForEachWithIndex((idx, n) => Console.Write(((idx % 7 == 0) ? "\r\n" : "") + (n?.ToString()?.PadLeft(5) ?? " ")));

                          Which of course generates a leading CRLF, but oh well, that wasn't specifically indicated not to do so in the spec. :laugh: For the curious, this came up in a conversation with a coworker.

                          Latest Articles:
                          16 Days: A TypeScript application from concept to implementation

                          E Offline
                          E Offline
                          EliaMelfior
                          wrote on last edited by
                          #14

                          This could be a fun little puzzle. I just get tired thinking about solving it, i would have to search for a time library in javascript/php, and then format the output nicely. Just not in the mood for a little puzzle, does anyone identify with this feeling sometimes? Nice quiz though.

                          1 Reply Last reply
                          0
                          • M Member 11005478

                            Sunday is part of the weekend. That's a clue as to the correct day that a week starts on

                            Z Offline
                            Z Offline
                            ZurdoDev
                            wrote on last edited by
                            #15

                            Member 11005478 wrote:

                            weekend. That's a clue as to the correct day that a week starts on

                            Just like bookends are all on one side. :laugh:

                            Social Media - A platform that makes it easier for the crazies to find each other. Everyone is born right handed. Only the strongest overcome it. Fight for left-handed rights and hand equality.

                            J 1 Reply Last reply
                            0
                            • M Marc Clifton

                              OK, reformatted to protect the guilty. Write a function in your favorite language to create a "day view" in an array for a month, like 11/2019. So for that month/year, the array should look like this, where the day numbers not in the month are null or undefined:

                                                       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

                              My solution in C#, which uses an extension method:

                              int?[] days = new int?[6 * 7];
                              var dow = (int)new DateTime(2019, 11, 1).DayOfWeek;
                              Enumerable.Range(1, DateTime.DaysInMonth(2019, 11)).ForEachWithIndex((idx, n) => days[dow + idx] = n);

                              And the output function:

                              days.ForEachWithIndex((idx, n) => Console.Write(((idx % 7 == 0) ? "\r\n" : "") + (n?.ToString()?.PadLeft(5) ?? " ")));

                              Which of course generates a leading CRLF, but oh well, that wasn't specifically indicated not to do so in the spec. :laugh: For the curious, this came up in a conversation with a coworker.

                              Latest Articles:
                              16 Days: A TypeScript application from concept to implementation

                              S Offline
                              S Offline
                              Stuart Dootson
                              wrote on last edited by
                              #16

                              In Haskell (because, of course Haskell!): ```haskell import Data.List.Split import Data.Time.Calendar; import Data.Time.Calendar.Compat; import Data.Time.Calendar.Julian; import Text.Printf printMonth :: Integer -> Int -> IO () printMonth year month = printWeeks entriesInMonth where -- printWeeks chunks the month's entries into weeks & prints each week on a new line printWeeks days = mapM_ (putStrLn.unwords) (chunksOf 7 days) -- entriesInMonth concatenates the empty entries at the start of the month with the days entriesInMonth = startPadding ++ daysOfMonth -- daysOfMonth generates a list of days of the month as strings daysOfMonth = map (printf "%2d") [1..(julianMonthLength year month)] -- startPadding generates blank strings for each empty entry before day '1' startPadding = replicate blanksBeforeDay1 " " -- blanksBeforeDay1 is the number of empty entries before day '1' blanksBeforeDay1 = (fromEnum $ dayOfWeek (fromJulian year month 1)) `mod` 7 ``` which gives: ``` > printMonth 2019 12 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 > printMonth 20190 12 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 > printMonth 20190 11 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 ``` [ETA] That's using the Julian calendar - the standard Gregorian calendar can be used by replacing the word 'julian' or 'Julian' with 'gregorian'/'Gregorian'. And yes, I should be able to remember which calendar we use :-O [/ETA]

                              Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

                              J 1 Reply Last reply
                              0
                              • S Stuart Dootson

                                In Haskell (because, of course Haskell!): ```haskell import Data.List.Split import Data.Time.Calendar; import Data.Time.Calendar.Compat; import Data.Time.Calendar.Julian; import Text.Printf printMonth :: Integer -> Int -> IO () printMonth year month = printWeeks entriesInMonth where -- printWeeks chunks the month's entries into weeks & prints each week on a new line printWeeks days = mapM_ (putStrLn.unwords) (chunksOf 7 days) -- entriesInMonth concatenates the empty entries at the start of the month with the days entriesInMonth = startPadding ++ daysOfMonth -- daysOfMonth generates a list of days of the month as strings daysOfMonth = map (printf "%2d") [1..(julianMonthLength year month)] -- startPadding generates blank strings for each empty entry before day '1' startPadding = replicate blanksBeforeDay1 " " -- blanksBeforeDay1 is the number of empty entries before day '1' blanksBeforeDay1 = (fromEnum $ dayOfWeek (fromJulian year month 1)) `mod` 7 ``` which gives: ``` > printMonth 2019 12 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 > printMonth 20190 12 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 > printMonth 20190 11 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 ``` [ETA] That's using the Julian calendar - the standard Gregorian calendar can be used by replacing the word 'julian' or 'Julian' with 'gregorian'/'Gregorian'. And yes, I should be able to remember which calendar we use :-O [/ETA]

                                Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

                                J Offline
                                J Offline
                                Jorgen Andersson
                                wrote on last edited by
                                #17

                                So, I don't know shit about Haskell, but shouldn't it be Data.Time.Calendar.Gregorian instead of Data.Time.Calendar.Julian? Unless you're living in Russia that is.

                                Wrong is evil and must be defeated. - Jeff Ello

                                S 1 Reply Last reply
                                0
                                • Z ZurdoDev

                                  Member 11005478 wrote:

                                  weekend. That's a clue as to the correct day that a week starts on

                                  Just like bookends are all on one side. :laugh:

                                  Social Media - A platform that makes it easier for the crazies to find each other. Everyone is born right handed. Only the strongest overcome it. Fight for left-handed rights and hand equality.

                                  J Offline
                                  J Offline
                                  Jorgen Andersson
                                  wrote on last edited by
                                  #18

                                  Otherwise you could use Genesis 2.2 as a reference.

                                  Wrong is evil and must be defeated. - Jeff Ello

                                  Z J 2 Replies Last reply
                                  0
                                  • J Jorgen Andersson

                                    Otherwise you could use Genesis 2.2 as a reference.

                                    Wrong is evil and must be defeated. - Jeff Ello

                                    Z Offline
                                    Z Offline
                                    ZurdoDev
                                    wrote on last edited by
                                    #19

                                    Jörgen Andersson wrote:

                                    Otherwise you could use Genesis 2.2 as a reference.

                                    Correct. The Old Testament week.

                                    Social Media - A platform that makes it easier for the crazies to find each other. Everyone is born right handed. Only the strongest overcome it. Fight for left-handed rights and hand equality.

                                    1 Reply Last reply
                                    0
                                    • J Jorgen Andersson

                                      So, I don't know shit about Haskell, but shouldn't it be Data.Time.Calendar.Gregorian instead of Data.Time.Calendar.Julian? Unless you're living in Russia that is.

                                      Wrong is evil and must be defeated. - Jeff Ello

                                      S Offline
                                      S Offline
                                      Stuart Dootson
                                      wrote on last edited by
                                      #20

                                      >

                                      Jörgen Andersson wrote:

                                      shouldn't it be Data.Time.Calendar.Gregorian instead of Data.Time.Calendar.Julian You're quite right - and that's my mistake, getting Gregorian/Julian mixed up! Gregorian is actually the default, baked into `Data.Time.Calendar`, while Julian is the add-on... ```haskell import Data.List.Split import Data.Time.Calendar; import Data.Time.Calendar.Compat; import Text.Printf printMonth :: Integer -> Int -> IO () printMonth year month = printWeeks entriesInMonth where -- printWeeks chunks the month's entries into weeks & prints each week on a new line printWeeks days = mapM_ (putStrLn.unwords) (chunksOf 7 days) -- entriesInMonth concatenates the empty entries at the start of the month with the days entriesInMonth = startPadding ++ daysOfMonth -- daysOfMonth generates a list of days of the month as strings daysOfMonth = map (printf "%2d") [1..(gregorianMonthLength year month)] -- startPadding generates blank strings for each empty entry before day '1' startPadding = replicate blanksBeforeDay1 " " -- blanksBeforeDay1 is the number of empty entries before day '1' blanksBeforeDay1 = (fromEnum $ dayOfWeek (fromGregorian year month 1)) `mod` 7 ``` Being able to use `Data.Time.Calendar` rather than `Data.Time.Calendar.Julian` should have been a giveaway, shouldn't it :-O

                                      Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

                                      J 1 Reply Last reply
                                      0
                                      • M Marc Clifton

                                        OK, reformatted to protect the guilty. Write a function in your favorite language to create a "day view" in an array for a month, like 11/2019. So for that month/year, the array should look like this, where the day numbers not in the month are null or undefined:

                                                                 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

                                        My solution in C#, which uses an extension method:

                                        int?[] days = new int?[6 * 7];
                                        var dow = (int)new DateTime(2019, 11, 1).DayOfWeek;
                                        Enumerable.Range(1, DateTime.DaysInMonth(2019, 11)).ForEachWithIndex((idx, n) => days[dow + idx] = n);

                                        And the output function:

                                        days.ForEachWithIndex((idx, n) => Console.Write(((idx % 7 == 0) ? "\r\n" : "") + (n?.ToString()?.PadLeft(5) ?? " ")));

                                        Which of course generates a leading CRLF, but oh well, that wasn't specifically indicated not to do so in the spec. :laugh: For the curious, this came up in a conversation with a coworker.

                                        Latest Articles:
                                        16 Days: A TypeScript application from concept to implementation

                                        A Offline
                                        A Offline
                                        agolddog
                                        wrote on last edited by
                                        #21

                                        Checkin rejected, hard-coded values. Just kidding, the knuckleheads I work with hard-code things all over the place, and management won't let us have any code reviews. Sigh.

                                        1 Reply Last reply
                                        0
                                        • S Stuart Dootson

                                          >

                                          Jörgen Andersson wrote:

                                          shouldn't it be Data.Time.Calendar.Gregorian instead of Data.Time.Calendar.Julian You're quite right - and that's my mistake, getting Gregorian/Julian mixed up! Gregorian is actually the default, baked into `Data.Time.Calendar`, while Julian is the add-on... ```haskell import Data.List.Split import Data.Time.Calendar; import Data.Time.Calendar.Compat; import Text.Printf printMonth :: Integer -> Int -> IO () printMonth year month = printWeeks entriesInMonth where -- printWeeks chunks the month's entries into weeks & prints each week on a new line printWeeks days = mapM_ (putStrLn.unwords) (chunksOf 7 days) -- entriesInMonth concatenates the empty entries at the start of the month with the days entriesInMonth = startPadding ++ daysOfMonth -- daysOfMonth generates a list of days of the month as strings daysOfMonth = map (printf "%2d") [1..(gregorianMonthLength year month)] -- startPadding generates blank strings for each empty entry before day '1' startPadding = replicate blanksBeforeDay1 " " -- blanksBeforeDay1 is the number of empty entries before day '1' blanksBeforeDay1 = (fromEnum $ dayOfWeek (fromGregorian year month 1)) `mod` 7 ``` Being able to use `Data.Time.Calendar` rather than `Data.Time.Calendar.Julian` should have been a giveaway, shouldn't it :-O

                                          Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

                                          J Offline
                                          J Offline
                                          Jorgen Andersson
                                          wrote on last edited by
                                          #22

                                          Stuart Dootson wrote:

                                          Being able to use Data.Time.Calendar rather than Data.Time.Calendar.Julian should have been a giveaway, shouldn't it :O

                                          I wouldn't have known. :doh: :-O

                                          Wrong is evil and must be defeated. - Jeff Ello

                                          1 Reply Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • World
                                          • Users
                                          • Groups