【每日一题1116】格式转换

:mage:t2: 给定一个字符串:

"🍰天才小团团
26.6K 魅力值
惠子不在
24.7K 魅力值
🍰小天才团团~
26.7K"

请编写一个函数,将它输出成指定的格式:

1 🍰小天才团团~ 26.7K
2 🍰天才小团团 26.6K
3 惠子不在 24.7K

题目难度:简单
题目来源:叶开

def solution(s: str) -> str:
    # your code

before = "🍰天才小团团
26.6K 魅力值
惠子不在
24.7K 魅力值
🍰小天才团团~
26.7K"

after = """
1 🍰小天才团团~ 26.7K
2 🍰天才小团团 26.6K
3 惠子不在 24.7K
"""

assert solution(before) == after
def solution(s: str) -> str:
    after_list = []
    for w in [i for i in s.split('K') if len(i) > 0]:
        salary = float(w.split(';')[-1])
        w = w.replace('魅力值', '')
        w = w.replace('&#10', ' ')
        if w.startswith('  ;'):
            w = w[3:]
        if ';' + str(salary) in w:
            w = w.replace(';' + str(salary), str(salary))
        after_list.append((salary, w))
    after_list = sorted(after_list, key=lambda x: x[0])[::-1]
    return '\n' + '\n'.join([str(after_list.index(s) + 1) + ' ' + s[1] + 'K' for s in after_list]) + '\n'


before = "🍰天才小团团
26.6K 魅力值
惠子不在
24.7K 魅力值
🍰小天才团团~
26.7K"

after = """
1 🍰小天才团团~ 26.7K
2 🍰天才小团团 26.6K
3 惠子不在 24.7K
"""


assert solution(before) == after

好无聊的题!

from operator import itemgetter


def solution(s: str) -> str:
    result, str_res = [], '\n'
    result.append(';'.join(itemgetter(0, -1)(s.split(';', s.count(';') - 1))).replace('
', ' '))
    result.append(s.split(' ')[0].replace('
', ' '))
    result.append(' '.join(s.split(' ')[1].split('
')[1:]))
    return '\n' + ''.join([f"{index + 1} {line}\n" for index, line in enumerate(result)])


before = "🍰天才小团团
26.6K 魅力值
惠子不在
24.7K 魅力值
🍰小天才团团~
26.7K"

after = """
1 🍰小天才团团~ 26.7K
2 🍰天才小团团 26.6K
3 惠子不在 24.7K
"""

assert solution(before) == after

搞了半天才发现是头尾没加换行

def solution(s: str) -> str:
    list1 = s.split('魅力值
')
    list1 = [i.replace('
', ' ').strip() for i in list1 if '
' in i]
    return '\n' + '1 ' + list1[2] + '\n' + '2 ' + list1[0] + '\n' + '3 ' + list1[1] + '\n'


before = "🍰天才小团团
26.6K 魅力值
惠子不在
24.7K 魅力值
🍰小天才团团~
26.7K"

after = """
1 🍰小天才团团~ 26.7K
2 🍰天才小团团 26.6K
3 惠子不在 24.7K
"""

assert solution(before) == after
关闭