Repeat items in list for schedule - python-3.x

I’m trying to make a program that tells a shift worker what days he/she is on and what days he/she is off. For example, on 4, off 4.
Here’s my code:
schedule = ['monday','tuesday','wednesday','thursday','friday','saturday','sunday']
on = 4
off = 4
count = 0
while count < 3:
for x in range(on):
print('on: ', schedule[x])
for y in range(on,len(schedule)):
print('off: ', schedule[y])
count = count + 1
This prints:
on: monday
on: tuesday
on: wednesday
on: thursday
off: friday
off: saturday
off: sunday
X3
What I’m trying to get it to do is:
on: monday
on: tuesday
on: wednesday
on: thursday
off: friday
off: saturday
off: sunday
off: monday
on: tuesday
on: wednesday
on: thursday
on: friday
And so forth.

I think you can simply create a dictionary where you will map Key:Value pair as Days and status and then iterate through it.
for example
schedule = {'monday':"on",'tuesday':"on",'wednesday':"on",'thursday':"on",'friday':"off",'saturday':"off",'sunday':"off"}
and then iterate through the Schedule dict
for day, status in schedule.items():
print(f"{day}:{status}")

Related

Need to get output of all weekdays starting from given date

Output is not how it should be
given_day = datetime.date(2022,10,31)
dates = given_day + datetime.timedelta(days=+1)
for i in range(0 - given_day.weekday(), 7 - given_day.weekday()):
print(given_day.strftime("%A"))
Output should start from Monday and end to Sunday
Output I get is:
Monday
Monday
Monday
Monday
Monday
Monday
Monday
Where i made a mistake :(
I can't make head or tail of your code, but the following works:
given_day = datetime.date(2022,10,31)
for i in range(0 + given_day.weekday(), 7):
print(given_day.strftime("%A"))
given_day += datetime.timedelta(days=1)

using week number to get range of dates with sunday as first day of week in python

input: week_number = 34 (or 2022-34)
expected output:
["2022-08-21","2022-08-22", "2022-08-23","2022-08-24","2022-08-25","2022-08-26","2022-08-27"]
First date should be of Sunday
the last date should be Saturday
work with both leap and non leap year
Try:
import datetime
week_number = 34
out = []
date = datetime.datetime.strptime(f"2022-{week_number}-0", "%Y-%U-%w")
for day in range(7):
out.append((date + datetime.timedelta(days=day)).strftime("%Y-%m-%d"))
print(out)
Prints:
[
"2022-08-21",
"2022-08-22",
"2022-08-23",
"2022-08-24",
"2022-08-25",
"2022-08-26",
"2022-08-27",
]
From the reference:
%U - Week number of the year (Sunday as the first day of the week) as
a zero-padded decimal number. All days in a new year preceding the
first Sunday are considered to be in week 0.

moment.js how to set startOf week to monday on current week?

I want to make the date of startOf("week") its start from Monday on current week
Is it possible for make this? My code:
moment().startOf("week").toDate();
This startOf start from saturday proof startOf start from saturday
Take a look at the First Day of Week and First Week of Year section
From the docs (emphasis is mine)
Locale#week.dow should be an integer representing the first day of the week, 0 is Sunday, 1 is Monday, ..., 6 is Saturday.
So just by running this code before doing any calls to moment
// From 2.12.0 onward
moment.updateLocale('en', {
week : {
dow : 1
}
});
It will set the first day of the week to Monday.
After that calling moment().startOf("week").toDate() will give you a Monday.

Get the first Friday of the Quarter of Today's Date

I was trying to get the first Friday of the quarter of today's date or any given date. Let's say the date is 12/06/2020, which falls to the 2nd quarter of the year. I should be able to get the first Friday of the 2nd Quarter and that should be April 3rd.
I was only been able to get the quarter of a given date, which you can see in the code below. Been stuck here for a while. Thanks in advance.
quarter = Int((Month(Now) + 2) / 3)
Here's a function that takes a date and returns the first Friday of the quarter:
Function FirstFridayOfTheQuarter(MyDate As Date) As Date
Dim FirstDayOfTheQuarter As Date
FirstDayOfTheQuarter = DateSerial(Year(MyDate), Int((Month(MyDate) - 1) / 3) * 3 + 1, 1)
FirstFridayOfTheQuarter = DateAdd("d", (13 - Weekday(FirstDayOfTheQuarter)) Mod 7, FirstDayOfTheQuarter)
End Function
This function is taking advantage of the Weekday function that returns:
1 for a Sunday
2 for a Monday
3 for a Tuesday
4 for a Wednesday
5 for a Thursday
6 for a Friday
7 for a Saturday
In case you want a non VBA solution:
My formula is:
=CEILING(EOMONTH(DATE(YEAR(A1);ROUNDUP(MONTH(A1)/3;0)+(ROUNDUP(MONTH(A1)/3;0)-1)*2;1);-1)-5;7)+6

VLOOKUP copies and paste existing value instead of looking up table

I'm having real difficulties with a VLOOKUP formula on an excel spreadsheet. Instead of bringing back the correct values it appears to be copying and pasting the answer from the first cell.
Table 1 is:
uprn Day and Week
10024239501 Friday 2
452061980 Friday 1
452026455 Thursday 1
452010090 Thursday 2
452015382 Wednesday 2
452034567 Wednesday 1
452058333 Tuesday 1
452033532 Tuesday 2
452047401 Monday 1
452060801 Monday 2
table 2 is
uprn letter
10024239501 a
452061980 b
452026455 c
452010090 d
452015382 e
452034567 f
452058333 g
452033532 h
452047401 i
452060801 j
the vlookup formula i've used in table 1, column 3 is:
=VLOOKUP(A1,$F$1:$G$10,2,FALSE)
When I then drag this formula down for the rest of the table it gives me the following:
uprn day and week letter
10024239501 Friday 2 a
452061980 Friday 1 a
452026455 Thursday 1 a
452010090 Thursday 2 a
452015382 Wednesday 2 a
452034567 Wednesday 1 a
452058333 Tuesday 1 a
452033532 Tuesday 2 a
452047401 Monday 1 a
452060801 Monday 2 a
I've made sure that cells are numbers and text in each table, but still it only appears to be working for the top value and then copying this result down for the rest of the table.
What I would expect in a correct VLOOKUP table would be:
uprn day and week letter
10024239501 Friday 2 a
452061980 Friday 1 b
452026455 Thursday 1 c
452010090 Thursday 2 d
452015382 Wednesday 2 e
452034567 Wednesday 1 f
452058333 Tuesday 1 g
452033532 Tuesday 2 h
452047401 Monday 1 i
452060801 Monday 2 j

Resources