0

I'm trying to convert these strings of numbers into a "hh:mm:ss" format. The strings are all different lengths, here are a few:

(212812, 218654, 232527, 235959, 0, 181240, 25959, 153834)

So I want to turn the above numbers into this:

(21:28:12, 21:86:54, 23:25:27, 23:59:59, 00:00:00, 18:12:40, 2:59:59, 15:38:34)

I'm mostly having trouble with getting them all the same length, like converting 0 to 00:00:00.

Thanks!

2
  • Since you're making up (or someone you know is making up) your own time serialization format, perhaps you could explain it to us if we're meant to help you deserialize it? What time is 1234? Commented Jun 23, 2016 at 18:32
  • Why does 0 become 00:00:00, but 25959 only becomes 2:59:59 and not 02:59:59? Commented Jun 23, 2016 at 18:38

4 Answers 4

2

As others have pointed out in comments, it's unclear what the right answer is for some inputs (e.g. 1234, which my code would say is 00:12:34). I also decided that 02:59:59 is a better answer than 2:59:59, given the desire to get 00:00:00.

So here's my code, which deals with all of the above inputs correctly, modulo the 2:59:59 variation I chose:

import re

def convert(numeric_time):
    return ':'.join(re.findall('..', str(numeric_time).zfill(6)))

times = (212812, 218654, 232527, 235959, 0, 181240, 25959, 153834)
correct_answers = ['21:28:12', '21:86:54', '23:25:27', '23:59:59', '00:00:00', '18:12:40', '02:59:59', '15:38:34']

answers = map(convert, times)
for answer, correct_answer in zip(answers, correct_answers):
    assert answer == correct_answer, '{} != {}'.format(answer, correct_answer)

UPDATE

Since some people object to the regular expression, here's a similar version that doesn't rely on it:

def convert(numeric_time):
    padded_time = str(numeric_time).zfill(6)
    return ':'.join(padded_time[i:i+2] for i in range(0, len(padded_time), 2))
Sign up to request clarification or add additional context in comments.

Comments

1

Well since we're making up answers here's a "solution" that doesn't use regexes:

In [3]: def weird_time_format(fmt):
   ...:     fmt = str(fmt)
   ...:     hours = fmt[:2].ljust(2, '0')
   ...:     mins = fmt[2:4].ljust(2, '0')
   ...:     secs = fmt[4:6].ljust(2, '0')
   ...:     return ':'.join((hours, mins, secs))
   ...:

In [4]: weird_time_format(212812)
Out[4]: '21:28:12'

This takes advantage of the fact that string slices are nice about out-of-bound indexes and return an empty string rather than throwing an error:

In [1]: ''[1:2]
Out[1]: ''

In [2]: ''[1:2].ljust(2, '0')
Out[2]: '00'

Here's the results for your example input:

In [5]: example_input = (212812, 218654, 232527, 235959, 0, 181240, 25959, 153834)

In [6]: tuple(map(weird_time_format, example_input))
Out[6]:
('21:28:12',
 '21:86:54',
 '23:25:27',
 '23:59:59',
 '00:00:00',
 '18:12:40',
 '25:95:90',
 '15:38:34')

And since I brought it up, what it does to 1234:

In [7]: weird_time_format(1234)
Out[7]: '12:34:00'

OK, I felt (a little) bad for being facetious. If you're genuinely interested in this approach, this will work better and is more in line with the other answer's output:

In [3]: def weird_time_format(fmt):
   ...:     fmt = str(fmt).rjust(6, '0')
   ...:     return ':'.join((fmt[:2], fmt[2:4], fmt[4:6]))

4 Comments

What time is it? "95 minutes past 25, by my watch."
Why would you pad zeros to the right rather than the left?
Because then you get fun times like 25:95:90 to go along with 21:86:54 from the example.
Oh, I see. Taking the more cynical approach. :-)
0

Here is another attempt.

The basic concept is adding 0 if input string length less than 6.

a = (212812, 218654, 232527, 235959, 0, 181240, 25959, 153834)
input_list = map(str,a)
for input in input_list:
    if len(input) != 6:
        input = ''.join(['0' for i in range((6 - len(input)))]+list(input))
    print ':'.join([input[i:i+chunk_size] for i in range(0,len(input),len(input)/3)])  

Result

21:28:12
21:86:54
23:25:27
23:59:59
00:00:00
18:12:40
02:59:59
15:38:34

Comments

0

Hope this helps:

data = (212812, 218654, 232527, 235959, 0, 181240, 25959, 153834)
time = map(str, data) #int to string


def conv(time_array):
    for t in time_array:
        if(len(t) != 6):
            t = str(t.zfill(6)) #filling 0's if less than 6
        print ':'.join(t[i:i + 2] for i in range(0, len(t), 2)) #adding ':' 

conv(time)

OUTPUT Using some test data

test = (1234, 23, 133, 6)
00:12:34
00:00:23
00:01:33
00:00:06

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.