') + ')', 'gi'); if (regex.test(text)) { found = true; var frag = document.createDocumentFragment(); var parts = text.split(regex); parts.forEach(function(part, i) { if (i % 2 === 0) { frag.appendChild(document.createTextNode(part)); } else { var span = document.createElement('span'); span.className = 'userscript-highlight'; span.textContent = part; frag.appendChild(span); } }); node.parentNode.replaceChild(frag, node); } }); } else if (node.nodeType === 1 && node.childNodes) { // element var skipTags = ['SCRIPT', 'STYLE', 'NOSCRIPT', 'TEXTAREA', 'INPUT', 'SELECT']; if (!skipTags.includes(node.tagName)) { Array.from(node.childNodes).forEach(highlight); } } } highlight(document.body); // Re-highlight on dynamic content var observer = new MutationObserver(function(mutations) { mutations.forEach(function(m) { m.addedNodes.forEach(function(node) { if (node.nodeType === 1 || node.nodeType === 3) highlight(node); }); }); }); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:Highlight Search Terms]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + ', 'i'); if (__m === '*' || __re.test(location.href)) { // Strip utm_, fbclid, gclid, etc. from all links on page (function() { var trackingParams = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content', 'fbclid', 'gclid', 'dclid', 'msclkid', 'yclid', 'ref', 'ref_src', 'source', 'medium', 'campaign']; function cleanUrl(url) { try { var u = new URL(url, window.location.origin); var changed = false; trackingParams.forEach(function(p) { if (u.searchParams.has(p)) { u.searchParams.delete(p); changed = true; } }); return changed ? u.toString() : url; } catch (e) { return url; } } function cleanLinks() { document.querySelectorAll('a[href]').forEach(function(a) { var clean = cleanUrl(a.href); if (clean !== a.href) a.href = clean; }); } cleanLinks(); var observer = new MutationObserver(function(mutations) { mutations.forEach(function(m) { m.addedNodes.forEach(function(node) { if (node.nodeType === 1) { if (node.tagName === 'A') cleanLinks(); node.querySelectorAll('a[href]').forEach(function(a) { var clean = cleanUrl(a.href); if (clean !== a.href) a.href = clean; }); } }); }); }); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:Remove Tracking Parameters from Links]', __e); } })(); (function(){ try { var __m = "youtube.com"; var __re = new RegExp('^' + "youtube\\.com" + ', 'i'); if (__m === '*' || __re.test(location.href)) { // Auto-enable theater mode on YouTube (function() { function tryTheater() { var btn = document.querySelector('button[aria-label="Theater mode"], ytd-player #player button[title="Theater mode"]'); if (btn && !btn.classList.contains('activated')) { btn.click(); } } // Try immediately tryTheater(); // Try after navigation (SPA) var lastUrl = location.href; setInterval(function() { if (location.href !== lastUrl) { lastUrl = location.href; setTimeout(tryTheater, 500); } }, 1000); // Also try on player load var observer = new MutationObserver(tryTheater); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:YouTube Theater Mode Default]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + ', 'i'); if (__m === '*' || __re.test(location.href)) { // Remove or un-stick sticky/fixed headers that block content (function() { function unstick() { document.querySelectorAll('header, nav, [role="banner"], .header, .navbar, .sticky, .fixed-top, [style*="position: fixed"], [style*="position:sticky"]').forEach(function(el) { if (el.style.position === 'fixed' || el.style.position === 'sticky' || getComputedStyle(el).position === 'fixed' || getComputedStyle(el).position === 'sticky') { el.style.position = 'static'; el.style.top = 'auto'; el.style.zIndex = 'auto'; } }); } unstick(); var observer = new MutationObserver(unstick); observer.observe(document.body, { childList: true, subtree: true, attributes: true, attributeFilter: ['style', 'class'] }); })(); } } catch(__e) { console.warn('[Userscript:Kill Sticky Headers]', __e); } })(); })(); Zeller's Congruence Algorithm by mrvnmchm · Pull Request #1095 · TheAlgorithms/Python · GitHub
Skip to content

Zeller's Congruence Algorithm - #1095

Merged
mrvnmchm merged 4 commits into
TheAlgorithms:masterfrom
mrvnmchm:zeller
Aug 5, 2019
Merged

Zeller's Congruence Algorithm#1095
mrvnmchm merged 4 commits into
TheAlgorithms:masterfrom
mrvnmchm:zeller

Conversation

@mrvnmchm

@mrvnmchmmrvnmchm commented Aug 3, 2019

Copy link
Copy Markdown
Member

Let me know of any other improvements I can make. 👍

Marvins-MBP:maths marvinmichum$ python3 zellers_congruence.py --help
usage: zellers_congruence.py [-h] date_input
Find out what day of the week nearly any date is or was. Enter date as a
string in the mm-dd-yyyy or mm/dd/yyyy format
positional arguments:
date_input Date as a string (mm-dd-yyyy or mm/dd/yyyy)
optional arguments:
-h, --help show this help message and exit
>>>importZellersCongruenceasz>>>z.zeller('10/10/2018')
Yourdate10/10/2018, isaWednesday!
>>>z.zeller('04-23-1991')
Yourdate04-23-1991, isaTuesday!
>>>z.zeller('01-31-1976')
Yourdate01-31-1976, isaSaturday!
Marvins-MBP:maths marvinmichum$ python3 zellers_congruence.py -v
Trying:
zeller('01-31-2010')
Expecting:
Your date 01-31-2010, is a Sunday!
ok
Trying:
zeller('13-31-2010')
Expecting:
Traceback (most recent call last):
...
ValueError: Month must be between 1 - 12
ok
Trying:
zeller('.2-31-2010')
Expecting:
Traceback (most recent call last):
...
ValueError: invalid literal forint() with base 10: '.2'
ok
Trying:
zeller('01-33-2010')
Expecting:
Traceback (most recent call last):
...
ValueError: Date must be between 1 - 31
ok
Trying:
zeller('01-.4-2010')
Expecting:
Traceback (most recent call last):
...
ValueError: invalid literal forint() with base 10: '.4'
ok
Trying:
zeller('01-31*2010')
Expecting:
Traceback (most recent call last):
...
ValueError: Date seperator must be '-' or '/'
ok
Trying:
zeller('01^31-2010')
Expecting:
Traceback (most recent call last):
...
ValueError: Date seperator must be '-' or '/'
ok
Trying:
zeller('01-31-8999')
Expecting:
Traceback (most recent call last):
...
ValueError: Year out of range. There has to be some sort of limit...right?
ok
Trying:
zeller()
Expecting:
Traceback (most recent call last):
...
TypeError: zeller() missing 1 required positional argument: 'date_input'
ok
Trying:
zeller('')
Expecting:
Traceback (most recent call last):
...
ValueError: Must be 10 characters long
ok
Trying:
zeller('01-31-19082939')
Expecting:
Traceback (most recent call last):
...
ValueError: Must be 10 characters long
ok
1 items had no tests:
__main__
1 items passed all tests:
11 tests in __main__.zeller
11 tests in 2 items.
11 passed and 0 failed.
Test passed.

@cclauss

Copy link
Copy Markdown
Member

@mrvnmchm

Copy link
Copy Markdown
MemberAuthor

Removed unused import.

@cclausscclauss left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a great piece of work. I approve it in its current state.
Please consider adding one or more tests to compare zeller()'s results against the Python standard library results:

>>>fromdatetimeimportdatetime>>>f"{datetime(2019, 8, 4):%A}"

Just FYI: Dates are a pain in all programming languages but these two libraries help...
https://arrow.readthedocs.io
https://www.kennethreitz.org/essays/introducing-maya-datetimes-for-humans

Comment threadmaths/zellers_congruence.py Outdated
Comment threadmaths/zellers_congruence.py Outdated
Comment threadmaths/zellers_congruence.py Outdated
Comment threadmaths/zellers_congruence.py Outdated
Comment threadmaths/zellers_congruence.py Outdated
@mrvnmchm

Copy link
Copy Markdown
MemberAuthor

Updated to add suggestions, and to fix the issue with TravisCI and the Dict annotation.

@mrvnmchm
mrvnmchm merged commit bdbe682 into TheAlgorithms:masterAug 5, 2019
stokhos pushed a commit to stokhos/Python that referenced this pull request Jan 3, 2021
* doctest updates
* remove unused math import
* cleanup (suggestions)
* cleanup - Dict fix (TravisCI error)
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

@mrvnmchm@cclauss