来自一名 OIer 的 Github Copilot 体验记

· · 个人记录

简介

GitHub Copilot 是 GitHub 和 OpenAI 开发的人工智能工具,用户在使用 Visual Studio Code 时可以通过 GitHub Copilot 自动补全代码。它于 2021 年 6 月 29 日由 GitHub 向外推出。 ——wikipedia Github Copilot

Copilot 现在处于预览阶段,使用前需要申请。我在刚刚推出几天时提交了申请,然后 10 月 29 日才通过。等轮到这篇文章上咯咕日报不知道是多久了。

通过申请后,在 VSCode 上安装相应扩展,登录 Github 账号,即可使用 Copilot。

补全时,其可能给出一行,也可能给出一整个函数;使用 Tab 可以接受补全,而 Alt+[ 和 Alt+] 可以切换其他选项。

下面我们来测试一下 Copilot 的能力。测试内容包括:入门题目、板子、补全片段以及补全自然语言。

入门题目

首先我们让 Copilot 写几道入门题目。题目来源于洛谷的入门题库与 Codeforces 的 Div3。我们让 Copilot 在如下代码的基础上补全:

#include<bits/stdc++.h>
using namespace std;

/*
题面
*/

int main(){

}

带余除法

洛谷 B2010

#include<bits/stdc++.h>
using namespace std;

/*
(题面略)
*/

int main(){
    int a,b;
    cin>>a>>b;
    cout<<a/b<<" "<<a%b;
    return 0;
}

基本操作。

反向输出一个三位数

洛谷 B2028

#include<bits/stdc++.h>
using namespace std;

/*
(题面略)
*/

int main(){
    int n;
    cin >> n;
    cout << n / 100 << n % 100 / 10 << n % 10;
    return 0;
}

字符三角形

洛谷 B2005

#include<bits/stdc++.h>
using namespace std;

/*
(题面略)
*/

int main(){
    char c;
    cin >> c;
    cout << c << endl;
    cout << "  " << c << endl;
    cout << " " << c << " " << c << endl;
    cout << c << " " << c << " " << c << endl;
    return 0;
}

效果看起来并不好……

Linear Keyboard

CF1607A

#include<bits/stdc++.h>
using namespace std;

/*
(题面略)
*/

int main(){
    int t;
    cin>>t;
    while(t--){
        string keyboard,word;
        cin>>keyboard>>word;
        int n=keyboard.size();
        int dp[n+1][n+1];
        for(int i=0;i<=n;i++)
            for(int j=0;j<=n;j++)
                dp[i][j]=0;
        for(int i=1;i<=n;i++){
            dp[i][i]=0;
            for(int j=i+1;j<=n;j++){
                dp[i][j]=dp[i][j-1]+abs(keyboard[j-1]-keyboard[j-2]);
            }
        }
        int ans=0;
        for(int i=0;i<word.size();i++){
            int l=0,r=n;
            while(l<r){
                int mid=(l+r)/2;
                if(keyboard[mid]==word[i]){
                    ans+=dp[i][mid];
                    break;
                }
                else if(keyboard[mid]<word[i])
                    l=mid+1;
                else
                    r=mid-1;
            }
        }
        cout<<ans<<endl;
    }
    return 0;
}

这个代码的逻辑我无法理解,并且(不出人意料地)没有通过样例。

将题目描述改为:

There is a linear keyboard. And there is a word, which is made up of lowercase letters. We need to type the word. Each time we type a letter, the hand moves from the last letter to the next letter on the keyboard, and the distance will be the difference of the index of the two letters on the keyboard. Caculate the distance the hand moves when typing the word.

For example, when we type the word "abc" on "qwertyuiopasdfghjklzxcvbnm" keyboard, the distance will be |keyboard.find("a")-keyboard.find("b")|+|keyboard.find("b")-keyboard.find("c")|=|23-10|+|21-23|=15. Note that you should convert the return value of find() to int.

得到的代码要么没把 find() 转换成 int,要么 for 循环的边界条件不对。最后怎么调也调不出来。/kk

板子

下面我们用 /*<<*/ 和 /*<<*/ 来标出补全的部分

并查集

#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10;
int f[N];
int find/*<<*/(int x)
{
    if(f[x]==x)return x;
    return f[x]=find(f[x]);
}/*>>*/

int main(){
    /*<<*/
    int n,m;
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)f[i]=i;
    for(int i=0;i<m;i++){
        int x,y;
        scanf("%d%d",&x,&y);
        int a=find(x),b=find(y);
        if(a!=b)f[a]=b;
    }
    int ans=0;
    for(int i=1;i<=n;i++)
        if(f[i]==i)ans++;
    printf("%d\n",ans);
    return 0;
    /*>>*/
}

DFS

有意使用了拼音函数名(sousuo)。

#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10;

