Tuesday, August 16, 2011

Whoa did you know about googleCL

I am posting this post from the command line and we all know everything is cooler when it is from the command line. I wonder if I can do a google plus post from here as well

Saturday, August 13, 2011

So I have to concede a point about python.

I have heard and seen before several people make the case that in a more "powerful" language design patterns mostly disappear, and that use of design patterns show that the language is lacking. I have seen this pop up from paul graham(http://www.paulgraham.com/icad.html) or I think I saw a video from on of the google go guys putting forth the same idea.

For the most part I always thought that while there might be some truth it is mostly a conceit and favoritism for one's favorite language. Well now is that part where I swallow my pride and say that just isn't true.(Not really surprising most of my ideas eventually turn out wrong. :))

So as mentioned before I am hoping to play with the class at ai-class.com, Well I ordered the book and I was cruising through some of the code examples(python, I am sure lisp is more enlightening but my mind is not ready to be blown that much, plus I don't have too much time to pick up another language just right now) and I saw the neatest bit of code.

It was in some of the chapter 2 examples http://aima-python.googlecode.com/svn/trunk/agents.py

I shrunk down the code and modified to look and play with the feature that I thought was cool. Now keep in mind you crazy pythonistas I am not a long term python user so this is probably run of the mill to you. any way

class Agent ():

    def __init__(self):
        self.alive = True
        self.bump = False

    def make_agent_program (self):
       print ('Unmodified agent')

    def can_grab (self, obj):
        """Returns True if this agent can grab this object.
        Override for appropriate subclasses of Agent and Object."""
        return False
    
def TraceAgent(agent):
    """Wrap the agent's program to print its input and output. This will let
    you see what the agent is doing in the environment."""
    old_program = agent.make_agent_program
    def make_agent_program():
       print ('modifiesds'  )
       old_program()
    agent.make_agent_program = make_agent_program
    return agent

funk = Agent()
funk.make_agent_program() # prints 'Unmodified agent'
TraceAgent(funk)
funk.make_agent_program()  # prints 'modifiesds\n' + 'Unmodified agent'


Ok so it is kinda like a decorator pattern or something similar but you did it without modifying the original at all!!! I mean I have done a couple of similar things in c++ but you either have to do it with inheritance(and create the object as the child object to begin with) or add a visitor method or something to make it extensible. It is much more painful than doing this. All I can say is download python fire it up and type "import antigravity". Python will even let you fly!!! ;)

Saturday, August 6, 2011

Self-Study classes

So for a while now I have had quite the list of MIT OCW http://ocw.mit.edu/index.htm courses that I have been interested in taking. I have the list saved off under google tasks in a someday list, but someday hasn't seemed to come. I have used and made come progress on the signal processing class http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-011-introduction-to-communication-control-and-signal-processing-spring-2010/ but I have never been motivated enough to keep going. It takes quite a bit of self-control and drive to learn to get through a course like that. I have done something similiar a few times in the past. In high school I wanted to take Physics BC my senior year but that required concurrent enrollment in Calc BC and I where I was I would only take Pre-Calc my senior year. So after my freshman year of high school(I had planned ahead quite a bit) I checkout out a geometry book from the school district and studied all summer, in an attempt to test out of geometry and get ahead that year in math.

