Python Code for Primevilla (TCS Codevita) | PrepInsta


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.


2 <= n <= 10 ^ 10


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 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)


Examples :

Example 1


20211201 Sun 100


Yes 67


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


20211201 Wed 100


No 0


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


19470815 Sat 150


No 197


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”.