better living through python

An adventure in programming and recovery.

Stressball

September 09, 2011

I spent one day up in Portland, walking around, enjoying coffee and random treats with a couple of very good friends, and then I came back home.  First thing this morning, two calls regarding bills.  Now I’m horridly stressed.  

What do I do to deal with the stress?  

Reaction #1: Almost take it out on my co-worker.  Luckily I was able to keep it in check, despite it all occurring during a conversation I already have difficulties dealing with.  That is, a conversation where my programming entries are reviewed for grammar, understanding, and readability.  It’s also a conversation that occurs to make sure I actually understand what I’m trying to explain.  That....was hard.

Reaction #2: Use my normal distraction tactic, watch a TV episode to get my mind of things so I can get back to work.  Although it does help me distract myself, it doesn’t always help me get back to work.  It can also leave me feeling less than able to do my job, once finished.  Needless to say, that wasn’t what happened.

Reaction #3: Go on a walk to the bank and deposit my check.  This may sound like the best option, but I know I’ll just start thinking about it more and stressing out about it on the way there.  It’s only a 10 minute walk, but it’s enough time to allow a mini freak-out session on the way back, which will mean only more stress and avoidance.  Instead I biked there, so that I wouldn’t have all the time to think about it.  That worked out better.

Reaction #4: Talk about my stress with my co-worker.  This will probably encourage both of us to continue to be stressed for another couple of hours, as we get over the stress about bills together.  So...not good.

Reaction #5: Bottle it up, because honestly, what other option sounds good right now?  I can either distract myself from the stress, or talk about it.  Both of those are probably not going to help with solving the problem.  However this allows for the negative effect of poor reactions/stressing issues for anything else that happens today.  Not to mention that any distraction tactic is simply going to end up bottling it up in the process.  I always bottle it up for a little bit.  

Where did this all come from?  My dislike of the breathing method to calm ones-self down.  I understand it’s for the prevention of gut-induced reactions that produce job-loss, and to calm yourself down, but it still seems such a simple way to deal with it.  So simple it feels like it never works for me.  Perhaps I have too much anger at this point in my life?  I don’t know...How do you deal with stress other than taking care of the things that stress you out?  Do you have other tricks that help you to recover when you become angry?  

 

Python Lesson

September 08, 2011

 

The next problem in this set uses the same pattern from the previous functions, but in a way you’re reversing the process.  This time you’re to create a function that will take the total amount you saved for your retirement, and determine how much will be left based on your spending over each year also including the different growth rates for each year.  Here is the problem in its full definition:

Write a function, called postRetirement, which takes three arguments: an initial amount

of money in your retirement fund (savings), a list of annual growth percentages on

investments while you are retired (growthRates), and your annual expenses (expenses).

Assume that the increase in the investment account savings is calculated before

subtracting the annual expenditures (as shown in the above table).  Your function should

return a list of fund sizes after each year of retirement, accounting for annual expenses

and the growth of the retirement fund. Like problem 2, the length of the growthRates

argument defines the number of years you plan to be retired.

Note that if the retirement fund balance becomes negative, expenditures should continue

to be subtracted, and the growth rate comes to represent the interest rate on the debt

(i.e. the formulas in the above table still apply).


Here is my end function:

 

def postRetirement(savings,growthRates,expenses):
remainingSavings = [savings,]
for growthRate in growthRates:
remainingvalue = remainingSavings[-1]*(1 + 0.01 * growthRate) - expenses
remainingSavings.append(remainingvalue)
return remainingSavings[1:]

 


As you can see, as like the two previous functions, I begin with defining a new list ‘remainingSavings’, with my savings as the first entry.  Then I use a ‘for’ loop to process the ‘growthRates’ list I was provided.  First step in the ‘for’ loop, assess the ‘remainingvalue’ for the next year.  This is done by taking the last entry from the ‘remainingSavings’ list (which is denoted by the slice of [-1] previous the list name), and multiplying it by the total growth then subtracting the estimated expenses for each year.  The final part is to append each ‘remainingvalue’ to the ‘remainingSavings’ list, and then going through the loop again.  After the loop is finished it then returns a slice of that list (all of it excepting the first position), and it’s done.  Here are the variables and the subsequent result from my function.

 

