stevensonmt

stevensonmt

Algorithm to calculate assignments based on date and cycle length

This is not an elixir-specific question but this is the best community I know so I thought I’d ask anyway. I am creating an algorithmic scheduling tool for my colleagues. In essence there are 3 team members who will have to be assigned a weekend assignment every 3 weeks. I have set up the order in which each team member will take that role by assigning each of them a cycle “offset” – if offset is 0, they get the first week, if 1, second week, if 2 third week. The start date for this schedule is fixed as a constant which is a Sunday. I want to calculate the assignment for all future weekends based on the start date, the cycle length in days, the “offset”, and the weekend date.

What I’ve done is the following (in pseudocode):

days = current_date - start_date
days_in_current_cycle = mod(days, cycle_length)
week_in_current_cycle = div(days_in_current_cycle, 6)
offset = mod(week_in_current_cycle, 3)
assigned = filter(team_members, fn member -> member_offset(member) == offset end)

I’m using 6 as the divisor in finding which week in the current cycle because the start date is a Sunday and the next Saturday belongs to the next week. I also have a conditional to verify the current_date is either a Saturday or Sunday before going through the above. Does this seem sound? Is there a better way?

First 4 of 4 Posts! Switch mode

al2o3cr

al2o3cr

I don’t think dividing by 6 is correct; starting on Sunday makes the whole calculation a little trickier.

Here’s a chart of offset, calendar-style:

Su Mo Tu We Th Fr Sa
0 0 0 0 0 0 1
1 1 1 1 1 1 2
2 2 2 2 2 2 3
LostKobrakai

LostKobrakai

How far in the future do you need this? If not too far I’d be lazy and do something like this. It’s not O(something small), but it doesn’t take parsing some math to understand it.

team = [:mike, :brian, :jim]

x = ~D[2024-01-01]
y = ~D[2024-06-01]

start_date = Date.beginning_of_week(x, :sunday)

Stream.iterate(start_date, &Date.add(&1, 7))
|> Stream.zip(Stream.cycle(team))
|> Stream.drop_while(fn {start_of_week, _} -> 
  end_of_week = Date.end_of_week(start_of_week, :sunday)
  Date.compare(y, end_of_week) == :gt
end)
|> Enum.at(0)
stevensonmt

stevensonmt

The reason I thought using 6 as a divisor was acceptable is that the step before ensures that the dividend is < 21. Because it’s integer division the result can only be 0-3. The error this introduces would maybe lead to Fri or Thu being assigned the wrong offset, but I’m not accepting any dates that are not Sat or Sun.

I guess it would be more logically sound to insist the start date be a Saturday and then use 7. I think that would then eliminate the need to calculate the offset as mod 3 because integer division of a value below 21 by 7 must return 0, 1, or 2.

stevensonmt

stevensonmt

Very cool solution. Unfortunately this is actually something I have to do in Excel rather than with an Elixir script. It will be used to generate at least quarterly schedules, if not the full year.

As an aside, volunteering for this project made me learn just how Turing complete Excel is. It’s got lambdas and variable assignments within formulas. It even has map and filter for dealing with arrays as input.

Where Next?

Trending in Questions Top

jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
silverdr
Using Phoenix.LiveView.TagEngine as an EEx.Engine is deprecated! To compile HEEx, use Phoenix.LiveView.TagEngine.compile/2 instead. Sta...
New
saveman71
Hello ! We want new/edit form pages to POST/PUT to their own URL rather than the resources REST defaults (post /things, put /things/:id)...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
bottlenecked
Hi all, I wanted to ask how the community is dealing with post-release steps. Today we have Ecto migrations, which make sure that the db...
New
michallepicki
I am using Oban and occasionally, shortly after a deployment, a handful of jobs can fail because of dependency on other parts of the syst...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
type1fool
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New
akoutmos
@hugobarauna and I (Alex Koutmos) have been hard at work on writing a book on Nerves that takes you from simply blinking LEDs to building...
New

We're in Beta

About us Mission Statement