python on CyberSpy
https://cyberspy.io/categories/python/
Recent content in python on CyberSpyHugo -- gohugo.ioenTue, 28 Nov 2017 15:23:02 -0500Recursion Revisited
https://cyberspy.io/code/recursion/
Tue, 28 Nov 2017 15:23:02 -0500https://cyberspy.io/code/recursion/<h3 id="recursion-from-more-than-one-point-of-view">Recursion, from more than one point-of-view.</h3>
<p>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
implementations of the same problem.</p>
<h4 id="solve-a-recursive-problem-in-two-different-programming-language-paradigms">Solve a recursive problem in two different programming language paradigms</h4>
<p>Let’s look at the solution to a simple problem, compute $f(x)=e^{x}$.</p>
<p>We will illustrate two separate solutions - one in a procedural language (python), and the other in a functional language (elixir).</p>
<p>Let’s start off with the functional language. Were does recursion come into play?</p>
<p>We define the function $f(x)=e^x$ as the infinite sum, $\text{ }f(x) = \sum_{n=0}^\infty{\frac{x^n}{n!}}$</p>
<p>In our solution below, we define two separate recursive functions, <code>exp/2</code> and <code>fac/1</code>. What’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.</p>
<p>For example, looking at <code>exp/2</code>, the first function definition returns 1 for any value of x (as indicated by the <code>_</code> preceding the variable) and returns 1. This is the mathematical equivalent of $x^0=1\text{ for any }x$.</p>
<p>The second definition of <code>exp/2</code> is the recursive case. for any value of $n\gt0$. Moreover, we define <code>exp(x, n)</code> as $\frac{e^x}{n!}$ + <code>exp(x, n-1)</code>.</p>
<p>Similarly for the definition of <code>fac/1</code> we see two definitions; one for $n=0$ and another for all values of $n\gt0$.</p>Visualizing the News: Grab your PILlow
https://cyberspy.io/articles/2017-11-12t213223-0500--visualizing-the-news-grab-your-pillow/
Sun, 12 Nov 2017 21:32:23 -0500https://cyberspy.io/articles/2017-11-12t213223-0500--visualizing-the-news-grab-your-pillow/A Picture’s Worth a Gazillion Bits newsapi This weekend, I tripped over a neat news REST-ful api called newsapi. Grab an API key and you’re off to the races.
There are tons of live headlines - News API can provide headlines from 70 worldwide sources.
There are basically two api endpoints:
GET https://newsapi.org/v1/articles GET https://newsapi.org/v1/sources Register for an account and generate an api-key and let’s get started.
Accessing the API with Python newsapi can easily be accessed using a browser since the REST-ful method used is a GET method.Making Heads or Tails out of Recursion and Combinatorial Math
https://cyberspy.io/articles/2017-11-09t212705-0500--tail-recursion-and-combinatorial-math/
Thu, 09 Nov 2017 21:27:05 -0500https://cyberspy.io/articles/2017-11-09t212705-0500--tail-recursion-and-combinatorial-math/GNU — GNU’s Not Unix I thought I’d take a look at two topics at once and make some fun by mashing them together. Today’s topic is recursion. In computer science (and in math, although we call the equation a recurrence relation) recursion is an often misunderstood concept causing lots of panic and anxiety. In reality - it’s pretty easy once you wrap you head around the idiom.
What is recursion?Getting Visual With It
https://cyberspy.io/articles/2017-11-08t102620-0500--getting-visual-with-it/
Wed, 08 Nov 2017 10:26:20 -0500https://cyberspy.io/articles/2017-11-08t102620-0500--getting-visual-with-it/Time to Get Vizzy With It! Like Will says:
Gettin vizzy wit it Na na na na na na na nana Na na na na nana Gettin vizzy wit it…
Plot thickens Time to turn on the lights and see what our data objects look like when we turn them into visualized plots. Sure, we can use the print function to see the numbers within our numpy objects. Better yet, let’s turn our objects into pretty graphical images.Do the Numpy
https://cyberspy.io/articles/2017-11-07t153643-0500--do-the-numpy/
Tue, 07 Nov 2017 15:36:43 -0500https://cyberspy.io/articles/2017-11-07t153643-0500--do-the-numpy/Do the Numpy Dance, Is your Chance to do the Nump! Sir Numpy says:
The Numpy Dance is your chance to do the nump Do the Numpy Nump, come on and do the Numpty Nump Do the Numpy Nump, just watch me do the Numpty Nump Do ya know what I’m doin’, doin’ the Numpty Nump Do the Numpy Nump, do the Numpty Nump
Start at the beginning: NumPy The first library that we will investigate is numpy.