Hochzeitsdaten die Zweite (Palindrom-Tage) (Schleifen)
Wem die Hochzeitstage aus Aufgabe (06s4-5wnh) noch nicht aufregend genug sind, der kann ja mal nach Palindrom-Tagen schauen. Das ist ein Datum, welches sowohl vorwärts als auch rückwärts gelesen gleich ist.
Beispiel:
20.02.2002 -> 20022002
Schreibe einen Algorithmus, der alle Palindrom-Tage zwischen dem 01.01.2000 und dem 31.12.2999 ausgibt.
0 Kommentare
6 Lösung(en)
String.prototype.isPalindrome = function() {
let t = this.toUpperCase();
return t.split('').reverse().join('') === t;
}
function isPlausibleDate(y, m, d) {
let p = new Date(y, m, d);
return (
p.getFullYear() == y &&
p.getMonth() == m &&
p.getDate() == d
);
}
let out = [];
console.time();
for (let y = 2000; y <= 2999; y++)
for (let m = 1; m <= 12; m++)
for (let d = 1; d <= 31; d++) {
let test = d.toString().padStart(2, '0') +
m.toString().padStart(2, '0') +
y.toString();
if (test.isPalindrome()) {
if (isPlausibleDate(y, m-1, d)) out.push([d, m, y].join('.'));
}
}
console.timeEnd();
console.log(out);
Lösung von: Lisa Salander (Heidi-Klum-Gymnasium Bottrop)
// NET 6.x | C# 10.x | VS-2022
static IEnumerable<DateOnly> GetPalindromeDates(DateSpan s) {
while (s.Begin <= s.End) {
var d = s.Begin.ToShortDateString().Replace(".", "");
if (d == new string(d.Reverse().ToArray()))
yield return s.Begin;
s.Begin = s.Begin.AddDays(1);
}
}
DateSpan s = new(new(2001, 1, 1), new(2999, 12, 31));
GetPalindromeDates(s).ToList().ForEach(x => Console.WriteLine(x.ToShortDateString()));
record struct DateSpan (DateOnly Begin, DateOnly End);
Lösung von: Jens Kelm (@JKooP)
#include <iostream>
#include <vector>
#include <boost/date_time/gregorian/gregorian.hpp>
using namespace boost::gregorian;
struct datespan { date begin; const date end; };
std::vector<date> get_palindrome_dates(datespan d) {
std::vector<date>v;
days add{ 1 };
const auto get_leading_zero{ [](auto i) { std::stringstream ss; ss << 0 << i;
return ss.str().substr(ss.str().length() - 2); } };
while (d.begin < d.end) {
const auto day_{ get_leading_zero(d.begin.day()) };
const auto month_{ get_leading_zero(d.begin.month().as_number()) };
std::stringstream ss;
ss << day_ << month_ << d.begin.year();
auto r{ ss.str() };
std::reverse(r.begin(), r.end());
if (r == ss.str())
v.push_back(d.begin);
d.begin += add;
}
return v;
}
int main() {
const auto p{ get_palindrome_dates({ { 2000, 1, 1 }, { 2999, 12, 31 } }) };
date_facet* f(new date_facet("%d.%m.%Y"));
std::cout.imbue(std::locale(std::cout.getloc(), f));
for (const auto& i : p)
std::cout << i << "\n";
}
Lösung von: Jens Kelm (@JKooP)
' VBA-Funktion
Function GetPalindromeDates(ByVal first As Date, ByVal last As Date)
Dim dates() As Variant
Dim c As Integer
Dim tmp As String
Do While first < last
tmp = Right("0" & Day(first), 2) & Right("0" & Month(first), 2) & Year(first)
If tmp = StrReverse(tmp) Then
ReDim Preserve dates(c)
dates(c) = first
c = c + 1
End If
first = DateAdd("d", 1, first)
Loop
GetPalindromeDates = dates
End Function
Sub Main()
Const START_D As Date = #1/1/2000#
Const END_D As Date = #12/31/2999#
Dim dat As Variant
For Each dat In GetPalindromeDates(START_D, END_D)
Debug.Print dat
Next
End Sub
Lösung von: Jens Kelm (@JKooP)
zaehler=0
daten=[]
for jahr in range(2000,2999):
for monat in range(1,13):
for tag in range(1,32):
if tag<10: datum=str(0)+str(tag)
else:datum=str(tag)
if monat<10: datum+=str(0)+str(monat)
else: datum+=str(monat)
datum+=str(jahr)
if datum[3::-1]==datum[4:]:
if tag>28 and monat==2 and jahr%4!=0:
break
zaehler+=1
daten.append(datum[:2]+'.'+datum[2:4]+'.'+datum[4:])
for datum in daten:
print(datum)
print("Anzahl der Palidromtage: {}".format(zaehler))
Lösung von: Name nicht veröffentlicht
// C++ 20 | VS-2022
#include <iostream>
#include <chrono>
#include <vector>
#include <format>
using namespace std::chrono;
using ymd = year_month_day;
using namespace std::string_literals;
struct span {
ymd begin, end;
};
constexpr auto get_leading_zero(const auto& num_) {
const auto num_str{ "0"s + std::to_string(num_) };
return num_str.substr(num_str.length() - 2);
}
constexpr auto get_palindrome_dates(const span& span_) {
std::vector<ymd>dates;
auto begin{ span_.begin };
while (begin <= span_.end) {
const auto day_str{ get_leading_zero(unsigned(begin.day())) };
const auto month_str{ get_leading_zero(unsigned(begin.month())) };
const auto year_str{ std::to_string(int(begin.year())) };
const auto date_str{ day_str + month_str + year_str };
auto date_rev_str{ date_str };
std::reverse(date_rev_str.begin(), date_rev_str.end());
if (date_str == date_rev_str)
dates.push_back(begin);
begin = sys_days{ begin } + days{ 1 };
}
return dates;
}
const auto& operator<<(std::ostream& os_, const ymd& ymd_) {
os_ << std::format("{}.{:02}.{}\n", ymd_.day(), unsigned(ymd_.month()), ymd_.year());
return os_;
}
int main() {
constexpr ymd begin{ 2000y / 1 / 1 }, end{ 2999y / 12 / 31 };
constexpr span span{ begin, end };
const auto dates{ get_palindrome_dates(span) };
std::cout << std::format("Anzahl Palindromtage: {}\n\n", dates.size());
for (const auto& date : dates)
std::cout << date;
}
Lösung von: Jens Kelm (@JKooP)
Verifikation/Checksumme:
Anzahl Lösungen: 60
Aktionen
Neue Lösung hinzufügen
Bewertung
Durchschnittliche Bewertung:
Meta
Zeit: | |
Schwierigkeit: | Mittel |
Webcode: | 56n3-azmb |
Autor: | () |