Thursday, June 21, 2018

Python List Exercises

There are hundreds of list exercises in Python.
Here are just a few of them.
I'm just working from web pages I found on the internet.
I intend to add to this over time.

# Print the last element on the list
# In Python, array indexes start with 0
# len[alist] prints the length
mylist = ['a', 'b', 'c', 'd']
print ("last element: ", mylist [len(mylist)-1])
last element:  d

# Find last but one in a list
# [Ummm, this is clearly harder in other languages]
mylist = ['a', 'b', 'c', 'd']
print ("last element: ", mylist [len(mylist)-2])
last element:  c

# Find the ith element of a list where the first one starts at 1
i=7
mylist = ['a', 'b', 'c', 'd','e','f', 'g', 'h', 'i', 'j', 'k']
print(mylist[i-1])
g

# Find the number of elements of a list.
mylist = ['a', 'b', 'c', 'd','e','f', 'g', 'h', 'i', 'j', 'k']
print("Number of elements in list is: ", len(mylist))
Number of elements in list is:  11

# Reverse a list.
mylist = ['a', 'b', 'c', 'd','e','f', 'g', 'h', 'i', 'j', 'k']
# this works in place so you can't print it all on the same line
mylist.reverse()
print (mylist)
['k', 'j', 'i', 'h', 'g', 'f', 'e', 'd', 'c', 'b', 'a']


# Test if a list is a palindrome
# palindrome check - this is more convoluted than some but it helps to see the steps detailed
# Check if a given list is a palindrome
# Check indexes from 0 to the middle of the list
# Check if first index = last index, then move in by one
# Some diagnostics left in as comments

def palcheck(mylist):
 NotAPal = 0
 #print("Middle is: ",int(len(mylist)/2))
 #print ("Length of list: ", len(mylist))
 #print ("Last index: ", len(mylist)-1, "Contains: ", mylist[len(mylist)-1])
 lastindex = len(mylist)-1
 for i in range (0, int(len(mylist)/2)):
   if (mylist[i] != mylist[lastindex-i]):
     print(mylist, "is not a palindrome")
     NotAPal = 1
     break
 if (NotAPal == 0):
   print (mylist, "is a palindrome")

palcheck ([1,2,3,4,5,4,3,2,1])
palcheck ([1,2,3,4,2,3,2,1])
palcheck ([1,1,1,1])
palcheck ([1,2,3,2,1])
palcheck ([1,4,4,2,1])


[1, 2, 3, 4, 5, 4, 3, 2, 1] is a palindrome
[1, 2, 3, 4, 2, 3, 2, 1] is not a palindrome
[1, 1, 1, 1] is a palindrome
[1, 2, 3, 2, 1] is a palindrome
[1, 4, 4, 2, 1] is not a palindrome


Sunday, June 17, 2018

Swap Two Integers without using a Temporary Variable

This is an older exercise, but it still comes up.
This falls under things you wouldn't bother worrying about in real life.

Swap Two Integers without using a Temporary Variable


The idea is to just add the two integer variables and then subtract each piece out in turn.

Say you have x=5 and y=9.


If you are using Python you can just stop right here as "tuple unpacking" will so all the work for you.

x, y = (y, x)

and you are done. x is 9 and y is 5.


Let's assume you want to do it the harder way...

Replace x with the sum of x and y.

x = x + y

x = 5 + 9 = 14

Now you have "lost" x, but you still have y and you can deduce what your old x is.

For the new y take your total and subtract out y, which will leave you with the "old" x in y's place.

y = x - y

y = 14 - 9 = 5

Now to get the new x, take the total and subtract out the new y. which will give you the old y which goes in x's place.  It's more confusing to write it out in English that to just see the math.

x = x - y

x = 14 - 5 = 9

To Summarize

x = x + y   [x changes]
y = x - y    [y changes: Total minus original y]
x = x - y    [x changes again]



Tuesday, May 22, 2018

The Non-Dog Blog is Staying at Blogger for Now

I have WordPress installed on my new website. It's very nice, but the blogging has these odd limitations. Mostly it's my own ignorance, but some of it is from years of being designed a certain way. I have more than one blog, but in WordPress they like to assume one blog per website unless you use categorization. I have 500 posts in this blog and there's no way I'm going to spend weeks categorizing them all. There are other workarounds, but I'm not sure I want to work that hard since I can easily have as many blogs as I like on Blogger. The tools are more limited in Blogger which is sad. I will make my author blog on WordPress, but that's with a brand new domain.

So what I can do is link to this site and I will have control over my DNS CNAMEs again so I will be able to use nondogblog.frap.org again. (Network Solutions took my ability to control them away from me - hmphf.)

One thing that was something of a heavy camel back-breaking straw was petty, but showed a glaring limitation - possibly of the theme I had chosen. I wanted to add the background image that you see here, but I didn't want it to affect the whole website. Sorry. Argh.

This is frustrating as I was so proud that I figured out how to download and them import my entire blog from blogger to WordPress. Fortunately I kept this site while I messed around with the new one.


Sunday, January 14, 2018

Figured out My Ski Turning Problem

For years, I've been struggling with an annoying problem with my ski turns.
My turns look like backwards 2's.  An instructor and I took a look at my tracks. The turn to the left is nice and smooth. The one to the right is sharp and abrupt.

You turn a parabolic ski by putting pressure on the ball of your foot on the outside ski. You are essentially weighting the inside edge of that ski. It's quite subtle and my less-coordinated left foot doesn't know how to do it properly, so I end up stomping on that ski and the ski turns too sharply.

I need to spend a lot of time on a slightly advanced bunny (green) or an easy intermediate (blue) slope. A slope I spend a lot of time on is "Trailblazer" on Jerome Hill at Sugarbowl.
The Nob Hill slope also at Sugarbowl is nice too. White Pine is a little easy, but ok. You do need to have enough incline to learn anything, so places like the very top of Squaw near the higher building are actually too easy.

I figured this out by actually sitting down in a chair and watching my feet while I envisioned turning left (just wearing running shoes). My right foot's heel rose, and it rolled inward a little, and my weight shifted to be on the ball of that foot. It's hard to imagine all this subtly going on inside a ski boot.

My left foot is a little clueless about how all this works, but now I can spend a lot of time training it away from the pricy ski slope. I can start in a chair and then move to standing up.

I can tell you that turning improperly is very tiring and will shorten the time you want to spend on the slope.

When turning, you should unweight the inner ski. If you're turning properly, it should just follow along with the turn. That foot will be behind the other foot.