vector<int>to[N];// a bidirectional graph
int dep[N];
void add(int u,int v)/*<<*/{
    to[u].push_back(v);
    to[v].push_back(u);
}/*>>*/
void sousuo/*<<*/(int u,int fa){
    dep[u]=dep[fa]+1;
    for(int i=0;i<to[u].size();i++){
        int v=to[u][i];
        if(v==fa)continue;
        sousuo(v,u);
    }
}/*>>*/
/*<<*/int main(){
    int n,m;
    scanf("%d%d",&n,&m);
    for(int i=1;i<=m;i++){
        int u,v;
        scanf("%d%d",&u,&v);
        add(u,v);
    }
    sousuo(1,0);
    for(int i=1;i<=n;i++){
        printf("%d\n",dep[i]);
    }
    return 0;
}/*>>*/

Tarjan

#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10;

vector<int>to[N];
int dep[N];
void add(int u,int v){
    to[u].push_back(v);
}

int dfn[N],cnt,/*<<*/low[N],stk[N],top;/*>>*/
int scc[N],scc_cnt;

void /*<<*/tarjan(int u){
    dfn[u]=low[u]=++cnt;
    stk[top++]=u;
    for(int i=0;i<to[u].size();i++){
        int v=to[u][i];
        if(!dfn[v]){
            tarjan(v);
            low[u]=min(low[u],low[v]);
        }
        else if(!scc[v])
            low[u]=min(low[u],dfn[v]);
    }
    if(dfn[u]==low[u]){
        scc_cnt++;
        while(stk[top-1]!=u){
            scc[stk[--top]]=scc_cnt;
        }
        scc[stk[--top]]=scc_cnt;
    }
}/*>>*/

int main(){
    /*<<*/
    int n,m;
    scanf("%d%d",&n,&m);
    for(int i=1;i<=m;i++){
        int u,v;
        scanf("%d%d",&u,&v);
        add(u,v);
        add(v,u);
    }
    for(int i=1;i<=n;i++)
        if(!dfn[i])
            tarjan(i);
    for(int i=1;i<=n;i++)
        printf("%d\n",scc[i]);
    return 0;
    /*>>*/
}

Dinic

代码太长,看剪贴板吧。

该板子没有当前弧等优化。

补全片段

对着我的提交手打了一遍,并标注出补全的部分。

#include <bits/stdc++.h>
using namespace std;
int getint(){ int x;cin>>x;return x; }
#define ll long long
const int N=1e5+10,mod=998244353;
int a[N];
int f[N],g[N];

int main(){
    ios::sync_with_stdio/*<<*/(0)/*>>*/;
    /*<<*/cin.tie(0);/*>>*/
    int T;cin>>T;
    while(T--){
        int n;cin>>n;
        for/*<<*/(int i=1;i<=n;i++)cin>>a[i];/*>>*/
        memset(/*<<*/f,0,sizeof(f));/*>>*/
        f[a[n]]=1;
        int ans=0;
        for(int i=n-1;i>=1;--i){
            int m=a[i+1];
            for(int J=1,j;J<=m;J=(m/j)+1){
                j=/*<<*/m/J;/*>>*/
                if(!/*<<*/f[j])continue;/*>>*/
                int w=(j+a[i]-1)/j;
                ans=(ans+(w-1ll)*f[j]%mod*i)/*<<*/%mod;/*>>*/
                g[a[i]/w]+=/*<<*/f[j];/*>>*/
            }
            g[/*<<*/a[i]]++;/*>>*/
            for(int J=1,j;J<=m/*<<*/;J=(m/j)+1){
                j=m/J;/*>>*/
                f[j]=0;
            }
            for(int J=1,j;J<=m;J=(a[i]/j)+1){
                j=a[i]/J;
                f[j]=/*<<*/g[j];/*>>*/
                /*<<*/g[j]=0;/*>>*/
            }
        }
        /*<<*/cout<<ans<<endl;/*>>*/
    }
}

自然语言

我们直接展现写作这一段时[的结果,不过这个结果是不对的,因为我们的答案是按照给定的顺序输出的,而不是按照给定的顺序输出的。] Copilot 给出的部分补全,用 [] [标注出补全的部分。]

我们知道,Copilot 是在代码上[提交的,所以我们可以直接提交。]训练的,因此它补全自然语言的效果[很好。]不尽人意。其经常会吐出一些废话文学,或者[是没有补全的部分。]给题面加上一个不可做[的限制。]数据范围。

But, [we can see that the Copilot is trained on the code, so the Copilot's completion is not very good. It sometimes spits out some rubbish, or adds a useless data range.]

由于 Copilot 的主业并非[是写代码,所以它的补全效果不是很好。]补全自然语言(更不是补全中文),所以其效果远差于前几节。

只是这一段 Copilot 的补全内容的自我定位[是有问题的,它的补全内容是指代码的补全,而不是自然语言的补全。]不太对劲(大雾)。

总结

Copilot 可以很好地提高代码效率,以及智能的抄板(自动适配码风和变量名)以及智能的复制粘贴(如果两段代码只有少量不同)。不过它完全没法帮你想做法或者推式子。反正 OIer 的饭碗是[没有饭的。]稳的。