程序员文章、书籍推荐和程序员创业信息与资源分享平台

网站首页 > 技术文章 正文

Python密钥格式化(python生成密钥)

hfteth 2025-03-25 13:38:56 技术文章 10 ℃
def reformat_string(S, K):
    # 移除所有分隔符
    cleaned_S = S.replace('-', '')
    # 转换为大写
    cleaned_S = cleaned_S.upper()
    # 从末尾开始,每 K 个字符插入一个分隔符
    result = []
    count = 0
    for i in range(len(cleaned_S) - 1, -1, -1):
        result.append(cleaned_S[i])
        count += 1
        if count % K == 0 and i != 0:
            result.append('-')
    # 反转结果并转换为字符串
    result = ''.join(reversed(result))
    return result

# 读取输入
K = int(input())
S = input().strip()

# 计算并输出结果
print(reformat_string(S, K))

方法思路

  1. 移除所有分隔符:首先,我们需要移除字符串中的所有分隔符(如破折号)。
  2. 转换小写字母为大写:将字符串中的所有小写字母转换为大写字母。
  3. 重新插入分隔符:从字符串的末尾开始,每 K 个字符插入一个分隔符,但第一个子串不需要插入分隔符。

Tags:

最近发表
标签列表