1 Human Readable Time
Task
Write a function, which takes a non-negative integer (seconds) as input and returns the time in a human-readable format (HH:MM:SS)
HH = hours, padded to 2 digits, range: 00 - 99
MM = minutes, padded to 2 digits, range: 00 - 59
SS = seconds, padded to 2 digits, range: 00 - 59
The maximum time never exceeds 359999 (99:59:59)
You can find some examples in the test fixtures.
我的解决方案
def make_readable(seconds):
a = seconds // 60
ss = seconds % 60
mm = a % 60
hh = a // 60
if hh > 99:
hh = 99
if ss < 10:
ss = "0" + str(ss)
if mm < 10:
mm = "0" + str(mm)
if hh < 10:
hh = "0" + str(hh)
return str(hh)+':'+ str(mm)+':'+ str(ss)
其他优秀解决方案
def make_readable(s):
return '{:02}:{:02}:{:02}'.format(s / 3600, s / 60 % 60, s % 60)
2 Replace With Alphabet Position
Task
In this kata you are required to, given a string, replace every letter with its position in the alphabet.
If anything in the text isn’t a letter, ignore it and don’t return it.
“a” = 1, “b” = 2, etc.
Example
alphabet_position(“The sunset sets at twelve o’ clock.”)
Should return “20 8 5 19 21 14 19 5 20 19 5 20 19 1 20 20 23 5 12 22 5 15 3 12 15 3 11” (as a string)
我的解决方案
def alphabet_position(text):
all_alpha = "".join([a for a in text if a.isalpha()])
lower_all = all_alpha.lower()
ap = " ".join([str(ord(a) - 96) for a in lower_all])
return ap
其他优秀解决方案
def alphabet_position(text):
return ' '.join(str(ord(c) - 96) for c in text.lower() if c.isalpha())