numbered weekdays: find the k-th integer - python-3.x

The task I´m given is as follows:
Days of week are numbered as:
0 — Sunday,
1 — Monday,
2 — Tuesday,
...
6 — Saturday.
An integer K in the range 1 to 365 is given. Find the number of the day of the week for the K-th day of the year provided that in this year January 1 was a Thursday.
I´m struggling to create a general and user-friendly code for the given problem. Thanks for any help!
date=int(input('enter the K-th day: '))
if(day==0):
print("Sunday")
if(day==1):
print("Monday")
if(day==2):
print("Tuesday")
if(day==3):
print("Wednesday")
if(day==4):
print("Thursday")
if(day==5):
print("Friday")
if(day==6):
print("Saturday")

We know that day 1 was a Thursday, this means we also know that day 8, 15, 21 and so on are also Thursdays. All these numbers have in common that if you divide them by 7, the remainder is 1.
Day 2 was a Friday, so day 9 and 16 are also Fridays. Divided by 7 the remainder for all Fridays is 2.
If you do this for all weekdays you will notice a pattern ;-) and if you check the python docs you'll stumble across the Modulo operator.
This should point you in the right direction without spoiling the fun. If you need any additional help, just let me know and I'll reify the answer.

here you go!
nday = int(1)
dday=((3+nday)%7)
print(dday)

Related

How do I get the last day of the quarter date using Date and ceiling?

I want to get the date that is the last day of the quarter. Can you help me solve this?
I've taken over an old model and trying to understand the logic behind it. The quarter end date is formulated as follows:
A2=DATE(Year(A1),CEILING(MONTH(A1),3)+1,0) Where cell A1=2019-07-01.
This formula= 2019-09-30 which is the right date, but I'm having a hard time understanding the ceiling, multiplier and +1. I think the +1 is to get September (9) instead of August (8) but I don't understand why 3 as a multiplier returns 8 if it is not a multiplier of 3. Shouldn't it return 6 or 9?
Thank you so much for your help!
Let's break this into parts:
MONTH(A1): equals 7.
CEILING(7,3): equals 9.
CEILING(7,3)+1: equals 10.
YEAR(A1): equals 2019.
DATE(2019, 10, 0): equals 2019-09-30.
Using 0 as the day with DATE is the trick - October "0th" is actually September 30th.

How can i develop a working shift formula that can have four posibilities of output

