aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-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()