aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2024-03-11 19:00:07 +0000
committerGitHub <noreply@github.com>2024-03-11 19:00:07 +0000
commit9ab95dda4f101c63ac38b810849665103b75f983 (patch)
tree7319dac6334253c28bb02fbdaa4f2994e32fb6db
parenta5faed18694c8397b7f42d27c85b4cf94f65c876 (diff)
parentf614ef1237a06089da652d5dcc3a927a1846fd22 (diff)
downloadperlweeklychallenge-club-9ab95dda4f101c63ac38b810849665103b75f983.tar.gz
perlweeklychallenge-club-9ab95dda4f101c63ac38b810849665103b75f983.tar.bz2
perlweeklychallenge-club-9ab95dda4f101c63ac38b810849665103b75f983.zip
Merge pull request #9732 from oWnOIzRi/week259260
add solutions week 259 260 task 1 in python
-rw-r--r--challenge-259/steven-wilson/python/ch-1.py47
-rw-r--r--challenge-260/steven-wilson/python/ch-1.py29
2 files changed, 76 insertions, 0 deletions
diff --git a/challenge-259/steven-wilson/python/ch-1.py b/challenge-259/steven-wilson/python/ch-1.py
new file mode 100644
index 0000000000..fe8d448022
--- /dev/null
+++ b/challenge-259/steven-wilson/python/ch-1.py
@@ -0,0 +1,47 @@
+#!/usr/bin/env python3
+
+from datetime import timedelta
+from datetime import date
+
+
+def banking_day_offset(start_date, offset, bank_holidays=None):
+ ''' Given a number (of days) and a start date, return the number (of days)
+ adjusted to take into account non-banking days. In other words: convert a
+ banking day offset to a calendar day offset.
+
+ >>> banking_day_offset('2018-06-28', 3, ['2018-07-03'])
+ '2018-07-04'
+ >>> banking_day_offset('2018-06-28', 3)
+ '2018-07-03'
+ '''
+ if bank_holidays is None:
+ bank_holidays = []
+
+ if not isinstance(offset, int) or offset < 0:
+ raise ValueError(f"{offset} is not a valid offset")
+
+ try:
+ odate = date.fromisoformat(start_date)
+ except ValueError:
+ raise ValueError(f"{start_date} is not a valid iso format start date")
+
+ for holiday in bank_holidays:
+ try:
+ date.fromisoformat(holiday)
+ except ValueError:
+ raise ValueError(f"{holiday} is not a valid iso format bank holiday")
+
+ weekend = [5, 6]
+
+ for _ in range(offset):
+ odate += timedelta(days=1)
+ while odate.weekday() in weekend or odate.isoformat() in bank_holidays:
+ odate += timedelta(days=1)
+
+ return odate.isoformat()
+
+
+if __name__ == "__main__":
+ import doctest
+
+ doctest.testmod()
diff --git a/challenge-260/steven-wilson/python/ch-1.py b/challenge-260/steven-wilson/python/ch-1.py
new file mode 100644
index 0000000000..4305bd667b
--- /dev/null
+++ b/challenge-260/steven-wilson/python/ch-1.py
@@ -0,0 +1,29 @@
+#!/usr/bin/env python3
+
+
+def unique_occurences(*elements):
+ '''
+ >>> unique_occurences(1,2,2,1,1,3)
+ 1
+ >>> unique_occurences(1,2,3)
+ 0
+ >>> unique_occurences(-2,0,1,-2,1,1,0,1,-2,9)
+ 1
+ '''
+ if not all(isinstance(elem, int) for elem in elements):
+ raise ValueError("Input must consist of integers")
+
+ from collections import Counter
+
+ counts = Counter(elements)
+ occurences = counts.values()
+ if len(counts) == len(set(occurences)):
+ return 1
+ else:
+ return 0
+
+
+if __name__ == "__main__":
+ import doctest
+
+ doctest.testmod()