Palindrome dates: Difference between revisions

Added Easylang
m (syntax highlighting fixup automation)
(Added Easylang)
 
(13 intermediate revisions by 10 users not shown)
Line 561:
 
=={{header|BASIC}}==
 
{{works with|QBasic}}
==={{header|BASIC256}}===
{{trans|FreeBASIC}}
<syntaxhighlight lang="vb">
dateTest = ""
mes = 0 : dia = 0 : anno = 0 : Pal = 0
total = 0
print "Siguientes 15 fechas palindrómicas al 2020-02-02:"
for anno = 2021 to 9999
dateTest = ltrim(string(anno))
for mes = 1 to 12
if mes < 10 then dateTest = dateTest + "0"
dateTest = dateTest + ltrim(string(mes))
for dia = 1 to 31
if mes = 2 and dia > 28 then exit for
if (mes = 4 or mes = 6 or mes = 9 or mes = 11) and dia > 30 then exit for
if dia < 10 then dateTest = dateTest + "0"
dateTest = dateTest + ltrim(string(dia))
for Pal = 1 to 4
if mid(dateTest, Pal, 1) <> mid(dateTest, 9 - Pal, 1) then exit for
next Pal
if Pal = 5 then
total += 1
if total <= 15 then print left(dateTest,4);"-";mid(dateTest,5,2);"-";right(dateTest,2)
end if
if total > 15 then exit for : exit for : exit for
dateTest = left(dateTest, 6)
next dia
dateTest = left(dateTest, 4)
next mes
dateTest = ""
next anno
end
</syntaxhighlight>
{{out}}
<pre>
Igual que la entrada de FreeBASIC.
</pre>
 
==={{header|QBasic}}===
{{works with|QBasic|1.1}}
{{works with|QuickBasic|4.5}}
{{trans|FreeBASIC}}
<syntaxhighlight lang="basic">
Line 595 ⟶ 636:
NEXT anno
END
</syntaxhighlight>
{{out}}
<pre>
Igual que la entrada de FreeBASIC.
</pre>
 
==={{header|BASIC256}}===
{{trans|FreeBASIC}}
<syntaxhighlight lang="lb">
dateTest = ""
mes = 0 : dia = 0 : anno = 0 : Pal = 0
total = 0
print "Siguientes 15 fechas palindrómicas al 2020-02-02:"
for anno = 2021 to 9999
dateTest = ltrim(string(anno))
for mes = 1 to 12
if mes < 10 then dateTest = dateTest + "0"
dateTest = dateTest + ltrim(string(mes))
for dia = 1 to 31
if mes = 2 and dia > 28 then exit for
if (mes = 4 or mes = 6 or mes = 9 or mes = 11) and dia > 30 then exit for
if dia < 10 then dateTest = dateTest + "0"
dateTest = dateTest + ltrim(string(dia))
for Pal = 1 to 4
if mid(dateTest, Pal, 1) <> mid(dateTest, 9 - Pal, 1) then exit for
next Pal
if Pal = 5 then
total += 1
if total <= 15 then print left(dateTest,4);"-";mid(dateTest,5,2);"-";right(dateTest,2)
end if
if total > 15 then exit for : exit for : exit for
dateTest = left(dateTest, 6)
next dia
dateTest = left(dateTest, 4)
next mes
dateTest = ""
next anno
end
</syntaxhighlight>
{{out}}
Line 802 ⟶ 805:
<pre>
Next 15 palindrome dates:
2021-12-02
2030-03-02
2040-04-02
2050-05-02
2060-06-02
2070-07-02
2080-08-02
2090-09-02
2101-10-12
2110-01-12
2111-11-12
2120-02-12
2121-12-12
2130-03-12
2140-04-12
</pre>
 
===Using C++20===
<syntaxhighlight lang="c++">
#include <algorithm>
#include <chrono>
#include <iostream>
#include <sstream>
 
bool is_palindrome(const std::chrono::sys_days& date) {
std::ostringstream stream;
stream << date;
std::string date_string = stream.str();
 
date_string.erase(std::remove(date_string.begin(), date_string.end(), '-'), date_string.end());
std::string original = date_string;
std::reverse(date_string.begin(), date_string.end());
 
return date_string == original;
}
 
