aboutsummaryrefslogtreecommitdiff
path: root/challenge-100/abigail/python/ch-1.py
diff options
context:
space:
mode:
authorAbigail <abigail@abigail.be>2021-02-18 18:37:20 +0100
committerAbigail <abigail@abigail.be>2021-02-18 18:37:20 +0100
commitfb62180005f55612297b8a97d061ace97e2d2dc7 (patch)
treea19a6b4c809418927cda318e44599ea8fb994c30 /challenge-100/abigail/python/ch-1.py
parent986d1fe0c2e8e37dbbda7fa50ef1e797d58b0df3 (diff)
downloadperlweeklychallenge-club-fb62180005f55612297b8a97d061ace97e2d2dc7.tar.gz
perlweeklychallenge-club-fb62180005f55612297b8a97d061ace97e2d2dc7.tar.bz2
perlweeklychallenge-club-fb62180005f55612297b8a97d061ace97e2d2dc7.zip
Python solution for week 100, part 1
Diffstat (limited to 'challenge-100/abigail/python/ch-1.py')
-rw-r--r--challenge-100/abigail/python/ch-1.py50
1 files changed, 50 insertions, 0 deletions
diff --git a/challenge-100/abigail/python/ch-1.py b/challenge-100/abigail/python/ch-1.py
new file mode 100644
index 0000000000..c236191663
--- /dev/null
+++ b/challenge-100/abigail/python/ch-1.py
@@ -0,0 +1,50 @@
+#!/opt/local/bin/python
+
+#
+# See ../README.md
+#
+
+#
+# Run as python ch-1.py < input-file
+#
+
+import fileinput
+import re
+
+
+for line in fileinput . input ():
+ #
+ # Parse input
+ #
+ hour, minute, ampm = re . compile (r'([0-9]+):([0-9]+)\s*([ap]?)') \
+ . match (line) \
+ . groups ()
+ #
+ # Make sure we have integers
+ #
+ hour = int (hour)
+ minute = int (minute)
+
+ #
+ # Calculate new AM/PM marker
+ #
+ new_ampm = ""
+ if ampm == "":
+ if hour >= 12:
+ new_ampm = "pm"
+ else:
+ new_ampm = "am"
+
+ #
+ # Calculate new hour
+ #
+ hour = hour % 12
+ if ampm == "" and hour == 0:
+ hour = 12
+ if ampm == "p":
+ hour = hour + 12
+
+ #
+ # Print result
+ #
+ print ("{:02d}:{:02d}{:s}" . format (hour, minute, new_ampm))