Bigfoot search

I have the lottery project for programming practice, but that would require much more statistics knowledge than I have to come up with the algorithm and to interpret the results.

Another I have might not require as much stats knowledge. Possibly some, because it’s looking for correlations, but not nearly as heavy on the numerical analysis. A bigfoot search.

Bigfoot is, of course, a legend of a large hairy primate running around North America. The big problem with that- there’s really no way a large primate could maintain a breeding population large enough to account for the legend, but small enough to escape scientific notice for so long.

But- while it’s rare, humans do occasionally get large and hairy enough to be mistaken for a bigfoot(and even normal sizes might be so identified under some viewing conditions). Humans also occasionally just disappear never to be seen again. It seems likely to me that if any Bigfoot sightings are actually of a large primate, Homo Sapiens is by far the most likely source.

So, my thought is to comb through missing persons data, and bigfoot sightings, and see if there are any good correlations between the sightings and the last known location of the missing person. Focusing mostly on any missing persons with gigantism and/or hypertrichosis. I won’t reject non-mutants from the analysis entirely, if someone

Is the Bigfoot legend based on humans with various mutations that left or were ejected from society? A handful of mutant humans could escape capture or positive ID in the wild indefinitely, and they don’t require a large breeding population that has somehow escaped notice.

Again, like the lottery idea, I’m not expecting much. But it would be useful practice in writing code to do data analysis, and has the added advantage of requiring less additional knowledge to implement it, so I can focus mostly on the programming itself.

Advertisements

Daily Programmer 239 Easy – Game of Threes

So, the latest Daily Programmer project I’m working on(I’ll get to finishing the last one, eventually) is this weeks Easy level project, Game of Threes.

This one wasn’t complicated, here’s the code.


def get_diff_from_multiple(n):
    """ Returns -1, 0, or 1, whichever will add to n to           
    make the sum divisible by 3
    """
    if n % 3 == 2:
        return 1
    elif n % 3 == 1:
        return -1
    elif n % 3 == 0:
        return 0

The core of the program is to either divide by three, or tweak the number to make it divisible by three.  The easiest way I saw was to just check how far it was from a multiple of three, and add the appropriate value.  It was pretty clear that modulus 3 would be the easy way- a remainder of 2, means adding 1 will make the number divisible by 3.  A remainder of 1, just subtract 1.  Remainder of 0, no change needed.

The function to play the game runs a loop until n reaches 1, outputting the appropriate status on each iteration of the loop.


def game_of_threes(n):
    """ Plays the game of threes with starting number n """

    while n > 1:
        diff = get_diff_from_multiple(n)
        print("{} {}".format(n, diff))
        n = (n + diff) // 3

    print(n)

There you go. Simple design for a simple project, though I’m sure there are some cleverer ways to do it.

Python For Loops

So, working on learning Python, I’ll be blogging short bits of code and tutorials to help hone my own understanding- and hopefully someone out there will find it helpful.

So, a for loop is used in Python for iterating over a container, and for other loops where you can easily know ahead of time how many times you need to loop.

In the first case, for iterating over a container, here’s how you do it.

for x in container:
    print(x)</pre>

This loop will print each item in container, one after another, each on their own lines.  Whereas simply using print(x) will print the entire container in one line(wrapped in the REPL).

Now, let’s say you need to do something x number of times.  You could create a container with the necessary values, but Python provides the range() function to handle this.

The simple case is start from zero, go up by 1, last iteration is 1 less than your limit.

for x in range(10):
    print(x)</pre>

This will print the digits 0-9.

Range can do more, of course, giving you a great deal of flexibility with your loop.

#Basic format.  start defaults to zero,
#step size to +1.  end is not included
#in the range, start is.
range(start, end, step_size)

#5-9
for x in range(5,10):
    print(x)

#Step size
for x in range(5,10,3):
    print(x)  # Will print 5 and 8

#You can go backwards!
for x in range(10, 5, -1):
    print(x) #10,9,8,7,6

So, that’s it for Python for loops. Of course, using them effectively can be much more complicated than the basic syntax would make it look, but you can work that out on your own.

bc

So, I just discovered the Unix tool bc.  Given that I’m a Mac guy as much for the Unix as for anything else, this got me interested.

bc is a mathematical programming language and interactive calculator.  The latter is useful, sometimes I need a calculator, and for what I’m doing that instant, a terminal app is most appropriate.  bc fills that role well.

bc also is a C like programming language for mathematical operations.  I haven’t gotten deeply into it, but here’s a factorial function, with a simple recursive implementation.  

/*Factorial function in bc*/

define fact(x)

{

    if( x <= 1){

        return 1

    }else{

        return x * fact(x-1)

    }

}

So. SML lets you redefine basic operators on basic types

For instance, I could do this-

fun op-(x,y) = x+y

This makes the ‘-’ operator do integer addition.  Reals?  Hah, no subtracting them now!  Fails type checking on anything but ints.

I can also do:

fun op-(x) = ~x;

Now, the ‘-’ operator does nothing except spawn error messages wherever it is used.  Well, wherever it is used except the definition of that function.  Which type checks perfectly fine.  Uses of this version of ‘-’ will either fail type checking, throw up syntax errors, or both.  I’m mystified as to why this function definition actually type checks, when it it guaranteed to throw up errors when used.

Redefining how operators behave can be useful, but when it can *only* be an infix operator, why allow me to redefine it to take only one argument?  I discovered this mostly by accident reading library documentation and saying “hey, I wonder if this would work…”, so maybe there’s some additional syntax that can convert it to prefix or postfix?  Even so, why allow the infix version to be defined to take one argument, guaranteeing a noncallable function?

Functional Programming- Currying

Some confusing stuff, but I’m starting to see the potential.  

Currying is a technique that lets you define a function taking multiple arguments as a series of functions that take one argument, and returns a function that takes the rest.  You can define it all the way to the last argument.

This lets you call a funciton with fewer arguments than it takes, and fill in the extras later.  For instance, in SML, assuming powint takes two ints and returns the first int to the power of the second:

val p = powint 2

This makes p a function taking one argument, and giving you back the corresponding power of two.  Oh, you want powers of 10?

val p10 = powint 10

Next call, you have to do 3^5?  You can call it with both arguments right away if you want.

val p3_5 = powint 3 5

I’m still a bit confused on working with this, but the potential is very nice.

Perfect numbers in Python