我正在寻找最酷的事情,你可以在几行简单的代码。我相信你可以用Haskell用15行写一个Mandelbrot集,但是很难理解。

我的目标是启发学生编程很酷。

我们知道编程很酷,因为你可以创造任何你想象到的东西——它是最终的创意出口。我想激励这些初学者,让他们尽可能多地克服早期学习的困难。

Now, my reasons are selfish. I'm teaching an Intro to Computing course to a group of 60 half-engineering, half business majors; all freshmen. They are the students who came from underprivileged High schools. From my past experience, the group is generally split as follows: a few rock-stars, some who try very hard and kind of get it, the few who try very hard and barely get it, and the few who don't care. I want to reach as many of these groups as effectively as I can. Here's an example of how I'd use a computer program to teach:

Here's an example of what I'm looking for: a 1-line VBS script to get your computer to talk to you: CreateObject("sapi.spvoice").Speak InputBox("Enter your text","Talk it") I could use this to demonstrate order of operations. I'd show the code, let them play with it, then explain that There's a lot going on in that line, but the computer can make sense of it, because it knows the rules. Then I'd show them something like this: 4(5*5) / 10 + 9(.25 + .75) And you can see that first I need to do is (5*5). Then I can multiply for 4. And now I've created the Object. Dividing by 10 is the same as calling Speak - I can't Speak before I have an object, and I can't divide before I have 100. Then on the other side I first create an InputBox with some instructions for how to display it. When I hit enter on the input box it evaluates or "returns" whatever I entered. (Hint: 'oooooo' makes a funny sound) So when I say Speak, the right side is what to Speak. And I get that from the InputBox. So when you do several things on a line, like: x = 14 + y; You need to be aware of the order of things. First we add 14 and y. Then we put the result (what it evaluates to, or returns) into x.

这就是我的目标,有一堆很酷的例子来演示,并在他们玩得开心的时候教他们。我在我室友身上尝试了这个例子,虽然我可能不会把它作为第一课,但她喜欢它,并学到了一些东西。

一些很酷的数学程序,可以做出漂亮的图形或容易理解的形状,这是很好的想法,我将研究这些。这里有一些复杂的actionscript示例,但这有点太高级了,我不能教flash。你还有什么其他的想法吗?


当前回答

我觉得这个问题真是个好主意。我有很多差劲的老师,其中最好的一个显然是那些有一点点想要炫耀的人。

你可以向他们展示大量的代码。我首先想到的是Ed Felten的TinyP2P源代码:

  import sys, os, SimpleXMLRPCServer, xmlrpclib, re, hmac # (C) 2004, E.W. Felten

  ar,pw,res = (sys.argv,lambda u:hmac.new(sys.argv[1],u).hexdigest(),re.search)

  pxy,xs = (xmlrpclib.ServerProxy,SimpleXMLRPCServer.SimpleXMLRPCServer)

  def ls(p=""):return filter(lambda n:(p=="")or res(p,n),os.listdir(os.getcwd()))

  if ar[2]!="client": # license: http://creativecommons.org/licenses/by-nc-sa/2.0

    myU,prs,srv = ("http://"+ar[3]+":"+ar[4], ar[5:],lambda x:x.serve_forever())

    def pr(x=[]): return ([(y in prs) or prs.append(y) for y in x] or 1) and prs

    def c(n): return ((lambda f: (f.read(), f.close()))(file(n)))[0]

    f=lambda p,n,a:(p==pw(myU))and(((n==0)and pr(a))or((n==1)and [ls(a)])or c(a))

    def aug(u): return ((u==myU) and pr()) or pr(pxy(u).f(pw(u),0,pr([myU])))

    pr() and [aug(s) for s in aug(pr()[0])]
    (lambda sv:sv.register_function(f,"f") or srv(sv))(xs((ar[3],int(ar[4]))))

  for url in pxy(ar[3]).f(pw(ar[3]),0,[]):
    for fn in filter(lambda n:not n in ls(), (pxy(url).f(pw(url),1,ar[4]))[0]):
      (lambda fi:fi.write(pxy(url).f(pw(url),2,fn)) or fi.close())(file(fn,"wc"))

好吧,它比你的“十”限制多了5行,但仍然是一个功能齐全的Peer 2 Peer应用程序,多亏了Python。

TinyP2P可以作为服务器运行:

python tinyp2p.py password server hostname portnum [otherurl]

还有一个客户:

python tinyp2p.py password client serverurl pattern

当然,讲故事是非常重要的。对于这样的目的,99瓶啤酒确实是一个很好的开始。

然后你可以选择几个funcky代码的例子,比如:

the famous Python one-liner : print("".join(map(lambda x: x and "%s%d bottle%s of beer on the wall, %d bottle%s of beer...\nTake one down, pass it around.\n"%(x<99 and "%d bottles of beer on the wall.\n\n"%x or "\n", x, x>1 and "s" or " ", x, x>1 and "s" or " ";) or "No bottles of beer on the wall.\n\nNo more bottles of beer...\nGo to the store and buy some more...\n99 bottles of beer.", range(99,-1,-1)))) the cheaty Python version (cool for student cause it shows network features) : import re, urllib print re.sub('</p>', '', re.sub('<br>|<p>|<br/> |<br/>','\n', re.sub('No', '\nNo', urllib.URLopener().open('http://www.99-bottles-of-beer.net/lyrics.html').read()[3516:16297])))

最后,我将遵循前面的建议并展示一些Javascript,因为它非常直观。jQuery UI演示网站有很多不错的小部件演示,包括代码片段。几行日历:

<script type="text/javascript">
    $(function() {
        $("#datepicker").datepicker();
    });
    </script>

<div class="demo">

<p>Date: <input id="datepicker" type="text"></p>

</div>

小书签也很有吸引力。可读性非常有趣:

function() {
    readStyle='style-newspaper';readSize='size-large';
    readMargin='margin-wide';
    _readability_script=document.createElement('SCRIPT');
    _readability_script.type='text/javascript';
    _readability_script.src='http://lab.arc90.com/experiments/readability/js/readability.js?x='+(Math.random());
    document.getElementsByTagName('head')[0].appendChild(_readability_script);
    _readability_css=document.createElement('LINK');
    _readability_css.rel='stylesheet';
    _readability_css.href='http://lab.arc90.com/experiments/readability/css/readability.css';
    _readability_css.type='text/css';_readability_css.media='screen';
    document.getElementsByTagName('head')[0].appendChild(_readability_css);
    _readability_print_css=document.createElement('LINK');
    _readability_print_css.rel='stylesheet';_readability_print_css.href='http://lab.arc90.com/experiments/readability/css/readability-print.css';
    _readability_print_css.media='print';
    _readability_print_css.type='text/css';
    document.getElementsByTagName('head')[0].appendChild(_readability_print_css);
}

其他回答

我没有这方面的代码,但是它可以在10行或更少的时间内被抽象出来。使鼠标画一个方框。不管你怎么移动它。当你点击(左)框消失,当你点击(右)框改变颜色。

学生们想要一些实用的东西,一些他们可以修改和定制的东西,一些表明这“不是典型的无聊课程”的东西。

Xen的mini-os内核现在可以做到这一点,但它需要额外的抽象来满足您的需求。

您还可以尝试绘制一个manderbolt (julia)集,同时从环境噪声获得二次平面的参数(如果机器有麦克风和声卡)..他们的声音会产生分形。同样,要在10行(在他们编辑的实际函数中)完成这一点很棘手,但并非不可能。

在现实世界中,您将使用现有的库。所以我认为,在main()(或任何你使用的语言)中使用10行代码更实用。我们让存在的东西为我们工作,而写不存在或不为我们工作的东西。你不妨一开始就介绍这个概念。

同时,行吗?Int main(void){无符号Int i;(我= 0;I < 10;我+ +);返回0;也许,10次函数调用将是一个更现实的目标?这不是一场模糊的代码竞赛。

好运!

wxPython第一步

import wx
app = wx.App()
wx.Frame(None, -1, 'simple.py').Show()
app.MainLoop()

Simple.py框架http://zetcode.com/wxpython/images/simple.jpg

使用游戏!不是编程游戏,而是编程比赛。想想谷歌AI挑战,把它简化。

让我举个例子。我曾经和我的朋友进行过一次小竞赛:我们中的一个人建立了一个模拟框架,其他人编写了一个AI,范围从简单到高度分析,我们进行了100次测试,看看哪个AI表现最好。

该框架?基本I/O:模拟控制由每个竞争AI生成一个子进程执行,每一轮模拟将数据写入标准输入管道,并读取输出。这样,我们就可以用任何我们想要的语言来编写我们的ai,只需要遵循一个非常简单的协议。

规则非常简单,但事情很有挑战性:我们有两个村庄,A村和B村,它们在居住在那里的家庭中平均分配资金。A有800个硬币,B有500个。每一轮,所有人工智能都被要求选择一个村庄居住(将“a”或“B”打印到stdout),然后返回每个村庄在此期间的总数(通过读取stdin中的数字)。我们的目标是在100轮比赛后获得最多的钱。

我们创造的一些ai拥有非常复杂的机制去尝试并猜测该去哪个村庄定居—-尽管它们并不是很好,因为获胜的策略总是选择上一轮给每个家庭最少钱的村庄(假设大多数人会在下一轮搬到另一个村庄)。

我认为这很吸引人,鼓励研究,是一种健康的竞争方式。有成千上万的游戏可以玩,它只需要基本的编程知识(标准I/O!)让玩家进行互动。

我发现最喜欢的(在GWBASIC中)是:

10 input "What is your name ";N$
20 i = int(rnd * 2)
30 if i = 0 print "Hello ";N$;". You are a <fill in insult number 1>"
40 if i = 1 print "Hello ";N$;". You are a <fill in insult number 2>"

我发现初学的学生有一些观念需要修正。

电脑不会读心术。 计算机一次只做一件事,即使它们做得如此之快,它们似乎一次就做完了。 计算机只是愚蠢的机器,只会做被告知的事情。 计算机只能识别特定的东西,而这些东西就像积木一样。 一个关键的概念是,变量是包含一个值的东西,它的名称与该值不同。 你编辑程序的时间和它运行的时间之间的区别。

祝你在课堂上好运。我相信你会做得很好的。

附言:我相信你明白,除了材料和技能,你还在教授一种态度,这也是同样重要的。

当我第一次想出bash叉式炸弹的时候,我觉得它真的很可爱。如此简单,但它能做的事情却很整洁:

:(){ :|:& };: