AI编程方法第二弹:边提问边调整

AI编程的提问词非常类似于传统搜索引擎中的检索词,虽然采取了自然语言表示,但是在获取结果的策略上却很一致。因为用户在一开始可能并不非常清楚AI编程工具如何理解用户的提问,因此输出结果可能并不能完全满足用户要求,此时用户可以不断的根据生成结果,动态的灵活的调整提问,使之不断趋近于自己满意的结果。

比如,对于“Python”等任意英文单词,允许用户指定总宽度后,通过自动填充空格,使之总宽度尽可能等于该宽度,如:

请输入长度:16
P  y  t  h  o  n

对于这样一个问题,初始的用户提问可能是“让用户输入一个整数长度,在“Python”中各个字符之间填充空格,使得总体字符串长度等于或者接近于输入的整数长度”。

可能的一个生成结果为:

def fill_spaces_in_string(target_length, input_string="Python"):
    # Calculate the number of spaces needed
    num_chars = len(input_string)
    if num_chars >= target_length:
        return input_string  # No need to add spaces if the string is already longer

    # Calculate the number of spaces to add
    # We need to distribute (target_length - num_chars) spaces between num_chars - 1 gaps
    spaces_to_add = target_length - num_chars
    if spaces_to_add <= 0:
        return input_string

    # Calculate the average number of spaces per gap and the remainder
    avg_spaces = spaces_to_add // (num_chars - 1)
    remainder = spaces_to_add % (num_chars - 1)

    # Create the result string with spaces
    result = []
    spaces_left = remainder
    for i, char in enumerate(input_string):
        result.append(char)
        if i < num_chars - 1:  # Don't add space after the last character
            spaces_to_add_here = avg_spaces + (1 if spaces_left > 0 else 0)
            result.append(' ' * spaces_to_add_here)
            spaces_left -= 1 if spaces_left > 0 else 0

    return ''.join(result)

# Get user input
try:
    target_length = int(input("请输入一个整数长度: "))
    result = fill_spaces_in_string(target_length)
    print("填充后的字符串:", result)
except ValueError:
    print("请输入一个有效的整数。")

大约生成了近35行代码,并且使用了诸如条件分支和循环等控制语句。显然,对于该问题,似乎有点小题大做了。

第一次调整:继续追加诸如“请不要超过10行代码”之类的附加提示:

def fill_python(length):
    s = "Python"
    if len(s) >= length:
        return s
    spaces = ' ' * (length - len(s) - (len(s) - 1))
    return s[0] + spaces[:len(spaces)//(len(s)-1)*(len(s)-1)] + ' '.join(s[1:]).join(' '*((length-len(s))%(len(s)-1) if length-len(s)%(len(s)-1) else '')) + s[-1] if length-len(s) > len(s)-1 else s[0] + ' '.join([c + ' ' * ((length - len(s)) // (len(s) - 1) + (1 if (i < (length - len(s)) % (len(s) - 1) else 0)) for i, c in enumerate(s[1:-1])]) + s[-1]

try:
    length = int(input("请输入一个整数长度: "))
    print(fill_python(length))
except ValueError:
    print("请输入一个有效的整数。")

但是可能生成的代码依然复杂。不过,该代码虽然略显复杂,但是稳健性更强,可以适应诸如输入数值小于现有字符串长度等特殊情况的处理,其主体逻辑功能基本一致。

第二次调整:再次优化提示词“让用户输入一个整数长度,在“Python”中各个字符之间填充空格,使得总体字符串长度等于或者接近于输入的整数长度,请不要超过10行代码,不必考虑输入数值小于现有字符串长度等特殊情况处理,力求代码简化

def fill_python(length):
    s = "Python"
    spaces = ' ' * (length - len(s) + len(s) - 1 if length > len(s) else 0)
    return ''.join(c + spaces[i:(i+len(s)-1)//(len(s)-1) if i < len(spaces) else ''] for i, c in enumerate(s[:-1])) + s[-1]

length = int(input("请输入一个整数长度: "))
print(fill_python(length))

第三次调整:有时根据用户已有的经验,可以很好的引导AI编程工具来改进,如“让用户输入一个整数长度,在“Python”中各个字符之间填充空格,使得总体字符串长度等于或者接近于输入的整数长度,请不要超过10行代码,使用字符串join函数来实现该功能,不必考虑输入数值小于现有字符串长度等特殊情况处理,如果空格分配导致整体长度也并不完全符合要求也行,不要使用任何自定义函数、循环遍历等复杂操作,力求代码简化

这是最终的效果:

AI编程方法第二弹:边提问边调整_第1张图片

因此,在设计AI编程提示词时,可以不断根据生成结果优化、调整和补充现有提示词,使之趋近于真实的代码需求。

你可能感兴趣的:(AI编程,AI编程)