Variables:
 
print postRetirement(100000,[10,5,0,5,1],30000)
 
Results:

[80000.000000000015, 54000.000000000015, 24000.000000000015,
   # -4799.9999999999854, -34847.999999999985]

Python Lesson

September 07, 2011

 

Continuing on with my retirement simulation here is problem two.
 

Write a function, called nestEggVariable, which takes three arguments: a salary

(salary), a percentage of your salary to save (save), and a list of annual growth

percentages on investments (growthRates). The length of the last argument defines the

number of years you plan to work; growthRates[0] is the growth rate of the first year,

growthRates[1] is the growth rate of the second year, etc.  (Note that because the

retirement fund’s initial value is 0, growthRates[0] is, in fact, irrelevant.)  This function

should return a list, whose values are the size of your retirement account at the end of

each year.

 
Without further ado, here is my function:

def nestEggVariable(salary,save,growthRates):
base = salary*save*.01
savingsRecord = [base,]
for growthRate in growthRates[1:]:
accountvalue = base + (1 + 0.01 * growthRate)*savingsRecord[-1]
savingsRecord.append(accountvalue)
return savingsRecord
 
First off, the main difference between this function and the previous function is that we’re interacting with variable interest rates.  That means that the variable ‘growthRates’ is actually a list, with each entry being a different interest rate percentage.  Because I have a list as a variable, I need to refine and adjust my function a bit.

I begin, as before, by creating my base year definition, defining the base year earnings.  Then I create a new list ‘savingsRecord’ with the first entry being my base year earnings.  This time, instead of using a while loop I use a for loop.  This way I can process through each entry in the list variable that was inputted into the function.  Therefore for each ‘growthRate’ within the ‘growthRates’ list I am to process the following: define an ‘accountvalue’ for each year and append each new ‘accountvalue’ to the ‘savingsRecord’ list.  Once all items in the ‘growthRates’ list have been processed I am to return the list.  When using the following variables I come up with these results:

Variables:

print nestEggVariable(10000,10,[3,4,5,0,3])

Solution:

[1000.0, 2040.0, 3142.0, 4142.0, 5266.26]

 

Python Lesson

September 06, 2011
This problem set is about simulating a retirement fund.  This was particularly helpful, because I learned more about how retirement savings work, especially for someone self-employed.  Due to the Kickstarter funding, we’ve also been working towards the finalization of our business.  This includes business registration, and whatnot else that you’ll read about soon.  To me our ‘venture’ is turning more and more into a reality.  That made this problem set fairly relative.  Here is problem one:

Write a function, called nestEggFixed, which takes four arguments: a salary, a

percentage of your salary to save in an investment account, an annual growth percentage

for the investment account, and a number of years to work. This function should return a

list, whose values are the size of your retirement account at the end of each year, with

the most recent year’s value at the end of the list.


This problem was fairly easy.  I was actually quite surprised with myself in how fast I solved it.  At first I stumped myself a bit, because I could easily understand how it could be solved recursively, however I couldn’t figure out how to write it.  I’m still working on understanding the implementation of recursive functions, despite recognizing when they are more functionally viable.  Here is my end function.

# salary = your starting salary
# save = the percentage of your salary you put into retirement
# growthRate = annual growth percentage of your retirement fund
# fund = your retirement fund

# returns a list with the value of the retirement fund at the end of each year

def nestEggFixed(salary,save,growthRate,years):
countyear = 1
base = salary*save*.01
F = [base,]
while years > countyear:
accountvalue = base + (1 + 0.01 * growthRate)*F[-1]
F.append(accountvalue)
countyear += 1
return F

The hashtags at the beginning denote my code comments (or part of the file that is not actually taken into account when the code is processed).  By reading my comments you will understand the designation of each variable.  

Now to explain the function itself.  I start by creating a counter, called ‘countyear’.  Then I define my ‘base’ investment as my ‘salary’ multiplied by the percentage of my salary I put into retirement, or ‘save’, multiplied by .01 to change the percentage into a working floating number (or a number with decimals).  Then I define ‘F’ as a list, with the base investment being my first entry in the list.  

Now comes the while loop, that processes my information and creates the list.  Here I state that as long as the total ‘years’ I am going to work is less than my ‘countyear’ it should do the following.  My function should create an ‘accountvalue’.  ‘Accountvalue’ is the total value of my retirement account for that particular year.  The equation that defines it includes the base investment, and the subsequent math needed to process it.  What makes this whole thing work is ‘F[-1]’.  Here I am taking a slice of the list F.  In particular I am taking the last piece of that list, and then using it in my equation.  This last item is the last item added to the list.  In this case it’s the base investment.  In subsequent runs through the function, it will be the last appended investment total of the account.    

The final step of the function is specifying to append the results of the ‘accountvalue’ calculation to my list ‘F’.  Then I add a counter to my ‘countyear’ and I process the information again and again until I’ve created a list that is as long as the amount of years until retirement.  So if you have five years until retirement, that list should be 5 items long.  
 
So what results to I obtain when processing this function?

Here are my variables:
 
print nestEggFixed(10000,10,15,5)
 
My results:

[1000.0, 2150.0, 3472.5, 4993.375, 6742.381]

Sweaty feet are not productivity enducing

September 02, 2011

I feel like utter crap today.  I swear I can be doing just fine and dandy, and then one late night and lots of soccer will totally beat it out of me.  I’m sitting in bed today, still getting work done, but I ache and just want to zone and sleep.  However, learning/works needs to get done, there’s no stopping it.  Therefore I sit in the bed with sweaty hands and feet, drinking coffee on and off again water.  A pro tip was suggested earlier.  It was stated that drinking 3 or so glasses of wine and taking a nap might help my problem.  As much as I’d like to agree I know that the wine would just make me pass out, and then I would be grumpy at 4am in the morning, as I would have passed out around 5pm the day before.

When you’re not feeling great and kind of out of it, you find yourself in a ridiculous zone in and of itself.  A ridiculous zone that is more often not productive.  Currently I’ve ‘Help’ by the Beatles, and “Oh, Shiela!” stuck in my head, and my feet...they’re still sweaty.  What is it about quasi insomnia that induces sweaty feet and achy hips?  I sound like I’m pregnant.  Thank some deity that, that is not true!

Just in case you forgot I was a woman...

 

Running with capes

September 01, 2011

Did I mention I love to run?  I forgot how much I enjoy running until I just had a sudden urge to go.  So, I took a short route in 85 degree weather and ended up sweaty and mentally refreshed.  Now I’m back home, ready to get more work done after my shower, and excited for my next project tonight!

When all else fails, do something you think of on the fly.  Only if that involves you doing something that makes you feel like flying.  Like wearing a cape.  Like going for a run with a cape on.  It’s good for you. :)

 

