Fun with Python: an Input Game

I know, I know. It’s not Mathematica, but please bear with me.

Python vs. Mathematica

For starters, it wouldn’t be fair for me to compare them for several reasons:

  1. I have years of experience with Mathematica vs. the few weeks I’ve been working with Python.
  2. The two languages were created for very different purposes.

I’ll try anyway, if only for fun.

It’s interesting to see how different it is from Mathematica. For example, whitespace characters (i.e. tabs) actually have a function. Unlike Mathematica, I can’t haphazardly put enters wherever I want, nor is it automatic! I also need to keep my code well-organized because it takes so many more lines to write what could be one function in Mathematica, and Canopy (my Python interpreter) lacks the organization and aesthetic beauty of notebooks.

pythoninputs1.png
Compare and contrast.

At the same time, Python seems to be much faster than Mathematica at running code, even though both are higher level languages; they probably wouldn’t compare with something like C in terms of speed. And although Mathematica is more intuitive with pulling off calculations and visualizing data, Python has much greater flexibility and readability for other kinds of tasks. There probably many more things Python can do that I’m not aware of. Otherwise, why would it be so popular?

Defining Essential Functions

Many of Python’s more useful functions come in modules. We should all thank the generous individuals who have created these functions for us. We will use the sys module for the function sys.exit().

import sys

This program relies on counting the number of characters in each input and determining whether they are odd or even and, in the case of the user’s name, whether it is prime. We could use len() by itself, but the problem is that it does not subtract spaces. We can define a simple function named character() just in case someone uses their full name.

def character(word):
    return len(word) - word.count(' ')

The next step is to define a function that can determine whether a number is prime. Technically, we could use an existing library of prime numbers, which would be faster. Instead of importing a library, how about we make something that can check if a number is prime on the go? Let’s make a series of tests:

  1. Is the number (we’ll call it “n”) less than 2? If so, it is not prime. This automatically excludes 0 and 1. Negative numbers cannot be prime, either, but that does not matter because we cannot have a string of negative length.
  2. If n is greater than 2, is it divisible every number between 2 and itself? A prime number is defined as something that can only be divided by 1 and itself. We’ll use a for loop to test every number. Remember that range() does not include the second parameter/final number.
  3. If n passes the above tests, return True. Otherwise, return False.
def primeQ(n):
    if n < 2:
        return False
    else:
        for i in range(2,n):
            if n % i == 0:
               return False
        return True
Creating the Story

Now that everything is in place, we can begin making the actual program. Start by taking the user’s name, birth month, and favorite color. Use raw_input() to turn the input into a string; using input() will try to run the input as a function. For example, entering “Ellen” as the name will store a string for raw_input() but will cause an error for input() if there is no variable called “Ellen.” Use the character() function we defined earlier to store the number of letters in each input as a variable.

name = raw_input('Enter your name: ')
nameLetters = character(name)
month = raw_input('Enter your birth month: ')
monthLetters = character(month)
color = raw_input('Enter your favorite color: ')
colorLetters = character(color)

Then, determine if the number of characters in “name” is prime, odd, or even.Because we’ll need this information later, we’ll create a variable to store a value that shows what kind of number it is.

if primeQ(nameLetters) == True:
    nameValue = 1
elif nameLetters % 2 == 0:
    nameValue = 2
else:
    nameValue = 3

Before we create the story, we’ll have to make sure the user entered the right information.

  1. Ask the user if the information they entered is correct in a way that is readable with rawinput(). Because the user could answer “yes” or “no” in many different ways, ask them to enter “Y” or “N.”
  2. If the answer is “Y” for “yes,” continue to the story.
  3. If the answer is “N” for “no,” end the program. We could use quit() or exit(), but it is better to explicitly import the sys module and use sys.exit(), which functions pretty much the same way. Using quit() or exit() relies on the site module, which might not always be there, unlike sys.
