TimeStamp to FiscalWeek - algorithm

I am trying to convert a timestamp in the format "yyyy-mm-dd" to an integer fiscal week. Currently, my algorithm is 4(k-1) + floor(d/7)+1 where k is the integer month and d is the integer day of the month. Saturday starts the new fiscal week.
This has some flaws and is incorrect. For instance consider Saturday, January 28th 2012:
My algorithm computes 5(which is correct).
Next consider, Friday February 3rd:
My algorithm computes 5(which is correct).
Now consider Saturday February 4th.
My algorithm computes 5(this is incorrect).
It appears my algorithm will always fail in between months and thus accumulate an increasing error.
How can I compute the correct fiscal week?

Compute the number of days elapsed before the beginning of the current month, add it to dd and then divide by 7. Finally, add 1 to the resulting number.
So for Feb 4th, the answer would be (31 + 4)/7 + 1 = 6.

Related

How to find the cheapest combo of metro subscription

Lets say we have the following fares available :
1 trip
2 trips
10 trips
unlimited weekend (saturday to sunday, not 2 days)
1 day
3 days
unlimited week (monday to sunday, not 7 days)
unlimited month (1st to last day of month)
... with a price for every one of them.
The problem is : **How to determine what set of subscriptions to chose given a date of arrival and a date of departure ? **
Lets say we want the solution for n between 1 and 8, n being the number of time we take the metro daily (so we assume we take the metro the same number of time every day)
For example it would say something like :
n = 1
Arriving on Friday 19th and leaving Thursday 23th, the best is taking the 1 trip, then the weekend, then the 2 trips (didnt calculate but you see the point)
n = 2
...
I have found examples with only 1 day, 2 days, 7 days fares with dynamic programming, but it looks a lot harder when you considerate the days of the week.
Thanks :)
I like to view this kind of dynamic program as finding a shortest path in a directed acyclic graph.
Each node of the graph encodes
what the current day is (either during the travel period or the day after), and
how many trips remain on trip-limited passes (at most n + 9).
Each arc represents either
purchasing a specific pass at a specific time (the length of the arc is the cost of this pass), or
using trip-limited passes to cover the day's trips (the length of the arc is zero).
The time-limited passes advance the day to the first day they no longer work. The trip-limited passes increase the number of remaining trips. The zero-cost arcs advance the day by one while decreasing the number of remaining trips by n.
Given the shortest path, it is easy to decode it to a plan for purchasing passes.
(P.S. I don't know what the rules are on, e.g., purchasing a week pass on a Tuesday for the rest of the week. Even if this is not allowed, you're going to want to put arcs for the time-limited passes that could have been purchased on a previous day during the travel period.)

Find the best time to change the other times the least

I want to find the best common time to change the other times the least (that the sum of the differences is the lowest possible).
On input, I have the array of times.
On output, there should be the new, common time.
Note that the times can be not ordered and be absolutely different (e.g. 02:00, 02:30 and 03:30)
Example 1
Input: ["01:00", "02:00", "03:00", "04:00", "05:00"]
Output should be 03:00, because it's the best to change 01:00 to 03:00 (2 hours of change), 02:00 to 03:00 (1 hour of change), keep 03:00 the same, 04:00 to 03:00 (1 hour), and 05:00 to 03:00 (2 hours).
Example 2
Input: ["12:00", "13:00", "14:00"]
Output should be 13:00 - change 12:00 to 13:00 (1 hour) and 14:00 to 13:00 (1 hour)
Example 3 (tricky)
Input: ["23:00", "01:00", "02:00"]
Output should be 01:00 - change 23:00 to 01:00 (2 hours) and 02:00 to 01:00 (1 hour) (the tricky thing is 23:00 - the best time is not 13:00)
I tried functions that make the average of times, e.g. https://stackoverflow.com/a/52839039/19022995, but unfortunately it doesn't work
I would be really grateful for tips how to do this
Thank you in advance
Lemma: an optimal answer coincides with one of the given times.
To prove it, consider a solution that doesn't coincide with any of the given times. Try to move it a bit forward and then a bit backward. In at least one of the cases, the total difference does not increase. So, continue moving in that direction until you hit one of the given times.
What is left now is to write a function that computes the difference, taking wraparound from 23:59 to 00:00 into account.
After that, try every given time as a candidate answer, calculate the total difference for each, and pick the best one as the final answer.
In pseudocode:
time (h, m) = h * 60 + m
diff (t1, t2) = min (abs (t1 - t2), 60 * 24 - abs (t1 - t2))
t[0..n) = given times
total (x) = sum {diff (x, t[i])} for i in [0..n)
answer = arg min {total (t[i])} for i in [0..n)
One approach is to look for the smallest "window" containing all the times. To do this, sort the times (naively) and, consider each time as being the "first". For instance, in your third example, considering 1:00 as the first time would lead to a 22 hour window (because that makes the "last" time 23:00); considering 2:00 as the first time would lead to a 23 hour window; but considering 23:00 as the first time would lead to a 3 hour window. From there, simply calculate relative to that first time, calculating differences mod 24.