Python Lesson

August 31, 2011
The last part of this problem set, is combining the rest of my functions together to create a final process.  This last process should create a tuple (an immutable list), containing all the starting points where a key shows up, allowing for the possible misspelling (think also mutation), of one letter in the string.  To create my final list, I combined almost all of my previous functions.  
Here is my solution.
First I identify ‘foundat’.  This variable is a list of falses that is the length of the target string I’m interpreting.  Then I identify an empty list as ‘finallist’.  Then I identify key1 as the list created when the function (explained in Problem Set 3, Problem 2), ‘breakup’ is run.  

The ‘breakup’ function creates a list of combined prefixes and suffixes.  Then I run each item of this list through the for loop, by stating ‘for p, s in key1’.  Therefore each prefix (p) and suffix (s) is then passed through the constrainedSubStringMatch function (see Problem Set 3, Problem 1).  Each of these prefix and suffixes create lists via this function, with the identifying locations of where they occur in the target string.  

Then, for the last section I process each of these lists through the final function I recently created (see Problem Set 3, Problem 3).  This takes the lists and creates a list of locations where the prefix plus one space, plus suffix occurs.  In the next line I state that each time an location is found, the list created at the beginning (foundat), is then edited.  For each location found, the false is changed to true.  Then a list is created of all the locations where true occurs in the target string.  Thusly is a function created that returns a tuple of all starting points of matches of the key to the target, such that at exactly one element of the key is incorrectly matched to the target.

