0
点赞
收藏
分享

微信扫一扫

C语言:L1-064 估值一亿的AI核心代码 (20 分)

小沙坨 2022-01-21 阅读 80

文章目录

一、题目

输入格式:

输出格式:

输入样例:

输出样例:

二、方法1

1、思路

本题运用了正则表达式

  1. 除去首尾的空格;
  2. 除去多余空格;
  3. 除去标点符号前的空格;
  4. 将 ? 替换为 !;
  5. 把除 I 以外的所以字符变为小写;
  6. 先分别存放 can I,could I 中的 can,could,再添加 you(将 I,me改为 you)。

2、代码

import re
for i in range(int(input())):
    str1=input()
    print(str1)
    print('AI: ', end='')
    str1=str1.strip()

    str1 = re.sub(r'(\s+)', ' ', str1)

    str3 = re.compile(r' +(\W)')
    str1 = str3.sub(r"\1", str1)

    str1 = re.sub('\?', '!', str1)

    for j in str1:
        if j != 'I':
            str1 = str1.replace(j, j.lower())

    str1 = re.sub(r'(\bcan you\b)', '_I can', str1)
    str1 = re.sub(r'(\bcould you\b)', '_I could', str1)
    str1 = re.sub(r'(\bI\b)', 'you', str1)
    str1 = re.sub(r'(\bme\b)', 'you', str1)
    str1 = re.sub(r'(\b_I\b)', 'I', str1)

    print(str1)
举报

相关推荐

0 条评论