Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7060351
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T04:20:33+00:00 2026-05-28T04:20:33+00:00

The user can select any number of week days from a list. An algorithm

  • 0

The user can select any number of week days from a list. An algorithm shall find the longest continuous group of selected days. The start day can be after the end day, if the group spans two weeks. If it makes it simpler, only a group of at least 3 days needs to be detected. With crossing the week border, this makes for a maximum of one group. (There can be no two groups of 3 days within a week that are not connected.)

For example, if the user selects Monday, Tuesday, Wednesday and Saturday from a list, the display should be something like “Monday-Wednesday and Saturday”.

Another example is: Wed, Fri, Sat, Sun, Mon -> “Wed, Fri-Mon”.

Is there an efficient algorithm for that, preferrably in C# or a similar language? My C# hackwork is now over a page long (incl. few comments) and still not finished.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T04:20:34+00:00Added an answer on May 28, 2026 at 4:20 am

    I’ve finished my version of it. It’s a bit longer than the other one, but then again it also handles the text representation and does exactly this task. How about that?

    using System;
    using System.Text;
    
    namespace WeekMathTest
    {
        class Program
        {
            static void Main(string[] args)
            {
                string[] weekDayNames = new string[] {
                    "Mon",
                    "Tue",
                    "Wed",
                    "Thu",
                    "Fri",
                    "Sat",
                    "Sun"
                };
    
                WeekDays weekDays = WeekDays.Monday | WeekDays.Tuesday | WeekDays.Thursday | WeekDays.Saturday | WeekDays.Sunday;
    
                Console.WriteLine(WeekDayGroup(weekDays, weekDayNames));
            }
    
            static string WeekDayGroup(WeekDays weekDays, string[] weekDayNames)
            {
                int groupStart = 0, groupEnd = 0, groupLength = 0;
                int maxGroupStart = 0, maxGroupEnd = 0, maxGroupLength = 0;
    
                // Iterate all days in a repeated range
                // (Sat/Sun doesn't need to be repeated or it would be in the first group)
                for (int day = 1; day <= 7 + 5; day++)
                {
                    // Is this day set?
                    int bitValue = 1 << ((day - 1) % 7);
                    bool daySet = ((int) weekDays & bitValue) != 0;
                    if (daySet)
                    {
                        if (groupStart == 0)
                        {
                            // First day set, remember it as group start
                            groupStart = day;
                            groupEnd = day;
                            groupLength = 1;
                        }
                        else
                        {
                            // Group has already been started, set new end
                            groupEnd = day;
                            groupLength = groupEnd - groupStart + 1;
                            if (groupLength == 7)
                            {
                                // Seen every day of the week, stop here
                                break;
                            }
                        }
                    }
                    else
                    {
                        if (groupLength >= 3 && groupLength > maxGroupLength)
                        {
                            // Group was long enough and longer than the last one, save it
                            maxGroupStart = groupStart;
                            maxGroupEnd = groupEnd;
                            maxGroupLength = groupLength;
                        }
                        // Reset operation variables
                        groupStart = 0;
                        groupEnd = 0;
                        groupLength = 0;
                    }
                }
                // Final check
                if (groupLength >= 3 && groupLength > maxGroupLength)
                {
                    // Group was long enough and longer than the last one, save it
                    maxGroupStart = groupStart;
                    maxGroupEnd = groupEnd;
                    maxGroupLength = groupLength;
                }
    
                // Clear all group days from the original value
                for (int day = maxGroupStart; day <= maxGroupEnd; day++)
                {
                    int bitValue = 1 << ((day - 1) % 7);
                    weekDays = (WeekDays) ((int) weekDays & ~bitValue);
                }
    
                // Generate output string
                StringBuilder sb = new StringBuilder();
                for (int day = 1; day <= 7; day++)
                {
                    int bitValue = 1 << ((day - 1) % 7);
                    bool daySet = ((int) weekDays & bitValue) != 0;
                    if (daySet)
                    {
                        if (sb.Length > 0) sb.Append(", ");
                        sb.Append(weekDayNames[day - 1]);
                    }
                    else if (day == maxGroupStart)
                    {
                        if (sb.Length > 0) sb.Append(", ");
                        sb.Append(weekDayNames[day - 1]);
                        sb.Append("-");
                        sb.Append(weekDayNames[(maxGroupEnd - 1) % 7]);
                    }
                }
                return sb.ToString();
            }
    
            [Flags]
            enum WeekDays
            {
                Monday = 1,
                Tuesday = 2,
                Wednesday = 4,
                Thursday = 8,
                Friday = 16,
                Saturday = 32,
                Sunday = 64
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a View in which the user can choose any number of Clubs
I am trying to make a calculator where a user can select from a
I'm building a list of contacts, where the user can select more than one
Can a JApplet use a JFileChooser so that the user can select a file
I have a View where the user can select Basic Configuration and Advanced Configuration
I have a segmented control where the user can select how to order a
I am building a table using the DataGridView where a user can select items
We want to show a hint for a JList that the user can select
I'm using the OpenFileDialog class so a user can select an image file. I'd
There is a common feature of modern browsers where a user can select some

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.