my name is bishop
I am a python programmer
I am a writer
I practice zen meditation
I am a musician
I am in peak physical health
I am a photographer
I speak fluent russian


and I do not exist.........yet

Morse code translator (take two)

March 26th, 2008 by Robert

PythonThanks to Yoshi for pointing out some redundancies I had in my code. In the original code that I was posting, I was using a “for” loop along with a counter. I was aware that the “for” loop acted as a counter itself, but I wasn’t aware that it actually took on the variable it was counting. This bit of information explains a lot. Thanks again Yoshi.

This is a better version, and less code.

#Python morse code translator

morse_code = {'a':'.-','b':'-...','c':'-.-.','d':'-..','e':'.',
            'f':'..-.','g':'--.','h':'....','i':'..','j':'.---',
            'k':'-.-','l':'.-..','m':'--','n':'-.','o':'---',
            'p':'.--.','q':'--.-','r':'.-.','s':'...','t':'_',
            'u':'..-','v':'...-','w':'.--','x':'-..-','y':'-.--',
            'z':'--..','0':'-----','1':'.----','2':'..---',
            '3':'...--','4':'....-','5':'.....','6':'-.... ',
            '7':'--...','8':'---..','9':'----.',' ':' | '}

#Function to translate string to morse code

def translate(data):
    text = data.lower()
    #guardian code

    for i in text:
        print morse_code[i] + '  ',

