Skip to content

Commit fedee7c

Browse files
committed
Add IOCCC example just for fun
1 parent 681e9fc commit fedee7c

File tree

1 file changed

+95
-4
lines changed

1 file changed

+95
-4
lines changed

lessons/C Bootcamp.ipynb

Lines changed: 95 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"metadata": {
33
"name": "",
4-
"signature": "sha256:d5ce0ce310e578ae5c5b67a5b03692dad5566ccacc895a2a63c1504c72209c34"
4+
"signature": "sha256:edd29b7861ab24314c1ed7aa0c93b83913d2a2521750649515c0da87244c9129"
55
},
66
"nbformat": 3,
77
"nbformat_minor": 0,
@@ -15,11 +15,102 @@
1515
"Why C?\n",
1616
"====\n",
1717
"\n",
18-
"1. Speed\n",
19-
"2. Use (or wrap) llibraries not available in language X\n",
20-
"3. Under the hood, both R and Python are written in C"
18+
"1. Speed (and size)\n",
19+
"2. Use (or wrap) libraries not available in language X\n",
20+
"3. C programs are portable across platforms\n",
21+
"4. It is good enough to write the Linux and OSX operating systems, as well as languages such as R and Python"
2122
]
2223
},
24+
{
25+
"cell_type": "markdown",
26+
"metadata": {},
27+
"source": [
28+
"### Why not C? \n",
29+
"\n",
30+
"What other language has an annual Obfuscated Code Contest <http://www.ioccc.org/>? \n",
31+
"\n",
32+
"Here is one winning [entry](http://www.ioccc.org/2013/dlowe/hint.html) that should warm the heart of statisticians - it displays sparklines (invented by Tufte).\n",
33+
"\n",
34+
"```c\n",
35+
"main(a,b)char**b;{int c=1,d=c,e=a-d;for(;e;e--)_(e)<_(c)?c=e:_(e)>_(d)?d=e:7;\n",
36+
"while(++e<a)printf(\"\\xe2\\x96%c\",129+(**b=8*(_(e)-_(c))/(_(d)-_(c))));}\n",
37+
"```"
38+
]
39+
},
40+
{
41+
"cell_type": "code",
42+
"collapsed": false,
43+
"input": [
44+
"%%file sparkl.c\n",
45+
"main(a,b)char**b;{int c=1,d=c,e=a-d;for(;e;e--)_(e)<_(c)?c=e:_(e)>_(d)?d=e:7;\n",
46+
"while(++e<a)printf(\"\\xe2\\x96%c\",129+(**b=8*(_(e)-_(c))/(_(d)-_(c))));}"
47+
],
48+
"language": "python",
49+
"metadata": {},
50+
"outputs": [
51+
{
52+
"output_type": "stream",
53+
"stream": "stdout",
54+
"text": [
55+
"Overwriting sparkl.c\n"
56+
]
57+
}
58+
],
59+
"prompt_number": 539
60+
},
61+
{
62+
"cell_type": "code",
63+
"collapsed": false,
64+
"input": [
65+
"!gcc -Wno-implicit-int -include stdio.h -include stdlib.h -D'_(x)=strtof(b[x],0)' sparkl.c -o sparkl"
66+
],
67+
"language": "python",
68+
"metadata": {},
69+
"outputs": [],
70+
"prompt_number": 540
71+
},
72+
{
73+
"cell_type": "code",
74+
"collapsed": false,
75+
"input": [
76+
"import numpy as np\n",
77+
"np.set_printoptions(linewidth=np.infty)\n",
78+
"print ' '.join(map(str, (100*np.sin(np.linspace(0, 8*np.pi, 30))).astype('int')))"
79+
],
80+
"language": "python",
81+
"metadata": {},
82+
"outputs": [
83+
{
84+
"output_type": "stream",
85+
"stream": "stdout",
86+
"text": [
87+
"0 76 98 51 -31 -92 -88 -21 60 99 68 -10 -82 -96 -41 41 96 82 10 -68 -99 -60 21 88 92 31 -51 -98 -76 0\n"
88+
]
89+
}
90+
],
91+
"prompt_number": 36
92+
},
93+
{
94+
"cell_type": "code",
95+
"collapsed": false,
96+
"input": [
97+
"%%bash\n",
98+
"\n",
99+
"./sparkl 0 76 98 51 -31 -92 -88 -21 60 99 68 -10 -82 -96 -41 41 96 82 10 -68 -99 -60 21 88 92 31 -51 -98 -76 0"
100+
],
101+
"language": "python",
102+
"metadata": {},
103+
"outputs": [
104+
{
105+
"output_type": "stream",
106+
"stream": "stdout",
107+
"text": [
108+
"\u2585\u2588\u2588\u2587\u2583\u2581\u2581\u2584\u2587\u2589\u2587\u2584\u2581\u2581\u2583\u2586\u2588\u2588\u2585\u2582\u2581\u2582\u2585\u2588\u2588\u2586\u2582\u2581\u2581\u2585"
109+
]
110+
}
111+
],
112+
"prompt_number": 37
113+
},
23114
{
24115
"cell_type": "markdown",
25116
"metadata": {},

0 commit comments

Comments
 (0)