Well it turns out to test for advancement you have to score a 90 or above on the test( as opposed to credit, which is where you took the class but failed it and are trying not to take it again). Well you might guess but I earned an 89 on that test. :( although that did mean that math class that year was exceptionally easy. It was a good thing as I enjoyed doing the proofs which is where I had lacked the most in learning it myself. Well the next summer I did a correspondance course for Algebra II, and this time I got credit and was able to get into the math cna physics classes I wanted. I also skipped Biology in college, by borrowing a book and studying over a christmas break. When I went to pick up the score I had passed(B-, :( but skipping it let me take one more Chinese class before I graduated), and the lady at the desk had to call someone else to figure out what to do. She had never had someone pass it before, apparently there are many people that try and test out and very few make it.

Anyway the point is I have some idea of the effort needed to learn a subject on your own and it is hard. Well just this week I saw something that really lit a fire for me. I have been thinking recently about learning more about AI(Artificial Intelligence) and NLP(Natural Language Processing) as it combines two on my interests. Computing and linguistics. I love playing with computers every day but I also enjoy learning and speaking Chinese. I haven't been sure how to pursue learning more about these topics until I saw this http://www.ai-class.com/ It is an ai class taught but some of the leaders in the field. Rather than just being an OCW like resource it is an actual class with deadlines and graded assignment. Perfect.

So over the next month or so I will be reviewing my Linear Alg. and Probability. I might even throw in a little bit of lisp or functional programming, and then I will be taking this class. I am hoping to really take advantage of the class and learn the material well, and then I hope to progress in the field and learn more. I think my next post will be some linear Alg. brush up information.

The Polylearner

So I am kinda re-purposing this blog/starting to actually use it. I really love to learn about many different subject but I find that since I am teaching myself it is often hard to retain and really internalize the information. So I am starting the blog so that I can try and take the information I have learned and share with the world. After all the best way to learn is to teach right?

So on that note this blog is more about me and less about you. :) I will try and make the information as clear as possible but my main objective is learning it myself, that also means that it will likely be full of misinformation and flat out wrong things. Sorry.

I may overhaul the blog on a regular basis as I am not sure what the best format to store this/teach information will be. If you(some random person) does find this useful let me know what helps, and what is not helpful and I can try and do more of the former and less of the latter. Oh another thing, I am also trying to improve my writing skills, which are quite poor, so for those grammar/language police out there you are welcome on this blog. Comment and tear things to pieces.

--Danny

Saturday, April 2, 2011

Programming praxis

So I am going to start using this blog as a place to sharpen my programming skills in addition to trying to keep track of my projects. anyway I am using the website http://programmingpraxis.com/ to try and play with python. The problem for today looks rather interesting. http://programmingpraxis.com/2011/04/01/maximum-difference-in-an-array/ and while I haven't actually written any code yet it was an interesting thought exercise. At first I thought of course very simple solution just run through and keep track of the max and min, but of course it is not really that easy. I thought of the case
3236189
When you are iterating along the array and you are at the point of(let () be the current min and [] be the max) and then {} be the item you are looking at)
3(2)3[6]{1}89
At this point you have a 2 as the min and a 6 as the max so your total min-max = 4 and you are looking at the 1 which has the potential to be a better candidate.  However that depends on what comes after, if instead of 89 you had 34 then the 2,6 pair would be the best in the bunch. You need to know(or at least I thought) all of the later items to figure out the current item. So I thought well stink there is no clean solution(even thought the website mentioned that there was) and you are at best O(n^2) but as I thought further I realized that you could easily sort the array O(nln(n)) and then step through the original array and know what the future holds so you could evaluate the pair as you are on that item. That would give you O(nln(n) + n) which is of course O(nln(n) so even if you did a dumb way the did way more work than required you could do better then n^2 so I thought about it some more and I think I found the solution. Just a sec while I go write some code to verify that.

Ok here we go, now keep in mind I am not a python guy so I probably did not do things the python way. Sorry recovering c++ programmer here.
import random

class pair:
 "Storing a pair of values and the magnitude of the difference"
 def __init__(self, minimum, minIndex, maximum, maxIndex):
  self.minimum = minimum
  self.maximum = maximum
  self.minIndex = minIndex
  self.maxIndex = maxIndex

 def magnitude(self):
  return self.maximum - self.minimum

 def updateMax(self, newMaximum, maxIndex):
  if newMaximum > self.maximum:
   self.maximum = newMaximum
   self.maxIndex = maxIndex
 
 def getMin(self):
  return self.minimum

 def getMax(self):
  return self.maximum

 def printResults(self):
  return "The answer is %d at the index of %d with a maximum of %d at the index of %d"  %(self.minimum, self.minIndex,  self.maximum, self.maxIndex)

randList = [] 

for i in range(100):
 randList.append(random.randint(0,100))

current = pair(randList[0], 0, randList[0], 0);
theory = pair(randList[0], 0, randList[0], 0);

for i in range(100):
 current.updateMax(randList[i],i)
 theory.updateMax(randList[i],i)
 if theory.magnitude() > current.magnitude():
  current = theory
 if theory.getMin() > randList[i] :
  theory = pair(randList[i], i, randList[i], i)


print current.printResults()
print theory.printResults()

Programming praxis

So I am going to start using this blog as a place to sharpen my programming skills in addition to trying to keep track of my projects. anyway I am using the website http://programmingpraxis.com/ to try and play with python. The problem for today looks rather interesting. http://programmingpraxis.com/2011/04/01/maximum-difference-in-an-array/ and while I haven't actually written any code yet it was an interesting thought exercise. At first I thought of course very simple solution just run through and keep track of the max and min, but of course it is not really that easy. I thought of the case
3236189
When you are iterating along the array and you are at the point of(let () be the current min and [] be the max) and then {} be the item you are looking at)
3(2)3[6]{1}89
At this point you have a 2 as the min and a 6 as the max so your total min-max = 4 and you are looking at the 1 which has the potential to be a better candidate.  However that depends on what comes after, if instead of 89 you had 34 then the 2,6 pair would be the best in the bunch. You need to know(or at least I thought) all of the later items to figure out the current item. So I thought well stink there is no clean solution(even thought the website mentioned that there was) and you are at best O(n^2) but as I thought further I realized that you could easily sort the array O(nln(n)) and then step through the original array and know what the future holds so you could evaluate the pair as you are on that item. That would give you O(nln(n) + n) which is of course O(nln(n) so even if you did a dumb way the did way more work than required you could do better then n^2 so I thought about it some more and I think I found the solution. Just a sec while I go write some code to verify that.

Ok here we go, now keep in mind I am not a python guy so I probably did not do things the python way. Sorry recovering c++ programmer here.

import random

class pair:
"Storing a pair of values and the magnitude of the difference"
def __init__(self, minimum, minIndex, maximum, maxIndex):
self.minimum = minimum
self.maximum = maximum
self.minIndex = minIndex
self.maxIndex = maxIndex

def magnitude(self):
return self.maximum - self.minimum

def updateMax(self, newMaximum, maxIndex):
if newMaximum > self.maximum:
self.maximum = newMaximum
self.maxIndex = maxIndex

def getMin(self):
return self.minimum

def getMax(self):
return self.maximum

def printResults(self):
return "The answer is %d at the index of %d with a maximum of %d at the index of %d" %(self.minimum, self.minIndex, self.maximum, self.maxIndex)

randList = []

for i in range(100):
randList.append(random.randint(0,100))

current = pair(randList[0], 0, randList[0], 0);
theory = pair(randList[0], 0, randList[0], 0);

for i in range(100):
current.updateMax(randList[i],i)
theory.updateMax(randList[i],i)
if theory.magnitude() > current.magnitude():
current = theory
if theory.getMin() > randList[i] :
theory = pair(randList[i], i, randList[i], i)


print current.printResults()
print theory.printResults()