|
Post by David Drake on Mar 31, 2020 14:09:45 GMT -5
I created this code as a response to the Rosetta Code task: Palindrome dates (http://rosettacode.org/wiki/Palindrome_dates). I created it from whole cloth (meaning I didn't borrow from other languages' solutions).
I look forward to seeing how it can be made better before posting.
'Write a program which calculates and shows the next 15 palindromic dates for those countries which express their dates in the yyyy-mm-dd format.
t=date$("days") for a=t to t+99000 dT$=date$(a) dT$=replstr$(dT$,"/","") lT$=left$(dT$,4) rT$=i$(right$(dT$,4)) if lT$=rT$ then print "Palidromic date: ";dT$ fC=fC+1 if fC=15 then exit for end if next a print "Done" end
function i$(a$) for b=4 to 1 step -1 i$=i$+mid$(a$,b,1) next b end function
|
|
|
Post by Brandon Parker on Apr 3, 2020 22:36:20 GMT -5
I would do it this way (also, programmed without looking at anything else) ... Since we know the year, we already have its palindrome month/day combo, so we just have to validate the date. The datePalindrome() function could be used to check that the Palindrome is correct, but it should not be needed unless you want to go beyond the year 9999. After 9999, with the check against datePalindrome() uncommented, the code does continue to work, but it takes a few seconds to find the next one because it is way out there. The code takes just 3 seconds to find the first 285, but then takes an additional 13 seconds to reach the next one.
Assuming the year only goes up to 9999 here are two options with the check against datePalindrome() commented out. In included one for "yyyy-mm-dd" and one for "mm/dd/yyyy" This should be much faster ...
Here is the code for "yyyy-mm-dd" (the one for the Rosetta Code task) ...
numDates = 15 currentYear$ = Word$(Date$("yyyy/mm/dd"), 1, "/")
reversedYear$ = reverseString$(currentYear$)
While (capturedDates < numDates) Scan myDate$ = makeDate$(reversedYear$, currentYear$) If validateFutureDate(myDate$) Then If datePalindrome(myDate$) Then capturedDates = (capturedDates + 1) Print "Date #";capturedDates;" - ";myDate$ End If End If currentYear$ = str$(Val(currentYear$) + 1) reversedYear$ = reverseString$(currentYear$) Wend
Print "Complete!"
Function reverseString$(string$) For i = Len(string$) To 1 Step -1 reverseString$ = reverseString$ + Mid$(string$, i, 1) Next i End Function
Function makeDate$(revYear$, currentYear$) makeDate$ = currentYear$;"-";Mid$(Right$(revYear$, 4), 1, 2);"-";Right$(revYear$, 2) End Function
Function validateFutureDate(date$) date$ = Word$(date$, 2, "-");"/";Word$(date$, 3, "-");"/";Word$(date$, 1, "-") validateFutureDate = ((Date$(ReplStr$(date$, "-", "/")) >= Date$("Days")) And (Date$("Days") > 0)) End Function
Function datePalindrome(date$) date$ = ReplStr$(date$, "-", "") datePalindrome = date$ = reverseString$(date$) End Function
Here is the code for "mm/dd/yyyy" ...
numDates = 15 currentYear$ = Word$(Date$("mm/dd/yyyy"), 3, "/")
reversedYear$ = reverseString$(currentYear$)
While (capturedDates < numDates) Scan myDate$ = makeDate$(reversedYear$, currentYear$) If validateFutureDate(myDate$) Then If datePalindrome(myDate$) Then capturedDates = (capturedDates + 1) Print "Date #";capturedDates;" - ";myDate$ End If End If currentYear$ = str$(Val(currentYear$) + 1) reversedYear$ = reverseString$(currentYear$) Wend
Print "Complete!"
Function reverseString$(string$) For i = Len(string$) To 1 Step -1 reverseString$ = reverseString$ + Mid$(string$, i, 1) Next i End Function
Function makeDate$(revYear$, currentYear$) makeDate$ = Mid$(revYear$, 1, 2);"/";Mid$(revYear$, 3, 2);"/";currentYear$ End Function
Function validateFutureDate(date$) validateFutureDate = ((Date$(date$) >= Date$("Days")) And (Date$("Days") > 0)) End Function
Function datePalindrome(date$) date$ = ReplStr$(date$, "/", "") datePalindrome = date$ = reverseString$(date$) End Function
Here is my run with the check against datePalindrome() uncommented trying to get to 300 dates.
Output to #289 ...
Date #1 - 2021-12-02
Date #2 - 2030-03-02
Date #3 - 2040-04-02
Date #4 - 2050-05-02
Date #5 - 2060-06-02
Date #6 - 2070-07-02
Date #7 - 2080-08-02
Date #8 - 2090-09-02
Date #9 - 2101-10-12
Date #10 - 2110-01-12
Date #11 - 2111-11-12
Date #12 - 2120-02-12
Date #13 - 2121-12-12
Date #14 - 2130-03-12
Date #15 - 2140-04-12
Date #16 - 2150-05-12
Date #17 - 2160-06-12
Date #18 - 2170-07-12
Date #19 - 2180-08-12
Date #20 - 2190-09-12
Date #21 - 2201-10-22
Date #22 - 2210-01-22
Date #23 - 2211-11-22
Date #24 - 2220-02-22
Date #25 - 2221-12-22
Date #26 - 2230-03-22
Date #27 - 2240-04-22
Date #28 - 2250-05-22
Date #29 - 2260-06-22
Date #30 - 2270-07-22
Date #31 - 2280-08-22
Date #32 - 2290-09-22
Date #33 - 3001-10-03
Date #34 - 3010-01-03
Date #35 - 3011-11-03
Date #36 - 3020-02-03
Date #37 - 3021-12-03
Date #38 - 3030-03-03
Date #39 - 3040-04-03
Date #40 - 3050-05-03
Date #41 - 3060-06-03
Date #42 - 3070-07-03
Date #43 - 3080-08-03
Date #44 - 3090-09-03
Date #45 - 3101-10-13
Date #46 - 3110-01-13
Date #47 - 3111-11-13
Date #48 - 3120-02-13
Date #49 - 3121-12-13
Date #50 - 3130-03-13
Date #51 - 3140-04-13
Date #52 - 3150-05-13
Date #53 - 3160-06-13
Date #54 - 3170-07-13
Date #55 - 3180-08-13
Date #56 - 3190-09-13
Date #57 - 3201-10-23
Date #58 - 3210-01-23
Date #59 - 3211-11-23
Date #60 - 3220-02-23
Date #61 - 3221-12-23
Date #62 - 3230-03-23
Date #63 - 3240-04-23
Date #64 - 3250-05-23
Date #65 - 3260-06-23
Date #66 - 3270-07-23
Date #67 - 3280-08-23
Date #68 - 3290-09-23
Date #69 - 4001-10-04
Date #70 - 4010-01-04
Date #71 - 4011-11-04
Date #72 - 4020-02-04
Date #73 - 4021-12-04
Date #74 - 4030-03-04
Date #75 - 4040-04-04
Date #76 - 4050-05-04
Date #77 - 4060-06-04
Date #78 - 4070-07-04
Date #79 - 4080-08-04
Date #80 - 4090-09-04
Date #81 - 4101-10-14
Date #82 - 4110-01-14
Date #83 - 4111-11-14
Date #84 - 4120-02-14
Date #85 - 4121-12-14
Date #86 - 4130-03-14
Date #87 - 4140-04-14
Date #88 - 4150-05-14
Date #89 - 4160-06-14
Date #90 - 4170-07-14
Date #91 - 4180-08-14
Date #92 - 4190-09-14
Date #93 - 4201-10-24
Date #94 - 4210-01-24
Date #95 - 4211-11-24
Date #96 - 4220-02-24
Date #97 - 4221-12-24
Date #98 - 4230-03-24
Date #99 - 4240-04-24
Date #100 - 4250-05-24
Date #101 - 4260-06-24
Date #102 - 4270-07-24
Date #103 - 4280-08-24
Date #104 - 4290-09-24
Date #105 - 5001-10-05
Date #106 - 5010-01-05
Date #107 - 5011-11-05
Date #108 - 5020-02-05
Date #109 - 5021-12-05
Date #110 - 5030-03-05
Date #111 - 5040-04-05
Date #112 - 5050-05-05
Date #113 - 5060-06-05
Date #114 - 5070-07-05
Date #115 - 5080-08-05
Date #116 - 5090-09-05
Date #117 - 5101-10-15
Date #118 - 5110-01-15
Date #119 - 5111-11-15
Date #120 - 5120-02-15
Date #121 - 5121-12-15
Date #122 - 5130-03-15
Date #123 - 5140-04-15
Date #124 - 5150-05-15
Date #125 - 5160-06-15
Date #126 - 5170-07-15
Date #127 - 5180-08-15
Date #128 - 5190-09-15
Date #129 - 5201-10-25
Date #130 - 5210-01-25
Date #131 - 5211-11-25
Date #132 - 5220-02-25
Date #133 - 5221-12-25
Date #134 - 5230-03-25
Date #135 - 5240-04-25
Date #136 - 5250-05-25
Date #137 - 5260-06-25
Date #138 - 5270-07-25
Date #139 - 5280-08-25
Date #140 - 5290-09-25
Date #141 - 6001-10-06
Date #142 - 6010-01-06
Date #143 - 6011-11-06
Date #144 - 6020-02-06
Date #145 - 6021-12-06
Date #146 - 6030-03-06
Date #147 - 6040-04-06
Date #148 - 6050-05-06
Date #149 - 6060-06-06
Date #150 - 6070-07-06
Date #151 - 6080-08-06
Date #152 - 6090-09-06
Date #153 - 6101-10-16
Date #154 - 6110-01-16
Date #155 - 6111-11-16
Date #156 - 6120-02-16
Date #157 - 6121-12-16
Date #158 - 6130-03-16
Date #159 - 6140-04-16
Date #160 - 6150-05-16
Date #161 - 6160-06-16
Date #162 - 6170-07-16
Date #163 - 6180-08-16
Date #164 - 6190-09-16
Date #165 - 6201-10-26
Date #166 - 6210-01-26
Date #167 - 6211-11-26
Date #168 - 6220-02-26
Date #169 - 6221-12-26
Date #170 - 6230-03-26
Date #171 - 6240-04-26
Date #172 - 6250-05-26
Date #173 - 6260-06-26
Date #174 - 6270-07-26
Date #175 - 6280-08-26
Date #176 - 6290-09-26
Date #177 - 7001-10-07
Date #178 - 7010-01-07
Date #179 - 7011-11-07
Date #180 - 7020-02-07
Date #181 - 7021-12-07
Date #182 - 7030-03-07
Date #183 - 7040-04-07
Date #184 - 7050-05-07
Date #185 - 7060-06-07
Date #186 - 7070-07-07
Date #187 - 7080-08-07
Date #188 - 7090-09-07
Date #189 - 7101-10-17
Date #190 - 7110-01-17
Date #191 - 7111-11-17
Date #192 - 7120-02-17
Date #193 - 7121-12-17
Date #194 - 7130-03-17
Date #195 - 7140-04-17
Date #196 - 7150-05-17
Date #197 - 7160-06-17
Date #198 - 7170-07-17
Date #199 - 7180-08-17
Date #200 - 7190-09-17
Date #201 - 7201-10-27
Date #202 - 7210-01-27
Date #203 - 7211-11-27
Date #204 - 7220-02-27
Date #205 - 7221-12-27
Date #206 - 7230-03-27
Date #207 - 7240-04-27
Date #208 - 7250-05-27
Date #209 - 7260-06-27
Date #210 - 7270-07-27
Date #211 - 7280-08-27
Date #212 - 7290-09-27
Date #213 - 8001-10-08
Date #214 - 8010-01-08
Date #215 - 8011-11-08
Date #216 - 8020-02-08
Date #217 - 8021-12-08
Date #218 - 8030-03-08
Date #219 - 8040-04-08
Date #220 - 8050-05-08
Date #221 - 8060-06-08
Date #222 - 8070-07-08
Date #223 - 8080-08-08
Date #224 - 8090-09-08
Date #225 - 8101-10-18
Date #226 - 8110-01-18
Date #227 - 8111-11-18
Date #228 - 8120-02-18
Date #229 - 8121-12-18
Date #230 - 8130-03-18
Date #231 - 8140-04-18
Date #232 - 8150-05-18
Date #233 - 8160-06-18
Date #234 - 8170-07-18
Date #235 - 8180-08-18
Date #236 - 8190-09-18
Date #237 - 8201-10-28
Date #238 - 8210-01-28
Date #239 - 8211-11-28
Date #240 - 8220-02-28
Date #241 - 8221-12-28
Date #242 - 8230-03-28
Date #243 - 8240-04-28
Date #244 - 8250-05-28
Date #245 - 8260-06-28
Date #246 - 8270-07-28
Date #247 - 8280-08-28
Date #248 - 8290-09-28
Date #249 - 9001-10-09
Date #250 - 9010-01-09
Date #251 - 9011-11-09
Date #252 - 9020-02-09
Date #253 - 9021-12-09
Date #254 - 9030-03-09
Date #255 - 9040-04-09
Date #256 - 9050-05-09
Date #257 - 9060-06-09
Date #258 - 9070-07-09
Date #259 - 9080-08-09
Date #260 - 9090-09-09
Date #261 - 9101-10-19
Date #262 - 9110-01-19
Date #263 - 9111-11-19
Date #264 - 9120-02-19
Date #265 - 9121-12-19
Date #266 - 9130-03-19
Date #267 - 9140-04-19
Date #268 - 9150-05-19
Date #269 - 9160-06-19
Date #270 - 9170-07-19
Date #271 - 9180-08-19
Date #272 - 9190-09-19
Date #273 - 9201-10-29
Date #274 - 9210-01-29
Date #275 - 9211-11-29
Date #276 - 9220-02-29
Date #277 - 9221-12-29
Date #278 - 9230-03-29
Date #279 - 9240-04-29
Date #280 - 9250-05-29
Date #281 - 9260-06-29 Date #282 - 9270-07-29
Date #283 - 9280-08-29
Date #284 - 9290-09-29
Date #285 - 11111-11-11
Date #286 - 111111-11-11
Date #287 - 1101101-10-11
Date #288 - 1111111-11-11
Date #289 - 1121121-12-11
I wonder what it will get to if I let it run through the night ... ?
{:0)
Brandon Parker
|
|
|
Post by tsh73 on Apr 4, 2020 6:41:42 GMT -5
Date #284 - 9290-09-29
Date #285 - 11111-11-11
Shouldn't 11110-11-11 happen somewhere in between?
|
|
|
Post by Brandon Parker on Apr 4, 2020 8:06:02 GMT -5
Yeah, there's something wrong with the yyyy-mm-dd format. I checked the other format, which is the first one I programmed and it spit out many more dates that were correct, making it to #300 in just a few seconds. Of course, the Palindromes would be different there due to the differing order in the date configuration.
I will look at it and see if I can figure out where that one went wrong. I will update it after I resolve the issue.
{:0)
Brandon Parker
|
|
|
Post by Brandon Parker on Apr 4, 2020 8:26:45 GMT -5
I believe the problem should be resolved now; code updated above. The issue was in the following function; it was not making the date correctly. It should have been grabbing the last two for the day and the next two for the month, as it is now.
Function makeDate$(revYear$, currentYear$) makeDate$ = currentYear$;"-";Mid$(Right$(revYear$, 4), 1, 2);"-";Right$(revYear$, 2) End Function
Just an FYI, the date "11110-11-11" is now #425 in the list, so you will have to change the variable "numDates" to get there if you wish. Also since it seems to be correct (or what I think is correct) now, it seems to scream through the dates up to 3000 in no time at all.
I also uncommented the checks against the datePalindrome() function as well for good measure.
{:0)
Brandon Parker
|
|