stevensonmt

stevensonmt

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?

Showing Posts 1 to 4

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 OP

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 OP

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.

— All posts loaded —

Where Next? Top

Trending in Questions Top

katta
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
achenet
Hello, I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind. However, when I launch mix phx.server, I get an error...
New
bradley
I really like the adapter patterns that ecto, nebulex, waffle, etc. use and would love find something similar for a key management servic...
New
unaware8150
Hello folks! So at work, we are seeing some situations where we have to define some “fixed” strings that are used across the codebase in...
New
Cxx-mlr
I’m working on a small exercise involving update_in/3, and I came up with this solution: data = %{ name: "Periodic Table", category:...
New
Alvinkariuki
How Can I Optimise Compile Time Dependencies I have been building an elixir application for about 2 years now. Many modules and files ha...
New
dillonoconnor
Is there any way to avoid the Hologram compiler running when using iex? It seems like the front-end code could potentially be disregarded...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
mudasobwa
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews