You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

技术问询:基于给定字符串与整数n生成递减前缀拼接字符串

Alright, let's tackle this string-building problem step by step! Here's everything you need to know to generate the required output.

Problem Breakdown

First, let's make sure we're on the same page with the requirements:

Given a string s and an integer n (where 0 ≤ n ≤ the length of s), create a new string by concatenating:

  • The first n characters of s,
  • Followed by the first n-1 characters,
  • Keep going until you append just the first 1 character of s.

Example: If the input string is "chocolate" and n=4, the output should be "Chocchochc" (note the first segment is capitalized here—we'll cover that formatting quirk too).

Approach

Here's the game plan to build this string:

  1. Handle edge cases first: If n is 0, we just return an empty string immediately—there's nothing to concatenate.
  2. Loop from n down to 1: For each number k in this range, take the substring of the first k characters from the original string and add it to our result.
  3. Optional capitalization: The example shows the first segment capitalized even though the input was lowercase. If you need that exact formatting, we can tweak the result to capitalize just the first n characters.
Solution Code (Python)

Python makes this super straightforward with string slicing. Here's a function that does exactly what we need:

def build_custom_string(input_str, n):
    result = ""
    # Edge case: n=0 means no characters to add
    if n == 0:
        return result
    # Iterate from n down to 1, appending each substring
    for k in range(n, 0, -1):
        result += input_str[:k]
    # Uncomment the line below if you need the first segment capitalized (matches the example)
    # result = result[:n].capitalize() + result[n:]
    return result

# Test with the example provided
print(build_custom_string("chocolate", 4))
# Without capitalization: outputs "chocchochc"
# With capitalization uncommented: outputs "Chocchochc" (matches the example)
How It Works
  • Edge case handling: When n=0, we skip the loop entirely and return an empty string—no wasted computation here.
  • The loop: Using range(n, 0, -1) gives us a sequence like 4,3,2,1 for the example. For each k, input_str[:k] grabs the first k characters (Python slicing is zero-indexed, so this works perfectly).
  • Capitalization tweak: The optional line takes the first n characters (the first segment we added), capitalizes them, and combines them with the rest of the result to match the example's formatting.
Quick Test Cases to Verify

Let's check a few more scenarios to make sure the function works:

  • Input: input_str="hello", n=3 → Output (without capitalization): "helheh"
  • Input: input_str="a", n=1 → Output: "a"
  • Input: input_str="test", n=0 → Output: ""

内容的提问来源于stack exchange,提问作者johnson

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 10:43:38