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()

Thursday, July 16, 2009

Don't you wish it were true

I saw this article http://www.newscientist.com/article/mg20327162.600-apollo-special-welcome-to-lunarville.html?full=true today and wow that would be awesome. I hope that mankind can get it together and get to space quickly. Hopefully all the recent private investment into space travel will got well. good luck to those working on it

Sunday, April 19, 2009

running stuff I found

Here is some new ab stuff
http://www.runnersworld.com/article/0,7120,s6-238-263--13030-3-1X2X3X4-5,00.html

also look up the workout from hell by john long that is awesome for climbing

Well I am starting a new training plan with jenni and we will turn in roaring running climbing beasts

Fun projects that I want to get to

Ham radio transceiver

First make a key
http://www.dxzone.com/cgi-bin/dir/jump2.cgi?ID=20221

finish learning cw with gf40n or aldo

then make a pixie or something similiar

and make an antenna

Read a Jin Yong chinese martials arts novel in chinese
I will need to brush up on some vocab for this one

lead 11a outside
Just get in shape I think I am close