-1

I have written a Python program that prints out all the dates in yyy-mm-dd format within the year 2020. And now i try to write a program which loops/iterate through the year 2020 and prints out the sum of all the given numbers for each date. For ex: the sum of the date 2020-01-01 is 6 (2+0+2+0+0+1+0+1), the sum of the date 2020-01-02 is 7, etc. My problem is that i do not know how to write a code which takes out those numbers, adding them up and printing out for each date within 2020. I have also seen another post with the headline "Sum the digits of a number" but it did not really help me to solve my problem because the length of some months is not the same as the rest and also not the whole code was obvious.

def date_range(start, end):         #creating a tuple function
    for i in range(int((end - start).days)):
        yield start + timedelta(i)

start = date(2020, 1, 1)
end = date(2020, 12, 31)
for each_date in date_range(start, end):
       print(each_date.strftime("%Y-%m-%d") #iterating and printing all dates within the year of 2020

  • Does this answer your question? [Sum the digits of a number](https://stackoverflow.com/questions/14939953/sum-the-digits-of-a-number) Do this for the `each_date.year`, `.month`, and `.day` attributes of each date in your range – Pranav Hosangadi Nov 29 '22 at 00:12
  • _"the length of some months is not the same as the rest"_: How does that affect anything? _"not the whole code was obvious"_: what part of it are you confused about? – Pranav Hosangadi Nov 29 '22 at 00:36
  • It seemed that my code was more complex. The code just showed a random number and then calculated the sum of it. Mine was range-based including iterating through different dates in a specific range (in this case 2020). I did not either know how to get rid of the "-" sign or change the date into simple numbers. I am new to programming and trying to learn new stuff. From your view, it might seem the same because maybe you are a pro programmer and can link things easily together. – Xerxes Google Nov 29 '22 at 00:51
  • I really don't mean to put you down in any way, and I apologize if my comment seemed to do so. Programming is _modular_. You have one snippet of code that calculates the sum of all digits in a number. It doesn't care what that number is, or where it comes from. Your code to generate a range of dates doesn't care what you do with those dates. So you just need to find the year, month, and day of each date object, and sum the digits of each of them. – Pranav Hosangadi Nov 29 '22 at 00:58
  • Remember that the code that will do this sum cares only about the number you asked it to find this sum for. When you have the sums for the year, month, and day, just add them to get the sum for that date! – Pranav Hosangadi Nov 29 '22 at 00:58

1 Answers1

1
[int(x) for x in "2020-01-02" if x!='-']

is

[2, 0, 2, 0, 0, 1, 0, 2]

So, from there

sum(int(x) for x in "2020-01-02" if x!='-')
# 7

And you can go further

[(ed,sum(int(x) for x in ed.strftime("%Y-%m-%d") if x!='-')) for ed in date_range(start, end)]
# [(datetime.date(2020, 1, 1), 6), (datetime.date(2020, 1, 2), 7), (datetime.date(2020, 1, 3), 8), ..., (datetime.date(2020, 12, 29), 18), (datetime.date(2020, 12, 30), 10)] 

Or to just print them

for ed in date_range(start, end):
    print(ed, sum(int(x) for x in ed.strftime("%Y-%m-%d") if x!='-'))
chrslg
  • 9,023
  • 5
  • 17
  • 31
  • Tnx. But I want the code to loop through the year 2020 (2020-01-01 to 2020-12-30) and prints out the sum of each date separately. Example for an output: the sum of the date 2020-01-01 is 6, the sum of the date 2020-01-02 is 7, .......... the sum of the date 2020-12-30 is 10 – Xerxes Google Nov 28 '22 at 23:55
  • @XerxesGoogle I am pretty sure, from what you were able to do before, that from what I gave you, you could easily have done that. The only missing piece in your code was the `sum(int(x) for x in dte.strftime("%Y-%m-%d") if x!='-')` part. But I edited accordingly. – chrslg Nov 29 '22 at 00:02
  • 1
    You could just `strftime` without the hyphens and remove the need for the `if x != '-'` in the comprehension – Pranav Hosangadi Nov 29 '22 at 00:13
  • @PranavHosangadi. Yeah, I could. Be sure, I thought of that. But since OP used that `strftime`, I opted to keep it. Because, probably, that 2 line code is not all of their code, and maybe in the full code, that strftime result was built and used for several usages. But, yes, indeed, if `strftime` is only an intermediate value just for that sole computation, then, it is simpler, faster and shorter to just `sum(int(x) for x in dte.strftime('%Y%m%d'))` – chrslg Nov 29 '22 at 00:17