{ "metadata": { "name": "", "signature": "sha256:1afdeae67521bda2e8cf5103d7cc4d80130bc00587a4125e79db6b67e7083ced" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "
\n",
"Now you have all the tools you need to write code that practically writes itself..."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"##Breakout 7\n",
"\n",
"Survival Driven Development\n",
"===========================\n",
"\n",
"Survival Driven Development (SDD) is the newest software development fad. In this development framework, you specify what the software is supposed to do, then randomly generate source code to fulfill these requirements. Most of these attempts will fail, but hopefully one will succeed.\n",
"\n",
"Your task is to use SDD to make a function to approximate `x**2 + x`.\n",
"\n",
"Hint 1: Randomly generate lambda functions using a restricted vocabulary of source fragments.
\n",
"`vocab = ['x', 'x', ' ', '+', '-', '*', '/', '1', '2', '3']`\n",
"\n",
"Hint 2: Only evaluate `x` at a small-ish number of values and save the difference between those answers and the true value of `x**2 + x`.\n",
"\n",
"Hint 3: SDD is error prone. Be sure to catch your errors!"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"![How could you possibly think typing \"import skynet\" was a good idea?](http://imgs.xkcd.com/comics/2008_christmas_special.png)"
]
},
{
"cell_type": "code",
"collapsed": true,
"input": [
"# if you were having trouble getting started...\n",
"import random\n",
"import numpy\n",
"\n",
"vocab = [\"x\",\"x\",\"\",\"+\",\"-\",\"*\",\"/\",\"1\",\"2\",\"3\"]\n",
"\n",
"max_try = 1000000\n",
"max_chars = 10 #max number of characters to generate\n",
"x_array = numpy.arange(-3,3,0.4) # over a smallish range\n",
"real_function = x_array**2 + x_array\n",
"\n",
"tries = []\n",
"\n",
"# for loop...\n",
"# you fill in the rest"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 16
},
{
"cell_type": "code",
"collapsed": false,
"input": [],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 17,
"text": [
"1L"
]
}
],
"prompt_number": 17
}
],
"metadata": {}
}
]
}