Python Code for Primevilla (TCS Codevita) | PrepInsta
Primevilla
TCS CodeVita is a coding competition organized by TCS every year, in search of world’s best coder. This is a global level coding competition in which coders from all around the world compete for the title of World’s Best Coder. Primevilla is one of the sample problem of this year TCS CodeVita season 11 competition.
Question -: It rains only on certain days in Primevilla. It is a rainy day when the number of days since a certain date is prime as well as the month is prime (i.e., month is one of Feb, Mar, May, Jul, Nov). Primevilla follows same calendar that we use.
Given the date d, identify if it would ever rain on a given weekday w within the next given n days. Also calculate the number of days r (where r > 0) after which it would rain within the next n days.
Constraints
2 <= n <= 10 ^ 10
Input
Input consists of three space delimited parts viz. <Date, DOW, n> where
- Date is of the format yyyymmdd
- DOW is Date of the Week in Ddd format
- n is a natural number (where n>=2)
Output
Output would be one of the three formats
- If it would rain on Ddd, r days after d within the next n days, print ‘Yes r’
- Else if p is the least prime > n such that it would rain on Ddd after p days, print ‘No p’
- Else print ‘No 0’, if it would never rain on Ddd.
Time Limit (secs)
1
Examples :
Example 1
Input
20211201 Sun 100
Output
Yes 67
Explanation
Starting from 20211201 we start checking for prime number days whether it would rain. The process will be as depicted below
20211201+2 is 20211203 Fri, month 12 is not prime —> It would not rain
20211201+3 20211204 Sat, month 12 is not prime —> It would not rain
..
20211201+31 is 20220101 Sat, month 1 is not prime —> It would not rain
..
20211201+67 is 20220206 Sun, month 2 is prime —> It would rain
Hence it could rain on a Sunday 67 days after the given date and within the given 100 days. Hence, output is “Yes 67”
Example 2
Input
20211201 Wed 100
Output
No 0
Explanation
The given date 20211201 is itself a Wednesday.
20211201+7 is 20211208 Wed, month 12 is not prime —> It would not rain
Also, any future Wednesday would be a multiple of 7 and hence non-prime days later than 20211201. So, it would never rain on future Wednesdays. Hence, output is “No 0”
Example 3
Input
19470815 Sat 150
Output
No 197
Explanation
If similar computation as depicted in prior examples is carried out one can figure out that it will rain on Sat, 28 Feb 1948 which is 197 days from 15 Aug 1947. However, since 197 is greater than 150 output is “No 197”.
from datetime import timedelta as t from datetime import datetime as d def gi(h): for k in range(2, h + 1): if pr_fun(k): pre.append(k) def pr_fun(h): if h <= 1: return False for k in range(2, h): if h % k == 0: return False return True dye, dwk, h = input().split() h = int(h) pre = [] gi(365) dadic = {0: "Mon", 1: "Tue", 2: "Wed", 3: "Thu", 4: "Fri", 5: "Sat", 6: "Sun"} dye = d.strptime(dye, "%Y%m%d") das = -1 for k in pre: dte = dye + t(k) if pr_fun(dte.month) and dadic[dte.weekday()] == dwk: das = k break if das == -1: print("No", 0, end="") elif das <= h: print("Yes", das, end="") else: print("No", das, end="")