Palindrome dates: Difference between revisions
Content added Content deleted
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
|||
Line 10: | Line 10: | ||
{{trans|Java}} |
{{trans|Java}} |
||
< |
<syntaxhighlight lang="11l">V date = Time(2020, 2, 3) |
||
print(‘First 15 palindrome dates after 2020-02-02 are:’) |
print(‘First 15 palindrome dates after 2020-02-02 are:’) |
||
V count = 0 |
V count = 0 |
||
Line 18: | Line 18: | ||
print(‘date = ’date.format(‘YYYY-MM-DD’)) |
print(‘date = ’date.format(‘YYYY-MM-DD’)) |
||
count++ |
count++ |
||
date += TimeDelta(days' 1)</ |
date += TimeDelta(days' 1)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 41: | Line 41: | ||
=={{header|Action!}}== |
=={{header|Action!}}== |
||
< |
<syntaxhighlight lang="action!">TYPE Date=[ |
||
INT year |
INT year |
||
BYTE month |
BYTE month |
||
Line 131: | Line 131: | ||
NextDay(d) |
NextDay(d) |
||
OD |
OD |
||
RETURN</ |
RETURN</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Palindrome_dates.png Screenshot from Atari 8-bit computer] |
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Palindrome_dates.png Screenshot from Atari 8-bit computer] |
||
Line 155: | Line 155: | ||
{{works with|ALGOL 68G|Any - tested with release 2.8.3.win32}} |
{{works with|ALGOL 68G|Any - tested with release 2.8.3.win32}} |
||
Uses the Algol 68G local time routine which is non-standard. |
Uses the Algol 68G local time routine which is non-standard. |
||
< |
<syntaxhighlight lang="algol68">BEGIN # print future palindromic dates # |
||
# a palindromic date must be of the form demn-nm-ed # |
# a palindromic date must be of the form demn-nm-ed # |
||
# returns a string representation of n with at least 2 digits # |
# returns a string representation of n with at least 2 digits # |
||
Line 211: | Line 211: | ||
OD |
OD |
||
OD |
OD |
||
END</ |
END</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 232: | Line 232: | ||
=={{header|Ada}}== |
=={{header|Ada}}== |
||
< |
<syntaxhighlight lang="ada">with Ada.Text_IO; |
||
with Ada.Calendar.Formatting; |
with Ada.Calendar.Formatting; |
||
with Ada.Calendar.Arithmetic; |
with Ada.Calendar.Arithmetic; |
||
Line 265: | Line 265: | ||
Date := Date + 1; |
Date := Date + 1; |
||
end loop; |
end loop; |
||
end Palindrome_Dates;</ |
end Palindrome_Dates;</syntaxhighlight> |
||
=={{header|AppleScript}}== |
=={{header|AppleScript}}== |
||
===Procedural=== |
===Procedural=== |
||
< |
<syntaxhighlight lang="applescript">on palindromeDates(startYear, targetNumber) |
||
script o |
script o |
||
property output : {} |
property output : {} |
||
Line 300: | Line 300: | ||
end palindromeDates |
end palindromeDates |
||
palindromeDates(2021, 15)</ |
palindromeDates(2021, 15)</syntaxhighlight> |
||
{{output}} |
{{output}} |
||
Line 307: | Line 307: | ||
===Functional=== |
===Functional=== |
||
< |
<syntaxhighlight lang="applescript">use AppleScript version "2.4" |
||
use framework "Foundation" |
use framework "Foundation" |
||
use scripting additions |
use scripting additions |
||
Line 423: | Line 423: | ||
set my text item delimiters to dlm |
set my text item delimiters to dlm |
||
str |
str |
||
end unlines</ |
end unlines</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
<pre>Count of palindromic dates [2021..9999]: 284 |
<pre>Count of palindromic dates [2021..9999]: 284 |
||
Line 462: | Line 462: | ||
=={{header|AutoHotkey}}== |
=={{header|AutoHotkey}}== |
||
< |
<syntaxhighlight lang="autohotkey">date := 20200202 |
||
counter := 0 |
counter := 0 |
||
Line 483: | Line 483: | ||
output := v output |
output := v output |
||
return output |
return output |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 504: | Line 504: | ||
=={{header|AWK}}== |
=={{header|AWK}}== |
||
<syntaxhighlight lang="awk"> |
|||
<lang AWK> |
|||
# syntax: GAWK -f PALINDROME_DATES.AWK |
# syntax: GAWK -f PALINDROME_DATES.AWK |
||
BEGIN { |
BEGIN { |
||
Line 537: | Line 537: | ||
return(rts) |
return(rts) |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 563: | Line 563: | ||
{{works with|QBasic}} |
{{works with|QBasic}} |
||
{{trans|FreeBASIC}} |
{{trans|FreeBASIC}} |
||
< |
<syntaxhighlight lang="basic"> |
||
dateTest$ = "" |
dateTest$ = "" |
||
total = 0 |
total = 0 |
||
Line 595: | Line 595: | ||
NEXT anno |
NEXT anno |
||
END |
END |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 603: | Line 603: | ||
==={{header|BASIC256}}=== |
==={{header|BASIC256}}=== |
||
{{trans|FreeBASIC}} |
{{trans|FreeBASIC}} |
||
<syntaxhighlight lang="lb"> |
|||
<lang lb> |
|||
dateTest = "" |
dateTest = "" |
||
mes = 0 : dia = 0 : anno = 0 : Pal = 0 |
mes = 0 : dia = 0 : anno = 0 : Pal = 0 |
||
Line 633: | Line 633: | ||
next anno |
next anno |
||
end |
end |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 641: | Line 641: | ||
=={{header|BBC BASIC}}== |
=={{header|BBC BASIC}}== |
||
{{works with|BBC BASIC for Windows}} |
{{works with|BBC BASIC for Windows}} |
||
< |
<syntaxhighlight lang="bbcbasic"> INSTALL @lib$ + "DATELIB" |
||
DIM B% 8 |
DIM B% 8 |
||
TestDate%=FN_today |
TestDate%=FN_today |
||
Line 652: | Line 652: | ||
TestDate%+=1 |
TestDate%+=1 |
||
UNTIL VPOS=15 |
UNTIL VPOS=15 |
||
END</ |
END</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 674: | Line 674: | ||
=={{header|C}}== |
=={{header|C}}== |
||
This only works if time_t is a 64-bit type. |
This only works if time_t is a 64-bit type. |
||
< |
<syntaxhighlight lang="c">#include <stdbool.h> |
||
#include <stdio.h> |
#include <stdio.h> |
||
#include <string.h> |
#include <string.h> |
||
Line 704: | Line 704: | ||
} |
} |
||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 727: | Line 727: | ||
=={{header|C sharp}}== |
=={{header|C sharp}}== |
||
< |
<syntaxhighlight lang="csharp">using System; |
||
using System.Linq; |
using System.Linq; |
||
using System.Collections.Generic; |
using System.Collections.Generic; |
||
Line 748: | Line 748: | ||
bool IsValidDate(int y, int m, int d, out DateTime date) => DateTime.TryParse($"{y}-{m}-{d}", out date); |
bool IsValidDate(int y, int m, int d, out DateTime date) => DateTime.TryParse($"{y}-{m}-{d}", out date); |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 769: | Line 769: | ||
=={{header|C++}}== |
=={{header|C++}}== |
||
{{libheader|Boost}} |
{{libheader|Boost}} |
||
< |
<syntaxhighlight lang="cpp">#include <iostream> |
||
#include <string> |
#include <string> |
||
#include <boost/date_time/gregorian/gregorian.hpp> |
#include <boost/date_time/gregorian/gregorian.hpp> |
||
Line 797: | Line 797: | ||
} |
} |
||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 820: | Line 820: | ||
=={{header|Clojure}}== |
=={{header|Clojure}}== |
||
<syntaxhighlight lang="clojure"> |
|||
<lang Clojure> |
|||
(defn valid-date? [[y m d]] |
(defn valid-date? [[y m d]] |
||
(and (<= 1 m 12) |
(and (<= 1 m 12) |
||
Line 838: | Line 838: | ||
(map date-str) |
(map date-str) |
||
(take n))) |
(take n))) |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
Line 846: | Line 846: | ||
=={{header|F_Sharp|F#}}== |
=={{header|F_Sharp|F#}}== |
||
< |
<syntaxhighlight lang="fsharp">// palindrome_dates.fsx |
||
open System |
open System |
||
Line 875: | Line 875: | ||
|> Seq.take 15 |
|> Seq.take 15 |
||
|> Seq.iter print_date |
|> Seq.iter print_date |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre>> dotnet fsi palindrome_dates.fsx |
<pre>> dotnet fsi palindrome_dates.fsx |
||
Line 898: | Line 898: | ||
A simple brute force solution that repeatedly increments a timestamp's day by one and checks whether it's a palindrome: |
A simple brute force solution that repeatedly increments a timestamp's day by one and checks whether it's a palindrome: |
||
{{works with|Factor|0.99 2020-01-23}} |
{{works with|Factor|0.99 2020-01-23}} |
||
< |
<syntaxhighlight lang="factor">USING: calendar calendar.format io kernel lists lists.lazy |
||
sequences sets ; |
sequences sets ; |
||
Line 906: | Line 906: | ||
[ "-" without dup reverse = ] lfilter ; |
[ "-" without dup reverse = ] lfilter ; |
||
15 palindrome-dates ltake [ print ] leach</ |
15 palindrome-dates ltake [ print ] leach</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 928: | Line 928: | ||
A faster version that directly generates palindromic numbers such as <tt>20200202</tt> and keeps those which are valid dates: |
A faster version that directly generates palindromic numbers such as <tt>20200202</tt> and keeps those which are valid dates: |
||
{{works with|Factor|0.99 2020-01-23}} |
{{works with|Factor|0.99 2020-01-23}} |
||
< |
<syntaxhighlight lang="factor">USING: calendar calendar.format continuations io kernel lists |
||
lists.lazy math math.functions math.parser math.ranges sequences ; |
lists.lazy math math.functions math.parser math.ranges sequences ; |
||
Line 950: | Line 950: | ||
"10,000th palindrome date after 2020-02-02: " write |
"10,000th palindrome date after 2020-02-02: " write |
||
10,000 palindrome-dates lnth timestamp>ymd print</ |
10,000 palindrome-dates lnth timestamp>ymd print</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 958: | Line 958: | ||
=={{header|FreeBASIC}}== |
=={{header|FreeBASIC}}== |
||
< |
<syntaxhighlight lang="freebasic"> |
||
Dim As String dateTest = "" |
Dim As String dateTest = "" |
||
Dim As Integer Pal =0, total = 0 |
Dim As Integer Pal =0, total = 0 |
||
Line 987: | Line 987: | ||
Next anno |
Next anno |
||
Sleep |
Sleep |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,011: | Line 1,011: | ||
=={{header|Go}}== |
=={{header|Go}}== |
||
Simple brute force as speed is not an issue here. |
Simple brute force as speed is not an issue here. |
||
< |
<syntaxhighlight lang="go">package main |
||
import ( |
import ( |
||
Line 1,043: | Line 1,043: | ||
} |
} |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,064: | Line 1,064: | ||
</pre> |
</pre> |
||
Or, a more ambitious version. |
Or, a more ambitious version. |
||
< |
<syntaxhighlight lang="go">package main |
||
import ( |
import ( |
||
Line 1,125: | Line 1,125: | ||
} |
} |
||
fmt.Println() |
fmt.Println() |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,145: | Line 1,145: | ||
=={{header|Haskell}}== |
=={{header|Haskell}}== |
||
< |
<syntaxhighlight lang="haskell">import Data.Time.Calendar (Day, fromGregorianValid) |
||
import Data.List.Split (chunksOf) |
import Data.List.Split (chunksOf) |
||
import Data.List (unfoldr) |
import Data.List (unfoldr) |
||
Line 1,174: | Line 1,174: | ||
mapM_ print $ take 15 palinDates |
mapM_ print $ take 15 palinDates |
||
putStrLn "\nLast 15:" |
putStrLn "\nLast 15:" |
||
mapM_ print $ take 15 (drop (n - 15) palinDates)</ |
mapM_ print $ take 15 (drop (n - 15) palinDates)</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
<pre>Count of palindromic dates [2021..9999]: 284 |
<pre>Count of palindromic dates [2021..9999]: 284 |
||
Line 1,213: | Line 1,213: | ||
=={{header|Java}}== |
=={{header|Java}}== |
||
< |
<syntaxhighlight lang="java"> |
||
import java.time.LocalDate; |
import java.time.LocalDate; |
||
import java.time.format.DateTimeFormatter; |
import java.time.format.DateTimeFormatter; |
||
Line 1,234: | Line 1,234: | ||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{Out}} |
{{Out}} |
||
<pre> |
<pre> |
||
Line 1,257: | Line 1,257: | ||
=={{header|Javascript}}== |
=={{header|Javascript}}== |
||
===Procedural=== |
===Procedural=== |
||
< |
<syntaxhighlight lang="javascript">/** |
||
* Adds zeros for 1 digit days/months |
* Adds zeros for 1 digit days/months |
||
* @param date: string |
* @param date: string |
||
Line 1,300: | Line 1,300: | ||
} |
} |
||
getPalindromeDates();</ |
getPalindromeDates();</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
<pre>2021-12-02 |
<pre>2021-12-02 |
||
Line 1,318: | Line 1,318: | ||
2140-04-12 </pre> |
2140-04-12 </pre> |
||
===Functional=== |
===Functional=== |
||
< |
<syntaxhighlight lang="javascript">(() => { |
||
'use strict'; |
'use strict'; |
||
Line 1,374: | Line 1,374: | ||
// MAIN --- |
// MAIN --- |
||
return main(); |
return main(); |
||
})();</ |
})();</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
<pre>Count of palindromic dates [2021..9999]: 284 |
<pre>Count of palindromic dates [2021..9999]: 284 |
||
Line 1,414: | Line 1,414: | ||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
Uses the built-in Dates package to check date validity but not for iteration. |
Uses the built-in Dates package to check date validity but not for iteration. |
||
< |
<syntaxhighlight lang="julia">using Dates |
||
function datepalindromes(nextcount=20) |
function datepalindromes(nextcount=20) |
||
Line 1,437: | Line 1,437: | ||
datepalindromes() |
datepalindromes() |
||
</ |
</syntaxhighlight>{{out}} |
||
<pre> |
<pre> |
||
Date palindromes: |
Date palindromes: |
||
Line 1,463: | Line 1,463: | ||
=={{header|Mathematica}} / {{header|Wolfram Language}}== |
=={{header|Mathematica}} / {{header|Wolfram Language}}== |
||
< |
<syntaxhighlight lang="mathematica">today = DateList[Today]; |
||
res = {}; |
res = {}; |
||
i = 0; |
i = 0; |
||
Line 1,474: | Line 1,474: | ||
i++; |
i++; |
||
] |
] |
||
Column[DateString[#, {"Year", "-", "Month", "-", "Day"}] & /@ res]</ |
Column[DateString[#, {"Year", "-", "Month", "-", "Day"}] & /@ res]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>2021-12-02 |
<pre>2021-12-02 |
||
Line 1,493: | Line 1,493: | ||
=={{header|Nim}}== |
=={{header|Nim}}== |
||
< |
<syntaxhighlight lang="nim">import strformat, times |
||
func digits(n: int): seq[int] = |
func digits(n: int): seq[int] = |
||
Line 1,513: | Line 1,513: | ||
echo &"{year}-{monthNum:02}-{dayNum:02}" |
echo &"{year}-{monthNum:02}-{dayNum:02}" |
||
inc count |
inc count |
||
inc year</ |
inc year</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,536: | Line 1,536: | ||
===Date calculation=== |
===Date calculation=== |
||
The more robust solution, using a date/time module. |
The more robust solution, using a date/time module. |
||
< |
<syntaxhighlight lang="perl">use Time::Piece; |
||
my $d = Time::Piece->strptime("2020-02-02", "%Y-%m-%d"); |
my $d = Time::Piece->strptime("2020-02-02", "%Y-%m-%d"); |
||
Line 1,544: | Line 1,544: | ||
print $d->strftime("%Y-%m-%d\n"); |
print $d->strftime("%Y-%m-%d\n"); |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,567: | Line 1,567: | ||
Given the limited look-ahead required by the task, processing date-like strings can also work. |
Given the limited look-ahead required by the task, processing date-like strings can also work. |
||
{{libheader|ntheory}} |
{{libheader|ntheory}} |
||
< |
<syntaxhighlight lang="perl">use strict; |
||
use warnings; |
use warnings; |
||
use feature 'say'; |
use feature 'say'; |
||
Line 1,582: | Line 1,582: | ||
say s/ /-/gr; |
say s/ /-/gr; |
||
last if 15 == ++$cnt; |
last if 15 == ++$cnt; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>2021-12-02 |
<pre>2021-12-02 |
||
Line 1,602: | Line 1,602: | ||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
While parse_date_string() copes with 1/2/4 digit years, it (reasonably enough) throws a wobbly given 5-digit years and beyond. |
While parse_date_string() copes with 1/2/4 digit years, it (reasonably enough) throws a wobbly given 5-digit years and beyond. |
||
<!--< |
<!--<syntaxhighlight lang="phix">(phixonline)--> |
||
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span> |
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span> |
||
<span style="color: #008080;">include</span> <span style="color: #000000;">builtins</span><span style="color: #0000FF;">\</span><span style="color: #004080;">timedate</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span> |
<span style="color: #008080;">include</span> <span style="color: #000000;">builtins</span><span style="color: #0000FF;">\</span><span style="color: #004080;">timedate</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span> |
||
Line 1,616: | Line 1,616: | ||
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"first 15:\n%s\n"</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">join_by</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">..</span><span style="color: #000000;">15</span><span style="color: #0000FF;">],</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">5</span><span style="color: #0000FF;">))</span> |
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"first 15:\n%s\n"</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">join_by</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">..</span><span style="color: #000000;">15</span><span style="color: #0000FF;">],</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">5</span><span style="color: #0000FF;">))</span> |
||
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"last 15:\n%s\n"</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">join_by</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">[-</span><span style="color: #000000;">15</span><span style="color: #0000FF;">..-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">],</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">5</span><span style="color: #0000FF;">))</span> |
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"last 15:\n%s\n"</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">join_by</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">[-</span><span style="color: #000000;">15</span><span style="color: #0000FF;">..-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">],</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">5</span><span style="color: #0000FF;">))</span> |
||
<!--</ |
<!--</syntaxhighlight>--> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,633: | Line 1,633: | ||
=={{header|PureBasic}}== |
=={{header|PureBasic}}== |
||
< |
<syntaxhighlight lang="purebasic">NewList pdates.s() |
||
Procedure.b IsLeap(y.i) |
Procedure.b IsLeap(y.i) |
||
Line 1,663: | Line 1,663: | ||
t$="Last 15:" : SelectElement(pdates(),ListSize(pdates())-15) |
t$="Last 15:" : SelectElement(pdates(),ListSize(pdates())-15) |
||
Next |
Next |
||
Input() : End</ |
Input() : End</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Count of palindromic dates [2021..9999]: 284 |
<pre>Count of palindromic dates [2021..9999]: 284 |
||
Line 1,706: | Line 1,706: | ||
Defined in terms of string reversal: |
Defined in terms of string reversal: |
||
{{Works with|Python|3.7}} |
{{Works with|Python|3.7}} |
||
< |
<syntaxhighlight lang="python">'''Palindrome dates''' |
||
from datetime import datetime |
from datetime import datetime |
||
Line 1,748: | Line 1,748: | ||
# MAIN --- |
# MAIN --- |
||
if __name__ == '__main__': |
if __name__ == '__main__': |
||
main()</ |
main()</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
<pre>Count of palindromic dates [2021..9999]: |
<pre>Count of palindromic dates [2021..9999]: |
||
Line 1,790: | Line 1,790: | ||
Or, defined in terms of integer operations, rather than string reversals: |
Or, defined in terms of integer operations, rather than string reversals: |
||
{{Works with|Python|3.7}} |
{{Works with|Python|3.7}} |
||
< |
<syntaxhighlight lang="python">'''Palindrome dates''' |
||
from functools import reduce |
from functools import reduce |
||
Line 1,917: | Line 1,917: | ||
# MAIN --- |
# MAIN --- |
||
if __name__ == '__main__': |
if __name__ == '__main__': |
||
main()</ |
main()</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
<pre>Count of palindromic dates [2021..9999]: |
<pre>Count of palindromic dates [2021..9999]: |
||
Line 1,957: | Line 1,957: | ||
=={{header|QB64}}== |
=={{header|QB64}}== |
||
<syntaxhighlight lang=" qb64"> |
|||
<lang QB64> |
|||
'Task |
'Task |
||
' Write a program which calculates and shows the next 15 palindromic dates |
' Write a program which calculates and shows the next 15 palindromic dates |
||
Line 2,021: | Line 2,021: | ||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Raku}}== |
=={{header|Raku}}== |
||
Line 2,028: | Line 2,028: | ||
Pretty basic, but good enough. Could start earlier but 3/2/1 digit years require different handling that isn't necessary for this task. (And would be pretty pointless anyway assuming we need 2 digits for the month and two digits for the day. ISO:8601 anybody?) |
Pretty basic, but good enough. Could start earlier but 3/2/1 digit years require different handling that isn't necessary for this task. (And would be pretty pointless anyway assuming we need 2 digits for the month and two digits for the day. ISO:8601 anybody?) |
||
<lang |
<syntaxhighlight lang="raku" line>my $start = '1000-01-01'; |
||
my @palindate = { |
my @palindate = { |
||
Line 2,052: | Line 2,052: | ||
say "\nTotal number of five digit year palindrome dates:\n" ~ |
say "\nTotal number of five digit year palindrome dates:\n" ~ |
||
+@palindate[$four .. $five]</ |
+@palindate[$four .. $five]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>2020-02-02 |
<pre>2020-02-02 |
||
Line 2,083: | Line 2,083: | ||
The '''date''' BIF (with the '''base''' argument) converts a date to the number of years since the beginning of |
The '''date''' BIF (with the '''base''' argument) converts a date to the number of years since the beginning of |
||
<br>the Gregorian calendar, the date is in the '''ISO''' format (International Standards Organization 8601:2004). |
<br>the Gregorian calendar, the date is in the '''ISO''' format (International Standards Organization 8601:2004). |
||
< |
<syntaxhighlight lang="rexx">/*REXX program finds & displays the next N palindromic dates starting after 2020─02─02*/ |
||
/* ───── */ |
/* ───── */ |
||
parse arg n from . /*obtain optional argumets from the CL*/ |
parse arg n from . /*obtain optional argumets from the CL*/ |
||
Line 2,095: | Line 2,095: | ||
say 'a palindromic date: ' aDate /*display a palindromic date ──► term. */ |
say 'a palindromic date: ' aDate /*display a palindromic date ──► term. */ |
||
#= # + 1 /*bump the counter of palindromic dates*/ |
#= # + 1 /*bump the counter of palindromic dates*/ |
||
end /*j*/ /*stick a fork in it, we're all done. */</ |
end /*j*/ /*stick a fork in it, we're all done. */</syntaxhighlight> |
||
{{out|output|text= when using the default inputs:}} |
{{out|output|text= when using the default inputs:}} |
||
<pre> |
<pre> |
||
Line 2,116: | Line 2,116: | ||
=={{header|Ring}}== |
=={{header|Ring}}== |
||
< |
<syntaxhighlight lang="ring"> |
||
load "stdlib.ring" |
load "stdlib.ring" |
||
Line 2,140: | Line 2,140: | ||
end |
end |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,163: | Line 2,163: | ||
=={{header|Ruby}}== |
=={{header|Ruby}}== |
||
< |
<syntaxhighlight lang="ruby">require 'date' |
||
palindate = Enumerator.new do |yielder| |
palindate = Enumerator.new do |yielder| |
||
Line 2,173: | Line 2,173: | ||
end |
end |
||
puts palindate.take(15)</ |
puts palindate.take(15)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,194: | Line 2,194: | ||
=={{header|Rust}}== |
=={{header|Rust}}== |
||
< |
<syntaxhighlight lang="rust">// [dependencies] |
||
// chrono = "0.4" |
// chrono = "0.4" |
||
Line 2,211: | Line 2,211: | ||
date = date.succ(); |
date = date.succ(); |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,235: | Line 2,235: | ||
=={{header|Sidef}}== |
=={{header|Sidef}}== |
||
{{trans|Ruby}} |
{{trans|Ruby}} |
||
< |
<syntaxhighlight lang="ruby">var palindates = gather { |
||
for y in (2020 .. 9999) { |
for y in (2020 .. 9999) { |
||
var (m, d) = Str(y).flip.last(4).split(2)... |
var (m, d) = Str(y).flip.last(4).split(2)... |
||
Line 2,251: | Line 2,251: | ||
]) { |
]) { |
||
say ("\n#{a}\n", b.slices(5).map { .join(" ") }.join("\n")) |
say ("\n#{a}\n", b.slices(5).map { .join(" ") }.join("\n")) |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,268: | Line 2,268: | ||
=={{header|Swift}}== |
=={{header|Swift}}== |
||
< |
<syntaxhighlight lang="swift">import Foundation |
||
func isPalindrome(_ string: String) -> Bool { |
func isPalindrome(_ string: String) -> Bool { |
||
Line 2,292: | Line 2,292: | ||
} |
} |
||
date = calendar.date(byAdding: .day, value: 1, to: date)! |
date = calendar.date(byAdding: .day, value: 1, to: date)! |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,316: | Line 2,316: | ||
=={{header|UNIX Shell}}== |
=={{header|UNIX Shell}}== |
||
printf format, rev and date commands are the keys : |
printf format, rev and date commands are the keys : |
||
< |
<syntaxhighlight lang="shell">is_palyndrom_date() { date -d "$1" 1>/dev/null 2>&1 && echo "$1" ; } |
||
for _H in {2..9}; do |
for _H in {2..9}; do |
||
Line 2,325: | Line 2,325: | ||
done |
done |
||
done |
done |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,350: | Line 2,350: | ||
=={{header|VBA}}== |
=={{header|VBA}}== |
||
<syntaxhighlight lang="vb"> |
|||
<lang vb> |
|||
Sub MainPalindromeDates() |
Sub MainPalindromeDates() |
||
Const FirstDate As String = "2020-02-02" |
Const FirstDate As String = "2020-02-02" |
||
Line 2,368: | Line 2,368: | ||
IsDatePalindrome = StrReverse(Left(strDate, 4)) = Right(strDate, 4) |
IsDatePalindrome = StrReverse(Left(strDate, 4)) = Right(strDate, 4) |
||
End Function |
End Function |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}}The next 15 palindromic dates in yyyy-mm-dd format after 2020-02-02 are : |
{{out}}The next 15 palindromic dates in yyyy-mm-dd format after 2020-02-02 are : |
||
Line 2,391: | Line 2,391: | ||
{{libheader|Wren-fmt}} |
{{libheader|Wren-fmt}} |
||
{{libheader|Wren-date}} |
{{libheader|Wren-date}} |
||
< |
<syntaxhighlight lang="ecmascript">import "/fmt" for Fmt |
||
import "/date" for Date |
import "/date" for Date |
||
Line 2,409: | Line 2,409: | ||
count = count + 1 |
count = count + 1 |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,432: | Line 2,432: | ||
=={{header|XPL0}}== |
=={{header|XPL0}}== |
||
< |
<syntaxhighlight lang="xpl0">func Rev(N); |
||
int N; |
int N; |
||
[N:= N/10; |
[N:= N/10; |
||
Line 2,459: | Line 2,459: | ||
]; |
]; |
||
]; |
]; |
||
]</ |
]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,482: | Line 2,482: | ||
=={{header|Yabasic}}== |
=={{header|Yabasic}}== |
||
{{trans|FreeBASIC}} |
{{trans|FreeBASIC}} |
||
< |
<syntaxhighlight lang="yabasic"> |
||
dateTest$ = "" |
dateTest$ = "" |
||
total = 0 |
total = 0 |
||
Line 2,511: | Line 2,511: | ||
next anno |
next anno |
||
end |
end |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,519: | Line 2,519: | ||
=={{header|zkl}}== |
=={{header|zkl}}== |
||
< |
<syntaxhighlight lang="zkl">TD,date,n := Time.Date, T(2020,02,02), 15; |
||
while(n){ |
while(n){ |
||
ds:=TD.toYMDString(date.xplode()) - "-"; |
ds:=TD.toYMDString(date.xplode()) - "-"; |
||
if(ds==ds.reverse()){ n-=1; println(TD.toYMDString(date.xplode())); } |
if(ds==ds.reverse()){ n-=1; println(TD.toYMDString(date.xplode())); } |
||
date=TD.addYMD(date,0,0,1); |
date=TD.addYMD(date,0,0,1); |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |