<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Program This</title>
	<atom:link href="http://programthis.net/feed/" rel="self" type="application/rss+xml" />
	<link>http://programthis.net</link>
	<description>Take control of your boredom</description>
	<lastBuildDate>Fri, 13 Apr 2012 05:27:03 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>A Colorful Challenge</title>
		<link>http://programthis.net/a-colorful-challenge/</link>
		<comments>http://programthis.net/a-colorful-challenge/#comments</comments>
		<pubDate>Fri, 13 Apr 2012 05:23:49 +0000</pubDate>
		<dc:creator>jordan</dc:creator>
				<category><![CDATA[Challenge]]></category>

		<guid isPermaLink="false">http://programthis.net/?p=243</guid>
		<description><![CDATA[MISSION: Print colors to the terminal SKILLS: Basic string manipulation Computer graphics have always been a curiosity for me. Ever since I started programming, printing pretty things to screen was always something I just found to be really cool. Unfortunately, &#8230; <a href="http://programthis.net/a-colorful-challenge/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<div class="mission">MISSION: Print colors to the terminal</div>
<div class="skills">SKILLS: Basic string manipulation</div>
<p>Computer graphics have always been a curiosity for me. Ever since I started programming, printing pretty things to screen was always something I just found to be really cool. Unfortunately, graphics involve a lot of setup, libraries, and copy-and-paste code - not something I'm too fond of. And (as I found out the hard way) even hard-coding the most basic 3D scene requires a ton of preliminary code and a good understanding of topics like Linear Algebra. So today we're going to take it slow. </p>
<h1>Colors on the Terminal</h1>
<p>You might be thinking, "The only colors I see in my terminal are a few shades of blue and red, highlighting file names. What am I supposed to do with that?" Well, you'd be surprised. We're going to be using some <b>ANSI Escape Sequences</b> to actually draw our colors. </p>
<p>So how do they work?</p>
<p>These escape sequence, when picked up by your terminal, will mess with its settings a bit. Think of the newline character (\n). When the terminal comes across a back slash and an n, it moves the cursor to the next line. This is what's known as an <b><u>escape code</u></b> and we'll be using some of them to draw colors. Now, when I say draw colors, what we're actually going to do is <i>change</i> our terminal background color, write something, then change it back. It's a little confusing, so let's go through it step by step.</p>
<p>My motivation for this post was the fact that, when I wanted to experiment with color escape codes, I had a tough time figuring out what exactly I had to do. I hope this solves such frustration for others. There are many ways (and libraries) to go about printing colors to your terminal, but this is something that worked for me. So let's recognize an escape code in the format</p>
<p><code>\033[XYm</code></p>
<p>Where X and Y are two numbers. Here,</p>
<ul>
<li>X sets the mode (foreground, background): 3 or 4</li>
<li>Y sets the color: 0-7</li>
<li>The code \033[0m sets everything back to default</li>
</ul>
<p>As a reference, I set the follow constants in my code.</p>
<pre>
BLACK_FG  = '\033[30m'
RED_FG    = '\033[31m'
GREEN_FG  = '\033[32m'
YELLOW_FG = '\033[33m'
BLUE_FG   = '\033[34m'
PURPLE_FG = '\033[35m'
CYAN_FG   = '\033[36m'
GRAY_FG   = '\033[37m'

BLACK_BG  = '\033[40m'
RED_BG    = '\033[41m'
GREEN_BG  = '\033[42m'
YELLOW_BG = '\033[43m'
BLUE_BG   = '\033[44m'
PURPLE_BG = '\033[45m'
CYAN_BG   = '\033[46m'
GRAY_BG   = '\033[47m'

END       = '\033[0m'
</pre>
<p>So let's say I wanted to print the following<br />
<code><span style="color: yellow">Hello, </span><span style="color:blue; background-color:red">world</span></code></p>
<p>That is, yellow on black for "Hello, " and blue on red for "world!" How would we do this? It's simple, really.</p>
<ul>
<li>We set the foreground color to yellow</li>
<li>Write "Hello, "</li>
<li>Set the foreground color to blue</li>
<li>Set the background color to red</li>
<li>Write "world"</li>
<li>End.</li>
</ul>
<p>That's right, you can stack many of these bad boys on top of each other. So what would the string look like that we need to print?</p>
<p><code>\033[33mHello, \033[34m\033[41mworld\033[0m</code></p>
<p>Go ahead and verify this with</p>
<p><code>python -c "print '\033[33mHello, \033[34m\033[41mworld\033[0m'"</code></p>
<p>To see the expected output. </p>
<p>So, can you see the individual pieces? First we set the FG to yellow with \033[34m. Then, we put "Hello, ". Next, \033[34m to set the FG to blue, followed by \033[41m to set the background to red. Finally we wrote "world" and printed the "END" sequence. Using the constants, this could be easily achieved with</p>
<p><code>print YELLOW_FG + "Hello, " + BLUE_FG + RED_BG + "world" + END</code></p>
<p>Now, what if you wanted to print a "pixel," that is, a colored square with nothing in it. Not a problem, just escape the string with a background color, <b>print a space</b>, and end it. That's all!</p>
<p>So now, your challenge? I want you to experiment with this stuff and have fun your own way, but I am proposing a graphical puzzle of sorts to those looking for something to code (that's the point here anyway, right?) Your task, shall you accept it, is to print the American flag. Here's my attempt at it. </p>
<p><center><img src="/resources/flag.png" /><br/><i>See my terminal prompt? That, too, uses color escape codes. Check out your environment variable, $PS1, and play around with it.</i></center></p>
<p>And, of course, if you're not a US citizen, feel free to print your own flag - or any flag you'd like! Bonus points for clean code, which is tough to do without extracting some sort of <b>pattern</b> from the design. Some basic guidelines for the American flag...</p>
<ul>
<li>The flag should have 13 stripes - starting with red and ending with red</li>
<li>50 stars, arranged in 9 rows of (7,6,7,6,7,6,7,6,7)</li>
<li>Dimensions close to the actual <a href="https://www.google.com/search?q=american+flag&#038;um=1&#038;ie=UTF-8&#038;hl=en&#038;tbm=isch&#038;source=og&#038;sa=N&#038;tab=wi&#038;ei=yraHT4KKBIrs0gHCxvDsCQ&#038;biw=1440&#038;bih=779&#038;sei=y7aHT8KUFenz0gHYv-XwCQ" target="_blank">American flag</a></li>
</ul>
<p>That's all for now, I'm looking forward to your solutions and cool drawings. Be sure to show them off in the comments section below.<br />
Jordan</p>
<p><i>PS - For those curious, my $PS1 is exported to \033[32m\u\033[0m\033[36m@\033[0minitech(\033[44m\W\033[0m) \033[95m§\033[0m in ~/.bash_profile</i></p>
]]></content:encoded>
			<wfw:commentRss>http://programthis.net/a-colorful-challenge/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Numbers for People</title>
		<link>http://programthis.net/numbers-for-people/</link>
		<comments>http://programthis.net/numbers-for-people/#comments</comments>
		<pubDate>Sun, 12 Feb 2012 00:02:18 +0000</pubDate>
		<dc:creator>jordan</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://programthis.net/?p=236</guid>
		<description><![CDATA[MISSION: To print a number in human-readable format SKILLS: Modular arithmetic, String operations Numbers are pretty awesome. People are also pretty awesome. However, the relationship between humans and numbers is fuzzy to most. We do some pretty complex stuff with &#8230; <a href="http://programthis.net/numbers-for-people/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<div class="mission">MISSION: To print a number in human-readable format</div>
<div class="skills">SKILLS: Modular arithmetic, String operations</div>
<p>Numbers are pretty awesome. People are also pretty awesome. However, the relationship between humans and numbers is fuzzy to most. We do some pretty complex stuff with numbers in our heads without even knowing it. Let's take reading numbers for instance.</p>
<h1>464,723,104,643</h1>
<p>If I asked you to read this number aloud, you'd do without issue. What's going on in your head when you do this? Well, a few things pop into my head.</p>
<ul>
<li>Well, there are 4 groups of numbers, so we'll start at <i>billion</i></li>
<li>Okay the first one is 4. <i>four hundred</i></li>
<li>The second is 6. <i>sixty</i></li>
<li>And lastly, 4. <i>four</i></li>
<li>Putting this all together, we start with <i>four hundred sixty-four billion</i></li>
</ul>
<p>I don't need to teach you how to read numbers - you all know how. But modeling this process with computer code is pretty interesting! So that's what we'll be doing today. </p>
<p>Now, guidelines are kind of fuzzy - but I'm hoping you can closely match my output. A lot of people get confused when to use 'and' and when to use, hyphens, etc. As usual, feel free to do whatever looks best or makes the most sense for you! However, many of you will try to match my output, which is cool too. </p>
<pre>
initech:numbers-for-people jordan$ ./num-to-human.py 9
nine
initech:numbers-for-people jordan$ ./num-to-human.py 12
twelve
initech:numbers-for-people jordan$ ./num-to-human.py 64
sixty-four
initech:numbers-for-people jordan$ ./num-to-human.py 587
five hundred eighty-seven
initech:numbers-for-people jordan$ ./num-to-human.py 590
five hundred ninety
initech-2:numbers-for-people jordan$ ./num-to-human.py 10000000000
ten billion
initech:numbers-for-people jordan$ ./num-to-human.py 45671
fourty-five thousand six hundred seventy-one
initech:numbers-for-people jordan$ ./num-to-human.py 123456789
one hundred twenty-three million four hundred fifty-six thousand seven hundred eighty-nine
initech:numbers-for-people jordan$ ./num-to-human.py 1234567890
one billion two hundred thirty-four million five hundred sixty-seven thousand eight hundred ninety
initech:numbers-for-people jordan$ ./num-to-human.py 464723104643
four hundred sixty-four billion seven hundred twenty-three million one hundred four thousand six hundred forty-three
initech:numbers-for-people jordan$
</pre>
<p>Be sure to save this code (why wouldn't you?) because we'll be using it for some analysis next time.</p>
<p>All the best,<br />
Jordan</p>
]]></content:encoded>
			<wfw:commentRss>http://programthis.net/numbers-for-people/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Further Development</title>
		<link>http://programthis.net/further-development/</link>
		<comments>http://programthis.net/further-development/#comments</comments>
		<pubDate>Mon, 06 Feb 2012 00:20:57 +0000</pubDate>
		<dc:creator>jordan</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://programthis.net/?p=232</guid>
		<description><![CDATA[Hey folks, I hadn't used WordPress before, and I must say I am pretty impressed with the way things have been working lately. However, I prefer to do my own thing. So for the next couple of weeks/months/millenia I'll be &#8230; <a href="http://programthis.net/further-development/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Hey folks,</p>
<p>I hadn't used WordPress before, and I must say I am pretty impressed with the way things have been working lately. However, I prefer to do my own thing. </p>
<p><center><br />
<img src="http://media.smashingmagazine.com/images/introduction-to-rails/rails.jpg" /><br />
</center></p>
<p>So for the next couple of weeks/months/millenia I'll be making a custom version of ProgramThis to suit my needs. This includes a less-bloggy platform and a more challengey feel to the site. Some things I hope to achieve...</p>
<ul>
<li>Mailing lists</li>
<li>API</li>
<li>Better viewer submissions model</li>
<li>Submission voting</li>
<li>Post ratings</li>
<li>Lots of other goodies</li>
</ul>
<p>So yeah, things will be pretty awesome <img src='http://programthis.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  What this means for you as a reader is, well, nothing. I'll still be updating this site with challenges, and I promise you won't need to re-register. All of the accounts and challenges will be here once the new site launches.</p>
<p>Here's to the future,<br />
Jordan</p>
]]></content:encoded>
			<wfw:commentRss>http://programthis.net/further-development/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>ProgramThis needs YOU!</title>
		<link>http://programthis.net/programthis-needs-you/</link>
		<comments>http://programthis.net/programthis-needs-you/#comments</comments>
		<pubDate>Sun, 22 Jan 2012 22:59:10 +0000</pubDate>
		<dc:creator>jordan</dc:creator>
				<category><![CDATA[Note]]></category>

		<guid isPermaLink="false">http://programthis.net/?p=229</guid>
		<description><![CDATA[Hi folks. Glad to be back to talk to you after starting school / parttime work / madness. I apologize for not keeping the site as updated as I'd like to, but I'm trying! As I mentioned earlier, ProgramThis is &#8230; <a href="http://programthis.net/programthis-needs-you/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Hi folks. Glad to be back to talk to you after starting school / parttime work / madness. I apologize for not keeping the site as updated as I'd like to, but I'm trying!</p>
<p><center><img src="http://gems.github.com/octocat.png" width=300 height=300 /></center></p>
<p>As I mentioned earlier, <a href="http://github.com/prezjordan/programthis">ProgramThis is on GitHub</a>. This means I'm looking for some collaboration, I want <b>YOUR</b> solutions in this repository! Why? Because community involvement is never a bad thing, and I'm interested to see your solutions outside of the buggy comments section. For those unfamiliar with Git and GitHub, I'll be writing some articles/tutorials in the near future. But, at a glance (from the README), here's the process for contributing your solutions to our project.</p>
<ul>
<li>Fork this repo</li>
<li>Go into the folder of the challenge you have solved</li>
<li>Place your code in the folder</li>
<li>Rename your code as username.extension (if your username is JohnD and you wrote this in Python - johnd.py)</li>
<li>Commit and push updates to your fork</li>
<li>Submit a pull request with a title like: JohnD's solution to a-day-at-the-lanes
</li>
</ul>
<p>So that's all! Secure your fame by contributing your solutions to the ProgramThis GitHub repository <a href="http://github.com/prezjordan/programthis">here</a>.</p>
<p>Cheers,<br />
Jordan</p>
]]></content:encoded>
			<wfw:commentRss>http://programthis.net/programthis-needs-you/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Creating a Word Search Puzzle</title>
		<link>http://programthis.net/word-search/</link>
		<comments>http://programthis.net/word-search/#comments</comments>
		<pubDate>Sat, 07 Jan 2012 05:28:13 +0000</pubDate>
		<dc:creator>jordan</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://programthis.net/?p=226</guid>
		<description><![CDATA[MISSION: To generate a Word Search puzzle from a list of words SKILLS: String comparison, Matrix operations Wow, it's a been a while since I've talked to all of you. I hope you've all had a wonderful holiday season - &#8230; <a href="http://programthis.net/word-search/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<div class="mission">MISSION: To generate a Word Search puzzle from a list of words</div>
<div class="skills">SKILLS: String comparison, Matrix operations</div>
<p>Wow, it's a been a while since I've talked to all of you. I hope you've all had a wonderful holiday season - I know I sure did. I've been keeping myself busy relaxing in Florida and working on my open source Python project, <a href="http://github.com/prezjordan/Melopy">Melopy</a>, sometimes at the same time.</p>
<p>Anyway, I've got a neat little project for you to work on today. We'll be creating a <a href="http://en.wikipedia.org/wiki/Word_search">Word Search</a> puzzle, with our own custom list of words.</p>
<p><center><img src="http://upload.wikimedia.org/wikipedia/commons/thumb/f/fa/Wordsearch.svg/300px-Wordsearch.svg.png" /></center></p>
<p>If you're unfamiliar with the concept of a Word Search, it involves a grid consisting of letters. Hidden in this jumble of letters are words, which flow horizontally, vertically, or even diagonally. Words may also share letters.</p>
<p>So today our objective is to make our own word search generator, populated with words from a text file. Some things to consider...</p>
<ul>
<li>You'll need to fit all of the words, so create a square grid with a big enough width and height</li>
<li>Words SHOULD overlap, so be sure to allow that sort of behavior (hey, maybe encourage it!)</li>
<li>Normal word searches consist of diagonals, but for our purposes let's go ahead and just worry about vertical and horizontal words</li>
<li>Your program should print a neat grid with all the letters, as well as an answer key, so you can print this and solve your own puzzle!</li>
</ul>
<p>So here's some output - to make it more clear I went ahead and added a 'debug' flag to make sure that my words were printing correctly. Naturally, I chose the first 25 chemical elements as my dictionary.</p>
<pre>
initech:wordsearch jordan$ ./wordsearch.py debug `cat words.txt`
          p h o s p h o r o u s
  s             s m a g n e s i u m
  o             i                         b
  d n e o n     l                         e
  i             i                   h     r
  u             c       o           y     y
  m             o       x           d c   l
l               n       y           r a   l
i m   v a n a d i u m   g           o r c i
t a s                   e           g b h u
h n u             s c a n d i u m   e o l m
i g l   b o r o n           a r g o n n o
u a f         c h r o m i u m           r
m n u h e l i u m a l u m i n u m       i
  e r                                   n
  s                                     e
  e         n i t r o g e n
c a l c i u m
                        p o t a s s i u m  

  f l u o r i n e
                    t i t a n i u m        

hydrogen
helium
lithium
beryllium
boron
carbon
nitrogen
oxygen
fluorine
neon
sodium
magnesium
aluminum
silicon
phosphorous
sulfur
chlorine
argon
potassium
calcium
scandium
titanium
vanadium
chromium
manganese
initech:wordsearch jordan$
</pre>
<p>And the normal game (with jumbled letters everywhere)</p>
<pre>

initech:wordsearch jordan$ ./wordsearch.py `cat words.txt`
n j s c a n d i u m g t l u k b p o j d n a
f i b s v u f k m w n s c f b n a y j p s v
b o o q d c f l p k m a d v z e o d s l m k
x x r n p h n o q b l e r c f o o n o h y k
p y o t h l k k c n o u j l n n b j d k m q
i g n q d o n k w s i l i c o n i e i q b a
k e m n z r p c i g l w h j u d h e u v c c
b n a v g i q l m v p o t a s s i u m t m a
p b s g x n y c e h w h e l i u m x s i n l
j d k x p e j q h a m v p w q h h v z t f c
n i t r o g e n u y i b z b e x t u t a m i
k v a n a d i u m z c e l f l u o r i n e u
v w v a c i a l u m i n u m z i z c g i n m
d l p e x u l n r g b e r y l l i u m u a b
g l q t c a r b o n l a f q h f q h k m q r
x k w l k f f c h y d r o g e n t w o c i b
p h o s p h o r o u s y q e a r g o n k w v
j g j z k g n r r t h u c j v r d e e c k b
c p m a n g a n e s e c h r o m i u m s y r
f v c w d m a g n e s i u m r a r n i b l p
v c x f d c e y h l i t h i u m e e u k w a
b k u l u l s u l f u r d e x k b r g n y q

hydrogen
helium
lithium
beryllium
boron
carbon
nitrogen
oxygen
fluorine
neon
sodium
magnesium
aluminum
silicon
phosphorous
sulfur
chlorine
argon
potassium
calcium
scandium
titanium
vanadium
chromium
manganese
initech:wordsearch jordan$
</pre>
<p>Enjoy, this challenge isn't at hard as it looks - it took me about a half hour with ~70 lines of clean Python code. See what you can do. More importantly, have fun with it. Be sure to share your solutions in the comments below.</p>
<p>Jordan</p>
]]></content:encoded>
			<wfw:commentRss>http://programthis.net/word-search/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Simple Encryption</title>
		<link>http://programthis.net/simple-encryption/</link>
		<comments>http://programthis.net/simple-encryption/#comments</comments>
		<pubDate>Thu, 22 Dec 2011 07:13:50 +0000</pubDate>
		<dc:creator>jordan</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://programthis.net/?p=213</guid>
		<description><![CDATA[MISSION: To write a small XOR encryption tool SKILLS: XOR binary operations, File I/O Today I wrote a small utility (and put it in my personal utilities folder) that I think you'll all enjoy. It gives me a chance to &#8230; <a href="http://programthis.net/simple-encryption/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<div class="mission">MISSION: To write a small XOR encryption tool</div>
<div class="skills">SKILLS: XOR binary operations, File I/O</div>
<p>Today I wrote a small utility (and <a href="/custom-terminal-commands/">put it in my personal utilities folder</a>) that I think you'll all enjoy. It gives me a chance to dive into binary operations, and, in the process, help you make something really cool. </p>
<p>So I've talked about <a href="/hail-caesar/">ciphers</a> before, but let's take another approach at basic encryption, XOR. </p>
<h1>What is XOR?</h1>
<p>Well, remember my brief article on <a href="/the-wonderful-world-of-binary/">binary</a>? We're gonna need to apply the same skills right off the bat. You see, XOR is a <b><i>binary operation</i></b> meaning - you guessed it - an operation on one or more binary numbers. In the same way that addition (+) can be an operation between two numbers, XOR is an operation between two <b><i>binary numbers</i></b>. Consider the following. (XOR is commonly denoted with a caret symbol, ^)</p>
<pre>
42 ^ 36
</pre>
<p>(putting this in binary)</p>
<pre>
101010 ^ 100100

       101010
 (xor) 100100
 ------------
       ??????
</pre>
<p>Now, let's think about addition a little. Although you generally add two complete numbers together, this is done a series of steps. Namely, we add each pair of digits (and sometimes carry-over). So 42 + 36 is usually written down in a neat little chart with the calculations for 4+3 and 2+6 done for each digit. Simple enough, right?</p>
<p>Well binary operations are performed in the same way. We're going to go to each pair of digits and fool around with them a little. Consider the following charts.</p>
<pre>
OR  1  0    AND  1  0
 1  1  1      1  1  0
 0  1  0      0  0  0

XOR  1  0
  1  0  1
  0  1  0
</pre>
<p>I'll probably write up an article about this - if some of you are unfamiliar with the concept and want a thorough explanation, but for now let's look up. What the hell do those charts mean? Well, we're going to read them like a table, with 1 and 0 at the top, and 1 and 0 at the side.</p>
<p>Here you can see various results of binary operations, but the one we're focusing on today is XOR. That is - <u>exactly one bit is 1 and exactly one bit is 0</u>. They don't have to be in any particular order. So 1 XOR 1 is 0 (false), 0 XOR 1 is 1 (true), and 0 XOR 0 is 0 (false). Here I'm just using the basic idea of 1 = TRUE and 0 = FALSE.</p>
<p>So now, we're going to XOR each pair of digits!</p>
<pre>
       101010
 (xor) 100100
 ------------
       ?????0
</pre>
<p>We'll start slow. 0 XOR 0? 0. Let's do another.</p>
<pre>
       101010
 (xor) 100100
 ------------
       ????10
</pre>
<p>1 XOR 0? 1. Let's just go ahead and finish it so I don't waste any more of your time.</p>
<pre>
       101010
 (xor) 100100
 ------------
       001110 = 14
</pre>
<p>So now, we arrive at the result 42 XOR 36 = 14. Cool, right? Sorry it took so long to arrive there - you could say I'm thorough.</p>
<h1>So what's special about XOR?</h1>
<p>Well, we did 42 XOR 36, and got 14, right? What if we do 42 XOR 14? </p>
<pre>
       101010
 (xor) 001110
 ------------
       100100 = 36
</pre>
<p>Interesting. We got 36. How about 36 XOR 14?</p>
<pre>
       100100
 (xor) 001110
 ------------
       101010 = 42
</pre>
<p>As you can see, if we XOR two numbers and get a third, we can XOR THAT number with any of the two to get the other one. Quite an interesting property.</p>
<h1>So how about this encryption thing?</h1>
<p>Well, say we had the number 42, and 36 was our <u>key</u>. We could "encrypt" 42 with our key to get 14. And the cool part? We could "encrypt" 14 with our key to get the original 42. It works both ways! So that's the idea for today.</p>
<h1>Stop boring me, let's code</h1>
<p>Alright, alright, sorry. I just want to make sure you all have a good understanding before I move on. So now what? Well, what if we were to read in a text file, and encrypt each character (byte) by some sort of key? Let's explore! Consider the message "hello." Let's crank out the <a href="/the-wonderful-world-of-binary/">binary code</a> while we're at it.</p>
<pre>
new-host-2:binary jordan$ echo hello | ./binary.py
01101000 01100101 01101100 01101100 01101111
new-host-2:binary jordan$
</pre>
<p>As I've said in the past, each series of 8 bits corresponds to one byte, which corresponds to a character in our string. Now, let's go ahead and pick a key. How about 42?</p>
<p>Now. We XOR the whole damn thing. You'll see I've padded the 42 to 8 bits, by adding two zeroes in the front.</p>
<pre>
   01101000 01100101 01101100 01101100 01101111
 ^ 00101010 00101010 00101010 00101010 00101010
 ----------------------------------------------
   01000010 01001111 01000110 01000110 01000101
</pre>
<p>Which gives us new values - 66, 79, 70, 70, 69. Luckily, each of these corresponds to an ASCII value. So "hello" has been encoded into "BOFFE". It may seem like a shift, but it's actually a lot different!</p>
<p>Okay, so now we give our friend "BOFFE" and the key, 42. What does he get?</p>
<pre>
   01000010 01001111 01000110 01000110 01000101
 ^ 00101010 00101010 00101010 00101010 00101010
 ----------------------------------------------
   01101000 01100101 01101100 01101100 01101111 = hello
</pre>
<p>Damn amazing, huh?</p>
<h1>Where to?</h1>
<p>Well, at this point, there's a number of things you can do. As we saw in the <a href="/vigenere-cipher/">Vigenère Cipher</a>, we can easily pick a multi-character (or digit) code, say (31, 30, 98) or even "hey" and continuously repeat it about our word before encoding each byte. </p>
<p>Even cooler, try and do this on other files, like mp3s, pictures, and anything gross you have on your computer that you don't want people to see. The only difference between a text document and these other file formats is that each byte of a text document conveniently corresponds to a character (ASCII values). There's nothing stopping you from encrypting other stuff, though!</p>
<p>The basic idea is always the same, we take a key and apply it (using XOR) over a series of bytes in a file. Then, later on, we can apply the same key on our encrypted bytes to get the original file.</p>
<p>So have at it, I hope you learned something today, and while I don't have a legitimate challenge for you - I thought maybe you'd all get something out of this. Next time we see each other, I'll go over my code (I wrote this in C for speed purposes) and explain how you can get started encrypting (and decrypting!) files on your own computer.</p>
<p>Enjoy,<br />
Jordan</p>
]]></content:encoded>
			<wfw:commentRss>http://programthis.net/simple-encryption/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>How-to: Custom Terminal Commands</title>
		<link>http://programthis.net/custom-terminal-commands/</link>
		<comments>http://programthis.net/custom-terminal-commands/#comments</comments>
		<pubDate>Wed, 14 Dec 2011 01:19:59 +0000</pubDate>
		<dc:creator>jordan</dc:creator>
				<category><![CDATA[The Basics]]></category>

		<guid isPermaLink="false">http://programthis.net/?p=199</guid>
		<description><![CDATA[HOWTO: Create your own terminal commands TL;DR Linux and Mac users, I figure it's about time I address something many of you have been emailing me about. When you write code in an interpreted language (such as Python, Ruby, etc.), &#8230; <a href="http://programthis.net/custom-terminal-commands/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<div class="skills">HOWTO: Create your own terminal commands</div>
<p><center><br />
<h1><a href="#linux-cmds-tldr">TL;DR</a></h1>
<p></center></p>
<p>Linux and Mac users, I figure it's about time I address something many of you have been emailing me about. When you write code in an interpreted language (such as Python, Ruby, etc.), most guides will explain to you that in order to run your code, you use the follow syntax.</p>
<pre>
$ python myscript.py
blah!
$
</pre>
<p>Now, that's nice, but what if we want to run code that we want with only one phrase? For instance, wouldn't it be nice if we could type this instead?</p>
<pre>
$ sayblah
blah!
$
</pre>
<p>There are several ways to achieve this result, and I'm going to cover 2 of them - one is a hack and the other is an elegant solution.</p>
<h1>The Hack</h1>
<p>In the Linux universe, there exist these things called <b><i>aliases</i></b>. Aliases allow you to rename certain commands, and make them easier to type. For instance, on my Mac, accessing mysql CLI requires the following input</p>
<pre>
$ /usr/local/mysql/bin/mysql
</pre>
<p>I don't know about you, but that's really stupid. Instead, I much prefer to type 'mysql'. So I use an alias! Consider the following syntax.</p>
<pre>
alias mysql="/usr/local/mysql/bin/mysql"
</pre>
<p>Pretty straight-forward, this tells your shell that when you type `mysql`, you actually meant to type `/usr/local/mysql/bin/mysql`. So where do we put these aliases? Well, you can go ahead and type the above command in your terminal, but unfortunately that will only stay as long as your shell stays up (meaning if you log out, restart, etc, it will be gone). To combat this, we place the following line in ~/.bashrc (where ~ denotes your home directory) <b><u>For Mac users: use ~/.bash_profile instead</u></b>. When you're done adding your aliases, remember to type `source ~/.bashrc` (or `source ~/.bash_profile`) for immediate results. <b><u>EDIT: Please note that it's now preferred to place aliases in a separate file, ~/.bash_aliases</u></b></p>
<p>So, a hacky solution to our original problem is thus..</p>
<pre>
== IN ~/.bash_profile ==
alias sayblah="python ~/code/python/myscript.py"
</pre>
<p>It works, I guess. But let's try it another way.</p>
<h1>A Better Solution</h1>
<p>As an introduction, let's talk about $PATH. The PATH environment variable is a string stored in your shell that contains various folders that your shell will look in when you type a command. Let's take a peek at my $PATH</p>
<pre>
initech-2:binary jordan$ echo $PATH
/Users/jordan/.rvm/gems/ruby-1.9.2-p290/bin:/Users/jordan/.rvm/gems/ruby-1.9.2-p290@global/bin:
/Users/jordan/.rvm/rubies/ruby-1.9.2-p290/bin:/Users/jordan/.rvm/bin:/usr/bin:/bin:/usr/sbin:/sbin:
/usr/local/bin:/usr/X11/bin:/usr/texbin:/home/jordan/utils
initech-2:binary jordan$
</pre>
<p>As you can see, my PATH contains a list of directories (many of which attributed to the RVM and RubyGems) separated by colons. See that last one? `/Users/jordan/utils`. That implies that if the command I type does not match any files in the previous directories, it will look in /Users/jordan/utils. That's where I store my personal scripts. <b><i>(Keep in mind I use Mac OS, so my home directory is located in /Users/jordan/. On Ubuntu and other flavors of Linux, this would be /home/jordan/)</i></b></p>
<p>To add to your PATH variable, use the `export` command as follows (hint: place this in ~/.bashrc or ~/.bash_profile)</p>
<pre>
export PATH=$PATH:/Users/jordan/utils
</pre>
<p>Right, so now we see how your operating system interprets most commands, but how can I make my scripts executable without the need of typing python? (or ruby, perl, etc). Simple, let's make use of the Shebang (#!). Let's look at the source code for `myscript.py`</p>
<pre>
print 'blah!'
</pre>
<p>Now, in order to take this text and interpret it as python code, we use the python interpreter. As far as most people are concerned, we do this by typing `python` followed by the script name. Now, can we get python to run our code without explicitly calling `python` ? Of course we can. First, let's add the python shebang (the location of python) to our script</p>
<pre>
#!/usr/bin/python

print 'blah!'
</pre>
<p>Awesome, now we should be able to run it without using `python`. Let's try it out.</p>
<pre>
initech-2:utils jordan$ ./myscript.py
-bash: ./myscript.py: Permission denied
initech-2:utils jordan$
</pre>
<p>Uh oh, what happened? The problem here is that `myscript.py` does not have executable permission. Essentially, we can't just run it. We can change this, however, with a simple call to `chmod`. We'll use +x to ADD executable permission.</p>
<pre>
initech-2:utils jordan$ chmod +x myscript.py
initech-2:utils jordan$ ./myscript.py
blah!
initech-2:utils jordan$
</pre>
<p>Great! Now we can execute our python script. Pretty damn useful. But what if we wanted to apply what we learned before about the $PATH variable? To start, let's get rid of that awful .py extension. It won't look good on our command. </p>
<pre>
initech-2:utils jordan$ mv myscript.py sayblah
initech-2:utils jordan$
</pre>
<p>Now, since ~/utils/ is in our PATH, we should be able to go somewhere else, call sayblah, and execute our pure python code.</p>
<pre>
initech-2:utils jordan$ cd ~
initech-2:~ jordan$ sayblah
blah!
initech-2:~ jordan$
</pre>
<p>It works. </p>
<p>So that concludes our tutorial on essentially "making your own Linux commands." I encourage all of you to look into chmod, shebangs, environment variables, and ~/.bashrc (or ~/.bash_profile). I hope to see you write your own *nix commands in the future - it'll teach you quite a bit.</p>
<p><a id="linux-cmds-tldr"></a></p>
<h1>TL;DR</h1>
<pre class="brush: bash; title: ; notranslate">
initech-2:utils jordan$ python myscript.py
blah!
initech-2:utils jordan$ cat myscript.py
print 'blah!'
initech-2:utils jordan$ vi myscript.py
initech-2:utils jordan$ cat myscript.py
#!/usr/bin/python

print 'blah!'
initech-2:utils jordan$ ./myscript.py
-bash: ./myscript.py: Permission denied
initech-2:utils jordan$ chmod +x myscript.py
initech-2:utils jordan$ ./myscript.py
blah!
initech-2:utils jordan$ mv myscript.py sayblah
initech-2:utils jordan$ ./sayblah
blah!
initech-2:utils jordan$ cd ..
initech-2:~ jordan$ export PATH=$PATH:/Users/jordan/utils
initech-2:~ jordan$ sayblah
blah!
initech-2:~ jordan$
</pre>
<p>Best,<br />
Jordan</p>
]]></content:encoded>
			<wfw:commentRss>http://programthis.net/custom-terminal-commands/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>The Wonderful World of Binary</title>
		<link>http://programthis.net/the-wonderful-world-of-binary/</link>
		<comments>http://programthis.net/the-wonderful-world-of-binary/#comments</comments>
		<pubDate>Wed, 14 Dec 2011 00:28:01 +0000</pubDate>
		<dc:creator>jordan</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://programthis.net/?p=193</guid>
		<description><![CDATA[MISSION: To convert an input string into binary SKILLS: Just a little math Greetings! I'd like to start off by apologizing for my recent absence. For my readers who aren't in college - it's finals time! For my readers who &#8230; <a href="http://programthis.net/the-wonderful-world-of-binary/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<div class="mission">MISSION: To convert an input string into binary</div>
<div class="skills">SKILLS: Just a little math</div>
<p>Greetings!</p>
<p>I'd like to start off by apologizing for my recent absence. For my readers who aren't in college - it's finals time! For my readers who are - what are you doing? Get back to work! My recent days have been spent on finishing up the semester, attending recruiting events, applying for internships, and touching up my resumé. So, sorry about that!</p>
<p>Today's topic is pretty basic, and I'm sure a lot of you would rather skim right over this - that's totally cool. But today we're going to be looking at binary, or the <b>base two representation of numbers</b>. I'll start off basic, and exponentially continue as to not waste too much time. </p>
<h1>Level 1 - Super Basics!</h1>
<p>Our number system, that's right, the way you count, is based on <b>base 10</b>. For instance, take any number - 526 sounds good. What do those crazy symbols mean? Well, it's quite simple. Each digit corresponds to a multiple of a different power of 10. So 526 is actually represented as</p>
<p><img src="http://programthis.net/wp-content/ql-cache/quicklatex.com-5aaf3e72ffecab28402db1d00d28369f_l3.png" class="ql-img-inline-formula" alt="&#53;&#50;&#54;&#32;&#61;&#32;&#53;&#32;&#42;&#32;&#49;&#48;&#94;&#50;&#32;&#43;&#32;&#50;&#32;&#42;&#32;&#49;&#48;&#94;&#49;&#32;&#43;&#32;&#54;&#32;&#42;&#32;&#49;&#48;&#94;&#48;" title="Rendered by QuickLaTeX.com" style="vertical-align: -1px;"/></p>
<p>See those exponents of 10? Those correspond to our digits. 6 is our <i>0th digit</i> (remember, computers start at 0), and so on. Alright, I'm pretty sure ALL of you understand this.</p>
<h1>Level 6 - Converting Bases</h1>
<p>So yeah, base 10. That makes sense. But how about something like base 3? In our first case, we split the number up into multiples of exponents of 10. So let's do the same thing, only with exponents of 3. Let's take 41.</p>
<p><img src="http://programthis.net/wp-content/ql-cache/quicklatex.com-a610a05d9eb45ef811b72f960db69d7c_l3.png" class="ql-img-inline-formula" alt="&#52;&#49;&#32;&#61;&#32;&#49;&#32;&#42;&#32;&#51;&#94;&#51;&#32;&#43;&#32;&#49;&#32;&#42;&#32;&#51;&#94;&#50;&#32;&#43;&#32;&#49;&#32;&#42;&#32;&#51;&#94;&#49;&#32;&#43;&#32;&#50;&#32;&#42;&#32;&#51;&#94;&#48;" title="Rendered by QuickLaTeX.com" style="vertical-align: -1px;"/></p>
<p>Therefore, our representation of 41 in base 3 is 1112<sub>3</sub> (that little 3 tells our what base we're in - just notation).</p>
<h1>Level 36 - Using Binary to Pick Up Women</h1>
<p>Oops, I think I went a little too fast - that's really all you need to know to get started. I hope I didn't waste too much of your time. </p>
<p>Alright so what's your task today? Take an input string from a user, and display each character in 8-bit base two representation. If you do this manually, it's kind of annoying - so we're going to ask our fancy little computers to do it for us. To those of you confused on how to convert a letter to binary, think - how do we usually represent letters as numbers? If you remember our experiments with <a href="/vigenere-cipher/">ciphers</a>, then you should know we'll typically use the <b>ASCII value</b> of a particular character. That's what we'll be converting.</p>
<p>Now, why are we doing this? Typically, I'd ask you to write me a change-of-base calculator, but, to be honest, that's pretty damn boring. Instead, we're going to use this code later in the future - particularly for hashing.</p>
<p>Keep in mind, a lot of your fancy little programming languages will do this sort of thing for you. But, that's not the point. The point is to learn, and understand how these computations are done. If that bores you, I'm sorry. Let's move on to some output. </p>
<pre>
initech-2:binary jordan$ echo hello world | ./binary.py
01101000 01100101 01101100 01101100 01101111 00100000 01110111 01101111 01110010 01101100 01100100
initech-2:binary jordan$ echo binary | ./binary.py
01100010 01101001 01101110 01100001 01110010 01111001
initech-2:binary jordan$ echo 101010 | ./binary.py
00110001 00110000 00110001 00110000 00110001 00110000
initech-2:binary jordan$
</pre>
<p>One thing you must keep in mind when writing this program, I asked for the <b>8-bit representation</b>. As you'll see, a lot of these sequences start with 0s. They have to. Because each portion HAS to be 8 binary digits. Just a little something to chew on. </p>
<p>Enjoy,<br />
Jordan</p>
]]></content:encoded>
			<wfw:commentRss>http://programthis.net/the-wonderful-world-of-binary/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Working with Time (Part I)</title>
		<link>http://programthis.net/working-with-time-part-i/</link>
		<comments>http://programthis.net/working-with-time-part-i/#comments</comments>
		<pubDate>Sun, 04 Dec 2011 06:29:35 +0000</pubDate>
		<dc:creator>jordan</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://programthis.net/?p=184</guid>
		<description><![CDATA[MISSION: Given a month, day, and year, display the day of the week SKILLS: Knowledge of the Gregorian Calendar, Modular Arithmetic Greetings fellow coders, I apologize for my recent absence - I've been spending my days working on school projects &#8230; <a href="http://programthis.net/working-with-time-part-i/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<div class="mission">MISSION: Given a month, day, and year, display the day of the week</div>
<div class="skills">SKILLS: Knowledge of the Gregorian Calendar, Modular Arithmetic</div>
<p>Greetings fellow coders, I apologize for my recent absence - I've been spending my days working on school projects and studying, so time to write up challenges hasn't been readily available. Today, however, I've decided to take a break from my business/laziness and offer up a nice little challenge. </p>
<p>I'll be upfront with you - this was a challenge I worked on to test my programming skills back when I first started programming. It's not very difficult, but it really gets you thinking. It's also nice to see how your computer does these things automatically. </p>
<p>So today we'll be working on accepting a date (a month, day, and year) and outputting what day of the week it was (Monday, Tuesday, etc.) on that particular day. For beginners, it may seem daunting - and you might think this is only solvable with some pretty gross code. But have faith in yourselves, my friends - the solution is short, elegant, and makes sense! Let's break it down.</p>
<p>So the general approach I've taken is a) finding out how many days it has been since <b><u><i>January 1st, 1 AD</i></u></b> (which was a Monday) and b) performing some modular arithmetic on that number to tell me what day of the week it is. That's really all it is - but before you can continue, let's make sure you have a good enough understand on how our calendar works exactly. </p>
<p>There are 12 months in our calendar (January, February -- wait, I think I'm going a bit too slow here. Let's refer to an olde English rhyme.</p>
<pre>
Thirty days hath September,
April, June, and November;
All the rest have thirty-one,
Save February, with twenty-eight days clear,
And twenty-nine each leap year.
</pre>
<p>Pretty straight-forward, huh? Well, except that last part about the <i><b>leap year</b></i>. To correct for the fact that the Earth does not revolve around the sun exactly once every 365 days, every four years we add 1 day to February. The problem is, we don't do this <i>every year</i>...</p>
<p><b>Instead, a leap year occurs once every four years, EXCEPT years divisible by 100, BUT INCLUDING, years divisible by 400.</b></p>
<p>So the year 2008 had a leap year, as did 2000 (2000 is divisible by 400), but the year 1900 did not. Figure out a way to logically include this in your program. Hint: Don't you dare go through each year manually! Now let's check out some input.</p>
<pre>
new-host:working-with-time jordan$ ./time.py
Enter a month (1-12): 1
Enter a day (1-31): 1
Enter a year (> 0): 1
January 1, 1 is a Monday
new-host:working-with-time jordan$ ./time.py
Enter a month (1-12): 7
Enter a day (1-31): 20
Enter a year (> 0): 1992
July 20, 1992 is a Monday
new-host:working-with-time jordan$ ./time.py
Enter a month (1-12): 12
Enter a day (1-31): 4
Enter a year (> 0): 2011
December 4, 2011 is a Sunday
new-host:working-with-time jordan$
</pre>
<p>Remember, the point here is to <b><u>not</u></b> use your language's date/time libraries. Do it from scratch! So best of luck to you all! And we'll be working with time again very soon - when I get some. </p>
<p>Best,<br />
Jordan</p>
]]></content:encoded>
			<wfw:commentRss>http://programthis.net/working-with-time-part-i/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Playing with Polynomials</title>
		<link>http://programthis.net/playing-with-polynomials/</link>
		<comments>http://programthis.net/playing-with-polynomials/#comments</comments>
		<pubDate>Sat, 19 Nov 2011 18:47:18 +0000</pubDate>
		<dc:creator>jordan</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://programthis.net/?p=174</guid>
		<description><![CDATA[MISSION: To take a series of numbers and evaluate a polynomial SKILLS: Algebra I've got an interesting lesson in numbers for you math enthusiasts out there (did I mention I adore you?). Today we're going to be forming polynomials from &#8230; <a href="http://programthis.net/playing-with-polynomials/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<div class="mission">MISSION: To take a series of numbers and evaluate a polynomial</div>
<div class="skills">SKILLS: Algebra</div>
<p>I've got an interesting lesson in numbers for you math enthusiasts out there (did I mention I adore you?). Today we're going to be forming polynomials from command line arguments and evaluating them. We'll go over some interesting properties too. </p>
<p>So what am I talking about? Let's say we want to end up running our code as follows. (Written in C for our case, but main can also have any extension you'd like)</p>
<pre>
./main 2 4 6 8 1
</pre>
<p>For the extreme beginners, don't worry if you're unfamiliar with the concept of command line arguments. They're quite simple and we'll go over them another time. For now, feel free to (as always) finish the challenge however you'd like. </p>
<p>Okay so from these numbers, I want you to form the following polynomials.</p>
<p><img src="http://programthis.net/wp-content/ql-cache/quicklatex.com-0815d2582fedf6947404594b42073794_l3.png" class="ql-img-inline-formula" alt=" &#102;&#40;&#120;&#41;&#32;&#61;&#32;&#50;&#120;&#94;&#51;&#32;&#43;&#32;&#52;&#120;&#94;&#50;&#32;&#43;&#32;&#54;&#120;&#32;&#43;&#32;&#56; " title="Rendered by QuickLaTeX.com" style="vertical-align: -4px;"/></p>
<p>As you can see, we took the <i>nth degree</i> <b>polynomial</b> from our arguments (excluding the last one - you'll understand why). In case it's been a while since high school math, or if you're simply unfamiliar, a polynomial is formed by multiplying various <i>exponents</i> of x (think x<sup>3</sup>, x<sup>2</sup>, x<sup>b</sup> where x is our variable and the little number is our exponent) by coefficients. It's a pretty general term. So let's say we're given the numbers [1, 3, 3, 7]. We would then construct the following polynomial.</p>
<p><img src="http://programthis.net/wp-content/ql-cache/quicklatex.com-5b080cc365e5d16bb27376fd50c564a4_l3.png" class="ql-img-inline-formula" alt=" &#102;&#40;&#120;&#41;&#32;&#61;&#32;&#120;&#94;&#51;&#32;&#43;&#32;&#51;&#120;&#94;&#50;&#32;&#43;&#32;&#51;&#120;&#32;&#43;&#32;&#55; " title="Rendered by QuickLaTeX.com" style="vertical-align: -4px;"/></p>
<p>We take each number and multiply it by a decreasing exponent of x. Our last number (in this case, 7) will be multiplied by x<sup>0</sup>, or simply 1. Some more examples...</p>
<pre>
[9, 9] => 9x + 9
[1, 1, 1, 1, 1] => x^4 + x^3 + x^2 + x + 1
[1, -1, 1] => x^2 - x + 1
</pre>
<p>Now, from our command line example above (./main 2 4 6 8 1) we ignored the 1. Why? That's the value we want to be plugging into our shiny new equation. Our program will then return the result. Maybe we should go over another test case. </p>
<p class="ql-center-displayed-equation" style="line-height: 128px;"><span class="ql-right-eqno"> &nbsp; </span><span class="ql-left-eqno"> &nbsp; </span><img src="http://programthis.net/wp-content/ql-cache/quicklatex.com-5942d89eae5d4f9525dd037fab77efda_l3.png"class="ql-img-displayed-equation" alt=" &#92;&#98;&#101;&#103;&#105;&#110;&#123;&#97;&#108;&#105;&#103;&#110;&#42;&#125; &#105;&#110;&#112;&#117;&#116;&#32;&#38;&#61;&#32;&#52;&#44;&#32;&#52;&#44;&#32;&#53;&#44;&#32;&#54;&#44;&#32;&#51;&#32;&#92;&#92; &#102;&#40;&#120;&#41;&#32;&#38;&#61;&#32;&#52;&#120;&#94;&#51;&#32;&#43;&#32;&#52;&#120;&#94;&#50;&#32;&#43;&#32;&#53;&#120;&#32;&#43;&#32;&#54;&#32;&#92;&#92; &#102;&#40;&#51;&#41;&#32;&#38;&#61;&#32;&#52;&#40;&#51;&#94;&#51;&#41;&#32;&#43;&#32;&#52;&#40;&#51;&#94;&#50;&#41;&#32;&#43;&#32;&#53;&#40;&#51;&#41;&#32;&#43;&#32;&#54;&#32;&#92;&#92; &#102;&#40;&#51;&#41;&#32;&#38;&#61;&#32;&#49;&#48;&#56;&#32;&#43;&#32;&#51;&#54;&#32;&#43;&#32;&#49;&#53;&#32;&#43;&#32;&#54;&#32;&#92;&#92; &#102;&#40;&#51;&#41;&#32;&#38;&#61;&#32;&#49;&#54;&#53;&#32;&#92;&#92; &#92;&#101;&#110;&#100;&#123;&#97;&#108;&#105;&#103;&#110;&#42;&#125; " title="Rendered by QuickLaTeX.com"/></p>
<p>In math terms, we can represent (./main a<sub>1</sub> a<sub>2</sub> a<sub>3</sub> ... a<sub>n</sub> x) as</p>
<p><img src="http://programthis.net/wp-content/ql-cache/quicklatex.com-ee13db7506002f111f798702653b4ee6_l3.png" class="ql-img-inline-formula" alt=" &#102;&#40;&#120;&#41;&#32;&#61;&#32;&#97;&#95;&#123;&#49;&#125;&#120;&#94;&#110;&#32;&#43;&#32;&#97;&#95;&#123;&#50;&#125;&#120;&#94;&#123;&#110;&#45;&#49;&#125;&#32;&#43;&#32;&#97;&#95;&#123;&#51;&#125;&#120;&#94;&#123;&#110;&#45;&#50;&#125;&#32;&#43;&#32;&#46;&#46;&#46;&#32;&#43;&#32;&#97;&#95;&#123;&#110;&#125;&#120;&#94;&#48;&#32;&#32; " title="Rendered by QuickLaTeX.com" style="vertical-align: -4px;"/></p>
<p>Make sense? Now all that's left to do is to find an efficient way to do this calculation. Here's some output.</p>
<pre>
initech:polynomial jordan$ ./main 9 1772
9
initech:polynomial jordan$ ./main 1 1 1 1
3
initech:polynomial jordan$ ./main 1 1 1 1 1 2
31
initech:polynomial jordan$ ./main 4 4 5 6 3
165
initech:polynomial jordan$ ./main 9 9 8
81
initech:polynomial jordan$ ./main 1 3 3 7 10
1337
initech:polynomial jordan$
</pre>
<p>Now, if you're a REAL math geek, you may've noticed an interesting property of our program. Can you find it? Let me know in the comments.</p>
<p>Using our representation of a polynomial, I want you to also consider (and try!) the following.</p>
<ul>
<li>Can you calculate the derivative of our polynomial at our given point? How about the integral? (you'll need an arbitrary constant here)</li>
<li>Can you explain what sort of application a program like this has?</li>
<li>Can you make your algorithm compact? (My minified solution is 116 bytes)</li>
</ul>
<p>Keep on exploring,<br />
Jordan</p>
]]></content:encoded>
			<wfw:commentRss>http://programthis.net/playing-with-polynomials/feed/</wfw:commentRss>
		<slash:comments>15</slash:comments>
		</item>
	</channel>
</rss>

