<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>Python on CyberSpy</title>
    <link>https://cyberspy.io/categories/python/</link>
    <description>Recent content in Python on CyberSpy</description>
    <generator>Hugo</generator>
    <language>en</language>
    <lastBuildDate>Tue, 28 Nov 2017 15:23:02 -0500</lastBuildDate>
    <atom:link href="https://cyberspy.io/categories/python/index.xml" rel="self" type="application/rss+xml" />
    <item>
      <title>Recursion Revisited</title>
      <link>https://cyberspy.io/code/recursion/</link>
      <pubDate>Tue, 28 Nov 2017 15:23:02 -0500</pubDate>
      <guid>https://cyberspy.io/code/recursion/</guid>
      <description>&lt;h3 id=&#34;recursion-from-more-than-one-point-of-view&#34;&gt;Recursion, from more than one point-of-view.&lt;/h3&gt;&#xA;&lt;p&gt;A common programming idiom in computer science is solving a problem by self-reference, also known as recursion. In this post, we look at two different&#xA;implementations of the same problem.&lt;/p&gt;&#xA;&lt;h4 id=&#34;solve-a-recursive-problem-in-two-different-programming-language-paradigms&#34;&gt;Solve a recursive problem in two different programming language paradigms&lt;/h4&gt;&#xA;&lt;p&gt;Let&amp;rsquo;s look at the solution to a simple problem, compute $f(x)=e^{x}$.&lt;/p&gt;&#xA;&lt;p&gt;We will illustrate two separate solutions - one in a procedural language (python), and the other in a functional language (elixir).&lt;/p&gt;&#xA;&lt;p&gt;Let&amp;rsquo;s start off with the functional language. Were does recursion come into play?&lt;/p&gt;&#xA;&lt;p&gt;We define the function $f(x)=e^x$ as the infinite sum,  $\text{ }f(x) = \sum_{n=0}^\infty{\frac{x^n}{n!}}$&lt;/p&gt;&#xA;&lt;p&gt;In our solution below, we define two separate recursive functions, &lt;code&gt;exp/2&lt;/code&gt; and &lt;code&gt;fac/1&lt;/code&gt;. What&amp;rsquo;s interesting to note here is how each of these functions has two separate definitions. This is an aspect of programming in elixir that elegantly uses pattern-matching to return different results depending upon the input. Our two functions nicely dovetail into the base-case and recursive case of a recursive algorithm.&lt;/p&gt;&#xA;&lt;p&gt;For example, looking at &lt;code&gt;exp/2&lt;/code&gt;, the first function definition returns 1 for any value of x (as indicated by the &lt;code&gt;_&lt;/code&gt; preceding the variable) and returns 1. This is the mathematical equivalent of $x^0=1\text{ for any }x$.&lt;/p&gt;&#xA;&lt;p&gt;The second definition of &lt;code&gt;exp/2&lt;/code&gt; is the recursive case. for any value of $n\gt0$. Moreover, we define &lt;code&gt;exp(x, n)&lt;/code&gt; as $\frac{e^x}{n!}$ + &lt;code&gt;exp(x, n-1)&lt;/code&gt;.&lt;/p&gt;&#xA;&lt;p&gt;Similarly for the definition of &lt;code&gt;fac/1&lt;/code&gt; we see two definitions; one for $n=0$ and another for all values of $n\gt0$.&lt;/p&gt;</description>
    </item>
    <item>
      <title>Visualizing the News: Grab your PILlow</title>
      <link>https://cyberspy.io/articles/2017-11-12t213223-0500--visualizing-the-news-grab-your-pillow/</link>
      <pubDate>Sun, 12 Nov 2017 21:32:23 -0500</pubDate>
      <guid>https://cyberspy.io/articles/2017-11-12t213223-0500--visualizing-the-news-grab-your-pillow/</guid>
      <description>&lt;h3 id=&#34;a-pictures-worth-a-gazillion-bits&#34;&gt;A Picture&amp;rsquo;s Worth a Gazillion Bits&lt;/h3&gt;&#xA;&lt;h2 id=&#34;newsapi&#34;&gt;newsapi&lt;/h2&gt;&#xA;&lt;p&gt;This weekend, I tripped over a neat news REST-ful api called &lt;a href=&#34;https://newsapi.org&#34;&gt;newsapi&lt;/a&gt;. Grab an API key and you&amp;rsquo;re off to the&#xA;races.&lt;/p&gt;&#xA;&lt;p&gt;There are tons of live headlines - &lt;strong&gt;News API can provide headlines from 70 worldwide sources.&lt;/strong&gt;&lt;/p&gt;&#xA;&lt;p&gt;There are basically two api endpoints:&lt;/p&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;GET &lt;a href=&#34;https://newsapi.org/v1/articles&#34;&gt;https://newsapi.org/v1/articles&lt;/a&gt;&lt;/li&gt;&#xA;&lt;li&gt;GET &lt;a href=&#34;https://newsapi.org/v1/sources&#34;&gt;https://newsapi.org/v1/sources&lt;/a&gt;&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;p&gt;Register for an &lt;a href=&#34;https://newsapi.org/register&#34;&gt;account&lt;/a&gt; and generate an api-key and let&amp;rsquo;s get started.&lt;/p&gt;&#xA;&lt;h2 id=&#34;accessing-the-api-with-python&#34;&gt;Accessing the API with Python&lt;/h2&gt;&#xA;&lt;p&gt;&lt;strong&gt;newsapi&lt;/strong&gt; can easily be accessed using a browser since the REST-ful method used is a &lt;strong&gt;GET&lt;/strong&gt; method. But, accessing the api&#xA;from the browser is limiting.&lt;/p&gt;</description>
    </item>
    <item>
      <title>Making Heads or Tails out of Recursion and Combinatorial Math</title>
      <link>https://cyberspy.io/articles/2017-11-09t212705-0500--tail-recursion-and-combinatorial-math/</link>
      <pubDate>Thu, 09 Nov 2017 21:27:05 -0500</pubDate>
      <guid>https://cyberspy.io/articles/2017-11-09t212705-0500--tail-recursion-and-combinatorial-math/</guid>
      <description>&lt;h3 id=&#34;gnu--gnu&#34;&gt;GNU — &lt;a href=&#34;https://en.wikipedia.org/wiki/Recursive_acronym&#34;&gt;GNU&amp;rsquo;s Not Unix&lt;/a&gt;&lt;/h3&gt;&#xA;&lt;p&gt;I thought I&amp;rsquo;d take a look at two topics at once and make some fun by mashing them together. Today&amp;rsquo;s topic&#xA;is &lt;a href=&#34;https://en.wikipedia.org/wiki/Recursion&#34;&gt;&lt;strong&gt;recursion&lt;/strong&gt;&lt;/a&gt;. In computer science (and in math, although we call the equation a&#xA;&lt;a href=&#34;https://en.wikipedia.org/wiki/Recurrence_relation&#34;&gt;recurrence relation&lt;/a&gt;) recursion is an often misunderstood concept causing lots of&#xA;panic and anxiety. In reality - it&amp;rsquo;s pretty easy once you wrap you head around the idiom.&lt;/p&gt;&#xA;&lt;h2 id=&#34;what-is-recursion&#34;&gt;What is recursion?&lt;/h2&gt;&#xA;&lt;p&gt;When we define a function in such a way that it calls itself, we&amp;rsquo;ve defined a recusive function. The most basic example is the&#xA;&lt;a href=&#34;https://en.wikipedia.org/wiki/Factorial&#34;&gt;Factorial&lt;/a&gt; function, $f(n)=n!$. We define the function &lt;em&gt;recursively&lt;/em&gt; by defining it&#xA;in reference to itself. So, recursively, we define $f(n)$ as:&lt;/p&gt;</description>
    </item>
    <item>
      <title>Getting Visual With It</title>
      <link>https://cyberspy.io/articles/2017-11-08t102620-0500--getting-visual-with-it/</link>
      <pubDate>Wed, 08 Nov 2017 10:26:20 -0500</pubDate>
      <guid>https://cyberspy.io/articles/2017-11-08t102620-0500--getting-visual-with-it/</guid>
      <description>&lt;h3 id=&#34;time-to-get-vizzy-with-it&#34;&gt;Time to Get Vizzy With It!&lt;/h3&gt;&#xA;&lt;p&gt;Like Will says:&lt;/p&gt;&#xA;&lt;blockquote&gt;&#xA;&lt;p&gt;Gettin vizzy wit it&#xA;Na na na na na na na nana&#xA;Na na na na nana&#xA;Gettin vizzy wit it…&lt;/p&gt;&#xA;&lt;/blockquote&gt;&#xA;&lt;h3 id=&#34;plot-thickens&#34;&gt;Plot thickens&lt;/h3&gt;&#xA;&lt;p&gt;Time to turn on the lights and see what our data objects look like when we turn them&#xA;into visualized plots. Sure, we can use the &lt;code&gt;print&lt;/code&gt; function to see the &lt;em&gt;numbers&lt;/em&gt; within&#xA;our &lt;strong&gt;numpy&lt;/strong&gt; objects. Better yet, let&amp;rsquo;s turn our objects into pretty graphical images. To do so, we turn to&#xA;the &lt;a href=&#34;http://matplotlib.org/contents.html&#34;&gt;&lt;code&gt;matplotlib&lt;/code&gt;&lt;/a&gt; library. An extensive library of plotting routines that easily allows us to render&#xA;image plots into many different formats (png, jpg, pdf, etc&amp;hellip;) - both onto the screen for quick visual inspection, or written&#xA;to a file for general use.&lt;/p&gt;</description>
    </item>
    <item>
      <title>Do the Numpy</title>
      <link>https://cyberspy.io/articles/2017-11-07t153643-0500--do-the-numpy/</link>
      <pubDate>Tue, 07 Nov 2017 15:36:43 -0500</pubDate>
      <guid>https://cyberspy.io/articles/2017-11-07t153643-0500--do-the-numpy/</guid>
      <description>&lt;h3 id=&#34;do-the-numpy-dance-is-your-chance-to-do-the-nump&#34;&gt;Do the Numpy Dance, Is your Chance to do the Nump!&lt;/h3&gt;&#xA;&lt;p&gt;Sir Numpy says:&lt;/p&gt;&#xA;&lt;blockquote&gt;&#xA;&lt;p&gt;The Numpy Dance is your chance to do the nump&#xA;Do the Numpy Nump, come on and do the Numpty Nump&#xA;Do the Numpy Nump, just watch me do the Numpty Nump&#xA;Do ya know what I&amp;rsquo;m doin&amp;rsquo;, doin&amp;rsquo; the Numpty Nump&#xA;Do the Numpy Nump, do the Numpty Nump&lt;/p&gt;&#xA;&lt;/blockquote&gt;&#xA;&lt;h3 id=&#34;start-at-the-beginning-numpy&#34;&gt;Start at the beginning: &lt;a href=&#34;http://www.numpy.org&#34;&gt;&lt;em&gt;&lt;code&gt;NumPy&lt;/code&gt;&lt;/em&gt;&lt;/a&gt;&lt;/h3&gt;&#xA;&lt;p&gt;The first library that we will investigate is &lt;code&gt;numpy&lt;/code&gt;. Simply put, &lt;code&gt;numpy&lt;/code&gt; allows us to represent&#xA;mathematical objects like arrays and matricies of different datatypes and performs operations on those objects thereby&#xA;easing the burden of writing the tedious code within your applications to do the mundane representations and operations.&lt;/p&gt;</description>
    </item>
  </channel>
</rss>
