【每日一题20221009】处于顶端的字符串

:mage: 编写一个函数,该函数接受消息字符串并从最高到最低返回字符串的本地顶部。

字符串顶部是通过以下方式显示字符串:

                                                      3 
                              p                     2   4
            g               o   q                 1
  b       f   h           n       r             z 
a   c   e       i       m          s          y
      d           j   l             t       x
                    k                 u   w 
                                        v

下一个顶部总是比上一个高1个字符。对于上面的示例,abcdefghijklmnopqrstuvwxyz1234输入字符串的解决方案是3pgb。

当消息字符串为空时,返回一个空字符串。
输入字符串可能很长。确保您的解决方案具有良好的性能。

题目难度:简单
题目来源: String tops | Codewars

def tops(msg:str) -> str:
    # your code here
    return ""

assert tops("") == ""
assert tops("12") == "2"
assert tops("abcdefghijklmnopqrstuvwxyz12345") == "3pgb"
assert tops("abcdefghijklmnopqrstuvwxyz1236789ABCDEFGHIJKLMN") == "M3pgb"

性能方面有待优化

def tops(msg:str) -> str:
    length = len(msg)
    count = 0
    lists = []
    if length <= 1: return msg
    while 1:
        if (time:=(4*count +3)) <= length:
            lists.append(msg[:time])
            msg = msg[time:]
            length -= time
            count += 1
        else:
            lists.append(msg)
            break
    return''.join([ lists[i][(4*i+3)//2] for i in range(len(lists))][::-1])

assert tops("") == ""
assert tops("12") == "2"
assert tops("abcdefghijklmnopqrstuvwxyz12345") == "3pgb"
assert tops("abcdefghijklmnopqrstuvwxyz1236789ABCDEFGHIJKLMN") == "M3pgb"
def tops(msg:str) -> str:
    if msg=='':return ''
    try:
        x,n=1,5
        list_result=[msg[x],]
        for i in msg:
            list_result.append(msg[x+n])
            x,n=x+n,n+4
    except IndexError:
        return ''.join(list_result[::-1])
def tops(msg: str) -> str:
    # your code here
    result = []
    if msg != result:
        i = 1
        x = 0
        while i <= len(msg):
            result.append(msg[i])
            x += 1
            i += x * 4 + 1
        result.reverse()
        return "".join(result)
    else:
        return ""
def tops(msg):
    list1 = []
    i = 0
    j = 0
    str2 = ''
    while True:
        j = j+1
        str1 = msg[i:i+j]
        i = i + j
        if str1 != '' and (j%2)==0:
           list1.append(str1)
        if str1 == '':
            break

    for i in list1:
        str2 += i[0]
    return str2[::-1]

msg1 = 'abcdefghijklmnopqrstuvwxyz12345'
res = tops(msg1)
print(res)
def tops(msg: str) -> str:
    # 通过数学计算,可以得出第n个顶部值所在的位置为:2 * (n ** 2) - n + 1
    # 索引从0开始(需要减1),所以第n个顶部值所在的索引位置为:2 * (n ** 2) - n
    if msg == "":  # 当消息字符串为空时,返回一个空字符串
        return ""
    else:
        top_num_list = []
        for n in range(1, len(msg)):
            top_msg_index = 2 * (n ** 2) - n
            if top_msg_index >= len(msg):  # 当最后一个顶部值所在的索引位置 大于 字符长度,则退出循环
                break
            top_num_list.append(msg[top_msg_index])  # 获得字符串的顶部值,并按倒序排序
        top_num_list.reverse()
        return "".join(top_num_list)  # 列表格式转成字符串格式

    assert tops("") == ""
    assert tops("12") == "2"
    assert tops("abcdefghijklmnopqrstuvwxyz12345") == "3pgb"
    assert tops("abcdefghijklmnopqrstuvwxyz1236789ABCDEFGHIJKLMN") == "M3pgb"

def tops(msg:str) -> str:
    # your code here
    i = 0
    str_len = len(msg)
    result = ''
    n = 0
    if str_len > 0 :
        while True:
            step = 4*n+3
            sub_str = msg[i:i+step]
            sub_len = len(sub_str)
            if sub_len < step:
                sub_str = sub_str + ' '*(step - sub_len)
            result = sub_str[len(sub_str)//2] + result
            i += step
            n += 1
            if i > str_len:
                break
        return result
    return ""

找出来规律即可

// 1 6 15 28 45
// 1 5 9 13 17
// 4 4 4

func tops(s string) string {
	var res string
	for i, index := 0, 1; index <= len(s); i++ {
		res = string(s[index]) + res
		index += 1 + 4*(i+1)
	}
	return res
}
def tops(msg:str) -> str:
    res_list=[]
    if msg=="":
        return ""
    else:
        n=1
        for i in range(len(msg)):
            if 2*n**2-n==i:
                res_list.append(msg[i])
                n+=1
        return ''.join(res_list)[::-1]

assert tops("") == ""
assert tops("12") == "2"
assert tops("abcdefghijklmnopqrstuvwxyz12345") == "3pgb"
assert tops("abcdefghijklmnopqrstuvwxyz1236789ABCDEFGHIJKLMN") == "M3pgb"