Sorting shuffled dates - Counting or Radix?

I'm trying to solve this problem:
There is a shuffled list of the n most important events from year 0 to the end of the second millennium (year 2000).
What is the most efficient way to sort the list, when all the events of the same year are sorted in the list (for example, event form January 2014 comes before an event from February 2014, and the events don't have to be in adjacent indexes)?
I understand I should use a stable algorithm, and the answer depends on n: if n is bigger than some number, I should use radix, else I should use counting. But I don't know how to show it formally.
I also know that if the range of the years is much bigger than the linear time, than radix is preferable.

Group Maker Algorithm based on free time avaliable

I am trying to come up with an algorithm that would make groups based on the maximum free time available a group of people have in polynomial time, but I believe the solution to this problem might be NP.
The problem is as followed:
We divided the week into 1 hour slots where users can put down for each slot whether they are free or busy. Let's say we gather this information from 30 users. Let's also assume that users%group_size = 0
First:
Is it possible to put these people into groups of size G so that every member in each group of size G has one overlapping free time slot with each other?
Is it possible to put these people into groups of size G that results in an optimal solution, which is to have the maximum total overlapping free time slots among all groups?
For example, if we had a group of 6 people with the following free time:
A: 1pm-3pm Sunday AND 1pm-3pm Monday
B: 2pm-3pm Sunday AND 1pm-3pm Monday
C: 1pm-3pm Sunday AND 7pm-9pm Monday
D: 6pm-7pm Sunday AND 7pm-9pm Monday
E: 5pm-7pm Sunday AND 7pm-9pm Monday
F: 6pm-7pm Sunday AND 1pm-3pm Monday
The algorithm would determine that A,B,F would be one group and C,D,E would be another group because a maximum of two hours of free time overlaps between the groups. This is opposed to A,B,C and D,E,F which only contains 1 overlapping time slot for every member in the group. As a result, this is optimal solution which is greatest overlap in total among all groups.
I realized this problem is probably related to the Hopcroft-Karp Algorithm, but needs to be modified greatly to accomplish this task. Is their another algorithm that relates more closely to the solution then the Hopcroft-Karp Algorithm? Can this solution be achieved in polynomial time?
Background:
We have a bunch of people(30-50) who want to volunteer for a cause and they only have certain times they are free during the week. We want to break them into groups of 3-5 and have them work together for this cause. We want the group members to have as much time as possible with each other so we want to break them into groups where they have similar free times available.
Thanks a bunch and please let me know if this is an obvious question or if more clarification is needed.
On first look, it seems like a set cover problem, where a subset is number of persons sharing a time slot and the universal set would be all the persons.
U = {p0, p1, p2 ..... , p29} // Number of persons.
S = {S0, S1, S2, ....... S23} // number of 1 hour slots.
I am still not sure how to use the G(size of a ideal group) into account.

How do I create formula for counting diffrence between times

Can someone tell me how i can make arrival time counting (17:00) minus 22 minutes?
Arrival Time Time On Bay Time Off Bay
17:00
So every time when I put something in, say "time" I will get "time minus 25 minutes" in arrival.
In Excel, you can do simple arithmetic on time values. The difference between two times is a (fractional) number of days. For example, 0.25 represents a time difference of 6 hours.
As such, you can add/subtract a (fractional) number of days to a time. Example:
cell A1 is a fixed time value 17:00
cell A2 is a formula =A1-0.25; this will evaluate to 11:00
To work with minutes, you will have to convert the amount of minutes to an amount of days; just divide the amount of minutes by 1440.
So a time difference of 25 minutes is represented by 25/1440. Example:
cell A1 is a fixed time value 17:00
cell A2 is a formula =A1-25/1440; this will evaluate to 16:35

Resources