answer = raw_input('This is what you entered:\n'
+ 'name: ' + name + '\nmonth: ' + month + '\ncolor: ' + color
+ '\nIs this correct? Enter Y or N.\n')

if answer == 'Y':
    print('Beginning...\n')
else:
    print('Sorry. Please try again.')
    sys.exit()

The rest of the code is a bunch of if statements that check whether monthLetters() and colorLetters are odd or even. For example, the following code checks whether the remainder of colorLetters and 2 is zero (if it’s even). If so, it prints a certain line of text that incorporates the user’s name. A different line of text is produced if it is odd.

if colorLetters % 2 == 0:
    print(name + ' is removed from school for their concerningly erratic behavior. '
    + name + ' burns down the psychiatric ward and escapes to Europe with a fake identity.')
else:
    print(name + "'s teachers say that they are gifted. " + name + ' becomes a young oil tycoon'
    + ' and becomes the youngest billionaire in the world. Climate change worsens.')

Finally, we can test the code. Here’s a run with my information (the bolded words are my inputs).


Enter your name: Ellen

Enter your birth month: February

Enter your favorite color: purple

This is what you entered:
name: Ellen
month: February
color: purple
Is this correct? Enter Y or N.
Y
Beginning…

Ellen is synthetically created on a cloudy February day.
Ellen is actually a robot. Ellen is mistakenly adopted by their current family.
Ellen is removed from school for their concerningly erratic behavior. Ellen burns down the psychiatric ward and escapes to Europe with a fake identity.
Ellen gets married and has 6 children.
Ellen pokes a button and accidentally kills 6.5 billion people. The world is in a crisis.
Ellen falls in a pool and dies at the age of 72.
Ellen will be greatly missed. The end.


As usual, everything is here, on Pastebin. There’s a collapsed preformatted block of text at the bottom. Look! Now that I’m using Python, I can take full advantage of the preformatted function on WordPress!

"""
This project outputs a story based on the character length of their inputs.
Visit my WordPress at ellenleescience.wordpress.com
"""
import sys
def character(word):
    return len(word) - word.count(' ')
    # subtract the number of empty spaces just in case someone puts their full name
def primeQ(n):
    # we need to find whether the number of letters if prime for later
    # 1 and 0 are not prime, and negatives are impossible
    if n < 2:
        return False
    else:
        # divide n by every number from 2 to n, since a prime can only be divided by 1 itself
        for i in range(2,n):
            # if the remainder is zero for any number, it is not a prime number
            if n % i == 0:
               return False
        return True
        # if n passes every test, it is prime

name = raw_input('Enter your name: ')
nameLetters = character(name)
month = raw_input('Enter your birth month: ')
monthLetters = character(month)
color = raw_input('Enter your favorite color: ')
colorLetters = character(color)

# now we determine if nameLetters is prime for later
if primeQ(nameLetters) == True:
    nameValue = 1
elif nameLetters % 2 == 0:
    nameValue = 2
else:
    nameValue = 3

answer = raw_input('This is what you entered:\n'
+ 'name: ' + name + '\nmonth: ' + month + '\ncolor: ' + color
+ '\nIs this correct? Enter Y or N.\n')

if answer == 'Y':
    print('Beginning...\n')
else:
    print('Sorry. Please try again.')
    sys.exit()

# the rest of the story is determined by nameValue and whether each Letters value is odd or even
if nameValue == 1:
    print(name + ' is synthetically created on a cloudy ' + month + ' day.')
    print(name + ' is actually a robot. ' + name + ' is mistakenly adopted by their current family.')
elif nameValue == 2:
    print(name + ' is born on a stormy ' + month + ' night.')
    print(name + ' is abandoned by their family. They are raised by stray dogs.')
else:
    print(name + ' is born on a sunny ' + month + ' day.')
    print(name + ' lives with a happy family until their parents die in an accident in the lab.')

if colorLetters % 2 == 0:
    print(name + ' is removed from school for their concerningly erratic behavior. '
    + name + ' burns down the psychiatric ward and escapes to Europe with a fake identity.')
