File tree 2 files changed +49
-0
lines changed
2 files changed +49
-0
lines changed Original file line number Diff line number Diff line change
1
+ import pytest
2
+ from working import convert
3
+
4
+ def test_validate_correct ():
5
+ assert convert ("9 AM to 5 PM" ) == "09:00 to 17:00"
6
+ assert convert ("9:00 AM to 5:30 PM" ) == "09:00 to 17:30"
7
+ assert convert ("9:00 PM to 5:30 AM" ) == "21:00 to 05:30"
8
+
9
+ def test_value_error_to ():
10
+ with pytest .raises (ValueError ):
11
+ assert convert ("9:00 AM lol 5:30 PM" )
12
+
13
+ def test_value_error_range ():
14
+ with pytest .raises (ValueError ):
15
+ assert convert ("13:00 AM to 69:30 PM" )
Original file line number Diff line number Diff line change
1
+ import re
2
+
3
+
4
+ def main ():
5
+ print (convert (input ("Hours: " )))
6
+
7
+
8
+ def convert (s ):
9
+ if x := re .search (r"^(1?\d)(:[0-5]\d)? (AM|PM) to (1?\d)(:[0-5]\d)? (AM|PM)$" , s ):
10
+ if x .group (3 ) == "AM" and x .group (6 ) == "PM" :
11
+ if 0 < int (x .group (1 )) < 13 and 0 < int (x .group (4 )) < 13 :
12
+ p = x .group (2 )
13
+ q = x .group (5 )
14
+ if not p : p = ":00"
15
+ if not q : q = ":00"
16
+ if (x .group (1 ) and x .group (4 )) == "12" :
17
+ return f"00{ p } to { x .group (4 )} { q } "
18
+ return f"{ x .group (1 ).zfill (2 )} { p } to { int (x .group (4 ))+ 12 } { q } "
19
+ pass
20
+ elif x .group (3 ) == "PM" and x .group (6 ) == "AM" :
21
+ if 0 < int (x .group (1 )) < 13 and 0 < int (x .group (4 )) < 13 :
22
+ p = x .group (2 )
23
+ q = x .group (5 )
24
+ if not p : p = ":00"
25
+ if not q : q = ":00"
26
+ return f"{ int (x .group (1 ))+ 12 } { p } to { x .group (4 ).zfill (2 )} { q } "
27
+ pass
28
+ raise ValueError ("lol" )
29
+ else :
30
+ raise ValueError ("lol" )
31
+
32
+
33
+ if __name__ == "__main__" :
34
+ main ()
You can’t perform that action at this time.
0 commit comments