to_translate = raw_input("Type in the text that you would like
                              to translate into Morse code. ")

translate(to_translate)

Posted in python | 4 Comments »

Morse code translator

March 21st, 2008 by Robert

PythonI finally got around to whipping this up. It’s crude, but it’s a start. The next thing I need to do is fix the code tags for coldspell. They’re not behaving like good little tags should.

#Python morse code translator

morse_code = {'a':'.-','b':'-...','c':'-.-.','d':'-..','e':'.',
           'f':'..-.','g':'--.','h':'....','i':'..','j':'.---',
           'k':'-.-','l':'.-..','m':'--','n':'-.','o':'---',
           'p':'.--.','q':'--.-','r':'.-.','s':'...','t':'_',
           'u':'..-','v':'...-','w':'.--','x':'-..-','y':'-.--',
           'z':'--..','0':'-----','1':'.----','2':'..---',
           '3':'...--','4':'....-','5':'.....','6':'-.... ',
           '7':'--...','8':'---..','9':'----.',' ':' | '}

#Function to translate string to morse code

def translate(data):
    text = data.lower()
    #guardian code

    count = 0
    for i in text:
        print morse_code[text[count]] + '  ',
        count += 1

to_translate = raw_input("Type in the text that you would like
                             to translate into Morse code.  ")

translate(to_translate)

Posted in python | 4 Comments »

Python programs (someday/maybe)

February 2nd, 2008 by Robert

PythonSince I don’t write software for a living, and since I’m not in the IT business, per se, it can sometimes be hard for me to figure out what kinds of programs I should write. That’s why I’ve been slowly compiling a list of programs that might be interesting and fun to write. These should all be attainable at my current skill level…well, almost. I’m not saying they’ll be pretty, but hopefully functional. Here is what I have so far.

  • A texted based RPG - I love games. This would be a fun little project to tackle. It’s still a bit out of my reach though I think.
  • A small program that contains a dictionary of foods and their equivalent nutritional information (possibly just calories to start). - This will help out when I want to track my nutritional intake.
  • A math game for Cameron - I thought I could make a game to help my 10 year old daughter with her math lessons. Maybe even unlock a code if she does well that will get her some sort of prize. This would be fun to write and beneficial to her.
  • A program that modifies the Zip Lead sheets into lowercase letters. - At work, I get something called a Zip lead on a daily basis. It’s a spreadsheet of all the homes that have sold in our area. I would like to write a program that traverses the text, converts it into a format which I like, and places it in another file to use as mailing address for a later time. This one may also be a little out of my league, but maybe not for long.
  • A time line program. - Something basic at first, but maybe incorporate some flash or Ajax in the future.
  • A Morse code translator. - As I mentioned in my last post, I am reading a book about the rise (and fall) of the telegraph, and of course, Morse code. I thought it would be fun to write a little program that translates text into Morse code (and vice versa).

Those are some of the ideas I have right now. I’ll post more as I think of them.

Posted in python | 4 Comments »

Back on the Horse

January 29th, 2008 by Robert

Things have been looking good. I’ve been going to physical therapy for my hamstring and I can already fell the improvements. It will be some time before I get the go ahead to put my running shoes back on, so in the meantime, I’ve beefed up my weight training and added yoga to my regiment. Yeah, yoga. Let’s talk about yoga for a minute. If you asked a year ago if I would ever consider trying yoga, I would have said “no way”. Here I am one year later all twisted up like a pretzel. And I’ll tell you another thing, I really like it. I honestly can say I feel incredible after a yoga session. Not only that, this particular routine I’m doing focuses heavily on meditation. Multiple positive anyone? Then again, all yoga may focus heavily on meditation for all I know.

My Russian has been improving steadily too. Not only have I been doing my lessons on a regular basis, but I’ve been finding ways to incorporate the language throughout my day. For example, I often answer my co-workers questions in Russian. It only pisses them off a little. I also only count in Russian. I’ll do this a lot when I’m working out, counting reps or tracking time. It’s a great way to keep my mind thinking Russian.

I may be getting a new Camera too. My 10D has had quite a bit of dust on the mirror for the last year and every attempt I’ve made to remedy this problem has failed. This includes spending $75 to send it to Canon directly. Well, I’ve decided to cut my losses and buy the new Rebel XSi (12.2mp) when it comes out in April. It’s about time I upgraded anyways. I’ll keep you posted, but hopefully I’ll start taking significantly more photos after I make the plunge.

I’ve been doing a lot of writing too. Actually, I never really stopped. I write in my journal at least once a week, but more often several times a week. These entries can range anywhere from day to day activities, thoughts and ideas, character development, random lists, just about anything that pops into my mind. It’s oddly liberating to get all your ideas out of you head and into written form. I highly recommend it.

Python and music are next on my list. I plan to do some coding tomorrow morning when I get up. In all fairness, a lot of my time has been spent reading lately. Currently I’m in the middle of two great books; The Victorian Internet and Ben Franklin: An American Life. Both are really fascinating.

I’ve also been watching some of the open courses Yale is offering. Specifically, Professor Kagan’s Philosophy course on death. This is what makes the Internet great, free knowledge. I’m not watching online courses made specifically for the net, but an actual Yale course, taught at Yale, by an actual Yale professor, in front of a real Yale class. Life is good. I even have homework. I have to read Plato’s Phaedo before the next class.

Coincidentally enough, that link to Plato’s Phaedo takes you to M.I.T.’s site, another incredible organization that promotes free knowledge for everyone.

But now I’m just rambling, and I’m hungry, so it’s time to wrap this up.

Posted in health, music, photo, python, russian, writing, zen mind | 4 Comments »

Methods

October 15th, 2007 by Robert

Methods are just like functions, with two differences:

1. Methods are defined inside a class definition in order to make the relationship between the class and the method explicit.
2. The syntax for invoking a method is different from the syntax for calling a function.

I’ve always been confused with the difference between functions and methods. The above quote has helped clarify that for me.

Posted in python | No Comments »

Two versions of Fibonacci

September 11th, 2007 by Robert

Here are two versions of how to write a fibonacci function.

The first is pretty standard:

def fibonacci(n):
if n == 0 or n == 1:
return n
else:
return fibonacci(n-1) + fibonacci(n-2)

This second function uses dictionary hints to store previously computed data. It’s considerably faster than the first function shown.


previous = {0: 0, 1: 1}

def fibonacci(n):
if previous.has_key(n):
return previous[n]
else:
new_value = fibonacci(n-1) + fibonacci(n-2)
previous[n] = new_value
return new_value

I didn’t write these myself, but I wanted to blog them for future reference.

Posted in python | 2 Comments »

Fun with strings and the “for” loop

August 31st, 2007 by Robert

I was asked to take this code:


prefixes = "JKLMNOPQ"
suffix = "ack"

for letter in prefixes:
print letter + suffix

which produced the following output:


Jack
Kack
Lack
Mack
Nack
Oack
Pack
Qack

and rewrite it so that Oack and Qack would actually be Ouack and Quack. Here is what I did:


prefix = 'JKLMNOPQ'
suffix = 'ack'

for letter in prefix:
if letter == 'Q' or letter == 'O':
print letter + 'u' + suffix
else:
print letter + suffix

Here was the result:

Jack
Kack
Lack
Mack
Nack
Ouack
Pack
Quack

Yay! I’m going to revisit it though and see if there is a better way of coding it. For now, I’m just happy it worked.

Posted in python | 2 Comments »

My first functions

August 19th, 2007 by Robert

Here are some of the functions I created for my exercises. They may not be the most efficient way of coding them, but they all seem work.

I tested the isPrime() function with the number 2**70 and it was well over 5 minutes before I got a return result. I’ll have to look in to how to speed that up.

#a simple compare function
def compare(x,y):
if x > y:
return 1
if x < y:
return 0
else:
return -1

#determines if a number is even
def isEven(x):
if x % 2 == 0:
return True
else:
return False

#determines if a number is odd
def isOdd(y):
even = is_even(y)
if even == True:
return False
else:
return True

#determines if f is a factor of n
def isFactor(n,f):
if n%f == 0:
return True
else:
return False

#determines if m is a multiple of n
def isMultiple(n,m):
if m%n == 0:
return True
else:
return False

#finds the common multiple of 2 numbers
def comult(x,y):
if type(x) != int or type(y) != int:
'''Guardian code'''
print "numbers only please"
return
if x == 0 or y == 0:
print "please enter a number greater than zero"
return
'''End guardian code'''
num = 0
remainder = 1
while remainder != 0:
num = num + 1
remainder = (x * num) % y
return (x * num)

#lists power of 2 up to 2**16
def powof2():
x = 2.0
while x < 65536.0:
print x, ' ', '\t', x * 2
x = x * 2.0

#prints multiples of number n up to number high
def print_multiples(n, high):
i = 1
while i < = high:
print n*i, '\t',
i = i + 1
print

#prints a multiplication table up to number high
def print_mult_table(high):
i = 1
while i < = high:
print_multiples(i, i)
i = i + 1

#prints n newlines using the while condition
def n_lines(n):
i = 1
while i < = n:
print
i = i + 1

#determines if n is a prime number
def isPrime(n):
i = n - 1
while i > 1:
rem = n % i
if rem == 0:
return False
else:
i = i - 1
return True

#prints all prime numbers between 1 and x
def morePrime(x):
i = 1
while i < = x:
if isPrime(i) == True:
print i, " is a prime number"
i = i + 1

#prints all prime numbers between x and y
def evenMorePrime(x,y):
if x > y:
x,y = y,x
i = 1
print “Finding all prime numbers between “, x, ” and “, y
print
while x < = y:
if isPrime(x) == True:
print x, " is a prime number"
x = x + 1

Posted in python | 4 Comments »

How to think like a computer scientist

August 14th, 2007 by Robert

I have been reading the tutorial “How to Think Like a Computer Scientist

I highly recommend it.

Posted in python | No Comments »

Why Python?

July 15th, 2007 by Robert

I’ve always been interested in computer programming, scripting, and coding in general. Over the years, I’ve dabbled in C, C++, Basic, and Visual Basic, but none of these really ever captured my attention, and I never really got past the basics of each language. On the Internet side of things, I’m fairly proficient in html/xhtml and CSS, and have a healthy smattering of PHP and JavaScript. I think I have a decent foundation to take the plunge into a full-fledged programming language.

So why did I choose Python? It was an easy decision. Here are some of the factors that helped me make my final choice.

  • Great community
  • Mature language
  • Great syntax
  • Huge library / lots of modules
  • Extremely portable
  • Open Source
  • Support, support, support
  • Google uses it (nice reference)
  • Great for WebDev (which is primarily what I’m going for)

Some of the runner’s up were Ruby, PHP, Java, C++, and Lisp. One of the most important lessons that I took away from my research was this: a programming language is just another tool in your toolbox. The best programmers have a vast array of tools in their toolbox. With that said, there may be a time when I go back and study those other languages. Until then…


print "Hello Python"
Hello Python

Posted in python, why | No Comments »