else:
    print(name + "'s teachers say that they are gifted. " + name + ' becomes a young oil tycoon'
    + ' and becomes the youngest billionaire in the world. Climate change worsens.')

print(name + ' gets married and has ' + str(colorLetters) + ' children.')
if monthLetters % 2 == 0:
    print(name + ' pokes a button and accidentally kills 6.5 billion people. The world is in a crisis.')
else:
    print(name + ' finds a cure to rabies, but their research is stolen. They never recieve credit.')

if nameValue != 1:
    print(name + ' lives to the ripe age of ' + str(monthLetters*9) + '.')
    print("Cause of death:")
    if colorLetters % 2 == 0:
        print('tripped on a banana peel')
    else:
        print('hit by a meteor')
else:
    print(name + ' falls in a pool and dies at the age of ' + str(monthLetters*9) + '.')

print(name + ' will be greatly missed. The end.')

P.S.: How does the Python logo work?

Sampling Error in Mathematica

Happy Halloween!

In the spirit of Halloween, it seems that my Mathematica stopped functioning for the past week (too spooky for me :O). Manipulate[], arguably the most useful function in the program, refused to work. It got to the point where something as simple as this

Manipulate[ToString[a], {a, 0, 1}]

would not work at all!

Thankfully, we’re back up and running. Off to sampling errors!

Sampling Sound Waves

Although we often like to think that sampling will always yield accurate results, this often is not the case. Consider the images below:

sampling1

The red points represent how often a point from the function is taken. The first example shows that the predicted red function matches the actual wave. However, the next one shows how it seems as if the amplitude of the function is changing. And the last one shows that by taking samples 2Pi increments away from each other, you get a straight line. It should be noted that the sampling itself is a flaw caused by human error; this error is very much different from the fact that there is “infinite precision” for vector graphs.

Such is the danger of interpolating data without sufficient sampling. An interactive version of the above and the code for the images themselves can be found here.

Sampling errors are present in our daily lives as well! The most intuitive example is with sound. Hypothetically, a higher frequency means a higher pitched sound.

(Note: I actually wasn’t aware that you could play sounds in Mathematica until a little less than a year after I started using it. That just goes to show that there are so many unexplored features of this program… I wonder how many people specialize in fields that I’m not even aware of? I feel as if I am pulling so many layers off this onion that the pile of onion shavings is hundreds of times larger than what would seem possible for its size. /rant)

sampling2

And for a while, the sounds actually do get higher pitched. If you look at the wave with a frequency of 20,000, you can see that Mathematica is struggling.

sampling3But if you suddenly increase the frequency to 256,000, the pitch dramatically decreases. So what gives?

sampling4

This is actually a problem concerning hardware. By trying to play this sound, you’re literally forcing your headphones (or speakers) to vibrate so quickly that there is now way for it to keep up. By trying to oscillate quickly, it downgrades the frequency of its pitch to a harmonic of 256,000; if you want to see a really good example of this, set the interactive code’s sampling value at 5.5.

sampling5.png

Everything suddenly seems lower pitched. If there’s a lesson here, I guess it’s that you should always be careful how you sample… You may even end up with a flat line.

sampling6.png
Probably not how this works, but still.

P.S.: Many of the ideas for this post came from my research mentor Dr. James Choi. I am very grateful for his help!

Another Way to Visualize Riemann Sums

Most of the projects I have posted on here have been rather abstract. I want to connect what I do with reality more often (although math is the very essence of the universe).

In mathematics, we often “cheat” by going as close to infinity as possible and pulling out at the last second. This can be seen in integrals, derivatives, and Taylor series. All of these make use of infinitely small or infinitely large values. However, most functions cannot be integrated by hand and most Taylor series cannot be calculated to an infinite degree. As a result, we often rely on computers to calculate these values with brute force, that is, calculating the Riemann sum in the case of the definite integral or finding the Taylor series to a certain degree.

