aboutsummaryrefslogtreecommitdiff
path: root/challenge-183/sgreen/python
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-183/sgreen/python')
-rwxr-xr-xchallenge-183/sgreen/python/ch-1.py28
-rwxr-xr-xchallenge-183/sgreen/python/ch-2.py41
2 files changed, 69 insertions, 0 deletions
diff --git a/challenge-183/sgreen/python/ch-1.py b/challenge-183/sgreen/python/ch-1.py
new file mode 100755
index 0000000000..64bd316ef4
--- /dev/null
+++ b/challenge-183/sgreen/python/ch-1.py
@@ -0,0 +1,28 @@
+#!/usr/bin/env python
+
+import sys
+import yaml
+
+
+def main(y):
+ array = yaml.safe_load(y)
+
+ solution = []
+ for i in range(len(array)):
+
+ # See if we have found this list before
+ is_unique = True
+ for j in range(i):
+ if array[i] == array[j]:
+ is_unique = False
+ break
+
+ if is_unique:
+ # We haven't. Add it to the solutions
+ solution.append(array[i])
+
+ print(*solution, sep=', ')
+
+
+if __name__ == '__main__':
+ main(sys.argv[1])
diff --git a/challenge-183/sgreen/python/ch-2.py b/challenge-183/sgreen/python/ch-2.py
new file mode 100755
index 0000000000..92a8493bc5
--- /dev/null
+++ b/challenge-183/sgreen/python/ch-2.py
@@ -0,0 +1,41 @@
+#!/usr/bin/env python3
+
+import sys
+from datetime import date
+from dateutil.relativedelta import relativedelta
+
+
+def main(date1, date2):
+ date1 = date.fromisoformat(date1)
+ date2 = date.fromisoformat(date2)
+
+ # Make date2 the latest date
+ if date2 < date1:
+ date2, date1 = date1, date2
+
+ # Calculate the years between the two dates. If date2 is earlier in the
+ # year than date1, then the year difference is one less
+ years = date2.year - date1.year
+ if date2.month < date1.month or (date2.month == date1.month and date2.day < date1.day):
+ years -= 1
+
+ # Calculate the days difference
+ days = (date2 - relativedelta(years=years) - date1).days
+
+ # Print the output
+ diff = []
+ if years > 1:
+ diff.append(f'{years} years')
+ elif years == 1:
+ diff.append(f'1 year')
+
+ if days > 1:
+ diff.append(f'{days} days')
+ elif days == 1:
+ diff.append(f'1 day')
+
+ print(*diff, sep=' ')
+
+
+if __name__ == '__main__':
+ main(sys.argv[1], sys.argv[2])