int main() {
std::chrono::year_month_day date{std::chrono::year{2020}, std::chrono::month{02}, std::chrono::day{02}};
std::chrono::sys_days current_date = std::chrono::sys_days(date);
 
std::cout << "The first 15 palindrome dates after 2020-02-02 are:" << std::endl;
int32_t count = 0;
while ( count < 15 ) {
current_date += std::chrono::days{1};
if ( is_palindrome(current_date) ) {
std::cout << current_date << std::endl;
count++;
}
}
}
</syntaxhighlight>
{{ out }}
<pre>
The first 15 palindrome dates after 2020-02-02 are:
2021-12-02
2030-03-02
Line 844 ⟶ 901:
("2021-12-02" "2030-03-02" "2040-04-02" "2050-05-02" "2060-06-02" "2070-07-02" "2080-08-02" "2090-09-02" "2101-10-12" "2110-01-12" "2111-11-12" "2120-02-12" "2121-12-12" "2130-03-12" "2140-04-12")
</pre>
 
=={{header|Delphi}}==
{{works with|Delphi|6.0}}
{{libheader|SysUtils,StdCtrls}}
 
 
<syntaxhighlight lang="Delphi">
procedure ShowPalindromeDate(Memo: TMemo);
var DT: TDateTime;
var S,S2: string;
var I,Cnt: integer;
begin
Cnt:=0;
DT:=EncodeDate(2020,02,02);
for I:=0 to High(integer) do
begin
S:=FormatDateTime('yyyymmdd',DT);
S2:=ReverseString(S);
if S=S2 then
begin
Inc(Cnt);
Memo.Lines.Add(IntToStr(Cnt)+' '+FormatDateTime('yyyy-mm-dd',DT));
if Cnt>15 then break;
end;
DT:=DT+1;
end;
end;
 
</syntaxhighlight>
{{out}}
<pre>
1 2020-02-02
2 2021-12-02
3 2030-03-02
4 2040-04-02
5 2050-05-02
6 2060-06-02
7 2070-07-02
8 2080-08-02
9 2090-09-02
10 2101-10-12
11 2110-01-12
12 2111-11-12
13 2120-02-12
14 2121-12-12
15 2130-03-12
16 2140-04-12
Elapsed Time: 44.090 ms.
 
</pre>
 
 
=={{header|EasyLang}}==
<syntaxhighlight>
t = 1580641200
print "First 15 palindrome dates after 2020-02-02 are:"
repeat
t += 86400
a$ = timestr t
a$[] = strchars substr a$ 1 4 & substr a$ 6 2 & substr a$ 9 2
for i = 1 to 4
if a$[i] <> a$[9 - i]
break 1
.
.
if i = 5
cnt += 1
print substr a$ 1 10
.
until cnt = 15
.
</syntaxhighlight>
 
