Codewars-python每日练习(4)

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())


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值