I have a list of tuples where each tuple within the list contains 3 elements.

  1. Is the year in 4 digits i.e 2014
  2. Month in integer i.e from 1-12
  3. And another integer value which is the number of months needed to be subtracted.

So if the date is May 2020 and I want to subtract 3 months then the tuple would look like (2020,5,3) . And I need to loop over such tuples to calculate the month after subtracting the integer. So in the above case it would be 5-3 = 2 i.e February 2020. Any idea how to create a function to do this?

Recommended Answers

What have you tried so far?

What you have to keep in mind is that if the number of months you are subtracting is greater than the number of months in the given date you will have to subtract from the given year. For example, if your tuple is

       
Jump to Post

All 2 Replies

What have you tried so far?

What you have to keep in mind is that if the number of months you are subtracting is greater than the number of months in the given date you will have to subtract from the given year. For example, if your tuple is

(2020, 5, 9)

your result will be

(2019, 8)

and if your tuple is

(2020, 5, 21)

your result will be

(2018, 8)

If you write out a few examples your steps should be clear.

Here's a hint. You will be using the modulus operator (%) as well as integer divide (//). The general solution will not require loops or if statements.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.