我正在寻找你能用几行简单代码做的最酷的事情。我相信你可以写Mandelbrot set in Haskell in 15 lines,但很难遵循。
我的目标是激发学生编程很酷。
我们知道编程很酷,因为你可以创造任何你想象的东西 - 它是最终的创意出路。我想激励这些初学者,让他们尽可能多地学习早期学习。
现在,我的理由是自私的。我正在为60个半工程,半个商业专业的团队教授计算机简介课程;所有新生。他们是来自贫困高中的学生。根据我过去的经验,该小组通常分为如下:一些rock-stars,一些非常努力并且有点得到它,少数人非常努力并且勉强得到它,以及少数不关心的人。我想尽可能多地接触这些群体。以下是我如何使用计算机程序进行教学的示例:
这是我正在寻找的一个例子 for:1行VBS脚本来获取你的 电脑跟你说话:
CreateObject("sapi.spvoice").Speak InputBox("Enter your text","Talk it")
我可以用它来证明顺序 的运作。我会展示代码,让我们 他们玩它,然后解释 那条线上有很多事, 但是电脑可以理解它, 因为它知道规则。然后我会 告诉他们这样的事情:
4(5*5) / 10 + 9(.25 + .75)
你可以先看到我需要的东西 做(5 * 5)。然后我可以倍增 4.现在我创建了对象。除以10与呼叫相同 说话 - 在我拥有之前我不能说话 对象,我不能在我之前分裂 有100.然后在另一边我 首先用一些创建一个InputBox 有关如何显示它的说明。 当我在输入框上按Enter键时 评估或“返回”任何我 进入。 (提示:'oooooo'制作了一个 有趣的声音)所以当我说Speak时, 右边是说什么。和我 从InputBox中获取。
所以当你做几件事时 一行,如:
x = 14 + y;
您需要了解其顺序 的东西。首先我们添加14和y。然后 我们把结果(它评估的内容) 到,或返回)到x。
这是我的目标,在他们玩得开心的时候,有一堆很酷的例子来展示和教授课程。我在我的室友身上尝试了这个例子,虽然我可能不会将它作为第一课,但她喜欢它并学到了一些东西。
一些很容易理解的酷mathematica programs that make beautiful graphs or shapes会是好主意,我会调查一下。这里有一些complicated actionscript examples,但有点太高级,我不能教flash。您有什么其他想法?
答案 0 :(得分:339)
在地址栏中输入此代码(在浏览器中),然后按Enter键。然后,您可以编辑网页的所有内容!
javascript:document.body.contentEditable='true'; document.designMode='on'; void 0
这是我所知道的最酷的“单线”=)
答案 1 :(得分:201)
当我第一次写这篇文章时。
10 PRINT "What is your name?"
20 INPUT A$
30 PRINT "Hello " A$
40 GOTO 30
它把人们吹走了!电脑记得他们的名字!
编辑:只是添加到此。如果你能说服新程序员这是他们能做的最酷的事情,那么他们将成为优秀的程序员。现在,您可以使用一行代码执行几乎任何操作,以运行其他人编写的库。我个人对此并不满意,并且在教学方面几乎没有任何好处。答案 2 :(得分:180)
好的,这是15行代码,结果很棒!这就是那种让我在孩提时感到惊慌失措的东西。这来自PHP手册:
$x = 200;
$y = 200;
$gd = imagecreatetruecolor($x, $y);
$corners[0] = array('x' => 100, 'y' => 10);
$corners[1] = array('x' => 0, 'y' => 190);
$corners[2] = array('x' => 200, 'y' => 190);
$red = imagecolorallocate($gd, 255, 0, 0);
for ($i = 0; $i < 100000; $i++) {
imagesetpixel($gd, round($x),round($y), $red);
$a = rand(0, 2);
$x = ($x + $corners[$a]['x']) / 2;
$y = ($y + $corners[$a]['y']) / 2;
}
header('Content-Type: image/png');
imagepng($gd);
答案 3 :(得分:105)
Microsoft拥有Small Basic,一个用于“孩子”的IDE。
pic = Flickr.GetRandomPicture("beach")
Desktop.SetWallpaper(pic)
它专门用于展示编程的酷炫程度。
答案 4 :(得分:83)
我倾向于认为人们对他们可能与他们的生活有关或与他们的生活相关的东西印象深刻。我会尝试将我的10行代码基于他们知道和理解的东西。以Twitter及其API为例。为什么不使用这个API来构建一个很酷的东西。以下10行代码将从Twitter返回“公共时间轴”并将其显示在控制台应用程序中......
using (var xmlr = XmlReader.Create("http://twitter.com/statuses/public_timeline.rss"))
{
SyndicationFeed
.Load(xmlr)
.GetRss20Formatter()
.Feed
.Items
.ToList()
.ForEach( x => Console.WriteLine(x.Title.Text));
}
我的代码示例可能不适合您的学生。它是用C#编写的,使用的是.NET 3.5。因此,如果您要教他们PHP,Java或C ++,这将没有用。但是,我的观点是,通过将您的10行代码与“酷,有趣,与学生相关”的内容相关联,您的样本也会变得酷炫,有趣和相关。
祝你好运![是的,我知道我错过了几行使用语句和Main方法,但我猜测10行不需要字面上10行]
答案 5 :(得分:80)
这是一个Python telnet服务器,它会询问用户名并向他们打招呼。这看起来很酷,因为您通过网络从另一台计算机与您的程序进行通信。
from socket import *
s=socket(AF_INET, SOCK_STREAM)
s.bind(("", 3333))
s.listen(5)
while 1:
(c, a) = s.accept()
c.send("What is your name? ")
name = c.recv(100)
c.send("Hello "+name)
c.close()
答案 6 :(得分:78)
我的孩子们用一个快速的VB脚本来操作Microsoft Agent角色,得到了很好的回应。对于那些不熟悉MS Agent的人来说,它是一系列可以通过COM接口操作的动画屏幕字符。您可以在Microsoft Agent下载页面下载代码和字符。
以下几行将使Merlin角色出现在屏幕上,飞来飞去,敲击屏幕引起你的注意,并打个招呼。
agentName = "Merlin"
agentPath = "c:\windows\msagent\chars\" & agentName & ".acs"
Set agent = CreateObject("Agent.Control.2")
agent.Connected = TRUE
agent.Characters.Load agentName, agentPath
Set character = agent.Characters.Character(agentName)
character.Show
character.MoveTo 500, 400
character.Play "GetAttention"
character.Speak "Hello, how are you?"
Wscript.Sleep 15000
character.Stop
character.Play "Hide"
您可以使用许多其他命令。查看 http://www.microsoft.com/technet/scriptcenter/funzone/agent.mspx了解详情。
编辑2011-09-02 我最近发现Microsoft Agent未在Windows 7上本机安装。但是它是作为单独的下载here提供的。我没有对此进行测试,因此无法验证它是否正常运行。
答案 7 :(得分:69)
我认为现在成为一名计算机教育工作者很难。我是。我们面临着越来越陡峭的艰苦战斗。我们的学生是非常复杂的用户并且需要花费很多精力才能给他们留下深刻的印象。他们有很多可供他们使用的工具,可以做出惊人的事情。
10行代码中的简单计算器? 为什么?我有一台TI-86。
将特殊效果应用于图像的脚本? 这就是Photoshop的用途。 Photoshop会吹掉10行中你能做的任何事情。
如何翻录CD并将文件转换为MP3? 呃,我已经从BitTorrent收到了5万首歌。它们已经是MP3格式了。我在iPhone上播放它们。谁还买CD?
为了向精明的用户介绍编程,你将不得不找到以下内容:
a)适用于他们觉得有趣和酷的东西,和 b)做了他们不能做的事情。
假设您的学生已经可以访问最昂贵的软件。他们中的许多人都拥有完整版的Adobe CS5.5(零售价:2,600美元;实际价格:免费),并且可以轻松获得通常会损害部门预算的任何应用程序。
但绝大多数人都不知道这些“计算机内容”是如何运作的。
他们是一群非常有创意的人:他们喜欢创造东西。他们只是想做或做一些他们的朋友不能做的事情。他们想要吹嘘一些东西。
以下是我发现的一些与学生产生共鸣的事情:
这些都不涉及传统意义上的任何编程。但他们确实利用了强大的库。我认为它们是一种不同的编程方式。
答案 8 :(得分:63)
我发现了一个很受欢迎的(在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>"
我发现初学者有一些需要修复的概念。
P.S。我相信你明白,除了材料和技巧,你也在教导一种态度,这同样重要。
答案 9 :(得分:62)
这个C代码可能会被混淆,但我发现它非常强大
#include <unistd.h>
float o=0.075,h=1.5,T,r,O,l,I;int _,L=80,s=3200;main(){for(;s%L||
(h-=o,T= -2),s;4 -(r=O*O)<(l=I*I)|++ _==L&&write(1,(--s%L?_<L?--_
%6:6:7)+"World! \n",1)&&(O=I=l=_=r=0,T+=o /2))O=I*2*O+h,I=l+T-r;}
这是结果......只有3行......
一种分形Hello World
......
WWWWWWWWWWWWWWWWooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
WWWWWWWWWWWWWWooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
WWWWWWWWWWWWWooooooooooooooooorrrrrrrrrrrrrrrrrrrrroooooooooooooooooooooooooooo
WWWWWWWWWWWoooooooooooorrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrooooooooooooooooooooo
WWWWWWWWWWooooooooorrrrrrrrrrrrrrrrrrrrrrrllllld!!ddllllrrrrrrooooooooooooooooo
WWWWWWWWoooooooorrrrrrrrrrrrrrrrrrrrrrllllllldd!oWW!!dllllllrrrrroooooooooooooo
WWWWWWWoooooorrrrrrrrrrrrrrrrrrrrrrlllllllldddd!orro!o!dllllllrrrrrrooooooooooo
WWWWWWooooorrrrrrrrrrrrrrrrrrrrrllllllllldddd!WorddddoW!ddllllllrrrrrrooooooooo
WWWWWoooorrrrrrrrrrrrrrrrrrrrrlllllllllddd!!!o!!! !dWW!ddddllllrrrrrrrooooooo
WWWWooorrrrrrrrrrrrrrrrrrrrllllllllldd!!!!WWWoo WloW!!!ddddllrrrrrrrrooooo
WWWWoorrrrrrrrrrrrrrrrrrrlllllllddddWldolrrlo!Wl r!dlooWWWoW!dllrrrrrrroooo
WWWoorrrrrrrrrrrrrrrrrlllllddddddd!!Wdo l! rdo!l!r!dlrrrrrrrrooo
WWoorrrrrrrrrrrrrrrlllddddddddd!!!!oolWW lW!ddlrrrrrrrroo
WWorrrrrrrrrrrrllld!!!!!dddd!!!!WWrd ! rlW!ddllrrrrrrrro
Worrrrrrrllllllddd!oooWWWoloWWWWoodr drrWdlllrrrrrrrr
Worrrlllllllldddd!WolWrr!!dWWWlrrldr ro!dlllrrrrrrrr
Wrrllllllllddddd!WWolWr oWoo r!dllllrrrrrrr
Wlllllllldddd!!odrrdW o lWddllllrrrrrrr
Wlddddd!!!!!WWordlWrd oW!ddllllrrrrrrr
olddddd!!!!!WWordlWrd oW!ddllllrrrrrrr
Wlllllllldddd!!odrrdW o lWddllllrrrrrrr
Wrrllllllllddddd!WWolWr oWoo r!dllllrrrrrrr
Worrrlllllllldddd!WolWrr!!dWWWlrrldr ro!dlllrrrrrrrr
Worrrrrrrllllllddd!oooWWWoloWWWWoodr droWdlllrrrrrrrr
WWorrrrrrrrrrrrllld!!!!!dddd!!!!WWrd ! rlW!ddllrrrrrrrro
WWoorrrrrrrrrrrrrrrlllddddddddd!!!!oolWW lW!ddlrrrrrrrroo
WWWoorrrrrrrrrrrrrrrrrlllllddddddd!!Wdo l! rdo!l!r!dlrrrrrrrrooo
WWWWoorrrrrrrrrrrrrrrrrrrlllllllddddWldolrrlo!Wl r!dlooWWWoW!dllrrrrrrroooo
WWWWooorrrrrrrrrrrrrrrrrrrrllllllllldd!!!!WWWoo WloW!!!ddddllrrrrrrrrooooo
WWWWWoooorrrrrrrrrrrrrrrrrrrrrlllllllllddd!!!o!!! WdWW!ddddllllrrrrrrrooooooo
WWWWWWooooorrrrrrrrrrrrrrrrrrrrrllllllllldddd!WorddddoW!ddllllllrrrrrrooooooooo
WWWWWWWoooooorrrrrrrrrrrrrrrrrrrrrrlllllllldddd!orro!o!dllllllrrrrrrooooooooooo
WWWWWWWWoooooooorrrrrrrrrrrrrrrrrrrrrrllllllldd!oWW!!dllllllrrrrroooooooooooooo
WWWWWWWWWWooooooooorrrrrrrrrrrrrrrrrrrrrrrllllld!!ddllllrrrrrrooooooooooooooooo
WWWWWWWWWWWoooooooooooorrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrooooooooooooooooooooo
WWWWWWWWWWWWWooooooooooooooooorrrrrrrrrrrrrrrrrrrrroooooooooooooooooooooooooooo
WWWWWWWWWWWWWWooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
WWWWWWWWWWWWWWWWooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
WWWWWWWWWWWWWWWWWWWoooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
WWWWWWWWWWWWWWWWWWWWWoooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
答案 10 :(得分:45)
如何显示您可以使用任何Web浏览器并在地址栏中输入JavaScript并获取执行代码?
编辑:转到包含大量图片的页面,并在地址栏中尝试:
javascript:R=0; x1=.1; y1=.05; x2=.25; y2=.24; x3=1.6; y3=.24; x4=300; y4=200; x5=300; y5=200; DI=document.images; DIL=DI.length; function A(){for(i=0; i<DIL; i++){DIS=DI[ i ].style; DIS.position='absolute'; DIS.left=Math.sin(R*x1+i*x2+x3)*x4+x5; DIS.top=Math.cos(R*y1+i*y2+y3)*y4+y5}R++ }setInterval('A()',5); void(0)
答案 11 :(得分:37)
您可以创建一个选择随机数的应用程序。你必须猜测它。如果你错了,它说:更高或更低。如果你猜对了,那就是一个好消息。
为学生们演奏很酷。
没有正确错误检查的简单Python版本:
import random
while input('Want to play higher/lower? ').lower().startswith('y'):
n = random.randint(1, 100)
g = int(input('Guess: '))
while g != n:
print(' %ser!' % (g > n and 'low' or 'high'))
g = int(input('Guess: '))
print(' Correct! Congratulations!')
Erik表示计算机应该猜测数字。这也可以在10行代码内完成(尽管现在缺少正确的错误检查更严重:范围之外的有效数字会导致无限循环):
while input('Want to let the pc play higher/lower? ').lower().startswith('y'):
n = int(input('Give a number between 1 and 100: '))
lo, hi, guess, tries = 1, 100, 50, 1
while guess != n:
tries += 1
lo, hi = (guess + 1, hi) if guess < n else (lo, guess - 1)
guess = (lo + hi) // 2
print('Computer guessed number in %d tries' % tries)
答案 12 :(得分:26)
回到高中的计算机课上,我和几个朋友教授课程如何用Delphi编程。该课程主要侧重于使用Pascal进行编程,因此Delphi是一个很好的下一步。我们演示了Delphi的事件驱动特性及其RAD功能。在课程结束时,我们向班级展示了一个示例应用程序,并要求他们重现它。该应用程序通过两个按钮是和否询问“你喝醉了吗?”。 ...我想你知道接下来会发生什么......没有按钮改变鼠标位置,几乎无法点击。
学生和老师得到了很好的帮助。
该程序只需要几行用户编写的代码,并用一个简单的等式来计算按钮的移动位置。我不认为其他任何学生都知道这一点,但有些学生很接近。
答案 13 :(得分:23)
当我第一次想出bash forkbomb时,我觉得它真的很甜蜜。如此简单,但却能做得很好:
:(){ :|:& };:
答案 14 :(得分:22)
这是作弊,甚至不是很简单,但我曾经使用Allegro图形库在20行C ++中写了一篇文章。 line 是什么的真正标准,但它是在很久以前,它纯粹是为了好玩。它甚至有粗糙的声音效果。
这就是它的样子:
20 Lines http://img227.imageshack.us/img227/8770/20linesxx0.png
这是代码(应该编译):
bool inside(int x, int y, int x2, int y2) { return (x>x2&&x<x2+20&&y>y2&&y<y2+10); }
int main() {
BITMAP* buffer;
float px,shotx,shoty,monstars[8],first,rnd,pressed,points = 0, maxp = 0;
unsigned char midi[5] = {0xC0,127,0x90,25,0x54}, plgfx[] = {0,0,0,10,3,10,3,5,6,5,6,10,8,12,10,10,10,5,13,5,13,10,16,10,16,0,13,0,13,2,3,2,3,0,0,0}, mongfx[] = {0,0, 10,5, 20,0, 17,8, 15,6, 10,16, 5,6, 3,8, 0,0};
allegro_init(), set_color_depth(32), set_gfx_mode(GFX_AUTODETECT_WINDOWED,320,240,0,0), install_timer(), install_keyboard(), install_mouse(), buffer = create_bitmap(320,240),srand(time(NULL)),install_sound(DIGI_AUTODETECT, MIDI_AUTODETECT,""),clear_to_color(buffer,makecol32(100,100,255));
while ((pressed=(!key[KEY_Z]&&pressed)?0:pressed)?1:1&&(((shoty=key[KEY_Z]&&shoty<0&&pressed==0?(pressed=1?200:200):first==0?-1:shoty)==200?shotx=px+9:0)==9999?1:1) && 1+(px += key[KEY_LEFT]?-0.1:0 + key[KEY_RIGHT]?0.1:0) && 1+int(px=(px<0?0:(px>228?228:px))) && !key[KEY_ESC]) {
rectfill(buffer,0,0,244,240,makecol32(0,0,0));
for(int i=0;i<8;i++) if (inside(shotx,shoty,i*32,monstars[i])) midi_out(midi,5);
for (int i=0; i<8; monstars[i] += first++>8?(monstars[i]==-100?0:0.02):-100, points = monstars[i]>240?points-1:points, monstars[i]=monstars[i]>240?-100:monstars[i], points = inside(shotx,shoty,i*32,monstars[i])?points+1:points, (monstars[i] = inside(shotx,shoty,i*32,monstars[i])?shoty=-1?-100:-100:monstars[i]), maxp = maxp>points?maxp:points, i++) for (int j=1; j<9; j++) line(buffer,i*32+mongfx[j*2 - 2],monstars[i]+mongfx[j*2-1],i*32+mongfx[j*2],monstars[i]+mongfx[j*2+1],makecol32(255,0,0));
if (int(first)%2000 == 0 && int(rnd=float(rand()%8))) monstars[int(rnd)] = monstars[int(rnd)]==-100?-20:monstars[int(rnd)]; // randomowe pojawianie potworkow
if (shoty>0) rectfill(buffer,shotx,shoty-=0.1,shotx+2,shoty+2,makecol32(0,255,255)); // rysowanie strzalu
for (int i=1; i<18; i++) line(buffer,px+plgfx[i*2 - 2],200-plgfx[i*2-1],px+plgfx[i*2],200-plgfx[i*2+1],makecol32(255,255,0));
textprintf_ex(buffer,font,250,10,makecol32(255,255,255),makecol32(100,100,255),"$: %i ",int(points)*10);
textprintf_ex(buffer,font,250,20,makecol32(255,255,255),makecol32(100,100,255),"$$ %i ",int(maxp)*10);
blit(buffer, screen, 0, 0, 0, 0, 320,240);
}
} END_OF_MAIN()
答案 15 :(得分:21)
在这个时代,JavaScript是展示如何使用一些非常基本的工具进行编程的绝佳方式,例如记事本中。
对于想要让他们的朋友惊叹的人来说,jQuery效果是一个很好的起点!在这个中,只需点击页面的空白区域即可。
<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script>
$(document.body).click(function () {
if ($("#pic").is(":hidden")) {
$("#pic").slideDown("slow");
} else {
$("#pic").slideUp();
}
});
</script>
</head>
<body><img id="pic" src="http://www.smidgy.com/smidgy/images/2007/07/26/lol_cat_icanhascheezburger.jpg"/>
</body>
</html>
答案 16 :(得分:20)
你可能会考虑的一件事就是Robocode,其中大量的编码被抽象出来,你基本上只是告诉机器人该做什么。一个简单的10行功能可以使机器人做很多事情,并且具有非常直观和易于理解的结果。
也许Robocode本身并不适合这项任务,但这种方式是将书面代码与计算机上的视觉动作联系起来的好方法,而且当你需要提供示例时,需要注意这一点很有趣。
public class MyFirstJuniorRobot extends JuniorRobot {
public void run() {
setColors(green, black, blue);
// Seesaw forever
while (true) {
ahead(100); // Move ahead 100
turnGunRight(360); // Spin gun around
back(100); // Move back 100
turnGunRight(360); // Spin gun around
}
}
public void onScannedRobot() {
turnGunTo(scannedAngle);
fire(1);
}
public void onHitByBullet() {
turnAheadLeft(100, 90 - hitByBulletBearing);
}
}
答案 17 :(得分:18)
所以有一天,我觉得我已经受够了。我会学习钢琴。看到像Elton John这样的人掌握了对键盘的掌握,这让我确信这就是我想做的事。
实际上学习钢琴是一个巨大的失望。即使在完成八年级的钢琴课之后,我对钢琴演奏的心理形象与我最初的欣赏活动的想法如此不同仍然没有留下深刻的印象。
然而,我非常喜欢的是我仅仅三年级的音乐理论基础。我了解了音乐的构建。我终于能够从执行书面音乐的世界走向写自己的音乐。随后,我开始玩我想玩的东西。
不要试图让新程序员,尤其是年轻程序员感到眩目。 “不到十行简单代码”的整个概念似乎引出了“给我一些东西聪明”的情绪。
你可以向新程序员展示一些聪明的东西。然后,您可以教同一个程序员如何复制这个“性能”。但这并不是让他们迷上编程的原因。教他们基本的习惯,让他们综合自己聪明的十行代码。
我会向新程序员展示以下Python代码:
input = open("input.txt", "r")
output = open("output.txt", "w")
for line in input:
edited_line = line
edited_line = edited_line.replace("EDTA", "ethylenediaminetetraacetic acid")
edited_line = edited_line.replace("ATP", "adenosine triphosphate")
output.write(edited_line)
我意识到我不需要将line
分配给edited_line
。但是,这只是为了保持清晰,并表明我没有编辑原始文档。
在不到十行中,我对一份文件进行了检验。当然,还要确保显示可用的新程序员all the string methods。更重要的是,我展示了三个我能做的基本有趣的事情:变量赋值,循环,文件IO和标准库的使用。
我认为你会同意这段代码并不令人眼花缭乱。事实上,这有点无聊。不 - 实际上,这很无聊。但是向新程序员展示代码并查看该程序员是否不能将该脚本的每个部分重新用于本周内更有趣的事情,如果不是那一天。当然,这对你来说是令人反感的(也许使用这个脚本来制作一个简单的HTML解析器),但其他一切只需要时间和经验。
答案 18 :(得分:17)
与大多数其他评论者一样,我开始编写代码来解决数学问题(或者为我设计的非常糟糕的游戏创建图形 - 像印第安纳琼斯和僵尸一样)。
真正开始我(在数学和编程方面)是从基于文本的,选择你自己的冒险风格游戏......到更多基于图形的游戏。我开始着色方格纸和绘制像素,直到我进入几何...并发现如何使用方程来绘制曲线和线条,方框等。
我的观点是,我可以真的进入像处理(http://processing.org/)这样的事情,其中典型的程序看起来像这样:
void setup()
{
size(200, 200);
noStroke();
rectMode(CENTER);
}
void draw()
{
background(51);
fill(255, 204);
rect(mouseX, height/2, mouseY/2+10, mouseY/2+10);
fill(255, 204);
int inverseX = width-mouseX;
int inverseY = height-mouseY;
rect(inverseX, height/2, (inverseY/2)+10, (inverseY/2)+10);
}
对我而言,这是未来的“标志”。
有一些简单的“hello world”示例可以快速地让某人绘制和更改代码,看到事情如何破碎以及可以创建什么奇怪的“事故”......一直到更高级的交互和分形创建......
答案 19 :(得分:15)
您可以使用用AutoIt编写的脚本,这会模糊使用传统应用程序和编程之间的界限。
E.g。一个打开记事本的脚本,让自己的计算机通过消息框侮辱它们,然后不留下它的动作:
Run("notepad.exe")
WinWaitActive("Untitled - Notepad")
Send("You smell of human.")
Sleep(10000)
MsgBox(0, "Humans smell bad", "Yuck!")
WinClose("Untitled - Notepad")
WinWaitActive("Notepad", "Do you want to save")
Send("!n")
答案 20 :(得分:13)
我认为学生入学的好地方可能是Greasemonkey。在userscripts.org上有成千上万的示例脚本,非常好的阅读材料,其中一些非常小。 Greasemonkey脚本会影响网页,学生们已经熟悉了这些网页,如果不操作的话。 Greasemonkey本身提供了一种在测试时编辑和启用/禁用脚本的简便方法。
例如,这是“Google Two Columns”脚本:
result2 = '<table width="100%" align="center" cellpadding="10" style="font-size:12px">';
gEntry = document.evaluate("//li[@class='g'] | //div[@class='g'] | //li[@class='g w0'] | //li[@class='g s w0']",document,null,XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,null);
for (var i = 0; i < gEntry.snapshotLength; i++) {
if (i==0) { var sDiv = gEntry.snapshotItem(i).parentNode.parentNode; }
if(i%2 == 0) { result2 += '<tr><td width="50%" valign="top">'+gEntry.snapshotItem(i).innerHTML+'</td>'; }
if(i%2 == 1) { result2 += '<td width="50%" valign="top">'+gEntry.snapshotItem(i).innerHTML+'</td></tr>'; }
}
sDiv.innerHTML = result2+'</table>';
if (document.getElementById('mbEnd') !== null) { document.getElementById('mbEnd').style.display = 'none'; }
答案 21 :(得分:13)
我记得当我第一次开始编码循环时总是给我留下深刻印象。您编写了5到10行代码(或更少)和数百(或许多指定)行打印输出。 (我先用PHP和Java学习过。)
for( int i = 0; i < 200; i++ )
{
System.out.println( i );
}
答案 22 :(得分:10)
这是一个非常基本的基于文本的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" );
答案 23 :(得分:9)
使用Tcl,你有一个简单的文本编辑器,在大约12行代码中有一个保存按钮(但没有打开,需要另外8行)。它适用于所有标准平台:
pack [frame .toolbar] -side top -fill x
pack [button .save -text save -command save] -in .toolbar -side left
pack [scrollbar .vsb -orient vertical -command [list .text yview]] -side right -fill y
pack [text .text -wrap word -yscrollcommand [list .vsb set]] -side left -fill both -expand true
proc save {} {
set filename [tk_getSaveFile]
if {$filename ne ""} {
set f [open $filename w]
puts $f [.text get 1.0 end-1c]
close $f
}
}
我意识到目标是10行,所以如果你想要坚持10行或更少,一个没有加载或保存的简单文本编辑器只有两行。那不算太糟糕。
pack [scrollbar .vsb -orient vertical -command [list .text yview]] -side left -fill y
pack [text .text -wrap word -yscrollcommand [list .vsb set]] -side left -fill both -expand true
在您选择的平台上使用“wish filename ”执行上述任一代码块。希望附带大多数* nix和Mac,但你必须手动安装它的Windows。
更进一步,这两行脚本也可以用python编写,虽然需要八行,仍然在10行目标下:
from Tkinter import *
root=Tk()
text = Text(wrap="word")
sb = Scrollbar(orient="vertical", command=text.yview)
text.configure(yscrollcommand=sb.set)
sb.pack(side="right", fill="y")
text.pack(side="left", fill="both", expand=True)
root.mainloop()
答案 24 :(得分:9)
书签怎么样?它将向他们展示如何操纵他们每天使用的东西(互联网),而无需任何开发工具。
答案 25 :(得分:8)
如果你能买得起硬件,使用Arduino板+处理会产生一些非常酷的东西,尽管对于那些可能对编程不感兴趣的人来说可能会有点先进。
答案 26 :(得分:8)
我最近在一篇文章"The Shortest, most useful program I have ever written."
中写到了这一点总结:我在1996年写了一个3行的VB6应用程序,我仍然每天使用它。一旦exe被删除在“发送到”文件夹中。它允许您右键单击资源管理器中的文件,并将该文件的完整路径发送到剪贴板。
Public Sub Main()
Clipboard.SetText Command$
End Sub
答案 27 :(得分:7)
有趣的是你提到Mandelbrot集合,因为用GW-BASIC创建分形是我在高中时期(1993年左右)引起我对编程的热爱。在我们开始学习分形之前,我们编写了无聊的标准偏差应用程序,我仍然计划进入新闻业。
但是一旦我看到那个漫长而难以编写的BASIC程序产生了“分形地形”,我就被迷住了,我再也没有回头。它改变了我对数学,科学,计算机以及我学习方式的思考方式。
我希望你能找到对学生有同样影响的课程。
答案 28 :(得分:7)
import wx
app = wx.App()
wx.Frame(None, -1, 'simple.py').Show()
app.MainLoop()
simple.py frame http://zetcode.com/wxpython/images/simple.jpg
答案 29 :(得分:6)
我确定它会变成超过10行的代码,但您是否考虑过基于表单的应用程序,按下按钮会改变背景的颜色或更改文本的大小?这将向他们展示交互式程序的工作原理。它还会向他们表明,作为程序员,他们可以完全控制计算机(程序)所做的事情。
希望这会引导他们为他们可以改变的其他事情提出建议,然后再提出他们可能想做的其他事情。
答案 30 :(得分:6)
作为对你提出的任何想法的补充,我说你应该向他们展示如何做一些基本的数学。把它作为
“现在你可能认为这很容易或者 复杂......但你有没有过 坚持你的数学作业?“
然后从某人的书中抽出一个例子。大多数数学问题可以在10行中解决,因为它可能是一个简单的问题。然后向他们展示如何花费10分钟来弄清楚他们可能会得到的A值。这是一个漫长的过程,但你可能会抓住一些想要花很少时间做功课的人。
这主要源于我希望我曾想过在化学中编写一个软件程序......所有这些测验和作业都会有100个......
编辑: 回应彼得的评论:
说出类似3a 2 的衍生物。所以你可以只显示一个他们可以从命令行调用的简单函数:
public int SimpleDerivative(int r, int exponent){
r = r * exponent
exponent =- 1
return (String "{0}a^{1}" where {0} = r, {1} = exponent)
}
答案 31 :(得分:5)
许多人发现赌博令人兴奋和激励。你可以自己建立一个二十一点经销商类,暴露一个界面。然后,让孩子们建立一个二十一点的玩家类。
您可以为每个学生的解决方案构建一个图表,显示资金与时间的关系来激励任务。
该系统的优点在于您可以在数周内制作增量解决方案:
天真的解决方案是始终达到一定水平以下。这可能是5行代码。
更好的解决方案是查看经销商所暴露的内容并调整您的打击率。
更好的解决方案是考虑到你拥有的实际牌 - 而不仅仅是他们的价值总和。
最终的解决方案可能是通过多手掌握所发出的牌。 (经销商对象可以在玩家对象上调用DealerIsShuffling(int numberofdecks),告诉玩家有多少套牌。)
另一个可能的方向是让游戏具有竞争力 - 而不是与经销商争夺金钱,而是与其他人的解决方案对抗。当然,你必须轮换谁先开始打击以使事情公平。
答案 32 :(得分:5)
答案 33 :(得分:4)
我为11-12岁的学习障碍学生开设了课程。我们使用的是Hypercard,他们发现他们可以记录物体的位置(图像,盒子等),因为它们会移动它并播放它(动画)。虽然这不是编码,但他们想要做更多的事情:删除其中一个动作而不再记录它。我告诉他们他们必须去代码并改变它。
当他们更喜欢使用代码时,您可以看到谁有计算机/编程的诀窍,因为他们有更多的控制权。
在Excel中执行复杂的宏,然后了解代码正在执行的操作可能是VBA的网关。
根据年龄组或兴趣程度,直接进入代码可能很难,但最重要的是它。
答案 34 :(得分:3)
计算pi值的C程序:
#include <stdlib.h>
#include <stdio.h>
long a=10000,b,c=2800,d,e,f[2801],g;
int main()
{
for(;b-c;)
f[b++]=a/5;
for(;d=0,g=c*2;c-=14,printf("%.4d",e+d/a),e=d%a)
for(b=c;d+=f[b]*a,f[b]=d%--g,d/=g--,--b;d*=b);
}
输出:
31415926535897932384626433832795028841971693993751058209749445923078164062862089
98628034825342117067982148086513282306647093844609550582231725359408128481117450
28410270193852110555964462294895493038196442881097566593344612847564823378678316
52712019091456485669234603486104543266482133936072602491412737245870066063155881
74881520920962829254091715364367892590360011330530548820466521384146951941511609
43305727036575959195309218611738193261179310511854807446237996274956735188575272
48912279381830119491298336733624406566430860213949463952247371907021798609437027
70539217176293176752384674818467669405132000568127145263560827785771342757789609
17363717872146844090122495343014654958537105079227968925892354201995611212902196
0864034418159813629774771309960518707211349999998372978049951059731732816096318
答案 35 :(得分:3)
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')
答案 36 :(得分:3)
Private Declare Function Beep Lib "kernel32" (ByVal dwFreq As Long, ByVal dwDuration As Long) As Long
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Public Sub JohnDenverAnniesSong(): Const E4# = 329.6276: Dim Note&, Frequencies$, Durations$: Frequencies = "iiihfihfffhidadddfhihfffhihiiihfihffihfdadddfhihffhiki": Durations = "aabbbfjaabbbbnaabbbfjaabcapaabbbfjaabbbbnaabbbfjaabcap": For Note = 1 To Len(Frequencies): Beep CLng(E4 * 2 ^ ((AscW(Mid$(Frequencies, Note, 1)) - 96) / 12)), CLng((Asc(Mid$(Durations, Note, 1)) - 96) * 200 - 10): Sleep 10: DoEvents: Next: End Sub
转储到Excel中运行:D
答案 37 :(得分:3)
我没有这方面的代码,但它可以抽象为10行或更少。让鼠标画一个盒子..然而你移动它。单击(左)时,框会消失,当您单击(右)时,框会更改颜色。
学生想要一些实用的东西,他们可以破解和定制的东西,这些东西说“这不是你典型的无聊课”。
Xen的mini-os内核现在可以做到这一点,但它需要额外的抽象来满足您的需求。
您还可以尝试绘制一个manderbolt(julia)集合,同时从环境噪声中获取二次平面的参数(如果机器有麦克风和声卡)......他们的声音会产生分形。再次,在10行(在他们编辑的实际函数中)执行此操作会很棘手,但并非不可能。
在现实世界中,您将使用现有的库。所以我认为,main()(或者你使用的任何语言)中的10行更实用。我们为我们创造存在的东西,同时写出不存在或不适合我们的东西。你也可以在开头介绍这个概念。
还有,行? int main(void){unsigned int i; for(i = 0; i <10; i ++);返回0;也许,10个函数调用将是一个更现实的目标?这不是一个模糊的代码竞赛。
祝你好运!答案 38 :(得分:2)
10 PRINT "HELLO"
20 GOTO 10
但那时我还是个孩子。这也是为什么它是最酷的原因。 我认为你从编程计算机的第一次开始就不会得到同样的冲动。 即使只是将“HELLO”无限地打印到控制台也很简单
答案 39 :(得分:2)
处理总是很有趣,它创造的东西对所有类型的人都很有吸引力。例如,布朗树:
int xf = (int) random(width);
int yf = (int) random(height);
int x = (int) random(width);
int y = (int) random(height);
background(0xFF);
while(x != xf || y != yf) {
set(x,y,color(0,0,0));
x = max(0, min(x + -1 + (int) random(3), width - 1) );
y = max(0, min(y + -1 + (int) random(3), height - 1) );
}
答案 40 :(得分:2)
看看这些项目:
答案 41 :(得分:2)
阅读这个问题的答案很有趣。一旦你从学生那里获得了“哇”因素,就可以说明一个人成为另一个人输入结果的菊花链效应。学习输入和输出如何工作将说明构建块的想法以及软件如何从解决特定问题的大量小事扩展到解决更大问题的更大应用程序。如果几个10行程序可以很酷,那么把它们放在一起会有多酷?这是非线性的。
答案 42 :(得分:2)
也许这是愚蠢的,但我认为孩子们会直观地抓住它 - 从整个“你最喜欢的”程序员“卡通片”开始的卡通片?在What's your favorite "programmer" cartoon?。
E.g。 Foxtrot的Jason Fox在执行循环的电路板上编写代码。
可能感兴趣的点:编程可能会帮助你摆脱困境...
答案 43 :(得分:1)
Fibonacci数是学习递归的一个很好的例子。 它表明递归可以很容易编写,并且执行起来可能很昂贵。 负面条目案例可以在以后介绍。
int fiboNumber(int index)
{
if (index <= 1)
{
return index;
}
return fiboNumber(index - 1) + fiboNumber(index - 2);
}
答案 44 :(得分:1)
Logo始终是一个非常好的起点。
Brian Harvey的UCBLogo页面有this short example:
这是伯克利标识中的一个简短但完整的程序:
to choices :menu [:sofar []] if emptyp :menu [print :sofar stop] foreach first :menu [(choices butfirst :menu sentence :sofar ?)] end
这就是你如何使用它。你输入
choices [[small medium large] [vanilla [ultra chocolate] lychee [rum raisin] ginger] [cone cup]]
和Logo回复
small vanilla cone small vanilla cup small ultra chocolate cone small ultra chocolate cup small lychee cone small lychee cup small rum raisin cone small rum raisin cup small ginger cone small ginger cup medium vanilla cone medium vanilla cup medium ultra chocolate cone medium ultra chocolate cup medium lychee cone medium lychee cup medium rum raisin cone medium rum raisin cup medium ginger cone medium ginger cup large vanilla cone large vanilla cup large ultra chocolate cone large ultra chocolate cup large lychee cone large lychee cup large rum raisin cone large rum raisin cup large ginger cone large ginger cup该程序没有任何关于内置菜单大小的内容。您可以使用任意数量的类别,以及每个类别中的任意数量的可能性。让我们看看你用四行Java做到了这一点!
答案 45 :(得分:1)
我对谈话中显示的一些内容感到震惊Easy AI with Python(视频和PDF)。例如,教一台计算机如何玩Mastermind,解决八个女王,化妆品(那些像“9567 + 1085 == 10652”这样的谜题并推断数据中的关系。所有大约10行(可能有20或30行) “幕后”代码)。
答案 46 :(得分:1)
我认为任何可以做一些有用的shell脚本都是向人们展示编程能力的好方法。能够在一个小脚本上花费10-20分钟来自动完成另一项任务,并为您节省无数小时,这非常令人印象深刻,imo。
例如,我曾经写过一个简单的Perl脚本,将一个目录中的mp3文件转换为另一种格式,然后将它们刻录成CD。您使用MP3目录的路径调用脚本,然后刻录CD。至少我当时留下了深刻的印象。
答案 47 :(得分:1)
来自Andrew Cooke's Malbolge: Hello World
让他们在Malbolge尝试这个:(=<`$9]7<5YXz7wT.3,+O/o'K%$H"'~D|#z@b=`{^Lx8%$Xmrkpohm-kNi;gsedcba`_^]\[ZYXWVUTSRQPONMLKJIHGFEDCBA@?>=<;:9876543s+O<oLm
这是全世界最困难的编程语言中的“Hello World”花了两年才找到:)
他们可以前往Lou Scheffer's Malbolge page after:)
当然你可以去No. 1 (!!!) 99 Bottles of Beer Program in the World by Hisashi Iizawa (it's a ONE LINER!),这里是pdf by Masahiko Sakai on Malbolge
答案 48 :(得分:1)
如果您正在教授工程师,那么Prolog可能会引起他们的注意:
d(x,x,1).
d(C,x,0):-number(C).
d(C*x,x,C):-number(C).
d(-U, X, -DU) :- d(U, X, DU).
d( U + V, x, RU + RV ):-d(U,x,RU), d(V,x,RV).
d( U - V, x, RU - RV ):-d(U,x,RU), d(V,x,RV).
d(U * V,x, U * DV + V * DU):- d(U,x,DU), d(V,x,DV).
d(U^N, x, N*U^(N-1)*DU) :- integer(N), d(U, x, DU).
只需写下规则,你就有一个程序可以用8行代码完成所有第一学期的微积分。
答案 49 :(得分:1)
当Steve Wozniak制作他的第一台Apple II时,他喜欢用Apple Basic中的Breakout游戏展示它,当场输入。我认为它实际上大约是10行;我希望我能在这里粘贴它。您也可以在Processing等系统中执行此操作。
答案 50 :(得分:1)
我记得发现简单的循环很棒。每次我学习一门新语言时,我都会把这样的东西放在一起:
<?php
$numberOfBottles = 99;
print("<h1>$numberOfBottles Bottles of Beer on the Wall</h1>");
print("$numberOfBottles bottles of beer on the wall,<br />");
print("$numberOfBottles bottles of beer!<br />");
print("Take one down, pass it around,<br />");
for($numberOfBottles--; $numberOfBottles>1; $numberOfBottles--)
{
print("$numberOfBottles bottles of beer on the wall!<br />");
print("<br />");
print("$numberOfBottles bottles of beer on the wall,<br />");
print("$numberOfBottles bottles of beer!<br />");
print("Take one down, pass it around,<br />");
}
print("One last bottle of beer on the wall!");
?>
也许使用while或foreach循环的一些变体也很容易。
答案 51 :(得分:1)
也许考虑到这两个标准,使用Processing.js或Flash的Javascript可能是一个很好的起点,尽管Flash明显有缺点要求......呃...... Flash。
切向思考:Flash实际上是教授OOP的一种非常好的方式,因为当你实际看到它们时,更容易掌握对象和类的概念!
答案 52 :(得分:1)
我认为这个问题真是个好主意。我有很多很棒的老师,也是最好的老师,显然那些有意愿的人会有点炫耀。
你可以展示很多代码。我想到的第一个是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 bottles of beer是一个非常好的开始。
然后,您可以选择几个有趣的代码示例,例如:
着名的Python单行:
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))))
欺骗性的Python版本(学生很酷,因为它显示网络功能):
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 Demo web site是很多不错的小部件演示,包括片段。几行calendar:
<script type="text/javascript">
$(function() {
$("#datepicker").datepicker();
});
</script>
<div class="demo">
<p>Date: <input id="datepicker" type="text"></p>
</div>
Bookmarklets也有很多性感。 Readibility非常有趣:
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);
}
答案 53 :(得分:1)
尝试让您的学生编写Magic 8ball。一个基本的8ball回答“是”或“否”可能在不到10行代码中编程,并且可以通过多种方式逐步扩展:
魔术8ball是大多数人可以联系到的东西,它是对基本字符串,浮点数/整数,IO,CLI,布尔值和RNG的介绍,仅使用最简单的工具。而且它很简单,(有点)有趣,并且可以轻松扩展。根据你的方法,你可以使用类8ball(),类YesAnswer()和诸如此类的东西,立即使编程面向对象。
祝你好运; - )答案 54 :(得分:1)
您可以让您的学生前往codeplex IronPython silverlight示例网站,该网站包含&lt; 10行演示改变画布并与鼠标交互。您可以找到silverlight示例here
只是看到在网络浏览器中编写的代码,然后执行一个改变一个小的WPF可能会让一些人陶醉。
答案 55 :(得分:1)
这是一个用A-Prolog语言编写的程序,它计算图形的N颜色(“c”表示颜色,“v”表示顶点,“e”表示边缘。)
c(1..n).
1 {color(X,I) : c(I)} 1 :- v(X).
:- color(X,I), color(Y,I), e(X,Y), c(I).
另一方面,我让学生上学期兴奋的方式是告诉他们一个故事。它有点像:“想象一个三角形。它是一个纯粹的数学对象,不存在真正的三角形。我们可以推理它们,发现它们的属性,然后将这些属性应用于现实世界的解决方案。算法也是纯粹的然而,编程是一种神奇的形式。我们可以采用数学对象,将其描述为一种语言,并且可以看出它可以操纵物理世界。编程是一个独特的学科,它将这两个世界联系起来。“
答案 56 :(得分:1)
此PHP代码仅适用于通过命令行在Mac上运行,但在所有人想要播放Twister
时非常有用$lr = array('left', 'right');
$hf = array('hand', 'foot');
$colour = array('red', 'yellow', 'blue', 'green');
while(true) {
$a = $lr[array_rand($lr)];
$b = $hf[array_rand($hf)];
$c = $colour[array_rand($colour)];
system("say $a $b $c");
sleep(5);
}
答案 57 :(得分:0)
使用javascript
这是一件有趣的事情function checkLove(love)
{
if (love)
alert("He he he, cool!");
else
{
if(love != undefined) alert("Sorry, you must love me.");
checkLove(confirm("Do you love me?"));
}
}
checkLove();
只有10行! 您可以将其包含在网页中,也可以将以下代码复制粘贴到浏览器的网址栏中,然后按Enter键
javascript:function checkLove(love){if (love)alert("He he he, cool!");else{if(love != undefined) alert("Sorry, you must love me.");checkLove(confirm("Do you love me?"));}}checkLove();
有趣,对吧?
答案 58 :(得分:0)
如何处理JavaScript?我不知道Processing,但代码总是看起来很小,它可以做什么,它非常直观,你可以在浏览器中运行它。
http://processingjs.org/exhibition
答案 59 :(得分:0)
我的第一个程序有点类似于one already mentioned here,但我的一行更短,更有礼貌:
10 PRINT "What is your name?"
20 INPUT A$
30 PRINT "Thanks"
答案 60 :(得分:0)
从my answer到How do I loop through every 4th pixel in every 4th row, using Python?:
#!/usr/bin/env python
import easygui # http://easygui.sourceforge.net/
import Image # http://www.pythonware.com/products/pil/
import numpy # http://numpy.scipy.org/
filename = easygui.fileopenbox() # pick a file
im = Image.open(filename) # make picture
im.show() # show picture
ar = numpy.asarray(im) # get all pixels
N = 4
pixels = ar[::N,::4] # every 4th pixel in every N-th row
notes = pixels.sum(axis=2) / 9 + 24 # compute notes [0, 52]
print "number of notes to play:", notes.size
注释可以对应不同的音调。我在这里使用equal tempered scale:
# play the notes
import audiere # http://pyaudiere.org/
import time
d = audiere.open_device()
# Notes in equal tempered scale
f0, a = 440, 2**(1/12.)
tones = [d.create_tone(f0*a**n) for n in range(-26, 27)] # 53
for y, row in enumerate(notes):
print N*y # print original row number
for t in (tones[note] for note in row):
t.volume = 1.0 # maximum volume
t.play()
time.sleep(0.1) # wait around 100 milliseconds
t.stop()
答案 61 :(得分:0)
使用游戏!不编码游戏,而是编码竞赛。想想Google AI挑战,并将其愚蠢。
让我举个例子。我曾经和我的朋友们进行了一些比赛:我们中的一个人建立了一个模拟框架,其余的编写了一个AI,从简单到大量的分析,我们开了一百次,看看哪个AI表现得最好。< / p>
框架?基本I / O:模拟控制由每个竞争AI生成一个子进程的进程执行,每轮仿真将数据写入标准输入管道,并读取输出。通过这种方式,我们可以用我们想要的任何语言编写AI,只需遵循一个非常简单的协议即可。
规则非常简单,但事情却充满挑战:我们有两个村庄,A和B,它们在居住在那里的家庭中平均分配资金。 A有800个硬币,B有500个。每一轮,所有的AI都被要求选择一个村庄居住(打印'A'或'B'到stdout),然后找回每个村庄的总数在此期间(通过从标准输入读取数字)。目标是在一百回合后获得最多的钱。
我们创建的一些AI有非常复杂的机制来尝试猜测要定居的村庄 - 尽管它们并不是真的很好,因为胜利者是一种策略,总是选择给钱最少的村庄最后一轮的每个家庭(假设大多数人下次会搬到另一个村庄)。
我认为这很有吸引力,鼓励研究并且是一种健康的竞争方式。有数以千计的游戏可以玩,它只需要基本的编程知识(标准I / O!)供玩家互动。
答案 62 :(得分:0)
这是我的10行网络蜘蛛。从技术上讲,它包括perl shell声明,但我希望这是可以原谅的!
我想让它识别某些文件类型并支持相对路径,但是线路用完了!
运行:
perl spider.pl http://yahoo.com/search?q=test
请注意,如果没有用户代理,Google不允许使用LWP Simple,因此搜索Google无法使用。也没有空间!无论如何,代码在哪里:
#!/usr/bin/perl -w
use LWP::Simple;
my @queue = ($ARGV[0]);
my %visited = ();
while (my $url = pop(@queue)) {
next if $visited{$url};
$visited{$url} = 1;
my $html = get($url) or next;
print "Spidering $url\n";
push(@queue, $html =~ m/(http:\/\/[^'"]*)/g);
}
答案 63 :(得分:0)
答案 64 :(得分:0)
您可以使用jQuery(Write Less,Do More)库在HTML webforms中以最少的编码实现出色的视觉效果。否则像F#这样的函数式语言也可以用很少的代码行完成很多东西。以下是Project euler问题8的解决方案: -
数据: - 50 * 20网格中的数字字符串
让data = txt |&gt; Seq.toList |&gt; List.filter System.Char.IsDigit |&gt; List.map System.Char.GetNumericValue
让rec partition_5 l = 匹配l | x1 ::(x2 :: x3 :: x4 :: x5 :: _ as t) - &gt; [x1; x2; x3; x4; x5] ::(partition_5 t) | _ - &gt; []
让euler_8 = List.map(有趣的x - &gt; List.fold(*)1.0 x)(partition_5数据)|&gt; List.max
答案 65 :(得分:0)
吱吱声是激发灵感的好工具
例如:http://squeakland.jp/school/drive_a_car/html/Drivecar12.html
答案 66 :(得分:0)
在http://www.youtube.com/watch?v=a9xAKttWgP4你可以观看康威的生命游戏编程(同时口头评论)大约5行APL (编程语言)。
观看它很有趣,可以激发学生编程很酷,数学和数学,简洁的编程语言:)
BTW,鲍勃·马丁叔叔在hanselminutes播客上提到了这个YouTube视频。答案 67 :(得分:0)
受Robin Day和John Topley的回答启发,让他们将以下内容粘贴到他们浏览器的地址栏中:
javascript:var name = prompt(“你的名字是什么?”,“”); var msg ='Hello'+ name +'&lt; br&gt;'; newwindow = window.open(); newdocument = newwindow.document ; for(var i = 0; i&lt; 100; i ++){newdocument.write(msg);} newdocument.close();
或更可读:
var name=prompt("What is your name?", "");
var msg='Hello '+name+'<br>';
newwindow=window.open();
newdocument=newwindow.document;
for (var i=0;i<100;i++)
{
newdocument.write(msg);
}
newdocument.close();
答案 68 :(得分:0)
这些答案中的大多数都使用了某种API,这有点打破了10行代码要求。每个API调用可以是数百行代码。最初的问题是使用“简单代码”。这对我来说意味着没有API调用。我们只能使用“简单代码”这个定义来得出什么样的答案?
答案 69 :(得分:0)
我认为Python中使用NodeBox的一些很酷的表达将是一个很酷的开始。它具有从正方形到复杂路径绘制东西的功能。它甚至可以从Mac iSight / Webcam中获取图像,并通过缩放,旋转和应用滤镜来对其进行操作。
可悲的是,它仅适用于Mac OS X,所以我认为教它不会有多大用处,但作为一个例子(如果你自己有一台Mac)可以用一点点代码来实现它,它会非常漂亮。
答案 70 :(得分:0)
然后我把火修改到立方体上,这是很好的时光。
必须看看我是否可以在某处找到那些旧脚本,它们不是很冗长。
答案 71 :(得分:0)
当我小时候,这是有史以来最酷的事情:
10 PRINT "BEDWYR "
20 GOTO 10
我想这些日子不会减少太多;)
答案 72 :(得分:0)
在WPF中,您可以在一些XAML行中编写功能齐全的缩略图视图:
<ListBox ItemsSource={Binding MyItems}
ScrollViewer.HorizontalScrollBarVisibility="Hidden">
<ListBox.ItemTemplate>
<DataTemplate>
<Image Source={Binding FullPath} Width="50" />
</DataTemplate>
</ListBox.ItemTemplate>
<ListBox.ItemsPanel>
<ItemsPanelTemplate><WrapPanel /></ItemsPanelTemplate>
</ListBox.ItemsPanel>
</ListBox>
这是假设您有一个MyItems
项集合,其中包含指向图像文件的FullPath
属性。
魔术来自将每个列表框项目转换为图像的ItemTemplate
,以及将默认垂直堆栈面板更改为包装面板的ItemsPanelTemplate
。
答案 73 :(得分:0)
首先要在最短时间内获得最大关注,您需要使用高级语言。 可能你想要展示3D。
我会选择Opengl - 我首先会从3D计算机游戏中展示一个短片,然后解释说,这是通过将大程序划分为较小的部分来完成的,然后向他们展示如何一小部分可能看起来像。 像nehe.gamedev.net上的第05课,或者甚至是更高级的课程。 它非常令人印象深刻,并不太复杂。
此外,您可能需要查看包含3d的Alice并且是为了教授..
答案 74 :(得分:0)
像...一样的东西。
10 rem twelve times table
20 For x = 1 to 12
30 For y = 1 to 12
40 print using"####";x*y;
50 next y
60 print
70 next x
80 end
答案 75 :(得分:0)
当我还是个小孩的时候,我对计算机(当时的MSX)非常感兴趣,因此编程(所有这些都是Basic的变体)。我长大后失去了它,但当我得知反恐精英只是一些粉丝通过修改半条命代码而制作的mod时,我又回到了它。这让我对编程再次感兴趣!
这不是10行代码,但是如果你向人们展示某些游戏的源代码,然后修改它并让它做一些与众不同的东西,然后向他们展示它们,它真的会把它们吹走。哇,这其实不是黑魔法!你可以做到!
现在有一天,你可以做很多游戏。我认为所有Quake系列(至少I到III)的源代码都已发布。我知道你可以为Half-Life和Half-Life2创建mods,我确信像Unreal和FarCry这样的其他游戏也提供类似的能力。
一些可以引发激励的简单事情:
修改本身不应该占用太多代码,但它的工作原理真是太棒了。
答案 76 :(得分:0)
我为论坛游戏写了这篇文章 - 尽可能少地编写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);
}
我认为三元运算符非常整洁,但我听说它比构造要慢。我还没有为自己计时......
答案 77 :(得分:0)
我一直很喜欢河内塔。在Scheme
(define (hanoi x from to spare)
(if (= x 1)
(begin
(display "move ")(display from)(display " to ")(display to)(display "\n"))
(begin
(hanoi (- x 1) from spare to)
(hanoi 1 from to spare)
(hanoi (- x 1) spare to from))))
示例输出
gosh> (hanoi 3 'start 'dest 'spare)
move start to dest
move start to spare
move dest to spare
move start to dest
move spare to start
move spare to dest
move start to dest
#<undef>
同样在Python中(虽然这不能像Scheme版本那样可以制作1000张光盘)
def hanoi(x, source, dest, spare):
if x == 1:
print "%s to %s" % (source, dest)
else:
hanoi(x - 1, source, spare, dest)
hanoi(1, source, dest, spare)
hanoi(x - 1, spare, dest, source)
答案 78 :(得分:0)
Mandelbrot Set 可以以非常复杂的方式呈现,例如在下面的Java中:
public class MiniMandelbrot {
public static void main(String[] args) {
int[] rgbArray = new int[256 * 256];
for (int y=0; y<256; y++) {
for (int x=0; x<256; x++) {
double cReal=x/64.0-2.0, cImaginary=y/64.0-2.0;
double zReal=0.0, zImaginary=0.0, zRealSquared=0.0, zImaginarySquared=0.0;
int i;
for (i = 0; (i < 63) && (zRealSquared + zImaginarySquared < 4.0); i++) {
zImaginary = (zReal * zImaginary) + (zReal * zImaginary) + cImaginary;
zReal = zRealSquared - zImaginarySquared - cReal;
zImaginarySquared = zImaginary * zImaginary;
zRealSquared = zReal * zReal;
}
rgbArray[x+y*256] = i * 0x040404;
}
}
java.awt.image.BufferedImage bufferedImage = new java.awt.image.BufferedImage(256, 256, 1);
bufferedImage.setRGB(0, 0, 256, 256, rgbArray, 0, 256);
javax.swing.JOptionPane.showMessageDialog(null, new javax.swing.ImageIcon(bufferedImage), "The Mandelbrot Set", -1);
}
}
答案 79 :(得分:0)
这是迄今为止我见过的最酷的事情......当被打破时,它实际上非常简单:
http://blogs.msdn.com/lukeh/archive/2007/04/03/a-ray-tracer-in-c-3-0.aspx
答案 80 :(得分:0)
Ruby / Python / Perl中的基本grep应用程序。
答案 81 :(得分:0)
答案 82 :(得分:0)
递归也可用于解决迷宫问题。就像Sierpinski三角形和其他艺术一样,对我来说,这比解决一些数学问题更有趣。
答案 83 :(得分:0)
在您提供的SAPI示例的基础上,我使用它来自己大声读取文件(只需将文本文件拖放到它的图标上或从命令行运行它)
<强> speakfile.vbs 强>:
strFileName = Wscript.Arguments(0)
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(strFileName, 1)
strText = objFile.ReadAll
Set objVoice = CreateObject("SAPI.SpVoice")
objVoice.Speak strText
答案 84 :(得分:0)
10打印“Mohan”
20转到10
答案 85 :(得分:-1)
从Quake 3我相信,非常快1 / sqrt(x):
#include <stdio.h>
#include <stdlib.h>
int main (int argc, char const* argv[])
{
if (argc != 2) {
printf("Need a number!\n");
return 0;
}
float number = atof(argv[1]);
long i;
float x2, y;
const float threehalfs = 1.5F;
x2 = number * 0.5F;
y = number;
i = * ( long * ) &y; // evil floating point bit level hacking
i = 0x5f3759df - ( i >> 1 ); // what the?
y = * ( float * ) &i;
y = y * ( threehalfs - ( x2 * y * y ) ); // 1st iteration
printf("%f\n", y);
return 0;
}
答案 86 :(得分:-1)
搞乱饼干。
它的饼干!孩子们loooovvve饼干!