#!/usr/bin/python
'''
lunar_lander.py
basic lunar lander simulation based on constraints found at:
http://www.cs.carleton.edu/faculty/dmusican/cs111s10/lunarlander.html
'''
import sys
class LunarLander(object):
'Lunar lander object'
def __init__(self):
self.altitude = 10
self.velocity = 4
self.fuel = 2
def getVelocity(self):
return self.velocity
def getAltitude(self):
return self.altitude
def getFuel(self):
return self.fuel
def thrust(self, fuel):
if fuel > self.fuel:
self.velocity -= fuel * 4
self.fuel = 0
self.velocity -= fuel * 4
self.fuel -= fuel
def tick(self, fuel):
self.thrust(fuel)
self.velocity += 2
self.altitude -= self .velocity
#check for landing or crash
if self.altitude <= 0 and self.velocity < 5:
print 'skrrttt~ Houston, we have landed. ~skrrtt~ Victory! \'Murica! Fuck yeah!'
self.restart()
elif self.altitude <= 0 and self.velocity > 4:
print 'Oh no, you crashed!'
self.restart()
def report(self):
print '\nAlt = %d Vel = %d Fuel = %d' % (self.getAltitude(),
self.getVelocity(),
self.getFuel())
def restart(self):
restart = raw_input('Replay? Y/N >')
if restart.lower() == 'y':
main()
sys.exit('game over!')
def main():
print 'Welcome to Lunar Lander!'
lander = LunarLander()
running = True
while running:
lander.report()
x = 0
x = input('input thrust: ')
lander.tick(x)
main()
Showing posts with label python. Show all posts
Showing posts with label python. Show all posts
Tuesday, December 11, 2012
Lunar Lander
Just made a lunar lander game thingy based on some of the constraints found at http://www.cs.carleton.edu/faculty/dmusican/cs111s10/lunarlander.html. /shruggies, took about 10 minutes.
Wednesday, October 24, 2012
Progress. Whoa..
It's been a bit of a hiatus, but I've been at it the whole time. I'm finally starting to feel comfortable writing code. That sounds a bit silly when I say it out loud; I've been reading and writing code on a daily basis for several weeks now. And yet, I never felt a sense of mastery over the concept I was trying to model, or the solution I was trying to implement. I've been unable to craft 100% exactly what I want into a set of precise instructions, though I've caught a glimpse of what that feels like here and there.
So there's this set of math-oriented programming challenges on the interwebs, called Project Euler. I'm (slowly) completing them, one-by-one. A maths wizard I am not, by any means, and I constantly feel like I'm in over my head when working some of these problems. They get progressively harder in succession, and with every one of these I knock down, inevitable fist pumping ensues.
Here's problem #5:
Anyway, I haven't yet reached a solution for this one, but I'm close. Thus far, I've been able to write a small program to tackle the smaller problem: producing the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder. This gives me the ability to test a small-scale solution before I blow it up for the actual problem (all digits 1-20).
In the first part, I'm trying to determine a starting number to begin testing for divisibility. I ended up multiplying all the numbers in the range of divisors and counting down from there. Why? I wanted to begin with a number that I knew already had to be divisible by all the members of the divisor set.... plus that was just the first method I could think of. This part could really use some work, generating a better guess is essential to having a fast iterative algorithm.
The second loop increments my guess counter, g, and then in a nested loop iterates through each element d in divisors, checking to see if they divide evenly. This is accomplished through the modulus operator (%), which instead of returning the result of a division, returns the remainder only. If a number passes all checks, I then store it in the variable ans, continuing on to see if I can find a smaller number.
I've been able to get it to spit out the number 2520 given a divisor list of the numbers 1 through 10, so I know it works. My current problem is when I try divisors from 11 to 20 my initial guess is too high. IDLE is giving me an error, I think the Python list object is unable to hold a range of numbers 670,442,572,800 digits long, and I wouldn't expect it to. That's quite big.
So I've gotta figure a workaround for that, but confidence abounds! Happy coding.
So there's this set of math-oriented programming challenges on the interwebs, called Project Euler. I'm (slowly) completing them, one-by-one. A maths wizard I am not, by any means, and I constantly feel like I'm in over my head when working some of these problems. They get progressively harder in succession, and with every one of these I knock down, inevitable fist pumping ensues.
Here's problem #5:
2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder. What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?
Now the cool thing about Euler problems is they are open-ended in their implementation. You can use any language, any model, and any algorithm, so long as you reach the correct solution. I really like this method of learning, as opposed to a problem set like pyschools. Pyschools does a lot of hand-holding in helping frame the problem and shape your program. Euler does not care about how you solve the problem in the slightest, and I think being 'stranded' in this way forces you to pull up your bootstraps and design your solution from the ground-up. I think us beginner programmers face an uneasiness once we step out of Tutorial Land and into Outer Problem Space. The only way to overcome this obstacle is to grab the nearest handhold and start climbing.
divisors = [2,3,4,5,6,7,8,9,10]
def euler_5(*divs):
start = divs[0]
for d in range(1, len(divs) + 1):
start *= d
g = 0
for n in range(start, 0, -divs[0]):
g += 1
check = 0
for d in divs:
check += n % d
if check == 0:
ans = n
print ans, g
euler_5(*divisors)
In the first part, I'm trying to determine a starting number to begin testing for divisibility. I ended up multiplying all the numbers in the range of divisors and counting down from there. Why? I wanted to begin with a number that I knew already had to be divisible by all the members of the divisor set.... plus that was just the first method I could think of. This part could really use some work, generating a better guess is essential to having a fast iterative algorithm.
The second loop increments my guess counter, g, and then in a nested loop iterates through each element d in divisors, checking to see if they divide evenly. This is accomplished through the modulus operator (%), which instead of returning the result of a division, returns the remainder only. If a number passes all checks, I then store it in the variable ans, continuing on to see if I can find a smaller number.
I've been able to get it to spit out the number 2520 given a divisor list of the numbers 1 through 10, so I know it works. My current problem is when I try divisors from 11 to 20 my initial guess is too high. IDLE is giving me an error, I think the Python list object is unable to hold a range of numbers 670,442,572,800 digits long, and I wouldn't expect it to. That's quite big.
So I've gotta figure a workaround for that, but confidence abounds! Happy coding.
Tuesday, September 25, 2012
I am science dog.
I'm taking the cs253 web development course on udacity. Coincidentally, the lectures are being delivered by Steve Huffman, one of the founders of reddit! He's really flying through the content, I'm struggling to keep up with a lot of the discussion. Steve tends to glaze over details that I get hung up on -- it's like following a kid through a narrow corridor. He keeps pausing, beckoning at you to follow, but the space gets ever narrower. Case in point, google app engine's request handlers, lambda functions, template variables, and more. My brain gets snagged on these and I have to take the time to research each one of them in turn, making sure I understand just to keep up with the exploding lexicon.
![]() |
| I am Science Dog. |
As discouraging as my pace has been, it's been motivating to hear from one of the people who shaped the internet into what it is today. One thing I really appreciate is Steve uses examples of things he learned while building his many web applications as lessons of what to do and what not to do. And I'm sure that at one point, he was just as clueless as I am. I hope.
Friday, September 21, 2012
Toes in the Water!
Here's my first progress update.
These concepts are rumbling around in my head like sneakers in a dryer:
I've been following the CS101 - Introduction to Computer Science course on udacity.com. I've just started Unit 6. Thus far, we've learned so much about many new concepts that I haven't encountered in any of the "Intro to Language X" tutorials out there. I've come to the realization that just getting a program to perform the desired function is really only the tip of the iceberg. Writing efficient, scalable, optimized, and understandable code is really going to be the hallmark of a pro vs. someone who's still fiddling with the basics.
These concepts are rumbling around in my head like sneakers in a dryer:
- Architecture of data structures
- Indexes & Hash Tables
- Fibonacci Sequences
- Recursion
We spent most of Unit 5 developing our own hash table from scratch, with create, update, and lookup functions based on a "bucket" system. The idea behind it was to return a hash value that could be used to narrow down an index search. When we finally got the thing up and running, the python data type dict was introduced and I'm sure I wasn't the only person having a "oh c'mon... I could have used this from the get-go" moment.
After implementing the dict in my index instead of a nested list, I really appreciated how fast it was to retrieve a value given a key.... and the fact that we built our own version of a dictionary is what allowed me to appreciate that. I'm not completely sure what kind of algorithm Python is using to make searching easier, I just know there is one. +1 Understanding!
Subscribe to:
Posts (Atom)