I've been trying to develop a formula in excel to calculate the working shift by date and time, however I've got some troubles trying to reach a efficient one, some of them are the next ones:
-The validity of the formula in time is short.
-It has to be completely condicionated to wrok fine, demands a lot of computing, and it still has the validity short.
Now the bullets to define a formula are the next ones:
-The output must any of "1", "2", "3" and "4" with its conditions, the conditions are the next ones:
*1 is the shift where you work since 6:10 am to 6:10 pm, from sunday to tuesday and the wednesday you work one week and the other you dont. (For this year, the week number of the year must be pair for you to work in shift 1 the wednesday, the unpair one you dont)
*2 is the shift where you work since 6:10 am to 6:10 pm, from thursday to saturday and the wednesyou alternate the day with shift 1, example shift 1 worked one week the wednesday then you work the next one the wednesday.
*3 is the shift where you work since 6:10 pm to 6:10 am, from sunday to tuesday and the wednesday you work one week and the other you dont.
*4 is the shift where you work since 6:10 pm to 6:10 am, from thursday to saturday and the wednesyou alternate the day with shift 3, example shift 3 worked one week the wednesday then you work the next one the wednesday.
I've tried conditioning and it works but it has the two points I explain in the question. conditioning you get it to work like 3 years.
I'm looking for a synthesized way to calculate it with its two variables (Date and time) in a way really complete and maybe analyze the way to do it.
The formula is the next one (in cell F14 I've date and time, it can be separated there's no problem with that) :
=IF(AND(WEEKDAY(F14)<=3,VALUE(RIGHT(ROUND(F14,5),6))>6.16666666/24,VALUE(RIGHT(ROUND(F14,5),6))<18.16666666/24),"T1",IF(AND(WEEKDAY(F14)>=5,VALUE(RIGHT(ROUND(F14,5),6))>6.16666666/24,VALUE(RIGHT(ROUND(F14,5),6))<18.16666666/24),"T2",IF(AND(WEEKDAY(F14)=4,VALUE(RIGHT(ROUND(F14,5),6))>6.16666666/24,VALUE(RIGHT(ROUND(F14,5),6))<18.16666666/24,VALUE(RIGHT(YEAR(F14)/2,2))=0.5,VALUE(RIGHT(ROUND(WEEKNUM(F14)/2,2),2))<>0.5),"T1",IF(AND(WEEKDAY(F14)=4,VALUE(RIGHT(ROUND(F14,5),6))>6.16666666/24,VALUE(RIGHT(ROUND(F14,5),6))<18.16666666/24,VALUE(RIGHT(ROUND(WEEKNUM(F14)/2,2),2))=0.5),"T2",IF(AND(WEEKDAY(F14)=1,VALUE(RIGHT(ROUND(F14,5),6))<6.16666666/24),"T4",IF(AND(WEEKDAY(F14)<=4,VALUE(RIGHT(ROUND(F14,5),6))<6.16666666/24),"T3","N"))))))
So, I have to be a little honest. I had every intent to walk through your formula, figure out why we got different results, and explain what happened. But I couldn't stay focused and my mind started drifting towards more pleasant things like responding to HOA letters, paying taxes, and solving Pi. So I didn't do it.... Moving on!
I believe I understood what you needed. There are four shifts in total, with two shifts per day. Shifts 1 and 3 work Sunday through Tuesday, 1 starts at 06:10 and is relieved at 18:10 when shift 3 starts. It is similar for Shifts 2 and 4, their days are Thursday through Saturday and they have the same hours. Wednesdays alternate between the two groups with 1 and 3 taking Wednesday when the week number is wholly divisible by two. Shifts 2 and 4 take the alternate Wednesday.
You can see my test data in the image below and I believe you'll find the results align with my explanation. You'll note there is a data column to the left of the date and another to the right. This is because I tested two different methods. The results align so I have high confidence in the quality of my work and I remain hopeful that put you in the right direction.
The column to the left of the dates uses a very short formula:
=CoveringShift(B1)
And the column to the right of the dates uses a much longer formula:
=IF(WEEKDAY(B1)<>4,IF(WEEKDAY(B1)<=3,IF(AND(TIME(HOUR(B1),MINUTE(B1),0)>=TIME(6,10,0),TIME(HOUR(B1),MINUTE(B1),0)<TIME(18,10,0)),1,3),IF(AND(TIME(HOUR(B1),MINUTE(B1),0)>=TIME(6,10,0),TIME(HOUR(B1),MINUTE(B1),0)<TIME(18,10,0)),2,4)),IF(ISEVEN(WEEKNUM(B1)),IF(AND(TIME(HOUR(B1),MINUTE(B1),0)>=TIME(6,10,0),TIME(HOUR(B1),MINUTE(B1),0)<TIME(18,10,0)),1,3),IF(AND(TIME(HOUR(B1),MINUTE(B1),0)>=TIME(6,10,0),TIME(HOUR(B1),MINUTE(B1),0)<TIME(18,10,0)),2,4)))
Ok, ok, you caught me. I used VBA to create a User Define Function. This is the code:
Option Explicit
Public Function CoveringShift(TimeQuery As Date) As Single
Dim AM As Date
Dim PM As Date
Dim Shifts As String
Dim iVal As Integer
AM = TimeSerial(6, 10, 0)
PM = TimeSerial(18, 10, 0)
iVal = Application.WorksheetFunction.WeekNum(TimeQuery)
Select Case Weekday(TimeQuery, vbSunday)
Case 1 To 3
Shifts = "1,3"
Case 5 To 7
Shifts = "2,4"
Case 4
If iVal / 2 - Int(iVal / 2) = 0 Then
Shifts = "1,3"
Else
Shifts = "2,4"
End If
Case Else
MsgBox "Something bad happened"
End Select
Select Case TimeValue(TimeQuery)
Case Is < AM
Shifts = Right(Shifts, 1)
Case AM To DateAdd("s", -1, PM)
Shifts = Left(Shifts, 1)
Case Is >= PM
Shifts = Right(Shifts, 1)
Case Else
MsgBox "Something bad happened"
End Select
CoveringShift = CSng(Shifts)
End Function
Personally, I prefer the cleanliness and readability of the UDF; but sometimes VBA's baggage isn't worth carrying.
I hope this helped in some way!

