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

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
ryanwinchester
apply_graft/2 doesn’t rewrite an add_many sub-workflow’s deps on an add step. Grafted jobs cancel with “upstream job was deleted” Version...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
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
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
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
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews