language: Python 3 (python-3.2.3)
date: 466 days 10 hours ago
link:
visibility: private
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import re
import sys
import time
 
def minutes(timestr):
    """Return number of minutes in timestr that must be either ##:## or #:##."""
    m = re.match(r"(\d?\d):(\d\d)$", timestr)
    if m is None:
       raise ValueError("Invalid timestr: %r" % (timestr,))
    h, m = map(int, m.groups())
    return 60*h + m
 
def minutes2(timestr):
    h, m = map(int, timestr.partition(':')[::2])
    return 60*h + m
 
def minutes3(timestr):
    t = time.strptime(timestr, "%H:%M")
    return 60*t.tm_hour + t.tm_min
 
 
for timestr in sys.stdin:
    timestr = timestr.rstrip('\n')
    for f in [minutes, minutes2, minutes3]:
        try:
            result = f(timestr)
        except ValueError as e:
            print("%-8s(%5r) -> error: %s" % (f.__name__, timestr, e))
        else:
            print("%-8s(%5r) -> %s" % (f.__name__, timestr, result))
  • upload with new input
  • result: Success     time: 0.03s    memory: 6000 kB     returned value: 0

    12:11
      12:11
    12:11  
    3:45
    03:45
    13:4
    13:04
    24:00
    11:60
    
    minutes ('12:11') -> 731
    minutes2('12:11') -> 731
    minutes3('12:11') -> 731
    minutes ('  12:11') -> error: Invalid timestr: '  12:11'
    minutes2('  12:11') -> 731
    minutes3('  12:11') -> error: time data '  12:11' does not match format '%H:%M'
    minutes ('12:11  ') -> error: Invalid timestr: '12:11  '
    minutes2('12:11  ') -> 731
    minutes3('12:11  ') -> error: unconverted data remains:   
    minutes ('3:45') -> 225
    minutes2('3:45') -> 225
    minutes3('3:45') -> 225
    minutes ('03:45') -> 225
    minutes2('03:45') -> 225
    minutes3('03:45') -> 225
    minutes ('13:4') -> error: Invalid timestr: '13:4'
    minutes2('13:4') -> 784
    minutes3('13:4') -> 784
    minutes ('13:04') -> 784
    minutes2('13:04') -> 784
    minutes3('13:04') -> 784
    minutes ('24:00') -> 1440
    minutes2('24:00') -> 1440
    minutes3('24:00') -> error: time data '24:00' does not match format '%H:%M'
    minutes ('11:60') -> 720
    minutes2('11:60') -> 720
    minutes3('11:60') -> error: unconverted data remains: 0