algorithm for a schedule for the four-brigade system

I need to program a shift schedule for three shift in a four-brigade system.
Each shift lasts eight hours.
First shift start at 6 am.
Second shift start at 2 pm.
Third shift start at 10 pm.
Each cycle has four days and 48 hours break.
The table below shows how is the schedule for January 20019.
"W" means a day off.
I would write a function which gets an argument date and number of shift and returns which brigade has a shift.
For example:
getBrigadeNumber('2019-01-27',1); // should return 'III' for schedule above
I completely don't have an idea how it writes.
I"ll write it in VBA, but I know php as well,
I will be grateful for any advice.
At first look the problem seems difficult - algorithm for three-shift schedule, but when you realize that the shifts are some patterns and these patterns doesn't change in time, than the problem starts to look very easy.
The idea to "Keeps first month in an array and when I will be need a specific day. I can count it for loops" will solve the problem. What I want to add is that it is not necessary to use loops. The shifts pattern repeats every 16 days – values for January 11th and January 27th are the same.
For a particular day d you can calculate the shift for each brigade considering the following pseudo-code:
D = The distance between a particular day d and January 1st in days.
A = Schedule for January 2019. A zero indexed array with 4 rows and 31 columns.
S1=A[1][D mod 16] will be the calculated shift number for brigade 1 at day d.
S2=A[2][D mod 16] will be the calculated shift number for brigade 2 at day d.
...
S4=A[4][D mod 16] will be the calculated shift number for brigade 4 at day d.
Knowing S1,S2,S3 and S4 is sufficient for getBrigadeNumber to return the correct value.
You can get this done without VBA, using the MATCH() worksheet function:
Imagine your month January is on row 1. You can use the MATCH() function to find the column where you have the 27th of January:
=MATCH(1;AB$2:AB$5;0)
Which stands for:
Find value 1, in column 'AB', inside the four rows (2 to 5), using exact matching.
Column 'AB' can be found as =OFFSET(A1;0;27).

Including start year and end year in YEAR calculations

For the calculation I'm trying to perform one year = one season and the data I have is start year and end year.
So 1952 1953 needs to add up to 2 (seasons) but if I use =YEAR(A1)-YEAR(A2) the result is 0 - is there a simple way to include the start year and end year as a value in these calculations?
It looks like you have found the problem and rectified it. For the benefit of others who might have found this thread, this is why it was behaving the ways it was.
Background - Excel (and many other applications) treat dates as 1 for every day past Dec 31, 1899. Today happens to be 42,079. Time is a decimal portion of a day so 42,079.75 would be Mar 16, 2015 06:00 PM.
You had the years as numbers in A1:A2; not as full dates. Using the 1-per-day formula, 1952 is May 5, 1905 and 1953 is May 6, 1905. If you peel out the year of each of those with the YEAR() function, you are subtracting 1905 from 1905; resulting in zero.
The solution would be to either type full dates into A1:A2 and format the cells as yyyy so they display 1952 & 1953 but retain their full date nature e.g. =ABS(YEAR(A1) - YEAR(A2)) + 1 , or use the years as numbers only and discard the YEAR() function altogether, e.g. =ABS(A1 - A2) + 1 to get the spanned (inclusive) number of seasons.

Numerical month to character month in excel

Is there a pre-installed function that directly converts 08 to Aug, 10 to Oct?
Currently I use text(date(0,have,1),"mmm").
You could simplify like this: =TEXT("8/0","mmm").
Update
I've come up with a new technique: =TEXT(number * 30,"mmm"):
How it works
Dates are stored as numbers in Excel. The number 1 is the date Jan 1, 1900; 2 is Jan 2, 1900; etc.
The 30th day in 1900 is in January; the 60th day is in February; the 90th day is in March.
Every multiple of 30 between 30 and 360 is in a different month. So we can simply multiply 30 by a number between 1 and 12, and the TEXT function will give us the month.
You can use
MonthName(yourmonthNumber)
or
MonthName(yourmonthNumber, True) 'to abbreviate the name
Or did you mean a worksheet function?

Resources