Palindrome dates: Difference between revisions
m
syntax highlighting fixup automation
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
|||
Line 10:
{{trans|Java}}
<
print(‘First 15 palindrome dates after 2020-02-02 are:’)
V count = 0
Line 18:
print(‘date = ’date.format(‘YYYY-MM-DD’))
count++
date += TimeDelta(days' 1)</
{{out}}
Line 41:
=={{header|Action!}}==
<
INT year
BYTE month
Line 131:
NextDay(d)
OD
RETURN</
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Palindrome_dates.png Screenshot from Atari 8-bit computer]
Line 155:
{{works with|ALGOL 68G|Any - tested with release 2.8.3.win32}}
Uses the Algol 68G local time routine which is non-standard.
<
# a palindromic date must be of the form demn-nm-ed #
# returns a string representation of n with at least 2 digits #
Line 211:
OD
OD
END</
{{out}}
<pre>
Line 232:
=={{header|Ada}}==
<
with Ada.Calendar.Formatting;
with Ada.Calendar.Arithmetic;
Line 265:
Date := Date + 1;
end loop;
end Palindrome_Dates;</
=={{header|AppleScript}}==
===Procedural===
<
script o
property output : {}
Line 300:
end palindromeDates
palindromeDates(2021, 15)</
{{output}}
Line 307:
===Functional===
<
use framework "Foundation"
use scripting additions
Line 423:
set my text item delimiters to dlm
str
end unlines</
{{Out}}
<pre>Count of palindromic dates [2021..9999]: 284
Line 462:
=={{header|AutoHotkey}}==
<
counter := 0
Line 483:
output := v output
return output
}</
{{out}}
<pre>
Line 504:
=={{header|AWK}}==
<syntaxhighlight lang="awk">
# syntax: GAWK -f PALINDROME_DATES.AWK
BEGIN {
Line 537:
return(rts)
}
</syntaxhighlight>
{{out}}
<pre>
Line 563:
{{works with|QBasic}}
{{trans|FreeBASIC}}
<
dateTest$ = ""
total = 0
Line 595:
NEXT anno
END
</syntaxhighlight>
{{out}}
<pre>
Line 603:
==={{header|BASIC256}}===
{{trans|FreeBASIC}}
<syntaxhighlight lang="lb">
dateTest = ""
mes = 0 : dia = 0 : anno = 0 : Pal = 0
Line 633:
next anno
end
</syntaxhighlight>
{{out}}
<pre>
Line 641:
=={{header|BBC BASIC}}==
{{works with|BBC BASIC for Windows}}
<
DIM B% 8
TestDate%=FN_today
Line 652:
TestDate%+=1
UNTIL VPOS=15
END</
{{out}}
<pre>
Line 674:
=={{header|C}}==
This only works if time_t is a 64-bit type.
<
#include <stdio.h>
#include <string.h>
Line 704:
}
return 0;
}</
{{out}}
Line 727:
=={{header|C sharp}}==
<
using System.Linq;
using System.Collections.Generic;
Line 748:
bool IsValidDate(int y, int m, int d, out DateTime date) => DateTime.TryParse($"{y}-{m}-{d}", out date);
}
}</
{{out}}
<pre>
Line 769:
=={{header|C++}}==
{{libheader|Boost}}
<
#include <string>
#include <boost/date_time/gregorian/gregorian.hpp>
Line 797:
}
return 0;
}</
{{out}}
Line 820:
=={{header|Clojure}}==
<syntaxhighlight lang="clojure">
(defn valid-date? [[y m d]]
(and (<= 1 m 12)
Line 838:
(map date-str)
(take n)))
</syntaxhighlight>
{{out}}
Line 846:
=={{header|F_Sharp|F#}}==
<
open System
Line 875:
|> Seq.take 15
|> Seq.iter print_date
</syntaxhighlight>
{{out}}
<pre>> dotnet fsi palindrome_dates.fsx
Line 898:
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}}
<
sequences sets ;
Line 906:
[ "-" without dup reverse = ] lfilter ;
15 palindrome-dates ltake [ print ] leach</
{{out}}
<pre>
Line 928:
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}}
<
lists.lazy math math.functions math.parser math.ranges sequences ;
Line 950:
"10,000th palindrome date after 2020-02-02: " write
10,000 palindrome-dates lnth timestamp>ymd print</
{{out}}
<pre>
Line 958:
=={{header|FreeBASIC}}==
<
Dim As String dateTest = ""
Dim As Integer Pal =0, total = 0
Line 987:
Next anno
Sleep
</syntaxhighlight>
{{out}}
<pre>
Line 1,011:
=={{header|Go}}==
Simple brute force as speed is not an issue here.
<
import (
Line 1,043:
}
}
}</
{{out}}
Line 1,064:
</pre>
Or, a more ambitious version.
<
import (
Line 1,125:
}
fmt.Println()
}</
{{out}}
Line 1,145:
=={{header|Haskell}}==
<
import Data.List.Split (chunksOf)
import Data.List (unfoldr)
Line 1,174:
mapM_ print $ take 15 palinDates
putStrLn "\nLast 15:"
mapM_ print $ take 15 (drop (n - 15) palinDates)</
{{Out}}
<pre>Count of palindromic dates [2021..9999]: 284
Line 1,213:
=={{header|Java}}==
<
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
Line 1,234:
}
</syntaxhighlight>
{{Out}}
<pre>
Line 1,257:
=={{header|Javascript}}==
===Procedural===
<
* Adds zeros for 1 digit days/months
* @param date: string
Line 1,300:
}
getPalindromeDates();</
{{Out}}
<pre>2021-12-02
Line 1,318:
2140-04-12 </pre>
===Functional===
<
'use strict';
Line 1,374:
// MAIN ---
return main();
})();</
{{Out}}
<pre>Count of palindromic dates [2021..9999]: 284
Line 1,414:
=={{header|Julia}}==
Uses the built-in Dates package to check date validity but not for iteration.
<
function datepalindromes(nextcount=20)
Line 1,437:
datepalindromes()
</
<pre>
Date palindromes:
Line 1,463:
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<
res = {};
i = 0;
Line 1,474:
i++;
]
Column[DateString[#, {"Year", "-", "Month", "-", "Day"}] & /@ res]</
{{out}}
<pre>2021-12-02
Line 1,493:
=={{header|Nim}}==
<
func digits(n: int): seq[int] =
Line 1,513:
echo &"{year}-{monthNum:02}-{dayNum:02}"
inc count
inc year</
{{out}}
Line 1,536:
===Date calculation===
The more robust solution, using a date/time module.
<
my $d = Time::Piece->strptime("2020-02-02", "%Y-%m-%d");
Line 1,544:
print $d->strftime("%Y-%m-%d\n");
}
}</
{{out}}
<pre>
Line 1,567:
Given the limited look-ahead required by the task, processing date-like strings can also work.
{{libheader|ntheory}}
<
use warnings;
use feature 'say';
Line 1,582:
say s/ /-/gr;
last if 15 == ++$cnt;
}</
{{out}}
<pre>2021-12-02
Line 1,602:
=={{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.
<!--<
<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>
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;">"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>
<!--</
{{out}}
<pre>
Line 1,633:
=={{header|PureBasic}}==
<
Procedure.b IsLeap(y.i)
Line 1,663:
t$="Last 15:" : SelectElement(pdates(),ListSize(pdates())-15)
Next
Input() : End</
{{out}}
<pre>Count of palindromic dates [2021..9999]: 284
Line 1,706:
Defined in terms of string reversal:
{{Works with|Python|3.7}}
<
from datetime import datetime
Line 1,748:
# MAIN ---
if __name__ == '__main__':
main()</
{{Out}}
<pre>Count of palindromic dates [2021..9999]:
Line 1,790:
Or, defined in terms of integer operations, rather than string reversals:
{{Works with|Python|3.7}}
<
from functools import reduce
Line 1,917:
# MAIN ---
if __name__ == '__main__':
main()</
{{Out}}
<pre>Count of palindromic dates [2021..9999]:
Line 1,957:
=={{header|QB64}}==
<syntaxhighlight lang=" qb64">
'Task
' Write a program which calculates and shows the next 15 palindromic dates
Line 2,021:
</syntaxhighlight>
=={{header|Raku}}==
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?)
<syntaxhighlight lang="raku"
my @palindate = {
Line 2,052:
say "\nTotal number of five digit year palindrome dates:\n" ~
+@palindate[$four .. $five]</
{{out}}
<pre>2020-02-02
Line 2,083:
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).
<
/* ───── */
parse arg n from . /*obtain optional argumets from the CL*/
Line 2,095:
say 'a palindromic date: ' aDate /*display a palindromic date ──► term. */
#= # + 1 /*bump the counter of palindromic dates*/
end /*j*/ /*stick a fork in it, we're all done. */</
{{out|output|text= when using the default inputs:}}
<pre>
Line 2,116:
=={{header|Ring}}==
<
load "stdlib.ring"
Line 2,140:
end
</syntaxhighlight>
{{out}}
<pre>
Line 2,163:
=={{header|Ruby}}==
<
palindate = Enumerator.new do |yielder|
Line 2,173:
end
puts palindate.take(15)</
{{out}}
<pre>
Line 2,194:
=={{header|Rust}}==
<
// chrono = "0.4"
Line 2,211:
date = date.succ();
}
}</
{{out}}
Line 2,235:
=={{header|Sidef}}==
{{trans|Ruby}}
<
for y in (2020 .. 9999) {
var (m, d) = Str(y).flip.last(4).split(2)...
Line 2,251:
]) {
say ("\n#{a}\n", b.slices(5).map { .join(" ") }.join("\n"))
}</
{{out}}
<pre>
Line 2,268:
=={{header|Swift}}==
<
func isPalindrome(_ string: String) -> Bool {
Line 2,292:
}
date = calendar.date(byAdding: .day, value: 1, to: date)!
}</
{{out}}
Line 2,316:
=={{header|UNIX Shell}}==
printf format, rev and date commands are the keys :
<
for _H in {2..9}; do
Line 2,325:
done
done
</syntaxhighlight>
{{out}}
<pre>
Line 2,350:
=={{header|VBA}}==
<syntaxhighlight lang="vb">
Sub MainPalindromeDates()
Const FirstDate As String = "2020-02-02"
Line 2,368:
IsDatePalindrome = StrReverse(Left(strDate, 4)) = Right(strDate, 4)
End Function
</syntaxhighlight>
{{out}}The next 15 palindromic dates in yyyy-mm-dd format after 2020-02-02 are :
Line 2,391:
{{libheader|Wren-fmt}}
{{libheader|Wren-date}}
<
import "/date" for Date
Line 2,409:
count = count + 1
}
}</
{{out}}
Line 2,432:
=={{header|XPL0}}==
<
int N;
[N:= N/10;
Line 2,459:
];
];
]</
{{out}}
Line 2,482:
=={{header|Yabasic}}==
{{trans|FreeBASIC}}
<
dateTest$ = ""
total = 0
Line 2,511:
next anno
end
</syntaxhighlight>
{{out}}
<pre>
Line 2,519:
=={{header|zkl}}==
<
while(n){
ds:=TD.toYMDString(date.xplode()) - "-";
if(ds==ds.reverse()){ n-=1; println(TD.toYMDString(date.xplode())); }
date=TD.addYMD(date,0,0,1);
}</
{{out}}
<pre>
|