“Real” Riemann Sums

A while back, I made a post (link) regarding Riemann sums. By using Mathematica, I was able to instantly generate hundreds of rectangles using left, right, and midpoint rule. Instead of relying on unrealistically large numbers of rectangles for my Riemann sum, I decided to go with 29 slices on a relatively large interval. I used the function shown below on the interval [1,6.25].

CodeCogsEqn

graph1

 

Instead of doing a 2-dimensional Riemann sum, I did a volume integral so a 3-dimensional model could be physically constructed. The Riemann sum was composed of squares perpendicular to the x-axis. I planned to cut out squares from foam board and glue them together to make a model of the function. The thickness of the foam board was 3/16″, and 1 unit corresponded to 1 inch.

All of the calculations were done in Google Sheets (link to the original document).

CodeCogsEqn (2)

sheet1.png

As you can see, the final Riemann sum, shown as the cumulative volume, was an overestimate, and the error was 3.6406. This number is surprisingly accurate of 27 slices on a large interval. The accuracy of the function can really be seen when the squares (which literally took hours to cut out) are glued together. The width of each slice is represented as f(x) on the table.

20160524_071856

Here are all of the slices laid out.

20160524_073227.jpg

Gluing in process. I really should have used wet glue, but everything worked out.

…And finally, here are two views of the final product! Even with extreme variation in the beginning of the interval, the model turned out fairly accurate. It is now easy to imagine how more complex models can be constructed with high accuracy in more professional settings, such as when a satellite is constructed.

**NOTE: All of the equations were done in a CodeCogsEqn (3) interpreter, because I cannot directly put nice-looking equations in WordPress.

xacto

Popular Videos and the Spread of Different Forms of Media

Lengths of Most Viewed YouTube Videos

Although it is clear that YouTube is becoming an increasingly popular platform for entertainment, how about the most popular videos on YouTube? Here is an official list of the most viewed videos on YouTube. Before I could begin finding and graphing the average length of the videos, I noticed that the top ten were all music videos. As a result, I found this list (also from YouTube) of the top ten most viewed non-music videos. Taking the time from each video and finding the average video length in seconds, I created a bar graph to compare the times.

BarChart[{247.9, 105.44},
 ChartStyle -> "IslandColors",
 ChartLegends -> {"Music Videos", "No Music Videos"},
 PlotLabel -> Style["Average Length (s) of Popular Videos", 15, Black],
 ChartLabels -> Placed[{"247.90", "105.44"}, Above]
]

averageBar

The 247.90 seconds (or 4:08) seems to be the average length of a popular music video. On the other hand, popular non-music videos is 105.44 seconds (or about 1:45). It’s easy to see how much shorter the non-music videos are, which may have to say something about our “short” attention spans. However, a more careful analysis of the video length shows that the average time fluctuates more wildly in non-music videos than in music videos. Consequently, the data is unreliable.

*Note: musTime and noMusTime are lists of the number of seconds in all of the music and non-music videos respectively
ListLinePlot[{musTime, noMusTime},
 PlotLegends -> {"Music Videos", "No Music Videos"},
 AxesLabel -> {"Rank", "Time (s)"},
 Ticks -> {Range[10], Range[0, 300, 50]},
 PlotLabel -> Style["Lengths (s) of Popular Videos", 15, Black]]

lengthLine

The Views

As unexciting as the results from above were, the views themselves were much more interesting. A bar graph is not needed to compare the views because the relationship can easily be seen.

*Note: sMusView and snoMusView are the number of views divided by 10^7 because the original values were too big, which is where the "in ten millions" came from)
ListLinePlot[{sMusView, snoMusView},
 PlotLegends -> {"Music Videos", "No Music Videos"},
 AxesLabel -> {"Rank", "Views (in ten millions)"},
 Ticks -> {Range[10], Range[0, 300, 50]},
 PlotLabel -> Style["Views of Popular Videos", 15, Black]]