=={{header|F_Sharp|F#}}==
Line 1,145 ⟶ 1,274:
 
=={{header|Haskell}}==
<syntaxhighlight lang="haskell">import Data.Time.CalendarList (Day, fromGregorianValidunfoldr)
import Data.List.Split (chunksOf)
import Data.List (unfoldr)
import Data.Tuple (swap)
import Data.Bool (bool)
import Data.Maybe (mapMaybe)
import Data.Time.Calendar (Day, fromGregorianValid)
import Data.Tuple (swap)
 
-------------------- PALINDROMIC DATES -------------------
 
palinDates :: [Day]
Line 1,158 ⟶ 1,288:
palinDay y = fromGregorianValid y m d
where
[m, d] = unDigits <$> chunksOf 2 (reversedDecimalDigits (fromInteger y))
(reversedDecimalDigits (fromInteger y))
 
reversedDecimalDigits :: Int -> [Int]
reversedDecimalDigits =
unfoldr ((flip bool Nothing . Just . swap . flip quotRem 10) <*> (0 ==))
 
unDigits :: [Int] -> Int
unDigits = foldl ((+) . (10 *)) 0
 
--------------------------- TEST -------------------------
main :: IO ()
main = do
let n = length palinDates
in putStrLn $ ("Count of palindromic dates [2021..9999]: " ++ show n
<> show n) >>
putStrLn "\nFirst 15:"
putStrLn "\nFirst 15:" >>
mapM_ print $ take 15 palinDates
mapM_ print (take 15 palinDates) >>
putStrLn "\nLast 15:"
putStrLn "\nLast 15:" >>
mapM_ print $ take 15 (drop (n - 15) palinDates)</syntaxhighlight>
mapM_ print (take 15 (drop (n - 15) palinDates))
 
 
------------------------- GENERIC ------------------------
 
reversedDecimalDigits :: Int -> [Int]
reversedDecimalDigits = unfoldr go
where
go n
| 0 < n = Just $ swap (quotRem n 10)
| otherwise = Nothing
 
unDigits :: [Int] -> Int
unDigits = foldl ((+) . (10 *)) 0</syntaxhighlight>
{{Out}}
<pre>Count of palindromic dates [2021..9999]: 284
Line 1,211 ⟶ 1,351:
9280-08-29
9290-09-29</pre>
 
=={{header|J}}==
Using J's [[j:Standard_Library/dates|date routines]]:
<syntaxhighlight lang=J>task=: {{
r=. ''
days=. todayno 2020 02 02
while. 15 > #r do.
days=. ({:days)+1+i.1e4
r=. r, days#~(-:|.)"1":,.1 todate days
end.
15 10{.isotimestamp todate r
}}</syntaxhighlight>
 
<syntaxhighlight lang=J> task''
2021-12-02
2030-03-02
2040-04-02
2050-05-02
2060-06-02
2070-07-02
2080-08-02
2090-09-02
2101-10-12
2110-01-12
2111-11-12
2120-02-12
2121-12-12
2130-03-12
2140-04-12
</syntaxhighlight>
 
=={{header|Java}}==
Line 1,411 ⟶ 1,581:
9280-08-29
9290-09-29</pre>
 
=={{header|jq}}==
'''Adapted from [[#Wren|Wren]]'''
{{works with|jq}}
'''Also works with gojq, the Go implementation of jq'''
 
'''Also works with fq, a Go implementation of a large subset of jq'''
<syntaxhighlight lang=jq>
# input and output: [year, month, day] using month=1 for Jan
# add the number of days to the input date
def addDays($days):
. as [$y,$m,$d]
| [$y,$m-1,$d + $days,0,0,0,0,0] | mktime | gmtime[0:3] | .[1]+=1;
 
# input: [year, month, day], an array of integers
# output: [yyyy, mm, dd] , an array of strings,
def yyyymmdd:
def l($len): tostring | ($len - length) as $l | ("0" * $l)[:$l] + .;
[(.[0]|l(4)), (.[1]|l(2)), (.[2]|l(2))] ;
# input: [year, month, day] using month=1 for Jan
def isPalDate:
yyyymmdd | add | explode | . == reverse;
 
def task:
"The next 15 palindromic dates in yyyy-mm-dd format after 2020-02-02 are:",
( [2020, 2, 2]
| addDays(1)
| limit(15; recurse(addDays(1)) | select(isPalDate) )
| yyyymmdd
| join("-") );
 
task
</syntaxhighlight>
{{output}}
As for [[#Wren|Wren]].
 
 
=={{header|Julia}}==
Line 1,461 ⟶ 1,668:
2190-09-12
</pre>
 
=={{header|Lua}}==
<syntaxhighlight lang="lua">
 
local year = 2020
for i=1, 15 do
local dateS
repeat
year = year+1
local yearS = tostring(year)
local monthS, dayS = yearS:reverse():match"^(%d%d)(%d%d)$"
local monthN, dayN = tonumber(monthS), tonumber(dayS)
dateS = yearS.."-"..monthS.."-"..dayS
until
monthN>0 and monthN<=12 -- real month
and dayN<32 -- possible day
and os.date( -- real date check
-- be aware that this check is unnecessary because it only affects the 13th century
"%Y-%m-%d"
,os.time{
year = year
,month = monthN
,day = dayN
}
) == dateS
print(dateS)
end
 
</syntaxhighlight>
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
Line 2,019 ⟶ 2,255:
dateTest = ""
Next
 
 
</syntaxhighlight>
 
=={{header|Quackery}}==
 
<syntaxhighlight lang="Quackery"> [ dup 400 mod 0 = iff
[ drop true ] done
dup 100 mod 0 = iff
[ drop false ] done
4 mod 0 = ] is leap ( y --> b )
 
[ 1 -
[ table
31 [ dup leap 28 + ]
31 30 31 30 31 31 30
31 30 31 ]
do nip ] is monthdays ( y m --> n )
 
[ 1+ dip [ 2dup monthdays ]
tuck < while
drop 1+ 1
over 13 = if
[ 2drop 1+ 1 1 ] ] is nextday ( y m d --> y m d )
 
 
[ dip 2dup dup dip unrot ] is 3dup ( a b c --> a b c a b c )
 
[ swap 100 * +
swap 10000 * +
number$ ] is date$ ( y m d --> $ )
 
[ dup reverse = ] is palindrome ( $ --> b )
 
[ char - swap 4 stuff
char - swap 7 stuff ] is +dashes ( $ --> $ )
 
[] temp put
2020 02 02
[ nextday
3dup date$
dup palindrome iff
[ temp take
swap nested join
temp put ]
else drop
temp share
size 15 = until ]
drop 2drop
temp take witheach
[ +dashes echo$ cr ]</syntaxhighlight>
 
{{out}}
 
<pre>2021-12-02
2030-03-02
2040-04-02
2050-05-02
2060-06-02
2070-07-02
2080-08-02
2090-09-02
2101-10-12
2110-01-12
2111-11-12
2120-02-12
2121-12-12
2130-03-12
2140-04-12
</pre>
 
 
=={{header|Raku}}==
Line 2,160 ⟶ 2,463:
2130-03-12
2140-04-12
</pre>
 
=={{header|RPL}}==
{{works with|Halcyon Calc|4.2.7}}
≪ DUP 100 MOD 4 400 IFTE MOD NOT ≫
‘'''LEAP?'''’ STO
≪ 100 + →STR 2 3 SUB ≫
‘'''→2STR'''’ STO
≪ STR→ → mm dd yy
≪ mm 1 ≥ mm 12 ≤ AND
IF DUP
THEN
{ 31 28 31 30 31 30 31 31 30 31 30 31 }
IF yy '''LEAP?''' THEN 2 29 PUT END
mm GET dd ≥ AND dd 1 ≥ AND
END
≫ ≫
‘'''DTOK?'''’ STO
≪ →STR → year
≪ year 4 DUP SUB year 3 DUP SUB + STR→
year 2 DUP SUB year 1 DUP SUB + STR→
IF DUP2 year '''DTOK?'''
THEN year "-" + ROT '''→2STR''' + "-" + SWAP '''→2STR''' +
ELSE DROP2 ""
END
≫ ≫
‘'''PALDT'''’ STO
 
≪ { } 2023
WHILE OVER SIZE 15 < REPEAT
DUP '''PALDT'''
IF DUP "" ≠ THEN ROT SWAP + SWAP ELSE DROP END
1 +
END
DROP
EVAL
{{out}}
<pre>
1: { "2030-03-02" "2040-04-02" "2050-05-02" "2060-06-02" "2070-07-02"
"2080-08-02" "2090-09-02" "2101-10-12" "2110-01-12" "2111-11-12"
"2120-02-12" "2121-12-12" "2130-03-12" "2140-04-12" "2150-05-12" }
</pre>
 
Line 2,391 ⟶ 2,739:
{{libheader|Wren-fmt}}
{{libheader|Wren-date}}
<syntaxhighlight lang="ecmascriptwren">import "./fmt" for Fmt
import "./date" for Date
 
var isPalDate = Fn.new { |date|
2,044

edits