Tadaa!!

 

Fake it til you make it....really?

August 29, 2011

When I start to search the Internet I become overwhelmed.  It’s very much like the feeling I get when I start to research.  There is just so much out there.  So much information, so many people telling me they have my answers.  They know how to fix my problem.  So many scam artists, so many bloggers trying to provide you with what you need.

Then there are those trying to provide you with items, or services that you may or may not need.  They’re selling you.  Trying to win your confidence so you’ll purchase what they have.  Accompanying them is their ever-present expression, fake it til you make it.  How does that work?  Do you actually make it?  Or, do you just become proficient at faking it?  With so many people attempting to make it, the Internet feels as if it’s awash with those faking it.  How do you know the difference?  How do you know I have anything to provide you, for that matter?

All I really hope to do is give you insight into who I am, and what my current experiences are like.  Hopefully you can understand my frustrations and excitements with learning to program, so that either as a programmer or an ordinary citizen, you might understand the other more.  This is my expressionistic catharsis.  Emotional recovery on a public scale.  How else is one to connect with the world if you don’t share your story?         

 

A reaction to Software Patenting laws

August 26, 2011

To provide a little break from the python lessons I want to bring to your attention the following This American Life episode, When Patents Attack.  This episode, in particular, brings up a little town about 30 minutes from where I grew up in East Texas.  Marshall seems to have encouraged a certain type leniency towards patent cases.  

If you listen to the entire episode you’ll hear about how so many of the larger tech companies are now gearing up and buying up patents, in order to be able to legally defend themselves.  As a start-up company, this makes me quite nervous.  There is a start-up, only slightly larger than us, who was sued for the simple idea of having a link in their free application, that connected a customer to the paid version of the app.  It seems like such a basic idea, why would someone patent something like that when it’s so difficult to identify where the true innovation occurred?  At least to me it seems that way.

It’s situations like this that intimidate so many younger innovators from pursuing their ideas.  What if I get sued, what would have been the point?  Which brings me back to the ever present stark question in my mind.  Why are there so many blatant money grubbing hands out there?  How can lax patent laws help create and produce jobs?  Based on this episode, it looks like they don’t.  It will be interesting to see the end reaction.         

 

An Oregonian Vacation

August 25, 2011

And we’ve returned!  Robey and I have spent the better part of a week vacationing at Gordon Lakes, a primitive campsite about 40 minutes east on Highway 20 outside of Sweet Home, Oregon.  It was fantastic and relaxing.  We spent a majority of our time there fishing, hiking, and crawfish huntin’.  
 
The campsite itself was a half mile hike in from the parking lot.  This meant we had to lug all our gear in a half mile on a decent hiking trail, to reach the site.  Therefore, we packed as little as possible and (due to Robey-ingenuity) used a hand-truck for anything unwieldy.  For the first half the week my nephew Alex joined us.  We also had some friends come and camp with us for the first half of our respite.  The first day we did a fair amount of hiking and fishing, along with some swimming at High Rock.  High Rock is a delicious chunk of the South Santiam river.  The river is pristine, clear to the bottom.  A great place to swim, but also freakishly cold.  On a 90 something degree day it’s fantastic for cooling off, however anything below that and it’s hardly bearable.  Thus the pictures of my nephew and I doing out best to stay out of the water.  

Halfway through I drove my nephew home, and our friends left, leaving Robey and I alone in the wilderness.  The first night there was much talk of cougars, coyotes and bears.  We didn’t actually see any of the sort during our stay, but we did hear some coyotes on our last night there.  We did drink some boiled lake water, and thus kept up a constant ‘giardia’ joke line anytime anything regarding our intestinal track happened.  Overall most of our trip was fraught with sitting in the sunshine fishing and drinking tea near the campfire.  There was also some occasional book reading and campfire cookin’.  We both incurred a fair amount of sun and dirty clothes, but overall enjoyed a satisfying vacation.  We hope you enjoy our pictures!

 

Links