aboutsummaryrefslogtreecommitdiff
path: root/challenge-237/sgreen/python
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2023-10-08 19:18:09 +0100
committerGitHub <noreply@github.com>2023-10-08 19:18:09 +0100
commitfc8ad5c4919e34026ab18433df42b53770b9bf9d (patch)
tree81c86be3330a2d9227db3f066afaa4713451fcdf /challenge-237/sgreen/python
parentb96cb50042c3641c0bdb54bd72ecb8604f9f53ee (diff)
parent79ea77999ba709f30b32931b73e8520202ba78b9 (diff)
downloadperlweeklychallenge-club-fc8ad5c4919e34026ab18433df42b53770b9bf9d.tar.gz
perlweeklychallenge-club-fc8ad5c4919e34026ab18433df42b53770b9bf9d.tar.bz2
perlweeklychallenge-club-fc8ad5c4919e34026ab18433df42b53770b9bf9d.zip
Merge pull request #8826 from simongreen-net/master
Simon's solution to challenge 237
Diffstat (limited to 'challenge-237/sgreen/python')
-rwxr-xr-xchallenge-237/sgreen/python/ch-1.py35
-rwxr-xr-xchallenge-237/sgreen/python/ch-2.py29
2 files changed, 64 insertions, 0 deletions
diff --git a/challenge-237/sgreen/python/ch-1.py b/challenge-237/sgreen/python/ch-1.py
new file mode 100755
index 0000000000..3c13419a0b
--- /dev/null
+++ b/challenge-237/sgreen/python/ch-1.py
@@ -0,0 +1,35 @@
+#!/usr/bin/env python3
+
+import sys
+from datetime import date, timedelta
+
+
+def main(year, month, week, dofw):
+ # Get the first of the month
+ dte = date(year, month, 1)
+
+ # Day of the week for the first of the month (1 - Monday, 7 - Sunday)
+ first_weekday = dte.isoweekday()
+
+ # To get the first day of week we take the diff between the two values. If
+ # it is negative, add 7 days
+ add_days = dofw - first_weekday
+ if add_days < 0:
+ add_days += 7
+
+ # Now add the weeks
+ add_days += (week-1) * 7
+
+ new_date = dte + timedelta(days=add_days)
+ if new_date.month != dte.month or new_date.year != dte.year:
+ # This date does not exist
+ print(0)
+ else:
+ # Print the day of month
+ print(new_date.day)
+
+
+if __name__ == '__main__':
+ # Convert input into integers
+ array = [int(n) for n in sys.argv[1:]]
+ main(*array)
diff --git a/challenge-237/sgreen/python/ch-2.py b/challenge-237/sgreen/python/ch-2.py
new file mode 100755
index 0000000000..51dbdc3f26
--- /dev/null
+++ b/challenge-237/sgreen/python/ch-2.py
@@ -0,0 +1,29 @@
+#!/usr/bin/env python3
+
+import sys
+
+
+def main(ints):
+ sorted_ints = sorted(ints, reverse=True)
+ count = 0
+
+ for i in sorted_ints:
+ # If there isn't a solution, exit the loop
+ if min(ints) >= i:
+ break
+
+ # Find the position of the maximum value < i, and delete it
+ m = max(j for j in ints if j < i)
+ idx = ints.index(m)
+ del ints[idx]
+
+ count += 1
+
+ # Print the solution
+ print(count)
+
+
+if __name__ == '__main__':
+ # Convert input into integers
+ array = [int(n) for n in sys.argv[1:]]
+ main(array)