使用exercism.io成为更好的程序员

在过去的一周里,我一直在尝试使用编程练习应用 exercism.io。我在去年12月通过Gabor 博客 了解到它,但直到现在才尝试。我希望我没有等那么久,因为exercism非常有乐趣。

exercism是如何工作的

exercism附带一个命令行应用程序,可以下载编程练习并将您的编码答案提交到exercism网站。每个编程练习都附带一个readme文件和一个测试文件。为了完成练习,您需要编写一个Perl模块,以便通过所有测试。

当您完成并通过命令行应用程序提交了您的Perl模块后,您和其他程序员可以“吹毛求疵”您的代码并对其进行评论。一旦您觉得已经收到了足够的评论,您就可以最终提交您的作品并查看其他程序员对同一练习的解决方案。关键是您一次只能收到一个编程练习,并且必须在完成当前练习之前才能访问另一个练习。

Perl的TIMTOWTDI特性意味着对于练习有多种正确答案,您经常可以从查看其他程序员的解决方案中学习到一些东西。例如,在一个练习中,我仅使用正则表达式,但发现更简单的转义运算符效果一样好。练习难度从简单到困难不等,但真正的挑战是找到一个干净、通用的解决方案。

尝试一下

下载您平台上的最新二进制文件并提取它。打开命令行并输入

$ exercism demo

这将获取第一个练习(撰写本文时为“Bob”)。测试文件和readme文件位于“perl5/bob/”中。打开readme文件,您会看到

# Bob

Bob is a lackadaisical teenager. In conversation, his responses are very limited.

Bob answers 'Sure.' if you ask him a question.

He answers 'Woah, chill out!' if you yell at him.

He says 'Fine. Be that way!' if you address him without actually saying anything.

He answers 'Whatever.' to anything else.

## Instructions

Run the test file, and fix each of the errors in turn. When you get the first test to pass, go to the first pending or skipped test, and make that pass as well. When all of the tests are passing, feel free to submit. 

Remember that passing code is just the first step. The goal is to work towards a solution that is as readable and expressive as you can make it. 

Please make your solution as general as possible. Good code doesn't just pass the test suite, it works with any input that fits the specification.

Have fun!

## Source

Inspired by the 'Deaf Grandma' exercise in Chris Pine's Learn to Program tutorial. [view source](http://pine.fm/LearnToProgram/?Chapter=06)

要运行测试文件,请切换到练习目录并使用prove

$ cd perl5/bob
$ prove bob.t

这将得到以下输出

bob.t .. 1/22 Bailout called.  Further testing stopped:  You need to create a module called Bob.pm with a function called hey() that gets one parameter: The text Bob hears.

#   Failed test 'missing Bob.pm'
#   at bob.t line 37.
FAILED--Further testing stopped: You need to create a module called Bob.pm with a function called hey() that gets one parameter: The text Bob hears.

让我们创建一个基本的Bob.pm模块

package Bob;
use warnings;
use strict;

sub hey {
    my $input = shift;
}

1;

这是解决方案的框架。我们的“hey”子程序返回它接收到的第一个输入。重新运行prove,我们得到以下输出

$ prove bob.t
bob.t .. 1/22 
#   Failed test 'stating something: Tom-ay-to, tom-aaaah-to.'
#   at bob.t line 52.
#          got: 'Tom-ay-to, tom-aaaah-to.'
#     expected: 'Whatever.'

...

# Looks like you failed 19 tests of 22.
bob.t .. Dubious, test returned 19 (wstat 4864, 0x1300)
Failed 19/22 subtests 

Test Summary Report
-------------------
bob.t (Wstat: 4864 Tests: 22 Failed: 19)
  Failed tests:  4-22
  Non-zero exit status: 19
Files=1, Tests=22,  1 wallclock secs ( 0.02 usr  0.00 sys +  0.04 cusr  0.00 csys =  0.06 CPU)
Result: FAIL

您可以看到Bailout不再被调用,所以我们的基本模块通过了前几个测试,但失败了22个中的19个。我已缩短输出以仅显示第一个失败的测试。输出告诉我们所需的一切:我们的“hey”子程序没有返回符合规范的所需内容。我将在这里留下这个练习——如果您觉得足够有灵感,请尝试完成它。

帮助代表Perl

exercism的一个优势是它在不同的编程语言中具有相同的编程练习,因此您可以培养您的多语言技能。查看源代码,JavaScript、Python、Ruby和Haskell似乎处于领先地位,每个都有大约55个练习。Perl有36个练习,其他语言大约有20个或更少。

本周,我将几个缺失的练习移植到了Perl 仓库。为了移植一个缺失的练习,您必须提供测试文件和模块解决方案。这比听起来容易,因为您只需将练习代码从另一种语言翻译成Perl。在所有语言中大约有80个不同的练习。我创建了一个任务,列出了缺失的练习。

将一个练习迁移只需要大约30分钟:如果今天有2%的这篇文章的读者各迁移一个练习,Perl的练习数量将立刻超过其他任何语言。还有一个空的Perl 6仓库;将Perl 5的练习迁移到Perl 6可以成为一个有趣的Hackathon目标。

结论

玩exercism非常有趣,我在过程中学到了一些Perl技巧(哈哈!)。源代码是MIT许可的,并且贡献者是友好的。看到更多的Perl程序员参与或迁移一些练习会非常棒。您可以随意复制或修改任务。让我们向人们展示我们的语言能做什么吧!

喜欢这篇文章吗?帮助我们,并推文一下!


这篇文章最初发布在PerlTricks.com

标签

David Farrell

David是一位专业程序员,他经常推文博客关于代码和编程艺术。

查看他们的文章

反馈

这篇文章有什么问题吗?请通过在GitHub上打开问题或拉取请求来帮助我们。