viewLine
The number of views non-music videos had were much smaller than the number of views of music videos. No non-music video reached the one billion mark, which every music video had done. In fact, five out of ten of the “non-music” videos were actually focused on music (there was even a lyric video for Adele’s “Set Fire to the Rain”).

Synthesis

What makes music so intriguing? It’s probably not the music, but the ability to rewind songs to the parts we like to listen to the most. When we hear a catchy tune on the radio (say, for example, while we’re driving) it gets stuck in our heads. YouTube and other music platforms such as SoundCloud and Spotify allow us to replay the same song over and over again, while we request the song to be played on the radio. Then the song is broadcasted over and over again, getting it stuck in other people’s heads and giving them a reason to watch the official YouTube video for it.

Conversely, non-music videos can only spread through social media or shown to our friends, limiting them to our social circles. It’s also worth mentioning the viral videos often cater to a specific audience. Music nearly is universal. The popularity of music on YouTube can teach us about the mechanisms by which different types of media spread.

music.png

Time Spent Using Technology for Entertainment

Let’s stray from the topic of weather for a moment, because climate change is too depressing of a reality.

Video Games, Social Media, and Cable TV

Technology is having an ever increasing presence in our lives, from data analysis to watching “funny cat videos” online. Technology is often used for entertainment, namely for playing video, checking one’s social media, and watching television. How do these different forms of media compare? The average American spends 6.3 hours (links are sources) a week, or 54 minutes per day, playing video games. On the other hand, 135.4 minutes is spent on major social networking websites (Facebook, Tumblr, Instagram, Pinterest, and Twitter) and 169 minutes is spent watching television. Here’s a graph comparing the data:

BarChart[{54, 135.4, 169},
 ChartStyle -> "BlueGreenYellow",
 ChartLegends -> {"Video Games", "Social Networking", "TV"},
 PlotLabel -> 
 Style["Daily Average Minutes Americans Spend using Technology for \
Entertainment", 15, Black],
 ChartLabels -> Placed[{"54", "134.5", "169"}, Above]
 ]

tech1

Not surprisingly, the least amount of time is dedicated to video games, which lack the accessibility of something as relatable as social media and of more traditional forms of entertainment, such as cable television.

YouTube

YouTube far outcompetes any cable company, with over 4 billion daily views on the videos uploaded by its users. In fact, roughly 300 hours of video are uploaded each minute to the website, which is equal to a whopping 432,000 hours or about 49 years every day.

49 years. Daily.

To better our understanding of the insane amount of video this is, let’s create a timeline. Suppose the rate of video production, which will inevitably rise in the future, remains constant. Each day on the timeline represents 49 more years of video produced.

timeline1

By the first day, we will have surpassed 25 years, which is how long the average human sleeps in an entire lifetime. Enough video will also have been uploaded for Voyager 1 to watch from its launch 38 years ago to where it is now, in interstellar space.

On day two, 98 years of video will have been created, exceeding the time it takes Halley’s comet to orbit the sun (which is why we only see it every 75 years).

By day five, there will be enough video–more than 122 years–for the oldest human being on record to watch, from their birth to their death. If we were to travel back to the creation of the Declaration of Independence, we would have enough content to watch until the present.

On the tenth day, 590 years of video (of questionable quality) will exist. We would be able to watch shamlessly stolen viral videos, sensationalist anti-government “documentaries”, and top ten videos from the plague epidemic hit the Ming Dynasty until today.

timeline2

Above, I have slightly extended the timeline. Doing this further would be rather superfluous. The insane amount of time involved makes any comprehensible visualization difficult to create.

The vast stretches of time this encompasses become even more difficult to understand when we remember that the rate video production will grow (as it was assumed to stay constant in the timeline). The timeline also failed to consider the myriad of video hosting websites that aren’t YouTube. Someday, there will be enough video to outlast the dinosaurs, the Sun, and even the universe itself.

extinction