+ 1
Any efficient solution for this problem ?
The day corresponding to the first day of a month and a date is given as input. The program must print the day of the date . I/p : MON 10 o/p : WED Exp: If the first day of the month is Monday and the day on 10th of that month is on Wednesday.
8 ответов
+ 8
Mndm , here is a short version of rotating a list. "d" is the number to rotate the items from the iterable "arr". pos number is shift left, neg number is shift right.
# rotate list
def rotate(arr, d):
return arr[d:] + arr[:d]
+ 8
Here is my version: (it uses a list that will be shifted once, so it avoids iterating through date)
day = input()
date = int(input())
days = [ 'MON', 'TUE', 'WED', 'THU', 'FRI', 'SAT', 'SUN']
shift = days.index(day)
arr = days[shift:] + days[:shift]
print(arr[(date % 7) -1])
+ 5
Please show us your attempt first, before we can help you. put your try in playground and link it here. Thanks!
+ 2
I give you hints
1. Make a tuple of 7 elements having Days of Week in sequence (important)
2. Find the First day in tuple and store it's index in a variable , suppose x....
3. Then make a loop that iterate over that tuple starting from index (x) and stop when we reaches the date...
+ 1
Do you have any idea written as some lines of code?
+ 1
Lothar Avalon
https://code.sololearn.com/cY69UFgs3XMX/?ref=app
I have attached the code. But I can't get the output.
Here I iterated through the list 'days'. Any simple code for list rotation ?? Thank you !!
+ 1
day = input().strip()
date = int(input())
days = [ 'MON', 'TUE', 'WED', 'THU', 'FRI', 'SAT', 'SUN']
x = days.index(day)
y = x
for i in range(date):
y+=1
if days[y]=='SUN':
y=0
print(days[y-1])
+ 1
This could help:
https://code.sololearn.com/c9UstEsAJo6v/?ref=app