Advertisement
acclivity

pyJamesBond-Explained

Apr 16th, 2021
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.82 KB | None | 0 0
  1. # Sean Connery, who played James Bond, 007, died on 31-10-20
  2. # The sum of the digits of that date is 007
  3.  
  4. # How many dates are there in 2020 that total 007 in this way?
  5.  
  6. # Mike Kerry - 15-April-2021
  7.  
  8. mm, dd, ctr = 1, 1, 0
  9. while mm < 13:
  10.     tot = 2         # Start a total containing 2 (as we are only dealing with year "02"
  11.     # combine month and day into one integer e.g. month 12 day 15 becomes 1215
  12.     x = mm * 100 + dd
  13.     # Write a loop which continues until x becomes zero, processing 1 digit at a time
  14.     while x:
  15.         tot += x % 10       # get remainder of x divided by 10 and add it to tot.
  16.                             # This will be the junior digit (5, then 1, then 2, then 1)
  17.  
  18.         x //= 10            # Floor-Divide x by 10. This drops the junior digit
  19.                             # so, in our example, 1215 becomes 121
  20.  
  21.     if tot == 7:            # If the total of all digits is 7 ...
  22.         ctr += 1            # ... add 1 to our counter of matching dates
  23.         # and print the counter and the date. I use zfill to get leading zeroes
  24.         print(ctr, str(dd).zfill(2) + "-" + str(mm).zfill(2) + "-20")
  25.  
  26.  
  27.     # Line 44 below is an algorithm I developed a few years ago for getting
  28.     # the number of days per month.    I think it's quite fun
  29.     # I had noticed that odd numbered months from Jan to Jly have 31 days
  30.     # But even numbered months from Aug to Dec have 31 days
  31.  
  32.     # Explanation: Start with (mm > 7)  This is an expression that is either True or False
  33.     # So if mm is August thru December, the expression evaluates to 1 (True)
  34.     # and for months Jan thru July, it evaluates to zero (False)
  35.  
  36.     # Then the 1 or zero is added to mm so Jan to Jly stay as 1 to 7
  37.     # and Aug to Dec become 9 to 13. Now it's all the odd numbers that have 31 days.
  38.  
  39.     # Then we use a bitwise AND (&) to determine if the number from above is odd or even
  40.     # This gives 1 for odd numbers, zero for even
  41.     # So Jan, Mar, May, Jly, Aug, Oct, Dec give a 1, the other months give zero
  42.     # The resultant 1 or zero is added to 30, giving us the correct number of days per month
  43.     # (Apart from Feb which is then handled discretely)
  44.     n = 30 + (mm + (mm > 7) & 1)
  45.  
  46.     if mm == 2:     # Special case for February
  47.         n = 29      # We are only dealing with 2020, which is a leap year
  48.  
  49.     dd += 1         # Bump day by 1
  50.     if dd > n:      # If beyond month end ...
  51.         mm += 1     # ... go to next month
  52.         dd = 1      # ... and day 1
  53.  
  54. # Output:-
  55. # 1 04-01-20
  56. # 2 13-01-20
  57. # 3 22-01-20
  58. # 4 31-01-20
  59. # 5 03-02-20
  60. # 6 12-02-20
  61. # 7 21-02-20
  62. # 8 02-03-20
  63. # 9 11-03-20
  64. # 10 20-03-20
  65. # 11 01-04-20
  66. # 12 10-04-20
  67. # 13 04-10-20
  68. # 14 13-10-20
  69. # 15 22-10-20
  70. # 16 31-10-20
  71. # 17 03-11-20
  72. # 18 12-11-20
  73. # 19 21-11-20
  74. # 20 30-11-20
  75. # 21 02-12-20
  76. # 22 11-12-20
  77. # 23 20-12-20
  78.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement