我正在寻找最酷的事情,你可以在几行简单的代码。我相信你可以用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。你还有什么其他的想法吗?


当前回答

我为一款论坛游戏写了这篇文章——用尽可能少的行数写出ROT13算法。那么在C中呢?

rot13(char*s)
{
    int i=-1;
    do{
        i++;
        s[i] = (s[i] >= 65 && s[i] <=90 || s[i] >= 97 &&s [i] <= 122) ?
            ((s[i] < 97) ? 65 : 97) + (((s[i] - ((s[i] < 97) ? 65 : 97)) + 13) % 26) :
            s[i];
    } while(s[i] > 0);
}

我认为三元运算符非常简洁,尽管我听说它比if结构要慢。我还没有为自己计时……

其他回答

这是一个非常基本的基于文本的c#程序,模拟老虎机的旋转动作。它不包括不同的中奖几率或现金奖励,但这对学生来说是一个很好的练习。

抱歉,超过10行。

string[] symbols = new[] { "#", "?", "~" }; // The symbols on the reel
Random rand = new Random();

do
{
    string a="",b="",c="";

    for( int i = 0; i < 20; i++ )
    {
        Thread.Sleep( 50 + 25 * i ); // slow down more the longer the loop runs

        if( i < 10 )
            a = symbols[rand.Next( 0, symbols.Length )];

        if( i < 15 )
            b = symbols[rand.Next( 0, symbols.Length )];

        c = symbols[rand.Next( 0, symbols.Length )];

        Console.Clear();
        Console.WriteLine( "Spin: " + a + b + c );
    }

    if( a == b && b == c )
        Console.WriteLine( "You win. Press enter to play again or type \"exit\" to exit" );
    else
        Console.WriteLine( "You lose. Press enter to play again or type \"exit\" to exit" );
}
while( Console.ReadLine() != "exit" );

有趣的是,你提到了Mandelbrot集,因为用GW-BASIC创建分形是激发我在高中(大约1993年)对编程的热爱的原因。在我们开始学习分形之前,我们写了无聊的标准差应用程序,而我仍然计划进入新闻业。

但当我看到这个冗长、难以编写的BASIC程序生成“分形地形”时,我就被迷住了,再也没有回头。它改变了我对数学、科学、计算机的看法,也改变了我的学习方式。

我希望你能找到对你的学生有同样影响的项目。

看看这些项目吧:

hackkety Hack:专门旨在让编码对非程序员更容易理解和有吸引力。 鞋子:桌面应用程序的有趣而简约的方法 处理:用于图像、动画等编程的环境和(类似java的)语言。

import sys
for y in range(80):
    for x in range(80):
        c = complex(x-40.0,y-40.0) / 20.0
        z = 0.0
        for i in range(100):
            z = z*z+c
        sys.stdout.write('#' if abs(z) < 2.0 else ' ')
    sys.stdout.write('\n')

10打印“Mohan” 20 Goto 10